Global similarity scores
The similarity.cosine
module provides tools for calculating global similarity scores between query and database. This is done using cosine similarity between fixed-length embeddings extracted by neural networks.
similarity.cosine
CosineSimilarity
Wraps cosine similarity to be usable in SimilarityPipeline.
__call__(query, database, **kwargs)
Calculates cosine similarity given query and database feature datasets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
FeatureDataset
|
Query dataset of deep features. |
required |
database |
FeatureDataset
|
Database dataset of deep features. |
required |
Returns:
Name | Type | Description |
---|---|---|
similarity |
array
|
2D numpy array with cosine similarity. |
cosine_similarity(a, b)
Calculate cosine similarity between two sets of vectors.
Pytorch Equivalent to sklearn.metrics.pairwise.cosine_similarity
.
Example
query
and database
are instance of FeatureDataset
with deep features.
from wildlife_tools.similarity import CosineSimilarity
similarity = CosineSimilarity()
sim = similarity(query, database)