The product of a tensor of numeric type and a tensor of boolean type

Recently, I was studying the calculation of the loss function in the yolov5 source code. In the build_targets function, there is a piece of code about removing the gt box with an excessively large aspect ratio with the specified anchor. The code is as follows:

if nt:
   # Matches
   r = t[..., 4:6] / anchors[:, None]  # wh ratio
   j = torch.max(r, 1 / r).max(2)[0] < self.hyp['anchor_t']  # compare 
                
   t = t[j]  # filter 过滤 

Among them, the sentence t=t[j] is really unclear, but it can be seen from the code that j is a tensor composed of True and False, so I wrote a few lines of code to verify the result. (In fact, you can understand the function of this line of code by observing the shape of t in debug)

import torch

j = torch.tensor([True, True, False, False])
t = torch.tensor([1, 2, 3, 4])

t = t[j]
print(t)

The result of the operation is as follows:

 

It can be seen that j plays a role of filtering and is used to filter the effective value of t. 

Guess you like

Origin blog.csdn.net/weixin_48747603/article/details/127335867