-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain_class_loss.py
More file actions
301 lines (236 loc) · 8.92 KB
/
train_class_loss.py
File metadata and controls
301 lines (236 loc) · 8.92 KB
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
import sys
import os
import json
from functools import partial
from tqdm import tqdm
import time
import datetime
import numpy as np
# needed by the computing infrastructure, you can remove it!
os.environ['CUDA_VISIBLE_DEVICES'] = os.environ.get('_CONDOR_AssignedGPUs', 'CUDA0').replace('CUDA', '')
import torch
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
import torchvision
import torchnet as tnt
from utils import filter_opt
import models
from models.utils import get_model
import log as log_utils
def train(
model,
train_loader,
val_loader,
optimizer,
scheduler,
training_options,
meters,
cuda,
exp_dir,
trace_file,
checkpoint_file
):
epoch = training_options['epoch']
max_epoch = training_options['max_epoch']
train_patience = training_options['train_patience']
best_loss = training_options['best_loss']
wait = training_options['wait']
# start the training loop from start epoch
stop = False
while epoch < max_epoch and not stop:
model.train()
for split, split_meters in meters.items():
for field, meter in split_meters.items():
meter.reset()
epoch_size = len(train_loader)
for sample in tqdm(train_loader, desc="Epoch {:d} train".format(epoch + 1)):
x = sample['data'] # input features
labels = sample['label_idx'] # label
if cuda:
x = x.cuda()
labels = labels.cuda()
optimizer.zero_grad()
loss, output = model.loss_class(x, labels)
loss.backward()
optimizer.step()
for field, meter in meters['train'].items():
meter.add(output[field])
# end epoch
scheduler.step()
epoch += 1
if val_loader is not None:
evaluate(model, val_loader, meters['val'], cuda,
desc="Epoch {:d} valid".format(epoch))
meter_vals = log_utils.extract_meter_values(meters)
print("Epoch {:02d}: {:s}".format(epoch, log_utils.render_meter_values(meter_vals)))
meter_vals['epoch'] = epoch
with open(trace_file, 'a') as f:
json.dump(meter_vals, f)
f.write('\n')
if val_loader is not None:
if meter_vals['val']['loss'] < best_loss:
best_loss = meter_vals['val']['loss']
print("==> best model (loss = {:0.6f}), saving model...".format(best_loss))
model.cpu()
torch.save(model, os.path.join(exp_dir, 'best_model.pt'))
if cuda:
model.cuda()
wait = 0
else:
wait += 1
if wait > train_patience:
print("==> patience {:d} exceeded".format(train_patience))
stop = True
else:
model.cpu()
torch.save(model, os.path.join(exp_dir, 'best_model.pt'))
if cuda:
model.cuda()
# save checkpoint
# if cuda is used, the checkpoint reload cuda tensors
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': scheduler.state_dict(),
'start_epoch': epoch,
'best_loss': best_loss,
'wait': wait,
}, checkpoint_file)
def evaluate(model, data_loader, meters, cuda, desc=None):
model.eval()
for field,meter in meters.items():
meter.reset()
if desc is not None:
data_loader = tqdm(data_loader, desc=desc)
for sample in data_loader:
x = sample['data'] # input features
labels = sample['label_idx'] # label
if cuda:
x = x.cuda()
labels = labels.cuda()
_, output = model.loss_class(x, labels)
for field, meter in meters.items():
meter.add(output[field])
return meters
if __name__ == '__main__':
from argparser_kws import *
args = parser.parse_args()
opt = vars(parser.parse_args())
# manual seed
# torch.manual_seed(1234)
# if opt['data.cuda']:
# torch.cuda.manual_seed(1234)
# Postprocess arguments FIXME
opt['model.x_dim'] = list(map(int, opt['model.x_dim'].split(',')))
opt['log.fields'] = opt['log.fields'].split(',')
# import task
speech_args = filter_opt(opt, 'speech')
dataset = opt['speech.dataset']
data_dir = opt['speech.default_datadir']
task = opt['speech.task']
if dataset == 'googlespeechcommand':
from data.GSCSpeechData import GSCSpeechDataset
ds = GSCSpeechDataset(data_dir, task, opt['data.cuda'], speech_args)
elif dataset == 'MSWC':
from data.MSWCData import MSWCDataset
ds = MSWCDataset(data_dir, task, False, speech_args)
else:
raise ValueError("Dataset not recognized")
num_classes = ds.num_classes()
print("The task {} of the {} Dataset has {} classes".format(task, dataset, num_classes))
# import dataloaders
train_loader = ds.get_iid_dataloader('training', opt['train.batch_size'])
val_loader = ds.get_iid_dataloader('validation', opt['train.batch_size'])
#import model
model_opt = filter_opt(opt, 'model')
if model_opt['model_name'] == 'e2e_conv':
# setup n_classes od the classifier
model_opt['num_classes'] = num_classes
elif model_opt['model_name'] == 'repr_conv':
# setup loss
model_opt['loss'] = {
'type': opt['train.loss'],
'margin': opt['train.margin'],
'n_classes': num_classes,}
else:
raise ValueError("Not valid Model Type")
# prepare preprocessing
if model_opt['preprocessing'] == 'mfcc':
print('MFCC preprocessing')
model_opt['mfcc'] = {
'window_size_ms': speech_args['window_size'],
'window_stride_ms': speech_args['window_stride'],
'sample_rate': speech_args['sample_rate'],
'n_mfcc': speech_args['n_mfcc'],
'feature_bin_count': speech_args['num_features']
}
# setup loss
model = get_model(model_opt)
print(model)
#move to cuda
cuda = opt['data.cuda']
if cuda:
model.cuda()
if 'mfcc' in model_opt.keys():
model.preprocessing.mfcc.cuda()
# import stats
meters = { 'train': { field: tnt.meter.AverageValueMeter() for field in opt['log.fields'] } }
if val_loader is not None:
meters['val'] = { field: tnt.meter.AverageValueMeter() for field in opt['log.fields'] }
# setup the optimizer
optim_method = getattr(optim, opt['train.optim_method'])
optim_config = { 'lr': opt['train.learning_rate'],
'weight_decay': opt['train.weight_decay'] }
# setup optimizer and schedule
optimizer = optim_method(model.parameters(), **optim_config)
scheduler = lr_scheduler.StepLR(optimizer, opt['train.decay_every'], gamma=0.5)
# setup experiment directory
opt['log.exp_dir'] = os.path.join('./results', opt['log.exp_dir'])
trace_file = os.path.join(opt['log.exp_dir'], 'trace.txt')
checkpoint_file = os.path.join(opt['log.exp_dir'], 'checkpoint.pt')
if os.path.isfile(checkpoint_file):
print('Found Checkpoint!')
checkpoint = torch.load(checkpoint_file)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
start_epoch = checkpoint['start_epoch']
best_loss = checkpoint['best_loss']
wait = checkpoint['wait']
else:
if not os.path.isdir(opt['log.exp_dir']):
os.makedirs(opt['log.exp_dir'])
#trace file
if os.path.isfile(trace_file):
os.remove(trace_file)
# save opts
with open(os.path.join(opt['log.exp_dir'], 'opt.json'), 'w') as f:
json.dump(opt, f)
f.write('\n')
start_epoch = 0
best_loss = np.inf
wait = 0
training_options= { 'epoch': start_epoch,
'max_epoch': opt['train.epochs'],
'train_patience': opt['train.patience'],
'best_loss': best_loss,
'wait': wait
}
# launch teh training
start = time.time()
train(
model,
train_loader,
val_loader,
optimizer,
scheduler,
training_options,
meters,
cuda,
opt['log.exp_dir'],
trace_file,
checkpoint_file
)
end = time.time()
elapsed = str(datetime.timedelta(seconds= end-start))
print("Total Time: {}".format(elapsed))