| 879 | |
| 880 | |
| 881 | class Meta_Attribute_Generator2(nn.Module): |
| 882 | def __init__(self, vit_backbone_model, dict_attribute, grad_from_block=11): |
| 883 | super().__init__() |
| 884 | backbone_feat_dim = vit_backbone_model.num_features |
| 885 | self.num_attribute_class = len(dict_attribute.keys()) |
| 886 | self.num_attribute_all = sum([len(v) for v in dict_attribute.values()]) |
| 887 | self.attribute_generator_list = nn.ModuleList() |
| 888 | |
| 889 | for key in dict_attribute.keys(): |
| 890 | _conv = attribute_subnet(backbone_feat_dim) |
| 891 | _classifier = nn.Linear(backbone_feat_dim, len(dict_attribute[key]) + 1) # 1 for no present |
| 892 | _softmax = nn.Softmax(dim=1) |
| 893 | self.attribute_generator_list.append(nn.Sequential(_conv, _classifier, _softmax)) |
| 894 | |
| 895 | del vit_backbone_model |
| 896 | torch.cuda.empty_cache() |
| 897 | self.apply(self._init_weights) |
| 898 | |
| 899 | def _init_weights(self, m): |
| 900 | if isinstance(m, nn.Linear): |
| 901 | trunc_normal_(m.weight, std=.02) |
| 902 | if isinstance(m, nn.Linear) and m.bias is not None: |
| 903 | nn.init.constant_(m.bias, 0) |
| 904 | |
| 905 | def forward(self, x): |
| 906 | fake_prob_list = [] |
| 907 | meta_embedding = torch.transpose(x, 1, 2) |
| 908 | for att_head in self.attribute_generator_list: |
| 909 | fake_prob = att_head(meta_embedding) |
| 910 | fake_prob_list.append(fake_prob) |
| 911 | return fake_prob_list |
| 912 | |
| 913 | |
| 914 | def at_small(pretrain_path): |
no outgoing calls
no test coverage detected