torchreid.metrics

Distance

torchreid.metrics.distance.compute_distance_matrix(input1, input2, metric='euclidean')[source]

A wrapper function for computing distance matrix.

Parameters
  • input1 (torch.Tensor) – 2-D feature matrix.

  • input2 (torch.Tensor) – 2-D feature matrix.

  • metric (str, optional) – “euclidean” or “cosine”. Default is “euclidean”.

Returns

distance matrix.

Return type

torch.Tensor

Examples::
>>> from torchreid import metrics
>>> input1 = torch.rand(10, 2048)
>>> input2 = torch.rand(100, 2048)
>>> distmat = metrics.compute_distance_matrix(input1, input2)
>>> distmat.size() # (10, 100)
torchreid.metrics.distance.cosine_distance(input1, input2)[source]

Computes cosine distance.

Parameters
  • input1 (torch.Tensor) – 2-D feature matrix.

  • input2 (torch.Tensor) – 2-D feature matrix.

Returns

distance matrix.

Return type

torch.Tensor

torchreid.metrics.distance.euclidean_squared_distance(input1, input2)[source]

Computes euclidean squared distance.

Parameters
  • input1 (torch.Tensor) – 2-D feature matrix.

  • input2 (torch.Tensor) – 2-D feature matrix.

Returns

distance matrix.

Return type

torch.Tensor

Accuracy

torchreid.metrics.accuracy.accuracy(output, target, topk=(1, ))[source]

Computes the accuracy over the k top predictions for the specified values of k.

Parameters
  • output (torch.Tensor) – prediction matrix with shape (batch_size, num_classes).

  • target (torch.LongTensor) – ground truth labels with shape (batch_size).

  • topk (tuple, optional) – accuracy at top-k will be computed. For example, topk=(1, 5) means accuracy at top-1 and top-5 will be computed.

Returns

accuracy at top-k.

Return type

list

Examples::
>>> from torchreid import metrics
>>> metrics.accuracy(output, target)

Rank

torchreid.metrics.rank.evaluate_rank(distmat, q_pids, g_pids, q_camids, g_camids, max_rank=50, use_metric_cuhk03=False, use_cython=True)[source]

Evaluates CMC rank.

Parameters
  • distmat (numpy.ndarray) – distance matrix of shape (num_query, num_gallery).

  • q_pids (numpy.ndarray) – 1-D array containing person identities of each query instance.

  • g_pids (numpy.ndarray) – 1-D array containing person identities of each gallery instance.

  • q_camids (numpy.ndarray) – 1-D array containing camera views under which each query instance is captured.

  • g_camids (numpy.ndarray) – 1-D array containing camera views under which each gallery instance is captured.

  • max_rank (int, optional) – maximum CMC rank to be computed. Default is 50.

  • use_metric_cuhk03 (bool, optional) – use single-gallery-shot setting for cuhk03. Default is False. This should be enabled when using cuhk03 classic split.

  • use_cython (bool, optional) – use cython code for evaluation. Default is True. This is highly recommended as the cython code can speed up the cmc computation by more than 10x. This requires Cython to be installed.