forked from OlgaChernytska/3D-Hand-Pose-Estimation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
215 lines (159 loc) · 7.62 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
import argparse
import yaml
import warnings
import torch
import numpy as np
from model import get_model
from dataset import get_dataloader
from optimizer import get_optimizer
from criterion import get_criterion
class Trainer:
def __init__(self, config):
self.cuda = int(config['cuda'])
#torch.cuda.empty_cache()
self.train_dataloader = get_dataloader(config, scope='train')
self.val_dataloader = get_dataloader(config, scope='val')
self.model = get_model(config)
try:
model_weights = 'experiment/' + config['dir'] + '/' + config['weights']
self.model.load_state_dict(torch.load(model_weights)['model'])
print('Weigths loaded')
except:
print('Weights randomized')
self.optimizer = get_optimizer(config, self.model)
self.total_epochs = config['epochs']
self.batches_per_epoch = config['batches_per_epoch']
self.val_batches_per_epoch = config['val_batches_per_epoch']
self.final_weights_file = 'experiment/' + config['dir'] + '/weights_last.pth'
self.best_weights_file = 'experiment/' + config['dir'] + '/weights_best.pth'
self.log_file = 'experiment/' + config['dir'] + '/logs.csv'
self.loss_dict = {'sample_name': config['sample_name'],
'output_name': config['output_name'],
'loss': [get_criterion(x) for x in config['loss_criterion']],
'weight': config['loss_weight']}
self.train_fe = bool(config['train_feature_extractor'])
def train(self):
if not self.train_fe:
for param in self.model.conv1.parameters():
param.requires_grad = False
for param in self.model.layer1.parameters():
param.requires_grad = False
for param in self.model.layer2.parameters():
param.requires_grad = False
for param in self.model.layer3.parameters():
param.requires_grad = False
for param in self.model.layer3.parameters():
param.requires_grad = False
best_val_loss = 10000
for epoch in range(self.total_epochs):
batches = 0
logging = {}
for sample in self.train_dataloader:
self.model.train()
self.optimizer.zero_grad()
sample['image'] = sample['image'].cuda(self.cuda)
output = self.model(sample)
loss_data = self._loss(sample, output)
loss = loss_data['total']
loss.backward()
self.optimizer.step()
# logging
for key, value in loss_data.items():
try:
logging[key].append(value.item())
except:
logging[key] = [value.item()]
batches += 1
if batches >= self.batches_per_epoch:
break
val_logging = self._validate()
mean_train_loss = np.mean(logging['total'])
mean_val_loss = np.mean(val_logging['total'])
print('====Epoch {}. Train loss: {}. Val loss: {}'.format(epoch, mean_train_loss, mean_val_loss))
# logging
if epoch>0:
with open(self.log_file, 'a') as fp:
fp.write(str(epoch+1))
for key, value in logging.items():
fp.write(',' + str(np.mean(value)))
for key, value in val_logging.items():
fp.write(',' + str(np.mean(value)))
fp.write('\n')
else:
with open(self.log_file, 'w') as fp:
fp.write('epoch')
for key, value in logging.items():
fp.write(',train_' + key)
for key, value in logging.items():
fp.write(',val_' + key)
fp.write('\n')
fp.write(str(epoch+1))
for key, value in logging.items():
fp.write(',' + str(np.mean(value)))
for key, value in val_logging.items():
fp.write(',' + str(np.mean(value)))
fp.write('\n')
# saving model
torch.save({
'model': self.model.state_dict()
}, self.final_weights_file)
#if mean_val_loss < best_val_loss:
# best_val_loss = mean_val_loss
# torch.save({
# 'model': self.model.state_dict()
# }, self.best_weights_file)
if (epoch+1) % 20 == 0:
torch.save({
'model': self.model.state_dict()
}, 'experiment/' + config['dir'] + '/weights_' + str(epoch+1).zfill(3) + '.pth' )
def _validate(self):
self.model.eval()
batches = 0
logging = {}
with torch.no_grad():
for sample in self.val_dataloader:
sample['image'] = sample['image'].cuda(self.cuda)
output = self.model(sample)
loss_data = self._loss(sample, output) ##add logging intermediate losses
loss = loss_data['total']
for key, value in loss_data.items():
try:
logging[key].append(value.item())
except:
logging[key] = [value.item()]
batches += 1
if batches >= self.val_batches_per_epoch:
break
return logging
def _loss(self, sample, output):
loss = 0
return_dict = {}
for i, l_name in enumerate(self.loss_dict['output_name']):
sample_name = self.loss_dict['sample_name'][i]
if l_name == 'heatmaps':
img_size = sample['heatmaps'].shape[2]
pres = sample['is_present'].unsqueeze(2).unsqueeze(2)
pres = pres.repeat([1,1,img_size,img_size]).cuda(self.cuda)
output = torch.mul(output[l_name],pres)
inter_loss = self.loss_dict['loss'][i] (output,
sample[sample_name].cuda(self.cuda))
else:
inter_loss = self.loss_dict['loss'][i] (output[l_name],
sample[sample_name].cuda(self.cuda))
return_dict[l_name] = inter_loss
loss += inter_loss * self.loss_dict['weight'][i]
return_dict['total'] = loss
return return_dict
if __name__ == '__main__':
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
parser.add_argument('exper_folder', help='Provide experiment folder')
args = parser.parse_args()
print('Experiment {} started'.format(args.exper_folder))
config_file = 'experiment/' + args.exper_folder + '/config.yaml'
with open(config_file, 'r') as f:
config = yaml.load(f)
config['dir'] = args.exper_folder
trainer = Trainer(config)
trainer.train()
print('Experiment {} ended'.format(args.exper_folder))