forked from littlejuyan/FusingGlobalandLocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·124 lines (98 loc) · 4.33 KB
/
train.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
import os
import sys
import time
import torch
import torch.nn
import argparse
from PIL import Image
from tensorboardX import SummaryWriter
import numpy as np
sys.path.append('./data')
from data import create_dataloader
from earlystop import EarlyStopping
from networks.trainer import Trainer
from options.train_options import TrainOptions
from sklearn.metrics import average_precision_score, precision_recall_curve, accuracy_score
def validate(model, opt):
data_loader = create_dataloader(opt)
print("number of validation dataset: ", len(data_loader))
with torch.no_grad():
y_true, y_pred = [], []
for data in data_loader:
input_img = data[0] #[batch_size, 3, height, width]
cropped_img = data[1].cuda() #[batch_size, 3, 224, 224]
label = data[2].cuda() #[batch_size, 1]
scale = data[3].cuda() #[batch_size, 1, 2]
y_pred.extend(model(input_img, cropped_img, scale).sigmoid().flatten().tolist())
y_true.extend(label.flatten().tolist())
y_true, y_pred = np.array(y_true), np.array(y_pred)
acc = accuracy_score(y_true, y_pred > 0.5)
return acc
def get_val_opt():
val_opt = TrainOptions().parse(print_options=False)
val_opt.dataroot = '{}/{}/'.format(val_opt.dataroot, val_opt.val_split)
val_opt.isTrain = False
val_opt.no_resize = False
val_opt.no_crop = False
val_opt.serial_batches = True
val_opt.jpg_method = ['pil']
if len(val_opt.blur_sig) == 2:
b_sig = val_opt.blur_sig
val_opt.blur_sig = [(b_sig[0] + b_sig[1]) / 2]
if len(val_opt.jpg_qual) != 1:
j_qual = val_opt.jpg_qual
val_opt.jpg_qual = [int((j_qual[0] + j_qual[-1]) / 2)]
return val_opt
if __name__ == '__main__':
opt = TrainOptions().parse()
opt.dataroot = '{}/{}/'.format(opt.dataroot, opt.train_split)
val_opt = get_val_opt()
data_loader = create_dataloader(opt)
dataset_size = len(data_loader)
print('#training images = %d' % dataset_size)
train_writer = SummaryWriter(os.path.join(opt.checkpoints_dir, opt.name, "train"))
val_writer = SummaryWriter(os.path.join(opt.checkpoints_dir, opt.name, "val"))
model = Trainer(opt)
early_stopping = EarlyStopping(patience=opt.earlystop_epoch, delta=-0.0001, verbose=True)
for epoch in range(opt.niter):
epoch_start_time = time.time()
iter_data_time = time.time()
epoch_iter = 0
for i, data in enumerate(data_loader):
model.total_steps += 1
epoch_iter += opt.batch_size
model.set_input(data)
model.optimize_parameters()
#exit()
if model.total_steps % opt.loss_freq == 0:
print("Train loss: {} at step: {}".format(model.loss, model.total_steps))
train_writer.add_scalar('loss', model.loss, model.total_steps)
if model.total_steps % opt.save_latest_freq == 0:
print('saving the latest model %s (epoch %d, model.total_steps %d)' %
(opt.name, epoch, model.total_steps))
model.save_networks('latest')
#if epoch % opt.save_epoch_freq == 0:
print('saving the model at the end of epoch %d, iters %d' %
(epoch, model.total_steps))
#model.save_networks('latest')
model.save_networks(epoch)
# Validation
model.eval()
acc = validate(model.model, val_opt)
val_writer.add_scalar('accuracy', acc, model.total_steps)
print("(Val @ epoch {}) acc: {}".format(epoch, acc))
info = [str(epoch), ',', str(acc)]
with open('xxxxx/acc_training.txt', 'a') as f: #path to save the accuracy during training.
f.writelines(info)
f.writelines('\n')
early_stopping(acc, model)
if early_stopping.early_stop:
cont_train = model.adjust_learning_rate()
if cont_train:
print("Learning rate dropped by 2, continue training...")
early_stopping = EarlyStopping(patience=opt.earlystop_epoch, delta=-0.00005, verbose=True)
else:
print("Learning rate dropped to minimum, still training with minimum learning rate...")
early_stopping = EarlyStopping(patience=opt.earlystop_epoch, delta=-0.00005, verbose=True)
# break
model.train()