| 49 | return encoded_feat |
| 50 | |
| 51 | def forward(self, x): |
| 52 | assert x.dim() == 4 and x.size(1) == self.channels |
| 53 | # [batch_size, channels, height, width] |
| 54 | batch_size = x.size(0) |
| 55 | # [batch_size, height x width, channels] |
| 56 | x = x.view(batch_size, self.channels, -1).transpose(1, 2).contiguous() |
| 57 | # assignment_weights: [batch_size, channels, num_codes] |
| 58 | assignment_weights = F.softmax(self.scaled_l2(x, self.codewords, self.scale), dim=2) |
| 59 | # aggregate |
| 60 | encoded_feat = self.aggregate(assignment_weights, x, self.codewords) |
| 61 | return encoded_feat |
| 62 | |
| 63 | def __repr__(self): |
| 64 | repr_str = self.__class__.__name__ |