Keypoint Attention Layer. Args: use_conv (bool): whether to use conv for the attended feature map. Default: False in_channels (List[int]): the in channel of shape_cam features and pose features. Default: (256, 64) out_chann
| 69 | |
| 70 | |
| 71 | class KeypointAttention(nn.Module): |
| 72 | """Keypoint Attention Layer. |
| 73 | |
| 74 | Args: |
| 75 | use_conv (bool): |
| 76 | whether to use conv for the attended feature map. |
| 77 | Default: False |
| 78 | in_channels (List[int]): |
| 79 | the in channel of shape_cam features and pose features. |
| 80 | Default: (256, 64) |
| 81 | out_channels (List[int]): |
| 82 | the out channel of shape_cam features and pose features. |
| 83 | Default: (256, 64) |
| 84 | Returns: |
| 85 | attended_features (torch.Tensor): |
| 86 | attended feature maps |
| 87 | """ |
| 88 | def __init__(self, |
| 89 | use_conv=False, |
| 90 | in_channels=(256, 64), |
| 91 | out_channels=(256, 64), |
| 92 | act='softmax', |
| 93 | use_scale=False): |
| 94 | super(KeypointAttention, self).__init__() |
| 95 | self.use_conv = use_conv |
| 96 | self.in_channels = in_channels |
| 97 | self.out_channels = out_channels |
| 98 | self.act = act |
| 99 | self.use_scale = use_scale |
| 100 | if use_conv: |
| 101 | self.conv1x1_pose = nn.Conv1d(in_channels[0], |
| 102 | out_channels[0], |
| 103 | kernel_size=1) |
| 104 | self.conv1x1_shape_cam = nn.Conv1d(in_channels[1], |
| 105 | out_channels[1], |
| 106 | kernel_size=1) |
| 107 | |
| 108 | def forward(self, features, heatmaps): |
| 109 | batch_size, num_joints, height, width = heatmaps.shape |
| 110 | |
| 111 | if self.use_scale: |
| 112 | scale = 1.0 / np.sqrt(height * width) |
| 113 | heatmaps = heatmaps * scale |
| 114 | |
| 115 | if self.act == 'softmax': |
| 116 | normalized_heatmap = F.softmax(heatmaps.reshape( |
| 117 | batch_size, num_joints, -1), |
| 118 | dim=-1) |
| 119 | elif self.act == 'sigmoid': |
| 120 | normalized_heatmap = torch.sigmoid( |
| 121 | heatmaps.reshape(batch_size, num_joints, -1)) |
| 122 | features = features.reshape(batch_size, -1, height * width) |
| 123 | |
| 124 | attended_features = torch.matmul(normalized_heatmap, |
| 125 | features.transpose(2, 1)) |
| 126 | attended_features = attended_features.transpose(2, 1) |
| 127 | |
| 128 | if self.use_conv: |