class_docstr: Computes the image-text retrieval metrics for a list of images and their captions
  using the distance between their embeddings.
  
  Expects predictions to contain two keys:
      image (Tensor): [batch, hidden_dim]
      text (Tensor): [batch * num_captions, hidden_dim]
  
  Computes the following metrics:
      image2text
          recall@1, recall@5, recall@10, mean_rank, median_rank
      text2image
          recall@1, recall@5, recall@10, mean_rank, median_rank
  
  NOTE: each image MUST have the same number of captions.
class_docstr_tok: ["Computes","the","image-text","retrieval","metrics","for","a","list","of","images","and","their","captions","using","the","distance","between","their","embeddings",".","Expects","predictions","to","contain","two","keys",":","image","(","Tensor",")",":","[","batch",",","hidden_dim","]","text","(","Tensor",")",":","[","batch","*","num_captions",",","hidden_dim","]","Computes","the","following","metrics",":","image2text","recall","@","1",",","recall","@","5",",","recall","@","10",",","mean_rank",",","median_rank","text2image","recall","@","1",",","recall","@","5",",","recall","@","10",",","mean_rank",",","median_rank","NOTE",":","each","image","MUST","have","the","same","number","of","captions","."]
docstr: Compute the distance between embeddings for text captions and their respective images.
  
  Args:
      image: A tensor of image embeddings. Shape: [batch, hidden_dim]
      text: A tensor of text embeddings. Shape: [batch * num_captions, hidden_dim]
      num_captions: The number of captions paired with a single image.
  
  Returns:
      A tensor containing ranks of the corresponding image among all images.
docstr_tok: ["Compute","the","distance","between","embeddings","for","text","captions","and","their","respective","images",".","Args",":","image",":","A","tensor","of","image","embeddings",".","Shape",":","[","batch",",","hidden_dim","]","text",":","A","tensor","of","text","embeddings",".","Shape",":","[","batch","*","num_captions",",","hidden_dim","]","num_captions",":","The","number","of","captions","paired","with","a","single","image",".","Returns",":","A","tensor","containing","ranks","of","the","corresponding","image","among","all","images","."]
code: def _text2image(
      self, images: Tensor, texts: Tensor, num_captions: int
  ) -> torch.LongTensor:
      """
      Compute the distance between embeddings for text captions and their respective images.
  
      Args:
          image: A tensor of image embeddings. Shape: [batch, hidden_dim]
          text: A tensor of text embeddings. Shape: [batch * num_captions, hidden_dim]
          num_captions: The number of captions paired with a single image.
  
      Returns:
          A tensor containing ranks of the corresponding image among all images.
      """
      ranks = torch.zeros(images.shape[0], dtype=torch.long)
      for i, image in enumerate(images):
          # [1, hidden_dim] dist [batch * num_captions, hidden_dim] --> [batch * num_captions]
          # i.e. dists of size: [num_texts]
          dists = self.measure(image.unsqueeze(0), texts).squeeze(0)
  
          # find the rank of the best scoring caption among num_captions
          inds = torch.argsort(dists) // num_captions
          ranks[i] = (inds == i).nonzero()[0, 0]
      return ranks
  
imports: import argparse
  from numbers import Number
  from typing import Any, Dict, Tuple, Union
  import torch
  from torch import Tensor
  from metrics import METRICS_REGISTRY
  from metrics.metric_base import BaseMetric
  from metrics.retrieval_cmc import DISTANCE_REGISTRY
  from utils import logger
  from utils.tensor_utils import all_gather_list
label_desc: Use image node_id 6 for calling the ImageTextRetrievalMetric obj's underlying member method code with example usage: obj._text2image(images, texts, num_captions) and returns: ranks