forked from BCV-Uniandes/AUNets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
594 lines (514 loc) · 21.8 KB
/
solver.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import pdb
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import os
import csv
import time
import datetime
from torch.autograd import Variable
from torchvision.utils import save_image
import tqdm
import glob
from utils import F1_TEST
import warnings
warnings.filterwarnings('ignore')
class Solver(object):
def __init__(self, rgb_loader, config, of_loader=None):
#pdb.set_trace()
# Data loader
self.rgb_loader = rgb_loader
# Optical Flow
self.of_loader = of_loader
self.of_loader_val = None
self.OF = config.OF
self.OF_option = config.OF_option
# Hydra
self.HYDRA = config.HYDRA
# Training settings
self.mode = config.mode
self.image_size = config.image_size
self.lr = config.lr
self.beta1 = config.beta1
self.beta2 = config.beta2
self.dataset = config.dataset
self.num_epochs = config.num_epochs
self.num_epochs_decay = config.num_epochs_decay
self.batch_size = config.batch_size
self.finetuning = config.finetuning
self.pretrained_model = config.pretrained_model
self.use_tensorboard = config.use_tensorboard
self.stop_training = config.stop_training
# Test settings
self.test_model = config.test_model
self.metadata_path = config.metadata_path
# Path
self.log_path = config.log_path
self.model_save_path = config.model_save_path
self.fold = config.fold
self.mode_data = config.mode_data
self.xlsfile = config.xlsfile
# Step size
self.log_step = config.log_step
# MISC
self.GPU = config.GPU
self.AU = config.AU
self.SHOW_MODEL = config.SHOW_MODEL
self.TEST_TXT = config.TEST_TXT
self.TEST_PTH = config.TEST_PTH
self.DONE = False
self.string_ = '00'
self.TRAINED_FILE = os.path.join(self.model_save_path, 'TRAINED')
# pdb.set_trace()
if self.pretrained_model and not self.SHOW_MODEL and not \
self.DEMO and self.mode == 'train':
txt_file = glob.glob(os.path.join(self.model_save_path, '*.txt'))
if txt_file and not self.TEST_PTH:
self.TEST_TXT = True
os.system('touch {}'.format(self.TRAINED_FILE))
if os.path.isfile(self.TRAINED_FILE):
print("!!!Model already trained")
self.DONE = True
if self.TEST_TXT:
return
# Build tensorboard if use
if config.mode != 'sample':
self.build_model()
if self.SHOW_MODEL:
return
if self.use_tensorboard:
self.build_tensorboard()
# Start with trained model
if self.pretrained_model!='False':
self.load_pretrained_model()
# ====================================================================#
# ====================================================================#
def display_net(self):
# pip install git+https://github.com/szagoruyko/pytorchviz
try:
from torchviz import make_dot
except ImportError:
raise ImportError(
"pip install git+https://github.com/szagoruyko/pytorchviz")
from utils import pdf2png
input_ = self.to_var(
torch.randn(1, 3, 224, 224).type(torch.FloatTensor))
start_time = time.time()
if self.OF_option != 'None':
y = self.C(input_, input_)
else:
y = self.C(input_)
elapsed = time.time() - start_time
elapsed = str(datetime.timedelta(seconds=elapsed))
print("Forward time: " + elapsed)
g = make_dot(y, params=dict(self.C.named_parameters()))
filename = 'misc/VGG16-OF_{}'.format(self.OF_option)
g.filename = filename
g.render()
os.remove(filename)
pdf2png(filename)
print('Network saved at {}.png'.format(filename))
# ====================================================================#
# ====================================================================#
def get_trainable_params(self):
trainable_params = self.C.parameters()
name_params = sorted(
[name for name, param in nn.Module.named_parameters(self.C)],
reverse=True)
if self.OF_option != 'None':
trainable_params = [
param for name, param in nn.Module.named_parameters(self.C)
if 'rgb' not in name
]
_name = [
name for name, param in nn.Module.named_parameters(self.C)
if 'rgb' not in name
]
name_params = sorted(_name, reverse=True)
if self.HYDRA:
trainable_params = self.C.model.classifier.parameters()
_name = [
name for name, param in nn.Module.named_parameters(self.C)
if 'features' not in name
]
name_params = sorted(_name, reverse=True)
return trainable_params, name_params
# ====================================================================#
# ====================================================================#
def build_model(self):
# Define a generator and a discriminator
if self.TEST_TXT:
return
from models.vgg16 import Classifier
self.C = Classifier(
pretrained=self.finetuning,
OF_option=self.OF_option,
model_save_path=self.model_save_path,
test_model=self.pretrained_model)
trainable_params, name_params = self.get_trainable_params()
if self.mode == 'train' and not self.DEMO:
print(
"==============\nTrainable layers:\n{}\n==============".format(
str(name_params)))
# Optimizer
self.optimizer = torch.optim.Adam(trainable_params, self.lr,
[self.beta1, self.beta2])
# Loss
self.LOSS = nn.BCEWithLogitsLoss()
# Print network
if self.mode == 'train' and not self.DEMO:
self.print_network(self.C, 'Classifier - OF: ' + self.OF_option)
if torch.cuda.is_available():
self.C.cuda()
# ====================================================================#
# ====================================================================#
def print_network(self, model, name):
num_params = 0
for p in model.parameters():
num_params += p.numel()
num_learn_params = 0
learnable_params, _ = self.get_trainable_params()
for p in learnable_params:
num_learn_params += p.numel()
if self.SHOW_MODEL:
print(name)
print(model)
print("The number of parameters (OF: {}): {}".format(
self.OF_option, num_params))
print("The number of learnable parameters (OF: {}): {}".format(
self.OF_option, num_learn_params))
# ====================================================================#
# ====================================================================#
def load_pretrained_model(self):
# pdb.set_trace()
if self.pretrained_model == '':
model = os.path.join(self.model_save_path,
'{}.pth'.format(self.pretrained_model))
else:
model = self.pretrained_model
self.C.load_state_dict(torch.load(model))
print(' [!!] loaded trained model: {}!'.format(model))
# ====================================================================#
# ====================================================================#
def build_tensorboard(self):
from logger import Logger
self.logger = Logger(self.log_path)
# ====================================================================#
# ====================================================================#
def update_lr(self, lr):
for param_group in self.optimizer.param_groups:
param_group['lr'] = lr
# ====================================================================#
# ====================================================================#
def reset_grad(self):
self.optimizer.zero_grad()
# ====================================================================#
# ====================================================================#
def to_var(self, x, volatile=False):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x, volatile=volatile)
# ====================================================================#
# ====================================================================#
def denorm(self, x):
if self.finetuning == 'imagenet':
normalize = {
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225]
}
else:
normalize = {'mean': [0.485, 0.456, 0.406], 'std': [1., 1., 1.]}
std = torch.from_numpy(
np.reshape(normalize['std'], (1, -1, 1, 1)).astype(np.float32))
mean = torch.from_numpy(
np.reshape(normalize['mean'], (1, -1, 1, 1)).astype(np.float32))
out = (x * std) + mean
# out = (x + 1) / 2
return out.clamp_(0, 1)
# ====================================================================#
# ====================================================================#
def threshold(self, x):
x = x.clone()
x = (x >= 0.5).float()
return x
# ====================================================================#
# ====================================================================#
def train(self):
if self.DONE:
return
iters_per_epoch = len(self.rgb_loader)
# lr cache for decaying
lr = self.lr
# Start with trained model if exists
if self.pretrained_model:
start = int(self.pretrained_model.split('_')[0])
# Decay learning rate
for i in range(start):
if (i + 1) > (self.num_epochs - self.num_epochs_decay):
lr -= (self.lr / float(self.num_epochs_decay))
self.update_lr(lr)
print('Decay learning rate to: {}.'.format(lr))
else:
start = 0
last_model_step = len(self.rgb_loader)
print("Log path: " + self.log_path)
Log = "[AUNets] OF:{}, bs:{}, AU:{}, fold:{}, \
GPU:{}, !{}, from:{}".format(self.OF_option, self.batch_size,
str(self.AU).zfill(2), self.fold,
self.GPU, self.mode_data,
self.finetuning)
loss_cum = {}
loss_cum['LOSS'] = []
flag_init = True
f1_val_prev = 0
non_decreasing = 0
# Start training
start_time = time.time()
for e in range(start, self.num_epochs):
E = str(e + 1).zfill(2)
self.C.train()
if flag_init:
f1_val, loss_val, f1_one = self.val(init=True)
log = '[F1_VAL: %0.3f (F1_VAL_1: %0.3f) LOSS_VAL: %0.3f]' % (
f1_val, f1_one, loss_val)
if self.pretrained_model:
f1_val_prev = f1_val
print(log)
flag_init = False
if self.OF:
of_loader = iter(self.of_loader)
print("--> RGB and OF # lines: %d - %d" % (len(
self.rgb_loader), len(of_loader)))
for i, (rgb_img, rgb_label, rgb_files) in tqdm.tqdm(
enumerate(self.rgb_loader),
total=len(self.rgb_loader),
ncols=10,
desc='Epoch: %d/%d | %s' % (e, self.num_epochs, Log)):
rgb_img = self.to_var(rgb_img)
rgb_label = self.to_var(rgb_label)
if not self.OF:
out = self.C(rgb_img)
else:
of_img, of_label, of_files = next(of_loader)
if not of_label.eq(rgb_label.data.cpu()).all():
print("OF and RGB must have the same labels")
of_img = self.to_var(of_img)
out = self.C(rgb_img, OF=of_img)
loss_cls = self.LOSS(out, rgb_label)
# # Backward + Optimize
self.reset_grad()
loss_cls.backward()
self.optimizer.step()
# Logging
loss = {}
loss['LOSS'] = loss_cls.data[0]
loss_cum['LOSS'].append(loss_cls.data[0])
# Print out log info
if (i + 1) % self.log_step == 0 or (i + 1) == last_model_step:
if self.use_tensorboard:
for tag, value in loss.items():
self.logger.scalar_summary(
tag, value, e * iters_per_epoch + i + 1)
# F1 val
f1_val, loss_val = self.val()
if self.use_tensorboard:
self.logger.scalar_summary('F1_val: ', f1_val,
e * iters_per_epoch + i + 1)
self.logger.scalar_summary('LOSS_val: ', loss_val,
e * iters_per_epoch + i + 1)
for tag, value in loss_cum.items():
self.logger.scalar_summary(tag,
np.array(value).mean(),
e * iters_per_epoch + i + 1)
# Stats per epoch
elapsed = time.time() - start_time
elapsed = str(datetime.timedelta(seconds=elapsed))
log = 'Elapsed: %s | [F1_VAL: %0.3f LOSS_VAL: %0.3f] | Train' % (
elapsed, f1_val, loss_val)
for tag, value in loss_cum.items():
log += ", {}: {:.4f}".format(tag, np.array(value).mean())
print(log)
if f1_val > f1_val_prev:
torch.save(
self.C.state_dict(),
os.path.join(self.model_save_path, '{}_{}.pth'.format(
E, i + 1)))
os.system('rm -vf {}'.format(
os.path.join(
self.model_save_path, '{}_{}.pth'.format(
str(int(E) - 1).zfill(2), i + 1))))
print("! Saving model")
f1_val_prev = f1_val
non_decreasing = 0
else:
non_decreasing += 1
if non_decreasing == self.stop_training:
print(
"During {} epochs LOSS VAL was not decreasing.".format(
self.stop_training))
os.system('touch {}'.format(self.TRAINED_FILE))
return
# Decay learning rate
if (e + 1) > (self.num_epochs - self.num_epochs_decay):
lr -= (self.lr / float(self.num_epochs_decay))
self.update_lr(lr)
print('Decay learning rate to: {}.'.format(lr))
# ====================================================================#
# ====================================================================#
def val(self, init=False, load=False):
if init:
from data_loader import get_loader
self.rgb_loader_val = get_loader(self.metadata_path,
self.image_size, self.image_size,
self.batch_size, 'val')
if self.OF:
self.of_loader_val = get_loader(
self.metadata_path,
self.image_size,
self.image_size,
self.batch_size,
'val',
OF=True)
txt_path = os.path.join(self.model_save_path, '0_init_val.txt')
if load:
last_name = os.path.basename(self.test_model).split('.')[0]
txt_path = os.path.join(self.model_save_path,
'{}_{}_val.txt'.format(last_name, '{}'))
try:
output_txt = sorted(glob.glob(txt_path.format('*')))[-1]
number_file = len(glob.glob(output_txt))
except BaseException:
number_file = 0
txt_path = txt_path.format(str(number_file).zfill(2))
D_path = os.path.join(self.model_save_path,
'{}.pth'.format(last_name))
self.C.load_state_dict(torch.load(D_path))
self.C.eval()
if load:
self.f = open(txt_path, 'a')
self.thresh = np.linspace(0.01, 0.99, 200).astype(np.float32)
if not self.OF:
self.of_loader_val = None
f1, _, _, loss, f1_one = F1_TEST(
self,
self.rgb_loader_val,
mode='VAL',
OF=self.of_loader_val,
verbose=load)
if load:
self.f.close()
if init:
return f1, loss, f1_one
else:
return f1, loss
# ====================================================================#
# ====================================================================#
def test(self):
print('Testing Model')
from data_loader import get_loader
if self.test_model == '':
last_file = sorted(
glob.glob(os.path.join(self.model_save_path, '*.pth')))[-1]
last_name = os.path.basename(last_file).split('.')[0]
else:
last_name = self.test_model
D_path = os.path.join(self.model_save_path, '{}.pth'.format(last_name))
txt_path = os.path.join(self.model_save_path, '{}_{}.txt'.format(
last_name, '{}'))
self.pkl_data = os.path.join(self.model_save_path, '{}_{}.pkl'.format(
last_name, '{}'))
if not self.DONE:
self.C.load_state_dict(torch.load(D_path))
self.C.eval()
data_loader_val = get_loader(
self.metadata_path,
self.image_size,
self.image_size,
self.batch_size,
'val',
imagenet=self.finetuning == 'imagenet')
data_loader_test = get_loader(
self.metadata_path,
self.image_size,
self.image_size,
self.batch_size,
'test',
imagenet=self.finetuning == 'imagenet')
if self.OF:
of_loader_val = get_loader(
self.metadata_path,
self.image_size,
self.image_size,
self.batch_size,
'val',
OF=True,
imagenet=self.finetuning == 'imagenet')
of_loader_test = get_loader(
self.metadata_path,
self.image_size,
self.image_size,
self.batch_size,
'test',
OF=True,
imagenet=self.finetuning == 'imagenet')
if not hasattr(self, 'output_txt'):
self.output_txt = txt_path
try:
txt_file = sorted(glob.glob(self.output_txt.format('*')))
number_file = len(txt_file)
except BaseException:
number_file = 0
self.output_txt = self.output_txt.format(
str(number_file).zfill(len(self.string_)))
print("Output directly to: {}".format(self.output_txt))
self.f = open(self.output_txt, 'a')
self.thresh = np.linspace(0.01, 0.99, 200).astype(np.float32)
if not self.OF:
F1_real, F1_max, max_thresh_val, _, _ = F1_TEST(
self, data_loader_val, mode='VAL')
F1_TEST(self, data_loader_test, thresh=max_thresh_val)
else:
F1_real, F1_max, max_thresh_val, _, _ = F1_TEST(
self, data_loader_val, mode='VAL', OF=of_loader_val)
F1_TEST(
self,
data_loader_test,
thresh=max_thresh_val,
OF=of_loader_test)
self.f.close()
# ====================================================================#
# ====================================================================#
def sample(self):
"""Get a dataset sample."""
if not os.path.isdir('show'):
os.makedirs('show')
for i, (rgb_img, rgb_label, rgb_files) in enumerate(self.rgb_loader):
min_size = min(rgb_img.size(0), 64)
img_file = 'show/%s.jpg' % (str(i).zfill(4))
save_image(self.denorm(rgb_img[:min_size]), img_file, nrow=8)
if i == 25:
break
def DEMO(self):
#pdb.set_trace()
print('Testing on Demo input')
self.C.eval()
if self.OF:
of_loader = iter(self.of_loader)
string = "AU{} - OF {} | Forward".format(
str(self.AU).zfill(2), str(self.OF_option))
for real_rgb, _, file_ in self.rgb_loader:
real_rgb = self.to_var(real_rgb, volatile=True)
if self.OF:
real_of = of_loader.next()[0]
real_of = self.to_var(real_of, volatile=True)
out_temp = self.C(real_rgb, OF=real_of)
else:
out_temp = self.C(real_rgb)
output = F.sigmoid(out_temp)
output = output.data.cpu().numpy().flatten().tolist()
with open(os.path.join(self.log_path, file_[0].split('/')[-2])+'.csv', 'a') as fp:
csvwriter = csv.writer(fp)
for fid, _file in enumerate(file_):
print('{} | {} : {}'.format(string, _file, output[fid]))
csvwriter.writerow([_file.split('/')[-1].replace('.png', ''), output[fid]])