-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
236 lines (176 loc) · 8.37 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import utils
import torch
import torch.nn as nn
import torchvision
import timm
from configs import configs as cfg
def _load_finernet(model, load_path):
finernet_dict = torch.load(load_path)['model_state_dict']
load_dict = dict()
finernet_keys = finernet_dict.keys()
model_keys = model.state_dict().keys()
for finernet_key, model_key in zip(finernet_keys, model_keys):
if finernet_dict[finernet_key].shape == model.state_dict()[model_key].shape:
load_dict[model_key] = finernet_dict[finernet_key]
return load_dict
def _load_feature(model, state_dict):
for key in ['fc.weight', 'fc.bias', 'classifier.weight', 'classifier.bias', 'head.weight', 'head.bias']:
if state_dict.get(key) is not None:
state_dict.pop(key)
msg = model.load_state_dict(state_dict, strict=False)
return msg
def Resnet(arch='resnet50', pretrained=None, num_classes=1000):
'''
pretrained: `imagenet`, `class`, `inat`, `binary`
- imagenet: pretrained from imagenet
- class: pretrained from multi-class classification task
- inat: pretrained from inaturalist
- finernet: pretrained from binary classification task
'''
if hasattr(torchvision.models, arch):
model = getattr(torchvision.models, arch)()
setattr(model, 'fc', nn.Linear(model.fc.in_features, num_classes))
else:
raise ValueError(f"Not supported model architecture {arch}")
if pretrained == 'imagenet':
load_path = 'torchvision'
state_dict = getattr(torchvision.models, arch)(pretrained=True).state_dict()
elif pretrained == 'class':
load_path = cfg.EXP.CLASS.PATH
state_dict = torch.load(load_path)
elif pretrained == 'inat':
load_path = 'pretrained_models/inat2021_supervised_large.pth.tar'
state_dict = torch.load(load_path)['state_dict']
elif pretrained == 'advnet':
load_path = cfg.EXP.ADVNET.PATH
state_dict = _load_finernet(model, load_path)
else:
load_path = None
if load_path is None:
utils.logger(f'Initialize {arch} from scratch', level='warning')
else:
msg = _load_feature(model, state_dict)
utils.logger(f'Loaded {load_path} to {arch} with msg {msg}', level="warning")
return model
def ViT(arch='vit_base_patch16_384', pretrained=None, num_classes=1000):
"""
arch: 'vit_base_patch16_384', 'vit_base_patch16_224'
"""
model = timm.create_model(arch, pretrained=False, num_classes=num_classes)
if pretrained == 'imagenet':
load_path = 'torchvision'
state_dict = timm.create_model(arch, pretrained=True, num_classes=num_classes).state_dict()
elif pretrained == 'class':
load_path = cfg.EXP.CLASS.PATH
state_dict = torch.load(load_path)
elif pretrained == 'advnet':
load_path = cfg.EXP.ADVNET.PATH
state_dict = _load_finernet(model, load_path)
else:
load_path = None
if load_path is None:
utils.logger(f'Initialize {arch} from scratch', level='warning')
else:
msg = _load_feature(model, state_dict)
utils.logger(f'Loaded {load_path} to {arch} with msg {msg}', level="warning")
return model
class BinaryMLP(nn.Module):
def __init__(self, input_dim, hidden_dim, dropout=0.0): # Set default dropout to 0.2
super().__init__()
project_dim = 192
self.net = nn.Sequential(
nn.Linear(input_dim, project_dim),
nn.BatchNorm1d(project_dim),
nn.GELU(),
nn.Dropout(dropout), # Add dropout after first activation
nn.Linear(project_dim, hidden_dim),
nn.BatchNorm1d(hidden_dim), # Make sure to use `hidden_dim` instead of fixed number
nn.GELU(),
nn.Dropout(dropout), # Add dropout after second activation
nn.Linear(hidden_dim, 2), # 2 for binary classification
)
def forward(self, x):
return self.net(x)
class CNN_finernetwork(nn.Module):
def __init__(self, model, pretrained=None, dropout=0.0, feature_dim=2048):
super(CNN_finernetwork, self).__init__()
backbone = Resnet(arch=model, pretrained=pretrained, num_classes=cfg.DATA.NUM_CLASSES)
conv_features = list(backbone.children())[:-2] # delete the last fc layer
self.conv_layers = nn.Sequential(*conv_features)
self.pooling_layer = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.binary_layer = BinaryMLP(
2 * feature_dim + 2, 32, dropout=dropout)
self.agg_branch = nn.Linear(2, 1)
# initialize all fc layers to xavier
for m in self.modules():
if isinstance(m, nn.Linear):
torch.nn.init.xavier_normal_(m.weight, gain=1)
def forward(self, images, explanations):
# Process the input images
input_spatial_feats = self.conv_layers(images)
input_feat = self.pooling_layer(input_spatial_feats).squeeze()
# Process the nearest neighbors
explanations = explanations.squeeze()
explanation_spatial_feats = self.conv_layers(explanations)
exp_feat = self.pooling_layer(explanation_spatial_feats).squeeze()
sep_token = torch.zeros([explanations.shape[0], 1], requires_grad=False).to(input_feat.device)
exp_feat = exp_feat.to(input_feat.device)
x = self.binary_layer(
torch.cat([sep_token, input_feat, sep_token, exp_feat], dim=1))
output3 = self.agg_branch(x)
output = output3
return output, input_feat, exp_feat, None
class Transformer_finernetwork(nn.Module):
def __init__(self, model, pretrained=None, dropout=0.0, feature_dim=768):
super(Transformer_finernetwork, self).__init__()
self.backbone = ViT(arch=model, pretrained=pretrained, num_classes=cfg.DATA.NUM_CLASSES)
feature_dim = self.backbone.head.in_features
self.binary_layer = BinaryMLP(
2 * feature_dim + 2, 192, dropout=dropout)
self.agg_branch = nn.Linear(2, 1)
# initialize all fc layers to xavier
# for m in self.modules():
# if isinstance(m, nn.Linear):
# torch.nn.init.xavier_normal_(m.weight, gain=1)
def forward(self, images, explanations):
# Process the input images
input_feat = self.backbone.forward_features(images)[:, 0].squeeze()
# Process the nearest neighbors
exp_feat = self.backbone.forward_features(explanations)[:, 0].squeeze()
sep_token = torch.zeros([explanations.shape[0], 1], requires_grad=False).to(input_feat.device)
exp_feat = exp_feat.to(input_feat.device)
x = self.binary_layer(
torch.cat([sep_token, input_feat, sep_token, exp_feat], dim=1))
output3 = self.agg_branch(x)
output = output3
## cosine similarity
input_feat = torch.nn.functional.normalize(input_feat, p=2, dim=1)
exp_feat = torch.nn.functional.normalize(exp_feat, p=2, dim=1)
sim = torch.nn.functional.cosine_similarity(input_feat, exp_feat)
return output, input_feat, exp_feat, sim
class ADVNET(nn.Module):
def __init__(self, model, pretrained=None, dropout=0.0, feature_dim=2048):
super(ADVNET, self).__init__()
backbone = Resnet(arch=model, pretrained=pretrained, num_classes=cfg.DATA.NUM_CLASSES)
conv_features = list(backbone.children())[:-2] # delete the last fc layer
self.conv_layers = nn.Sequential(*conv_features)
prev_dim = backbone.fc.in_features
self.pooling_layer = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.projection_layer = nn.Sequential(
nn.Linear(prev_dim, prev_dim, bias=False),
nn.BatchNorm1d(prev_dim),
nn.ReLU(),
backbone.fc,
)
def forward(self, images, explanations):
# Process the input images
input_spatial_feats = self.conv_layers(images)
input_feat = self.pooling_layer(input_spatial_feats).squeeze()
# Process the nearest neighbors
explanations = explanations.squeeze()
explanation_spatial_feats = self.conv_layers(explanations)
exp_feat = self.pooling_layer(explanation_spatial_feats).squeeze()
exp_feat = exp_feat.to(input_feat.device)
input_feat = self.projection_layer(input_feat)
exp_feat = self.projection_layer(exp_feat)
return input_feat, exp_feat