class_docstr: Compute CMC-top-k and mAP metrics in retrieval setup.
class_docstr_tok: ["Compute","CMC-top-k","and","mAP","metrics","in","retrieval","setup","."]
docstr: Compute retrieval metrics over full epoch.
  
  Args:
      embedding: tensor of m embeddings with shape (m, d), where d is embedding dimension.
      labels: tensor of m labels.
  
  Returns: A dictionary of `top1`, `top-{k}` and `mAP`.
docstr_tok: ["Compute","retrieval","metrics","over","full","epoch",".","Args",":","embedding",":","tensor","of","m","embeddings","with","shape","(","m",",","d",")",",","where","d","is","embedding","dimension",".","labels",":","tensor","of","m","labels",".","Returns",":","A","dictionary","of","`","top1","`",",","`","top-","{","k","}","`","and","`","mAP","`","."]
code: def compute_with_aggregates(
      self, embedding: torch.Tensor, labels: torch.Tensor
  ) -> Dict[str, float]:
      """Compute retrieval metrics over full epoch.
  
      Args:
          embedding: tensor of m embeddings with shape (m, d), where d is embedding dimension.
          labels: tensor of m labels.
  
      Returns: A dictionary of `top1`, `top-{k}` and `mAP`.
  
      """
      # (Possibly) use a smaller subset
      if self.subset_fraction < 1.0:
          gallery_size = embedding.shape[0]
          n_subset = int(self.subset_fraction * gallery_size)
          mask = torch.randperm(embedding.shape[0])[:n_subset]
          embedding = embedding[mask]
          labels = labels[mask]
  
      # Same embeddings are used for both gallery and query
      distance_matrix = self.get_distance_matrix(embedding, embedding)
  
      if self.is_master:
          logger.log(
              f"Begin CMC calculation on embeddings with shape = {embedding.shape}."
          )
  
      top1, topk = cmc_calculation(
          distance_matrix=distance_matrix,
          query_ids=labels,
          k=self.k,
      )
      top1 = float(top1)
      topk = float(topk)
  
      if self.compute_map:
          retrieval_map = mean_ap(
              distance_matrix=distance_matrix, labels=labels
          )
      else:
          retrieval_map = 0
  
      # Convert to percent and return
      return {
          "top1": 100 * top1,
          f"top{self.k}": 100 * topk,
          "mAP": 100 * retrieval_map,
      }
  
imports: import argparse
  import copy
  from typing import Dict, Tuple
  import numpy
  import torch
  from sklearn.metrics import average_precision_score
  from torch.nn import functional
  from metrics import METRICS_REGISTRY
  from metrics.metric_base import EpochMetric
  from utils import logger
  from utils.ddp_utils import is_master
  from utils.registry import Registry
label_desc: Use image node_id 3 for calling the RetrievalCMC obj's underlying member method code with example usage: obj.compute_with_aggregates(embedding, labels) and returns: dict