(self, tensor_list: NestedTensor)
| 705 | return tuple(outs) |
| 706 | |
| 707 | def forward(self, tensor_list: NestedTensor): |
| 708 | x = tensor_list.tensors |
| 709 | """Forward function.""" |
| 710 | x = self.patch_embed(x) |
| 711 | |
| 712 | Wh, Ww = x.size(2), x.size(3) |
| 713 | if self.ape: |
| 714 | # interpolate the position embedding to the corresponding size |
| 715 | absolute_pos_embed = F.interpolate(self.absolute_pos_embed, |
| 716 | size=(Wh, Ww), |
| 717 | mode='bicubic') |
| 718 | x = (x + absolute_pos_embed).flatten(2).transpose(1, |
| 719 | 2) # B Wh*Ww C |
| 720 | else: |
| 721 | x = x.flatten(2).transpose(1, 2) |
| 722 | x = self.pos_drop(x) |
| 723 | |
| 724 | outs = [] |
| 725 | for i in range(self.num_layers): |
| 726 | layer = self.layers[i] |
| 727 | x_out, H, W, x, Wh, Ww = layer(x, Wh, Ww) |
| 728 | |
| 729 | if i in self.out_indices: |
| 730 | norm_layer = getattr(self, f'norm{i}') |
| 731 | x_out = norm_layer(x_out) |
| 732 | |
| 733 | out = x_out.view(-1, H, W, |
| 734 | self.num_features[i]).permute(0, 3, 1, |
| 735 | 2).contiguous() |
| 736 | outs.append(out) |
| 737 | # in: |
| 738 | # torch.Size([2, 3, 1024, 1024]) |
| 739 | # out: |
| 740 | # [torch.Size([2, 192, 256, 256]), torch.Size([2, 384, 128, 128]), \ |
| 741 | # torch.Size([2, 768, 64, 64]), torch.Size([2, 1536, 32, 32])] |
| 742 | |
| 743 | # collect for nesttensors |
| 744 | outs_dict = {} |
| 745 | for idx, out_i in enumerate(outs): |
| 746 | m = tensor_list.mask |
| 747 | assert m is not None |
| 748 | mask = F.interpolate(m[None].float(), |
| 749 | size=out_i.shape[-2:]).to(torch.bool)[0] |
| 750 | outs_dict[idx] = NestedTensor(out_i, mask) |
| 751 | |
| 752 | return outs_dict |
| 753 | |
| 754 | def train(self, mode=True): |
| 755 | """Convert the model into training mode while keep layers freezed.""" |
nothing calls this directly
no test coverage detected