-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl_models.py
344 lines (261 loc) · 13.3 KB
/
ssl_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
# -*- coding: utf-8 -*-
import os
import sys
import copy
from tqdm import tqdm
import torch
import torchvision
from torchsummary import summary
import torchvision.transforms as transforms
# from vgg import VGG16
import config
def return_accuracy(outputs, targets):
print("len(outputs):", len(outputs), "len(targets):", len(targets))
print("outputs[0].shape:", outputs[0].shape)
print("argmax(outputs[0]).shape:", torch.argmax(outputs[0], dim=1).shape)
print("targets[0].shape", targets[0].shape)
accuracy = []
for output, target in zip(outputs, targets):
print("output.shape:", output.shape, "target.shape:", target.shape)
acc = torch.sum(torch.argmax(output, dim=1) == target) / len(target)
accuracy.append(acc)
return accuracy
class MultiTaskLoss(torch.nn.Module):
def __init__(self):
super(MultiTaskLoss, self).__init__()
self.bceloss = torch.nn.CrossEntropyLoss().to(config.device)
def regularize(self, curr_model, prev_model, alpha=config.alpha, beta=config.beta):
if prev_model == None:
return 0
fc_loss = 0.0
conv_loss = 0.0
cos = torch.nn.CosineSimilarity(dim=1, eps=1e-6)
for name, param in curr_model.named_parameters():
if name.startswith("fc"):
fc_loss += torch.norm(param)
else:
old_param_name = name[18:]
for o_name, o_param in prev_model.named_parameters():
if o_name == old_param_name:
if config.regularization == 'l2_norm':
loss = torch.norm(param - o_param)
elif config.regularization == 'cosine':
loss = cos(param, o_param)
else:
print("Unkown regularization..\ncheck config")
conv_loss += loss
return 0.5 * alpha * conv_loss + 0.5 * beta * fc_loss
def forward(self, preds, targets, curr_model, prev_model):
loss = []
for i in range(config.num_tasks):
task_loss = self.bceloss(preds[i].view(-1, config.num_classes), targets[:,i])
loss.append(task_loss)
total_loss = torch.Tensor([0])
total_loss = total_loss.to(config.device)
for i in range(config.num_tasks):
total_loss += loss[i]
reg_loss = self.regularize(curr_model, prev_model)
total_loss += reg_loss
return loss, reg_loss, total_loss
class OWM_ConvNet_3(torch.nn.Module):
def __init__(self, inputsize):
super(OWM_ConvNet_3, self).__init__()
ncha, size, _ = inputsize
self.relu = torch.nn.ReLU()
self.maxpool = torch.nn.MaxPool2d(2)
self.drop1 = torch.nn.Dropout(0.2)
self.padding = torch.nn.ReplicationPad2d(1)
self.c1 = torch.nn.Conv2d(ncha, 64, kernel_size=3, stride=1, padding=0, bias=False)
self.c2 = torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=0, bias=False)
self.c3 = torch.nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=0, bias=False)
#self.c4 = torch.nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=0, bias=False)
return
def forward(self, x,):
h_list = []
x_list = []
# Gated
x = self.padding(x)
x_list.append(torch.mean(x, 0, True))
con1 = self.drop1(self.relu(self.c1(x)))
con1_p = self.maxpool(con1)
con1_p = self.padding(con1_p)
x_list.append(torch.mean(con1_p, 0, True))
con2 = self.drop1(self.relu(self.c2(con1_p)))
con2_p = self.maxpool(con2)
con2_p = self.padding(con2_p)
x_list.append(torch.mean(con2_p, 0, True))
con3 = self.drop1(self.relu(self.c3(con2_p)))
con3_p = self.maxpool(con3)
return con3_p
class Network(torch.nn.Module):
def __init__(self, pretrained=True):
super(Network, self).__init__()
if config.model == 'owm_3':
self.flatten_shape = 256*4*4
self.feature_extractor = OWM_ConvNet_3((config.num_channels, config.image_shape, config.image_shape)).to(config.device)
print(self.feature_extractor)
elif config.model == 'owm_5':
self.flatten_shape = 1024*6*6
self.feature_extractor = OWM_ConvNet_5((config.num_channels, config.image_shape, config.image_shape)).to(config.device)
elif config.model == 'owm_8':
self.flatten_shape = 1024*4*4
self.feature_extractor = OWM_ConvNet_8((config.num_channels, config.image_shape, config.image_shape)).to(config.device)
else:
print("Unknown model")
self.softmax = torch.nn.Softmax(dim=1)
self.fc1 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes), # Because all attributes are binary
)
self.fc2 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes),
)
self.fc3 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes),
)
self.fc4 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes),
)
self.fc5 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes),
)
self.fc6 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes),
)
self.fc7 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes),
)
self.fc8 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes),
)
self.fc9 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes),
)
self.fc10 = torch.nn.Sequential(
torch.nn.Linear(in_features=self.flatten_shape, out_features=128),
torch.nn.ReLU(),
torch.nn.Linear(in_features=128, out_features=config.num_classes),
)
self.fcs = [self.fc1, self.fc2, self.fc3, self.fc4, self.fc5, self.fc6, self.fc7, self.fc8, self.fc9, self.fc10]
# self.fcs = [self.fc for i in range(config.num_attribs)]
def forward(self, x):
features = self.feature_extractor(x)
flat_features = features.view(features.size(0), -1)
if config.pretext_task == 'label_augmentation':
task_preds = []
for i in range(config.num_tasks):
#task_preds.append(torch.squeeze(torch.argmax(self.fcs[i](flat_features), dim=1)))
task_preds.append(self.fcs[i](flat_features))
return task_preds
elif config.pretext_task == 'rotation':
return self.fc_rotation(flat_features)
else:
print("Unknown pre-text task..")
class SSL_Network(torch.nn.Module):
def __init__(self):
super(SSL_Network, self).__init__()
self.model = Network().to(config.device)
self.previous_model = None
def forward(self, x):
task_predictions = self.model(x)
return task_predictions
def init_loss(self):
if config.pretext_task == 'rotation': return torch.nn.CrossEntropyLoss()
elif config.pretext_task == 'label_augmentation': return MultiTaskLoss()
def init_optimizer(self, lr=config.lr, w_d=config.w_d, mom=config.momentum):
return torch.optim.SGD(self.model.parameters(), lr=lr, weight_decay=w_d, momentum=mom)
def init_previous_downstream_model(self, model):
self.previous_model = model
def fit(self, trainloader, valloader=False, num_epochs=config.num_epochs):
self.model = self.model.train()
self.model = self.model.to(config.device)
d = {'loss': [], 'accuracy':[]}
total_loss_1 = 0
loss_function = self.init_loss()
optimizer = self.init_optimizer()
for epoch in range(1, num_epochs+1):
print("epoch : ", epoch)
running_loss, total_loss_1 = 0, 0
with tqdm(total=len(trainloader)) as pbar:
for batch_idx, batch in enumerate(trainloader):
images, targets = batch
images = images.to(config.device)
targets = targets.to(config.device).long()
outputs = self.model(images)
if config.pretext_task == 'rotation':
loss = loss_function(outputs, targets)
pbar.set_postfix(Epochs='{}'.format(epoch),
Loss='{0:6f}'.format(loss.item()))
elif config.pretext_task == 'label_augmentation':
individual_loss, reg_loss, loss = loss_function(outputs, targets, self.model, self.previous_model)
pbar.set_postfix(Epochs='{}'.format(epoch),
Reg_loss='{0:4f}'.format(reg_loss),
Loss='{0:6f}'.format(loss.item()))
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item()
pbar.update(1)
epoch_loss = running_loss/len(trainloader)
print("\nepoch_loss:", epoch_loss, " 1st attribute:", total_loss_1/len(trainloader))
d['loss'].append(epoch_loss)
print("................................")
return d
def evaluate(self, testloader):
self.model.eval()
bceloss = self.init_loss()
total_loss, acc, count = 0.0, 0.0, 0
with tqdm(total=len(testloader)) as pbar:
for batch_idx, batch in enumerate(testloader):
images, targets = batch
images = images.to(config.device)
targets = targets.to(config.device).long()
output = self.model(images)
loss = bceloss(output, targets)
output = torch.transpose(torch.stack([torch.reshape(p>=0.5, (config.batch_size,)) for p in output]), 0, 1).float()
acc += torch.sum(output == targets)
total_loss += loss.item()
count += config.batch_size
pbar.set_postfix(Loss='{0:10f}'.format(loss.item()),
accuracy='{0:.4f}'.format(float(float(acc)/float(count))))
pbar.update(1)
print(" Total Training loss: {0:.4f}".format(total_loss),
" Test Accuracy:{0:.4f}".format(float(float(acc)/float(len(testloader)*config.batch_size))))
def save_model(self, model_fname=None):
torch.save(self.model.feature_extractor.state_dict(), model_fname)
print("\nModel successfully saved.")
def load_model(self):
self.model.load_state_dict(torch.load(os.path.join('saved_models', 'ssl_model_'+config.model+'.pt')))
print("Saved model loaded..")
def load_weights(self, dmodel):
d_dict = dmodel.state_dict()
d = copy.deepcopy(self.model.state_dict())
d['feature_extractor.c1.weight'] = copy.deepcopy(d_dict['c1.weight'])
d['feature_extractor.c2.weight'] = copy.deepcopy(d_dict['c2.weight'])
d['feature_extractor.c3.weight'] = copy.deepcopy(d_dict['c3.weight'])
if config.num_layers > 3:
d['feature_extractor.c4.weight'] = copy.deepcopy(d_dict['c4.weight'])
d['feature_extractor.c5.weight'] = copy.deepcopy(d_dict['c5.weight'])
if config.num_layers > 5:
d['feature_extractor.c6.weight'] = copy.deepcopy(d_dict['c6.weight'])
d['feature_extractor.c7.weight'] = copy.deepcopy(d_dict['c7.weight'])
d['feature_extractor.c8.weight'] = copy.deepcopy(d_dict['c8.weight'])
self.model.load_state_dict(d)
print("Weights loaded from previous iteration for SSL network.")