Forward function for `SinePositionalEncoding`. Args: mask (Tensor): ByteTensor mask. Non-zero values representing ignored positions, while zero values means valid positions for this image. Shape [bs, h, w]. Returns: pos (Tenso
(self, mask)
| 51 | self.offset = offset |
| 52 | |
| 53 | def forward(self, mask): |
| 54 | """Forward function for `SinePositionalEncoding`. |
| 55 | |
| 56 | Args: |
| 57 | mask (Tensor): ByteTensor mask. Non-zero values representing |
| 58 | ignored positions, while zero values means valid positions |
| 59 | for this image. Shape [bs, h, w]. |
| 60 | |
| 61 | Returns: |
| 62 | pos (Tensor): Returned position embedding with shape |
| 63 | [bs, num_feats*2, h, w]. |
| 64 | """ |
| 65 | # For convenience of exporting to ONNX, it's required to convert |
| 66 | # `masks` from bool to int. |
| 67 | mask = mask.to(torch.int) |
| 68 | not_mask = 1 - mask # logical_not |
| 69 | y_embed = not_mask.cumsum(1, dtype=torch.float32) |
| 70 | x_embed = not_mask.cumsum(2, dtype=torch.float32) |
| 71 | if self.normalize: |
| 72 | y_embed = (y_embed + self.offset) / \ |
| 73 | (y_embed[:, -1:, :] + self.eps) * self.scale |
| 74 | x_embed = (x_embed + self.offset) / \ |
| 75 | (x_embed[:, :, -1:] + self.eps) * self.scale |
| 76 | dim_t = torch.arange(self.num_feats, |
| 77 | dtype=torch.float32, |
| 78 | device=mask.device) |
| 79 | dim_t = self.temperature**(2 * (dim_t // 2) / self.num_feats) |
| 80 | pos_x = x_embed[:, :, :, None] / dim_t |
| 81 | pos_y = y_embed[:, :, :, None] / dim_t |
| 82 | # use `view` instead of `flatten` for dynamically exporting to ONNX |
| 83 | B, H, W = mask.size() |
| 84 | pos_x = torch.stack( |
| 85 | (pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()), |
| 86 | dim=4).view(B, H, W, -1) |
| 87 | pos_y = torch.stack( |
| 88 | (pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()), |
| 89 | dim=4).view(B, H, W, -1) |
| 90 | pos = torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2) |
| 91 | return pos |
| 92 | |
| 93 | def __repr__(self): |
| 94 | """str: a string that describes the module""" |