在 def lossrankori(self, outputs, targetsi, tempannoi=None): “”“Compute the losses…
- User
在 def loss_rank_ori(self, outputs, targetsi, temp_annoi=None): “”“Compute the losses related to the bounding boxes, the L1 regression loss and the GIoU loss targets dicts must contain the key “boxes” containing a tensor of dim [nb_target_boxes, 4] The target boxes are expected in format (center_x, center_y, h, w), normalized by the image size. “”” attn = outputs[‘attn’] print(‘attn shape:’,attn.shape) # # attn shape: torch.Size([4, 12, 574, 574]) attn1 = torch.cat([attn[:,:,114:344,57:114], attn[:,:,114:344,344:]],dim=3) attn1 = attn1.mean(dim=0, keepdim=True).mean(dim=1, keepdim=True) print(‘attn1 shape’,attn1.shape) attn2 = torch.cat([attn[:,:,344:,:57], attn[:,:,344:,114:344]],dim=3) attn2 = attn2.mean(dim=0, keepdim=True).mean(dim=1, keepdim=True) print(‘attn2 shape’,attn2.shape) attn = torch.cat([attn1, attn2],dim=1) B, C, H, W = attn.shape ,s1, = torch.svd(attn.reshape([B*C, H, W])) s01 = torch.abs(s1 - 1) return torch.mean(s01)中attn的输入维度是# attn shape: torch.Size([4, 12, 574, 574]),然后进行了后续的操作,那么现在我们的attn.shape torch.Size([2, 12, 320, 320])改变之后,后续的操作应该如何修改?
- Assistant
在上述代码中,首先输出了输入的注意力图(attn)的维度,这些维度(
torch.Size([4, 12, 574, 574]))表示有4个批次(batch),每个批次有12个头部(heads),每个头部关注图大小为574x574。接下来,原代码通过对特定区域的焦点进行了切割、合并和平均操作来改变注意力图的维度,最终生成两个新的维度缩小的注意力图attn1和attn2。由于
attn的维度在此过程中发生了变化,我们需要相应地调整attn1和attn2的生成过程。给定新的
attn.shape(torch.Size([2, 12, 320, 320])),下面是调整后的代码:python