Retrieval Post-Processing
IDistancesPostprocessor
- class oml.interfaces.retrieval.IDistancesPostprocessor[source]
Bases:
object
This is a parent class for the classes which apply some postprocessing after query-to-gallery distance matrix has been calculated. For example, we may want to apply one of re-ranking techniques.
- process(distances: Tensor, queries: Any, galleries: Any) Tensor [source]
This method takes all the needed variables and returns the modified matrix of distances, where some distances are replaced with new ones.
- Parameters
distances – Matrix with the shape of
[Q, G]
queries – Queries in the amount of
Q
galleries – Galleries in the amount of
G
- Returns
An updated distances matrix with the shape of
[Q, G]
PairwisePostprocessor
- class oml.retrieval.postprocessors.pairwise.PairwisePostprocessor[source]
Bases:
IDistancesPostprocessor
,ABC
This postprocessor allows us to re-estimate the distances between queries and
top-n
galleries closest to them. It creates pairs of queries and galleries and feeds them to a pairwise model.- process(distances: Tensor, queries: Any, galleries: Any) Tensor [source]
- Parameters
distances – Matrix with the shape of
[Q, G]
queries – Queries in the amount of
Q
galleries – Galleries in the amount of
G
- Returns
- Distance matrix with the shape of
[Q, G]
, where
top_n
minimal values in each row have been updated by the pairwise model, other distances are shifted by a margin to keep the relative order.
- Distance matrix with the shape of
- inference(queries: Any, galleries: Any, ii_top: Tensor, top_n: int) Tensor [source]
Depends on the exact types of queries/galleries this method may be implemented differently.
- Parameters
queries – Queries in the amount of
Q
galleries – Galleries in the amount of
G
ii_top – Indices of the closest galleries with the shape of
[Q, top_n]
top_n – Number of the closest galleries to re-rank
- Returns
An updated distance matrix with the shape of
[Q, G]
PairwiseEmbeddingsPostprocessor
- class oml.retrieval.postprocessors.pairwise.PairwiseEmbeddingsPostprocessor(top_n: int, pairwise_model: IPairwiseModel, num_workers: int, batch_size: int, verbose: bool = False, use_fp16: bool = False, is_query_key: str = 'is_query', is_gallery_key: str = 'is_gallery', embeddings_key: str = 'embeddings')[source]
Bases:
PairwisePostprocessor
- __init__(top_n: int, pairwise_model: IPairwiseModel, num_workers: int, batch_size: int, verbose: bool = False, use_fp16: bool = False, is_query_key: str = 'is_query', is_gallery_key: str = 'is_gallery', embeddings_key: str = 'embeddings')[source]
- Parameters
top_n – Model will be applied to the
num_queries * top_n
pairs formed by each query andtop_n
most relevant galleries.pairwise_model – Model which is able to take two embeddings as inputs and estimate the distance (not in a strictly mathematical sense) between them.
num_workers – Number of workers in DataLoader
batch_size – Batch size that will be used in DataLoader
verbose – Set
True
if you want to see progress bar for an inferenceuse_fp16 – Set
True
if you want to use half precisionis_query_key – Key to access a binary mask indicates queries in case of using
process_by_dict
is_gallery_key – Key to access a binary mask indicates galleries in case of using
process_by_dict
embeddings_key – Key to access embeddings in case of using
process_by_dict
- inference(queries: Tensor, galleries: Tensor, ii_top: Tensor, top_n: int) Tensor [source]
- Parameters
queries – Queries representations with the shape of
[Q, *]
galleries – Galleries representations with the shape of
[G, *]
ii_top – Indices of the closest galleries with the shape of
[Q, top_n]
top_n – Number of the closest galleries to re-rank
- Returns
Updated distance matrix with the shape of
[Q, G]
PairwiseImagesPostprocessor
- class oml.retrieval.postprocessors.pairwise.PairwiseImagesPostprocessor(top_n: int, pairwise_model: IPairwiseModel, transforms: Union[Compose, Compose], num_workers: int = 0, batch_size: int = 128, verbose: bool = True, use_fp16: bool = False, is_query_key: str = 'is_query', is_gallery_key: str = 'is_gallery', paths_key: str = 'paths')[source]
Bases:
PairwisePostprocessor
- __init__(top_n: int, pairwise_model: IPairwiseModel, transforms: Union[Compose, Compose], num_workers: int = 0, batch_size: int = 128, verbose: bool = True, use_fp16: bool = False, is_query_key: str = 'is_query', is_gallery_key: str = 'is_gallery', paths_key: str = 'paths')[source]
- Parameters
top_n – Model will be applied to the
num_queries * top_n
pairs formed by each query and itstop_n
most relevant galleries.pairwise_model – Model which is able to take two images as inputs and estimate the distance (not in a strictly mathematical sense) between them.
transforms – Transforms that will be applied to an image
num_workers – Number of workers in DataLoader
batch_size – Batch size that will be used in DataLoader
verbose – Set
True
if you want to see progress bar for an inferenceuse_fp16 – Set
True
if you want to use half precisionis_query_key – Key to access a binary mask indicates queries in case of using
process_by_dict
is_gallery_key – Key to access a binary mask indicates galleries in case of using
process_by_dict
paths_key – Key to access paths to images in case of using
process_by_dict
- inference(queries: List[Path], galleries: List[Path], ii_top: Tensor, top_n: int) Tensor [source]
- Parameters
queries – Paths to queries with the length of
Q
galleries – Paths to galleries with the length of
G
ii_top – Indices of the closest galleries with the shape of
[Q, top_n]
top_n – Number of the closest galleries to re-rank
- Returns
Updated distance matrix with the shape of
[Q, G]