-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
412 lines (346 loc) · 19.4 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import torch
import numpy as np
import copy
from torchvision.models import resnet50
from collections import OrderedDict
from transformers import CLIPProcessor, CLIPVisionModel, CLIPVisionModelWithProjection, AutoTokenizer, CLIPTextModelWithProjection
from torch import nn
import torch.nn.functional as F
import timm
import warnings
warnings.filterwarnings("ignore")
'''
model definitions
'''
class FCNet(torch.nn.Module):
def __init__(self, num_feats, num_classes):
super(FCNet, self).__init__()
self.fc = torch.nn.Linear(num_feats, num_classes)
def forward(self, x):
x = self.fc(x)
return x
class Adaptor(torch.nn.Module):
def __init__(self, num_feats=768, num_hidden=384):
super(Adaptor, self).__init__()
self.fc1 = torch.nn.Linear(num_feats, num_hidden)
self.fc2 = torch.nn.Linear(num_hidden, num_feats)
def forward(self, x):
x = self.fc1(x)
x = self.fc2(x)
return x
class ImageClassifier(torch.nn.Module):
def __init__(self, P, Z, model_feature_extractor=None, model_linear_classifier=None):
super(ImageClassifier, self).__init__()
print('initializing image classifier')
model_feature_extractor_in = copy.deepcopy(model_feature_extractor)
model_linear_classifier_in = copy.deepcopy(model_linear_classifier)
self.arch = P['arch']
if self.arch == 'clip_vision':
print('training CLIP_Vision Encoder')
self.feature_extractor = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14-336",output_attentions=True)
for p in self.feature_extractor.parameters():
p.requires_grad = False
#CLIP-ViT-L
self.vision_extractor = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14-336",output_attentions=True)
for p in self.vision_extractor.parameters():
p.requires_grad = True
'''
self.text_extractor = CLIPTextModelWithProjection.from_pretrained("openai/clip-vit-large-patch14-336",output_attentions=True)
for p in self.text_extractor.parameters():
p.requires_grad = True
self.tokenizer = AutoTokenizer.from_pretrained
'''
#self.projection = nn.Linear(in_features=1024, out_features=768, bias=True)
#self.img_to_latents = EmbedToLatents(768, 384)
self.adaptor = Adaptor(768, 384)
self.linear_classifier = FCNet(768, P['num_classes'])
#self.threshold_up = torch.nn.Parameter(torch.randn(1))
self.threshold = P['threshold']
self.scalar_pos = torch.tensor(1, dtype=torch.float32).to(Z['device'])
self.scalar_neg = torch.tensor(-1, dtype=torch.float32).to(Z['device'])
#self.adaptor = Adaptor(768, 384)
self.temperature = P['temp']
self.partial = P['partial']
self.classes = P['num_classes']
self.device = Z['device']
elif self.arch == 'resnet50':
feature_extractor = resnet50(pretrained=True)
feature_extractor = torch.nn.Sequential(*list(feature_extractor.children())[:-1])
feature_extractor.avgpool = torch.nn.AdaptiveAvgPool2d(1)
self.vision_extractor = feature_extractor
for param in self.vision_extractor.parameters():
param.requires_grad = True
self.feature_extractor = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14-336",output_attentions=True)
for p in self.feature_extractor.parameters():
p.requires_grad = False
self.linear_classifier = FCNet(2048, P['num_classes'])
self.threshold = P['threshold']
self.scalar_pos = torch.tensor(1, dtype=torch.float32).to(Z['device'])
self.scalar_neg = torch.tensor(-1, dtype=torch.float32).to(Z['device'])
#self.adaptor = Adaptor(768, 384)
self.temperature = P['temp']
self.partial = P['partial']
self.classes = P['num_classes']
self.device = Z['device']
elif self.arch == 'convnext_xlarge_22k':
self.vision_extractor = timm.create_model('convnext_xlarge.fb_in22k', pretrained=True)
self.vision_extractor.head.fc=nn.Linear(in_features=2048, out_features=P['num_classes'], bias=True)
for param in self.vision_extractor.parameters():
param.requires_grad = True
self.feature_extractor = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14-336",output_attentions=True)
for p in self.feature_extractor.parameters():
p.requires_grad = False
#self.linear_classifier = FCNet(2048, P['num_classes'])
self.threshold = P['threshold']
self.scalar_pos = torch.tensor(1, dtype=torch.float32).to(Z['device'])
self.scalar_neg = torch.tensor(-1, dtype=torch.float32).to(Z['device'])
#self.adaptor = Adaptor(768, 384)
self.temperature = P['temp']
self.partial = P['partial']
self.classes = P['num_classes']
self.device = Z['device']
elif self.arch == 'convnext_xlarge_1k':
self.vision_extractor = timm.create_model('convnext_xlarge.fb_in22k_ft_in1k', pretrained=True)
self.vision_extractor.head.fc=nn.Linear(in_features=2048, out_features=P['num_classes'], bias=True)
for param in self.vision_extractor.parameters():
param.requires_grad = True
self.feature_extractor = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14-336",output_attentions=True)
for p in self.feature_extractor.parameters():
p.requires_grad = False
#self.linear_classifier = FCNet(2048, P['num_classes'])
self.threshold = P['threshold']
self.scalar_pos = torch.tensor(1, dtype=torch.float32).to(Z['device'])
self.scalar_neg = torch.tensor(-1, dtype=torch.float32).to(Z['device'])
#self.adaptor = Adaptor(768, 384)
self.temperature = P['temp']
self.partial = P['partial']
self.classes = P['num_classes']
self.device = Z['device']
else:
raise ValueError('Architecture not implemented.')
def temperature_scaled_softmax(self, logits, temperature):
return F.softmax(logits / temperature, dim=-1)
def forward(self, x, xs, y):
if self.arch == 'clip_vision':
# x is a batch of images
feats = self.feature_extractor(xs)
image_embedding = feats.image_embeds
image_embedding = F.normalize(image_embedding, dim=-1)
similarity = image_embedding @ y.T
#The similarity should be a T-softmax
similarity = self.temperature_scaled_softmax(similarity, temperature=self.temperature)
#print("similarity: ", similarity)
#options 1, setting a threshold to determin final pseduo-label (-1, 1) (too aggressive, not work)
#logits_pl = torch.where(similarity > self.threshold_up, self.scalar_pos, self.scalar_neg)
'''
#options 2, setting a thresholds to determin only final positive pseduo-label (0, 1)
logits_pl = torch.zeros_like(similarity)
logits_pl = torch.where(similarity > self.threshold_up, self.scalar_pos, logits_pl)
#logits_pl = torch.where(similarity < self.threshold_down, self.scalar_neg, logits_pl)
#print("logits_pl: ", logits_pl)
'''
#options 3, setting two thresholds to determin the final pos./neg. pseduo-label (-1, 0, 1)
logits_pl = torch.zeros_like(similarity)
#positive pseudo label
logits_pl = torch.where(similarity > self.threshold, self.scalar_pos, logits_pl)
#negative pseudo label
num_elements = int(self.partial * self.classes)
#print("num_elements: ", num_elements)
_, smallest_indices = torch.topk(similarity, num_elements, largest=False)
#print("smallest_indices: ", smallest_indices)
values = torch.full(smallest_indices.shape, -1, dtype=logits_pl.dtype).to(self.device)
logits_pl.scatter_(1, smallest_indices, values)
#print("logits_pl: ", logits_pl)
vision = self.vision_extractor(xs)
#vision = self.adaptor(vision.image_embeds)
#logits = self.linear_classifier(vision)
logits = self.linear_classifier(vision.image_embeds)
#print("logits: ", logits)
elif self.arch == 'resnet50':
# x is a batch of images
feats = self.feature_extractor(xs)
image_embedding = feats.image_embeds
image_embedding = F.normalize(image_embedding, dim=-1)
similarity = image_embedding @ y.T
#print("similarity: ", similarity.shape)
#The similarity should be a T-softmax
similarity = self.temperature_scaled_softmax(similarity, temperature=self.temperature)
#print("similarity: ", similarity)
#options 1, setting a threshold to determin final pseduo-label (-1, 1) (too aggressive, not work)
#logits_pl = torch.where(similarity > self.threshold_up, self.scalar_pos, self.scalar_neg)
'''
#options 2, setting a thresholds to determin only final positive pseduo-label (0, 1)
logits_pl = torch.zeros_like(similarity)
logits_pl = torch.where(similarity > self.threshold_up, self.scalar_pos, logits_pl)
#logits_pl = torch.where(similarity < self.threshold_down, self.scalar_neg, logits_pl)
#print("logits_pl: ", logits_pl)
'''
#options 3, setting two thresholds to determin the final pos./neg. pseduo-label (-1, 0, 1)
logits_pl = torch.zeros_like(similarity)
#positive pseudo label
logits_pl = torch.where(similarity > self.threshold, self.scalar_pos, logits_pl)
'''
#negative pseudo label
num_elements = int(self.partial * self.classes)
#print("num_elements: ", num_elements)
_, smallest_indices = torch.topk(similarity, num_elements, largest=False)
#print("smallest_indices: ", smallest_indices)
values = torch.full(smallest_indices.shape, -1, dtype=logits_pl.dtype).to(self.device)
logits_pl.scatter_(1, smallest_indices, values)
'''
vision = self.vision_extractor(x)
#print("logits_pl: ", logits_pl)
logits = self.linear_classifier(torch.squeeze(vision))
elif self.arch == 'convnext_xlarge_22k':
# x is a batch of images
feats = self.feature_extractor(xs)
image_embedding = feats.image_embeds
image_embedding = F.normalize(image_embedding, dim=-1)
similarity = image_embedding @ y.T
#print("similarity: ", similarity.shape)
#The similarity should be a T-softmax
similarity = self.temperature_scaled_softmax(similarity, temperature=self.temperature)
#print("similarity: ", similarity)
#options 1, setting a threshold to determin final pseduo-label (-1, 1) (too aggressive, not work)
#logits_pl = torch.where(similarity > self.threshold_up, self.scalar_pos, self.scalar_neg)
'''
#options 2, setting a thresholds to determin only final positive pseduo-label (0, 1)
logits_pl = torch.zeros_like(similarity)
logits_pl = torch.where(similarity > self.threshold_up, self.scalar_pos, logits_pl)
#logits_pl = torch.where(similarity < self.threshold_down, self.scalar_neg, logits_pl)
#print("logits_pl: ", logits_pl)
'''
#options 3, setting two thresholds to determin the final pos./neg. pseduo-label (-1, 0, 1)
logits_pl = torch.zeros_like(similarity)
#positive pseudo label
logits_pl = torch.where(similarity > self.threshold, self.scalar_pos, logits_pl)
'''
#negative pseudo label
num_elements = int(self.partial * self.classes)
#print("num_elements: ", num_elements)
_, smallest_indices = torch.topk(similarity, num_elements, largest=False)
#print("smallest_indices: ", smallest_indices)
values = torch.full(smallest_indices.shape, -1, dtype=logits_pl.dtype).to(self.device)
logits_pl.scatter_(1, smallest_indices, values)
'''
logits = self.vision_extractor(x)
elif self.arch == 'convnext_xlarge_1k':
# x is a batch of images
feats = self.feature_extractor(xs)
image_embedding = feats.image_embeds
image_embedding = F.normalize(image_embedding, dim=-1)
similarity = image_embedding @ y.T
#print("similarity: ", similarity.shape)
#The similarity should be a T-softmax
similarity = self.temperature_scaled_softmax(similarity, temperature=self.temperature)
#print("similarity: ", similarity)
#options 1, setting a threshold to determin final pseduo-label (-1, 1) (too aggressive, not work)
#logits_pl = torch.where(similarity > self.threshold_up, self.scalar_pos, self.scalar_neg)
'''
#options 2, setting a thresholds to determin only final positive pseduo-label (0, 1)
logits_pl = torch.zeros_like(similarity)
logits_pl = torch.where(similarity > self.threshold_up, self.scalar_pos, logits_pl)
#logits_pl = torch.where(similarity < self.threshold_down, self.scalar_neg, logits_pl)
#print("logits_pl: ", logits_pl)
'''
#options 3, setting two thresholds to determin the final pos./neg. pseduo-label (-1, 0, 1)
logits_pl = torch.zeros_like(similarity)
#positive pseudo label
logits_pl = torch.where(similarity > self.threshold, self.scalar_pos, logits_pl)
'''
#negative pseudo label
num_elements = int(self.partial * self.classes)
#print("num_elements: ", num_elements)
_, smallest_indices = torch.topk(similarity, num_elements, largest=False)
#print("smallest_indices: ", smallest_indices)
values = torch.full(smallest_indices.shape, -1, dtype=logits_pl.dtype).to(self.device)
logits_pl.scatter_(1, smallest_indices, values)
'''
logits = self.vision_extractor(x)
else:
# x is a batch of images
feats = self.resnet50(x)
logits = self.linear_classifier(torch.squeeze(feats))
return logits, logits_pl, similarity
class MultilabelModel(torch.nn.Module):
def __init__(self, P, Z, feature_extractor, linear_classifier):
super(MultilabelModel, self).__init__()
self.f = ImageClassifier(P, Z, feature_extractor, linear_classifier)
def forward(self, batch):
f_logits = self.f(batch['image'])
return f_logits
#Baseline Model
class ImageClassifier_baseline(torch.nn.Module):
def __init__(self, P, model_feature_extractor=None, model_linear_classifier=None):
super(ImageClassifier_baseline, self).__init__()
print('initializing image classifier')
model_feature_extractor_in = copy.deepcopy(model_feature_extractor)
model_linear_classifier_in = copy.deepcopy(model_linear_classifier)
self.arch = P['arch']
if self.arch == 'resnet50':
# configure feature extractor:
if model_feature_extractor_in is not None:
print('feature extractor: specified by user')
feature_extractor = model_feature_extractor_in
else:
if P['use_pretrained']:
print('feature extractor: imagenet pretrained')
feature_extractor = resnet50(pretrained=True)
else:
print('feature extractor: randomly initialized')
feature_extractor = resnet50(pretrained=False)
feature_extractor = torch.nn.Sequential(*list(feature_extractor.children())[:-1])
if P['freeze_feature_extractor']:
print('feature extractor frozen')
for param in feature_extractor.parameters():
param.requires_grad = False
else:
print('feature extractor trainable')
for param in feature_extractor.parameters():
param.requires_grad = True
feature_extractor.avgpool = torch.nn.AdaptiveAvgPool2d(1)
self.feature_extractor = feature_extractor
# configure final fully connected layer:
if model_linear_classifier_in is not None:
print('linear classifier layer: specified by user')
linear_classifier = model_linear_classifier_in
else:
print('linear classifier layer: randomly initialized')
linear_classifier = torch.nn.Linear(P['feat_dim'], P['num_classes'], bias=True)
self.linear_classifier = linear_classifier
elif self.arch == 'linear':
print('training a linear classifier only')
self.feature_extractor = None
self.linear_classifier = FCNet(P['feat_dim'], P['num_classes'])
elif self.arch == 'clip_vision_baseline':
print('training CLIP_Vision Encoder') #
self.feature_extractor = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14-336",output_attentions=True)
for p in self.feature_extractor.parameters():
p.requires_grad = True
self.linear_classifier = FCNet(768, P['num_classes'])
self.threshold_up = P['threshold']
self.scalar_pos = torch.tensor(1, dtype=torch.float32).to(Z['device'])
self.temperature = 0.1
self.classes = P['num_classes']
self.device = Z['device']
else:
raise ValueError('Architecture not implemented.')
def forward(self, x):
if self.arch == 'linear':
# x is a batch of feature vectors
logits = self.linear_classifier(x)
elif self.arch == 'clip_vision_baseline':
feats = self.feature_extractor(x)
logits = self.linear_classifier(feats.image_embeds)#feats.pooler_output
else:
# x is a batch of images
feats = self.feature_extractor(x)
logits = self.linear_classifier(torch.squeeze(feats))
return logits
class MultilabelModel_baseline(torch.nn.Module):
def __init__(self, P, feature_extractor, linear_classifier):
super(MultilabelModel_baseline, self).__init__()
self.f = ImageClassifier_baseline(P, feature_extractor, linear_classifier)
def forward(self, batch):
f_logits = self.f(batch['image'])
return f_logits