-
Notifications
You must be signed in to change notification settings - Fork 24
/
trainer.py
414 lines (297 loc) · 12.9 KB
/
trainer.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
412
413
414
import os
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from time import time
from gnn import GNN_module
def np2cuda(array):
tensor = torch.from_numpy(array)
if torch.cuda.is_available():
tensor = tensor.cuda()
return tensor
def tensor2cuda(tensor):
if torch.cuda.is_available():
tensor = tensor.cuda()
return tensor
class myModel(nn.Module):
def __init__(self):
super(myModel, self).__init__()
def load(self, file_name):
self.load_state_dict(torch.load(file_name, map_location=lambda storage, loc: storage))
def save(self, file_name):
torch.save(self.state_dict(), file_name)
###############################################################
## Vanilla CNN model, used to extract visual features
class EmbeddingCNN(myModel):
def __init__(self, image_size, cnn_feature_size, cnn_hidden_dim, cnn_num_layers):
super(EmbeddingCNN, self).__init__()
module_list = []
dim = cnn_hidden_dim
for i in range(cnn_num_layers):
if i == 0:
module_list.append(nn.Conv2d(3, dim, 3, 1, 1, bias=False))
module_list.append(nn.BatchNorm2d(dim))
else:
module_list.append(nn.Conv2d(dim, dim*2, 3, 1, 1, bias=False))
module_list.append(nn.BatchNorm2d(dim*2))
dim *= 2
module_list.append(nn.MaxPool2d(2))
module_list.append(nn.LeakyReLU(0.1, True))
image_size //= 2
module_list.append(nn.Conv2d(dim, cnn_feature_size, image_size, 1, bias=False))
module_list.append(nn.BatchNorm2d(cnn_feature_size))
module_list.append(nn.LeakyReLU(0.1, True))
self.module_list = nn.ModuleList(module_list)
def forward(self, inputs):
for l in self.module_list:
inputs = l(inputs)
outputs = inputs.view(inputs.size(0), -1)
return outputs
def freeze_weight(self):
for p in self.parameters():
p.requires_grad = False
class GNN(myModel):
def __init__(self, cnn_feature_size, gnn_feature_size, nway):
super(GNN, self).__init__()
num_inputs = cnn_feature_size + nway
graph_conv_layer = 2
self.gnn_obj = GNN_module(nway=nway, input_dim=num_inputs,
hidden_dim=gnn_feature_size,
num_layers=graph_conv_layer,
feature_type='dense')
def forward(self, inputs):
logits = self.gnn_obj(inputs).squeeze(-1)
return logits
class gnnModel(myModel):
def __init__(self, nway):
super(myModel, self).__init__()
image_size = 32
cnn_feature_size = 64
cnn_hidden_dim = 32
cnn_num_layers = 3
gnn_feature_size = 32
self.cnn_feature = EmbeddingCNN(image_size, cnn_feature_size, cnn_hidden_dim, cnn_num_layers)
self.gnn = GNN(cnn_feature_size, gnn_feature_size, nway)
def forward(self, data):
[x, _, _, _, xi, _, one_hot_yi, _] = data
z = self.cnn_feature(x)
zi_s = [self.cnn_feature(xi[:, i, :, :, :]) for i in range(xi.size(1))]
zi_s = torch.stack(zi_s, dim=1)
# follow the paper, concatenate the information of labels to input features
uniform_pad = torch.FloatTensor(one_hot_yi.size(0), 1, one_hot_yi.size(2)).fill_(
1.0/one_hot_yi.size(2))
uniform_pad = tensor2cuda(uniform_pad)
labels = torch.cat([uniform_pad, one_hot_yi], dim=1)
features = torch.cat([z.unsqueeze(1), zi_s], dim=1)
nodes_features = torch.cat([features, labels], dim=2)
out_logits = self.gnn(inputs=nodes_features)
logsoft_prob = F.log_softmax(out_logits, dim=1)
return logsoft_prob
class Trainer():
def __init__(self, trainer_dict):
self.num_labels = 100
self.args = trainer_dict['args']
self.logger = trainer_dict['logger']
if self.args.todo == 'train':
self.tr_dataloader = trainer_dict['tr_dataloader']
if self.args.model_type == 'gnn':
Model = gnnModel
self.model = Model(nway=self.args.nway)
self.logger.info(self.model)
self.total_iter = 0
self.sample_size = 32
def load_model(self, model_dir):
self.model.load(model_dir)
print('load model sucessfully...')
def load_pretrain(self, model_dir):
self.model.cnn_feature.load(model_dir)
print('load pretrain feature sucessfully...')
def model_cuda(self):
if torch.cuda.is_available():
self.model.cuda()
def eval(self, dataloader, test_sample):
self.model.eval()
args = self.args
iteration = int(test_sample/self.args.batch_size)
total_loss = 0.0
total_sample = 0
total_correct = 0
with torch.no_grad():
for i in range(iteration):
data = dataloader.load_te_batch(batch_size=args.batch_size,
nway=args.nway, num_shots=args.shots)
data_cuda = [tensor2cuda(_data) for _data in data]
logsoft_prob = self.model(data_cuda)
label = data_cuda[1]
loss = F.nll_loss(logsoft_prob, label)
total_loss += loss.item() * logsoft_prob.shape[0]
pred = torch.argmax(logsoft_prob, dim=1)
# print(pred)
# print(torch.eq(pred, label).float().sum().item())
# print(label)
assert pred.shape == label.shape
total_correct += torch.eq(pred, label).float().sum().item()
total_sample += pred.shape[0]
print('correct: %d / %d' % (total_correct, total_sample))
print(total_correct)
return total_loss / total_sample, 100.0 * total_correct / total_sample
def train_batch(self):
self.model.train()
args = self.args
data = self.tr_dataloader.load_tr_batch(batch_size=args.batch_size,
nway=args.nway, num_shots=args.shots)
data_cuda = [tensor2cuda(_data) for _data in data]
self.opt.zero_grad()
logsoft_prob = self.model(data_cuda)
# print('pred', torch.argmax(logsoft_prob, dim=1))
# print('label', data[2])
label = data_cuda[1]
loss = F.nll_loss(logsoft_prob, label)
loss.backward()
self.opt.step()
return loss.item()
def train(self):
if self.args.freeze_cnn:
self.model.cnn_feature.freeze_weight()
print('freeze cnn weight...')
best_loss = 1e8
best_acc = 0.0
stop = 0
eval_sample = 5000
self.model_cuda()
self.model_dir = os.path.join(self.args.model_folder, 'model.pth')
self.opt = torch.optim.Adam(
filter(lambda p: p.requires_grad, self.model.parameters()),
lr=self.args.lr,
weight_decay=1e-6)
# self.opt = torch.optim.Adam(self.model.parameters(), lr=self.args.lr,
# weight_decay=1e-6)
start = time()
tr_loss_list = []
for i in range(self.args.max_iteration):
tr_loss = self.train_batch()
tr_loss_list.append(tr_loss)
if i % self.args.log_interval == 0:
self.logger.info('iter: %d, spent: %.4f s, tr loss: %.5f' % (i, time() - start,
np.mean(tr_loss_list)))
del tr_loss_list[:]
start = time()
if i % self.args.eval_interval == 0:
va_loss, va_acc = self.eval(self.tr_dataloader, eval_sample)
self.logger.info('================== eval ==================')
self.logger.info('iter: %d, va loss: %.5f, va acc: %.4f %%' % (i, va_loss, va_acc))
self.logger.info('==========================================')
if va_loss < best_loss:
stop = 0
best_loss = va_loss
best_acc = va_acc
if self.args.save:
self.model.save(self.model_dir)
stop += 1
start = time()
if stop > self.args.early_stop:
break
self.total_iter += 1
self.logger.info('============= best result ===============')
self.logger.info('best loss: %.5f, best acc: %.4f %%' % (best_loss, best_acc))
def test(self, test_data_array, te_dataloader):
self.model_cuda()
self.model.eval()
start = 0
end = 0
args = self.args
batch_size = args.batch_size
pred_list = []
with torch.no_grad():
while start < test_data_array.shape[0]:
end = start + batch_size
if end >= test_data_array.shape[0]:
batch_size = test_data_array.shape[0] - start
data = te_dataloader.load_te_batch(batch_size=batch_size, nway=args.nway,
num_shots=args.shots)
test_x = test_data_array[start:end]
data[0] = np2cuda(test_x)
data_cuda = [tensor2cuda(_data) for _data in data]
map_label2class = data[-1].cpu().numpy()
logsoft_prob = self.model(data_cuda)
# print(logsoft_prob)
pred = torch.argmax(logsoft_prob, dim=1).cpu().numpy()
pred = map_label2class[range(len(pred)), pred]
pred_list.append(pred)
start = end
return np.hstack(pred_list)
def pretrain_eval(self, loader, cnn_feature, classifier):
total_loss = 0
total_sample = 0
total_correct = 0
with torch.no_grad():
for j, (data, label) in enumerate(loader):
data = tensor2cuda(data)
label = tensor2cuda(label)
output = classifier(cnn_feature(data))
output = F.log_softmax(output, dim=1)
loss = F.nll_loss(output, label)
total_loss += loss.item() * output.shape[0]
pred = torch.argmax(output, dim=1)
assert pred.shape == label.shape
total_correct += torch.eq(pred, label).float().sum().item()
total_sample += pred.shape[0]
return total_loss / total_sample, 100.0 * total_correct / total_sample
def pretrain(self, pretrain_dataset, test_dataset):
pretrain_loader = torch.utils.data.DataLoader(pretrain_dataset,
batch_size=self.args.batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset,
batch_size=self.args.batch_size, shuffle=True)
self.model_cuda()
best_loss = 1e8
self.model_dir = os.path.join(self.args.model_folder, 'pretrain_model.pth')
cnn_feature = self.model.cnn_feature
classifier = nn.Linear(list(cnn_feature.parameters())[-3].shape[0], self.num_labels)
if torch.cuda.is_available():
classifier.cuda()
self.pretrain_opt = torch.optim.Adam(
list(cnn_feature.parameters()) + list(classifier.parameters()),
lr=self.args.lr,
weight_decay=1e-6)
start = time()
for i in range(10000):
total_tr_loss = []
for j, (data, label) in enumerate(pretrain_loader):
data = tensor2cuda(data)
label = tensor2cuda(label)
output = classifier(cnn_feature(data))
output = F.log_softmax(output, dim=1)
loss = F.nll_loss(output, label)
self.pretrain_opt.zero_grad()
loss.backward()
self.pretrain_opt.step()
total_tr_loss.append(loss.item())
te_loss, te_acc = self.pretrain_eval(test_loader, cnn_feature, classifier)
self.logger.info('iter: %d, tr loss: %.5f, spent: %.4f s' % (i, np.mean(total_tr_loss),
time() - start))
self.logger.info('--> eval: te loss: %.5f, te acc: %.4f %%' % (te_loss, te_acc))
if te_loss < best_loss:
stop = 0
best_loss = te_loss
if self.args.save:
cnn_feature.save(self.model_dir)
stop += 1
start = time()
if stop > self.args.early_stop_pretrain:
break
if __name__ == '__main__':
import os
b_s = 10
nway = 5
shots = 5
batch_x = torch.rand(b_s, 3, 32, 32).cuda()
batches_xi = [torch.rand(b_s, 3, 32, 32).cuda() for i in range(nway*shots)]
label_x = torch.rand(b_s, nway).cuda()
labels_yi = [torch.rand(b_s, nway).cuda() for i in range(nway*shots)]
print('create model...')
model = gnnModel(128, nway).cuda()
# print(list(model.cnn_feature.parameters())[-3].shape)
# print(len(list(model.parameters())))
print(model([batch_x, label_x, None, None, batches_xi, labels_yi, None]).shape)