-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrainer.py
executable file
·186 lines (148 loc) · 8.22 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
# model training
import os
import time
import torch
import numpy as np
from models import build_model
from utils import Evaluater, list2str
from dataloader import build_dataloader
from predictor import Predictor
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
class Trainer(object):
def __init__(self, paths, training_params, augmentation_params):
self.training_params = training_params
self.paths = paths
self.augmentation_params = augmentation_params
self.imroot = paths['image_root']
self.train_source_collection = paths['train_source_collection']
self.train_target_collection = paths['train_target_collection']
self.val_target_collection = paths['val_target_collection']
self.collection_root = paths['collection_root']
self.config_path = paths['config_path']
self.config_name = self.config_path.split(os.sep)[-1]
self.mapping_path = paths['mapping_path']
self.num_workers = self.training_params['num_workers']
self.evaluater = Evaluater()
self.train_source_loader = build_dataloader(
paths=self.paths, collection_names=self.train_source_collection,
training_params=self.training_params, mapping_path=self.mapping_path,
augmentation_params=self.augmentation_params,
domain='source', train=True)
self.train_target_loader = build_dataloader(
paths=self.paths, collection_names=self.train_target_collection,
training_params=self.training_params, mapping_path=self.mapping_path,
augmentation_params=self.augmentation_params,
domain='target', train=True)
self.val_target_loader = build_dataloader(
paths=self.paths, collection_names=self.val_target_collection,
training_params=self.training_params, mapping_path=self.mapping_path,
augmentation_params=self.augmentation_params,
domain='target', train=False)
self.inter_val = int(self.train_target_loader.dataset.__len__() / self.train_target_loader.batch_size + 0.5)
self.training_params['inter_val'] = self.inter_val
self.model = build_model(training_params['net'], training_params, training=True)
self.model.set_device('cuda')
print('finish model loading')
# output folder
self.run_num = 0 # We run multiple times with the same settings.
self.out = os.path.join('out', self.train_target_collection + '_' + self.train_source_collection,
'Models', self.val_target_collection, self.config_name,
'runs_{}'.format(self.run_num))
while os.path.exists(self.out):
self.run_num += 1
self.out = os.path.join('out', self.train_target_collection + '_' + self.train_source_collection,
'Models', self.val_target_collection, self.config_name,
'runs_{}'.format(self.run_num))
os.makedirs(self.out)
# log head
self.log_headers = ['iteration', 'train/loss',
'train/ap', 'train/f1', 'train/precision', 'train/recall',
'valid/ap', 'valid/f1', 'valid/precision', 'valid/recall',
'total_time']
with open(os.path.join(self.out, 'log.csv'), 'w') as f:
f.write(','.join(self.log_headers) + '\n')
self.iteration = 0
self.best_ap = -1
self.no_improve = 0
self.start_time = time.time()
self.end = False # whether to stop training
print('model: {}'.format(training_params['net']))
print('dataset: ', self.train_source_collection, self.train_target_collection, self.val_target_collection)
print('optimizer: {}'.format(training_params['optimizer']))
def validate(self):
print('validating...')
predictor = Predictor(self.model, self.val_target_loader)
_, scores, labels = predictor.predict()
_, precisions, recalls, fs, specificities, aps, iaps, aucs = self.evaluater.evaluate(scores, labels)
precisions, recalls, fs, specificities, aps, iaps, aucs = np.nanmean(precisions), np.nanmean(recalls), np.nanmean(fs), np.nanmean(specificities), np.nanmean(aps), np.nanmean(iaps), np.nanmean(aucs)
with open(os.path.join(self.out, 'log.csv'), 'a') as f:
log_iter = [self.iteration, '']
log_train = [''] * 4
log_test = [aps, fs, precisions, recalls]
total_time = time.time() - self.start_time
log_iter = ','.join(map(str, log_iter))
log_train = ','.join(log_train)
log_test = list2str(lst=log_test, decimals=4, separator=',')
log = '{},{},{},{:.2f}\n'.format(log_iter, log_train, log_test, total_time)
f.write(log)
if aps > self.best_ap:
self.best_ap = aps
self.no_improve = 0
self.model.save_model(os.path.join(self.out, 'best_model.pkl'))
print('model saved')
else:
self.no_improve += 1
if self.no_improve >= 10:
self.end = True
def train(self):
self.iter_source_train_loader = iter(self.train_source_loader)
self.iter_target_train_loader = iter(self.train_target_loader)
while True:
try:
data_in_target = next(self.iter_target_train_loader)
except StopIteration:
self.iter_target_train_loader = iter(self.train_target_loader)
data_in_target = next(self.iter_target_train_loader)
try:
data_in_source = next(self.iter_source_train_loader)
except StopIteration:
self.iter_source_train_loader = iter(self.train_source_loader)
data_in_source = next(self.iter_source_train_loader)
if self.iteration % self.inter_val == 0:
self.model.set_model_mode('eval')
self.validate()
self.model.set_model_mode('train')
if self.end:
break
self.iteration += 1
_, source_x, source_y = data_in_source
_, target_x, target_y = data_in_target
source_x = source_x.type(torch.FloatTensor).cuda()
source_y = source_y.type(torch.FloatTensor).cuda()
target_x = target_x.type(torch.FloatTensor).cuda()
target_y = target_y.type(torch.FloatTensor).cuda()
target_score, train_loss = self.model.fit(source_x=source_x, target_x=target_x,
source_y=source_y, target_y=target_y)
target_score = target_score.data.cpu().numpy()
target_y = target_y.data.cpu().numpy()
if len(target_y) > len(target_score):
target_y = target_y[:len(target_score)]
_, precisions, recalls, fs, specificities, aps, iaps, aucs = self.evaluater.evaluate(target_score, target_y)
precisions, recalls, fs, specificities, aps, iaps, aucs = \
np.nanmean(precisions), np.nanmean(recalls), np.nanmean(fs), np.nanmean(specificities), np.nanmean(aps), np.nanmean(iaps), np.nanmean(aucs)
total_time = time.time() - self.start_time
"""
It is ok to receive aps=nan here. When all the labels in a batch are the same, AP cannot be calculated.
"""
print('iteration {:d}, loss={:.3f}, lr={:.3e}, ap={:.3f}, max_ap={:.3f}, no_improve:{:d}'.format(
self.iteration, train_loss.data.item(), self.model.opt.get_lr(), aps, self.best_ap, self.no_improve))
with open(os.path.join(self.out, 'log.csv'), 'a') as f:
log_iter = [self.iteration, train_loss.data.item()]
log_train = [aps, fs, precisions, recalls]
log_test = [''] * 4
log_iter = ','.join(map(str, log_iter))
log_test = ','.join(log_test)
log_train = list2str(lst=log_train, decimals=4, separator=',')
log = '{},{},{},{:.2f}\n'.format(log_iter, log_train, log_test, total_time)
f.write(log)