-
Notifications
You must be signed in to change notification settings - Fork 37
/
train.py
293 lines (209 loc) · 10.8 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
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
import os
import sys
import datetime
import yaml
import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import MultiStepLR
from tensorboardX import SummaryWriter
from parse_args import Parse
from models.models_import import create_model_object
from datasets.loading_function import data_loader
from losses import Losses
from metrics import Metrics
from checkpoint import save_checkpoint, load_checkpoint
def train(**args):
"""
Evaluate selected model
Args:
rerun (Int): Integer indicating number of repetitions for the select experiment
seed (Int): Integer indicating set seed for random state
save_dir (String): Top level directory to generate results folder
model (String): Name of selected model
dataset (String): Name of selected dataset
exp (String): Name of experiment
debug (Int): Debug state to avoid saving variables
load_type (String): Keyword indicator to evaluate the testing or validation set
pretrained (Int/String): Int/String indicating loading of random, pretrained or saved weights
opt (String): Int/String indicating loading of random, pretrained or saved weights
lr (Float): Learning rate
momentum (Float): Momentum in optimizer
weight_decay (Float): Weight_decay value
final_shape ([Int, Int]): Shape of data when passed into network
Return:
None
"""
print("\n############################################################################\n")
print("Experimental Setup: ", args)
print("\n############################################################################\n")
for total_iteration in range(args['rerun']):
# Generate Results Directory
d = datetime.datetime.today()
date = d.strftime('%Y%m%d-%H%M%S')
result_dir = os.path.join(args['save_dir'], args['model'], '_'.join((args['dataset'],args['exp'],date)))
log_dir = os.path.join(result_dir, 'logs')
save_dir = os.path.join(result_dir, 'checkpoints')
if not args['debug']:
os.makedirs(result_dir, exist_ok=True)
os.makedirs(log_dir, exist_ok=True)
os.makedirs(save_dir, exist_ok=True)
# Save copy of config file
with open(os.path.join(result_dir, 'config.yaml'),'w') as outfile:
yaml.dump(args, outfile, default_flow_style=False)
# Tensorboard Element
writer = SummaryWriter(log_dir)
# Check if GPU is available (CUDA)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Load Network
model = create_model_object(**args).to(device)
# Load Data
loader = data_loader(model_obj=model, **args)
if args['load_type'] == 'train':
train_loader = loader['train']
valid_loader = loader['train'] # Run accuracy on train data if only `train` selected
elif args['load_type'] == 'train_val':
train_loader = loader['train']
valid_loader = loader['valid']
else:
sys.exit('Invalid environment selection for training, exiting')
# END IF
# Training Setup
params = [p for p in model.parameters() if p.requires_grad]
if args['opt'] == 'sgd':
optimizer = optim.SGD(params, lr=args['lr'], momentum=args['momentum'], weight_decay=args['weight_decay'])
elif args['opt'] == 'adam':
optimizer = optim.Adam(params, lr=args['lr'], weight_decay=args['weight_decay'])
else:
sys.exit('Unsupported optimizer selected. Exiting')
# END IF
scheduler = MultiStepLR(optimizer, milestones=args['milestones'], gamma=args['gamma'])
if isinstance(args['pretrained'], str):
ckpt = load_checkpoint(args['pretrained'])
model.load_state_dict(ckpt)
if args['resume']:
start_epoch = load_checkpoint(args['pretrained'], key_name='epoch') + 1
optimizer.load_state_dict(load_checkpoint(args['pretrained'], key_name='optimizer'))
scheduler.step(epoch=start_epoch)
else:
start_epoch = 0
# END IF
else:
start_epoch = 0
# END IF
model_loss = Losses(device=device, **args)
best_val_acc = 0.0
############################################################################################################################################################################
# Start: Training Loop
for epoch in range(start_epoch, args['epoch']):
running_loss = 0.0
print('Epoch: ', epoch)
# Setup Model To Train
model.train()
# Start: Epoch
for step, data in enumerate(train_loader):
if step% args['pseudo_batch_loop'] == 0:
loss = 0.0
running_batch = 0
optimizer.zero_grad()
# END IF
x_input = data['data']
annotations = data['annots']
if isinstance(x_input, torch.Tensor):
mini_batch_size = x_input.shape[0]
outputs = model(x_input.to(device))
assert args['final_shape']==list(x_input.size()[-2:]), "Input to model does not match final_shape argument"
else: #Model takes several inputs in forward function
mini_batch_size = x_input[0].shape[0] #Assuming the first element contains the true data input
for i, item in enumerate(x_input):
if isinstance(item, torch.Tensor):
x_input[i] = item.to(device)
outputs = model(*x_input)
loss = model_loss.loss(outputs, annotations)
loss = loss * mini_batch_size
loss.backward()
running_loss += loss.item()
running_batch += mini_batch_size
if np.isnan(running_loss):
import pdb; pdb.set_trace()
# END IF
if not args['debug']:
# Add Learning Rate Element
for param_group in optimizer.param_groups:
writer.add_scalar(args['dataset']+'/'+args['model']+'/learning_rate', param_group['lr'], epoch*len(train_loader) + step)
# END FOR
# Add Loss Element
writer.add_scalar(args['dataset']+'/'+args['model']+'/minibatch_loss', loss.item()/mini_batch_size, epoch*len(train_loader) + step)
# END IF
if ((epoch*len(train_loader) + step+1) % 100 == 0):
print('Epoch: {}/{}, step: {}/{} | train loss: {:.4f}'.format(epoch, args['epoch'], step+1, len(train_loader), running_loss/float(step+1)/mini_batch_size))
# END IF
if (epoch * len(train_loader) + (step+1)) % args['pseudo_batch_loop'] == 0 and step > 0:
# Apply large mini-batch normalization
for param in model.parameters():
if param.requires_grad:
param.grad *= 1./float(running_batch)
# END FOR
# Apply gradient clipping
if ("grad_max_norm" in args) and float(args['grad_max_norm'] > 0):
nn.utils.clip_grad_norm_(model.parameters(),float(args['grad_max_norm']))
optimizer.step()
running_batch = 0
# END IF
# END FOR: Epoch
scheduler.step(epoch=epoch)
print('Schedulers lr: %f', scheduler.get_lr()[0])
if not args['debug']:
# Save Current Model
save_path = os.path.join(save_dir, args['dataset']+'_epoch'+str(epoch)+'.pkl')
save_checkpoint(epoch, step, model, optimizer, save_path)
# END IF: Debug
## START FOR: Validation Accuracy
running_acc = []
running_acc = valid(valid_loader, running_acc, model, device)
if not args['debug']:
writer.add_scalar(args['dataset']+'/'+args['model']+'/validation_accuracy', 100.*running_acc[-1], epoch*len(train_loader) + step)
print('Accuracy of the network on the validation set: %f %%\n' % (100.*running_acc[-1]))
# Save Best Validation Accuracy Model Separately
if best_val_acc < running_acc[-1]:
best_val_acc = running_acc[-1]
if not args['debug']:
# Save Current Model
save_path = os.path.join(save_dir, args['dataset']+'_best_model.pkl')
save_checkpoint(epoch, step, model, optimizer, save_path)
# END IF
# END IF
# END FOR: Training Loop
############################################################################################################################################################################
if not args['debug']:
# Close Tensorboard Element
writer.close()
def valid(valid_loader, running_acc, model, device):
acc_metric = Metrics(**args)
model.eval()
with torch.no_grad():
for step, data in enumerate(valid_loader):
x_input = data['data']
annotations = data['annots']
if isinstance(x_input, torch.Tensor):
outputs = model(x_input.to(device))
else:
for i, item in enumerate(x_input):
if isinstance(item, torch.Tensor):
x_input[i] = item.to(device)
outputs = model(*x_input)
running_acc.append(acc_metric.get_accuracy(outputs, annotations))
if step % 100 == 0:
print('Step: {}/{} | validation acc: {:.4f}'.format(step, len(valid_loader), running_acc[-1]))
# END FOR: Validation Accuracy
return running_acc
if __name__ == "__main__":
parse = Parse()
args = parse.get_args()
# For reproducibility
torch.backends.cudnn.deterministic = True
torch.manual_seed(args['seed'])
if not args['resume']:
np.random.seed(args['seed'])
train(**args)