Safemotion Lib
Loading...
Searching...
No Matches
Functions
fastreid.modeling.losses.utils Namespace Reference

Functions

 concat_all_gather (tensor)
 
 normalize (x, axis=-1)
 
 euclidean_dist (x, y)
 
 cosine_dist (x, y)
 

Detailed Description

@author:  xingyu liao
@contact: sherlockliao01@gmail.com

Function Documentation

◆ concat_all_gather()

fastreid.modeling.losses.utils.concat_all_gather ( tensor)
Performs all_gather operation on the provided tensors.
*** Warning ***: torch.distributed.all_gather has no gradient.

Definition at line 10 of file utils.py.

10def concat_all_gather(tensor):
11 """
12 Performs all_gather operation on the provided tensors.
13 *** Warning ***: torch.distributed.all_gather has no gradient.
14 """
15 tensors_gather = [torch.ones_like(tensor)
16 for _ in range(torch.distributed.get_world_size())]
17 torch.distributed.all_gather(tensors_gather, tensor, async_op=False)
18
19 output = torch.cat(tensors_gather, dim=0)
20 return output
21
22

◆ cosine_dist()

fastreid.modeling.losses.utils.cosine_dist ( x,
y )

Definition at line 43 of file utils.py.

43def cosine_dist(x, y):
44 bs1, bs2 = x.size(0), y.size(0)
45 frac_up = torch.matmul(x, y.transpose(0, 1))
46 frac_down = (torch.sqrt(torch.sum(torch.pow(x, 2), 1))).view(bs1, 1).repeat(1, bs2) * \
47 (torch.sqrt(torch.sum(torch.pow(y, 2), 1))).view(1, bs2).repeat(bs1, 1)
48 cosine = frac_up / frac_down
49 return 1 - cosine

◆ euclidean_dist()

fastreid.modeling.losses.utils.euclidean_dist ( x,
y )

Definition at line 34 of file utils.py.

34def euclidean_dist(x, y):
35 m, n = x.size(0), y.size(0)
36 xx = torch.pow(x, 2).sum(1, keepdim=True).expand(m, n)
37 yy = torch.pow(y, 2).sum(1, keepdim=True).expand(n, m).t()
38 dist = xx + yy - 2 * torch.matmul(x, y.t())
39 dist = dist.clamp(min=1e-12).sqrt() # for numerical stability
40 return dist
41
42

◆ normalize()

fastreid.modeling.losses.utils.normalize ( x,
axis = -1 )
Normalizing to unit length along the specified dimension.
Args:
  x: pytorch Variable
Returns:
  x: pytorch Variable, same shape as input

Definition at line 23 of file utils.py.

23def normalize(x, axis=-1):
24 """Normalizing to unit length along the specified dimension.
25 Args:
26 x: pytorch Variable
27 Returns:
28 x: pytorch Variable, same shape as input
29 """
30 x = 1. * x / (torch.norm(x, 2, axis, keepdim=True).expand_as(x) + 1e-12)
31 return x
32
33