-
Notifications
You must be signed in to change notification settings - Fork 19
/
train.py
185 lines (157 loc) · 7.1 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
import shutil
import time
from tensorboardX import SummaryWriter
import torch
from torch.autograd import Variable
from IPython.core.debugger import Pdb
from scheduler import CustomReduceLROnPlateau
import json
def train(model, dataloader, criterion, optimizer, use_gpu=False):
model.train() # Set model to training mode
running_loss = 0.0
running_corrects = 0
example_count = 0
step = 0
# Pdb().set_trace()
# Iterate over data.
for questions, images, image_ids, answers, ques_ids in dataloader:
# print('questions size: ', questions.size())
if use_gpu:
questions, images, image_ids, answers = questions.cuda(), images.cuda(), image_ids.cuda(), answers.cuda()
questions, images, answers = Variable(questions).transpose(0, 1), Variable(images), Variable(answers)
# zero grad
optimizer.zero_grad()
ans_scores = model(images, questions, image_ids)
_, preds = torch.max(ans_scores, 1)
loss = criterion(ans_scores, answers)
# backward + optimize
loss.backward()
optimizer.step()
# statistics
running_loss += loss.data[0]
running_corrects += torch.sum((preds == answers).data)
example_count += answers.size(0)
step += 1
if step % 5000 == 0:
print('running loss: {}, running_corrects: {}, example_count: {}, acc: {}'.format(
running_loss / example_count, running_corrects, example_count, (float(running_corrects) / example_count) * 100))
# if step * batch_size == 40000:
# break
loss = running_loss / example_count
acc = (running_corrects / len(dataloader.dataset)) * 100
print('Train Loss: {:.4f} Acc: {:2.3f} ({}/{})'.format(loss,
acc, running_corrects, example_count))
return loss, acc
def validate(model, dataloader, criterion, use_gpu=False):
model.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
example_count = 0
# Iterate over data.
for questions, images, image_ids, answers, ques_ids in dataloader:
if use_gpu:
questions, images, image_ids, answers = questions.cuda(
), images.cuda(), image_ids.cuda(), answers.cuda()
questions, images, answers = Variable(questions).transpose(
0, 1), Variable(images), Variable(answers)
# zero grad
ans_scores = model(images, questions, image_ids)
_, preds = torch.max(ans_scores, 1)
loss = criterion(ans_scores, answers)
# statistics
running_loss += loss.data[0]
running_corrects += torch.sum((preds == answers).data)
example_count += answers.size(0)
loss = running_loss / example_count
# acc = (running_corrects / example_count) * 100
acc = (running_corrects / len(dataloader.dataset)) * 100
print('Validation Loss: {:.4f} Acc: {:2.3f} ({}/{})'.format(loss,
acc, running_corrects, example_count))
return loss, acc
def train_model(model, data_loaders, criterion, optimizer, scheduler, save_dir, num_epochs=25, use_gpu=False, best_accuracy=0, start_epoch=0):
print('Training Model with use_gpu={}...'.format(use_gpu))
since = time.time()
best_model_wts = model.state_dict()
best_acc = best_accuracy
writer = SummaryWriter(save_dir)
for epoch in range(start_epoch, num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
train_begin = time.time()
train_loss, train_acc = train(
model, data_loaders['train'], criterion, optimizer, use_gpu)
train_time = time.time() - train_begin
print('Epoch Train Time: {:.0f}m {:.0f}s'.format(
train_time // 60, train_time % 60))
writer.add_scalar('Train Loss', train_loss, epoch)
writer.add_scalar('Train Accuracy', train_acc, epoch)
validation_begin = time.time()
val_loss, val_acc = validate(
model, data_loaders['val'], criterion, use_gpu)
validation_time = time.time() - validation_begin
print('Epoch Validation Time: {:.0f}m {:.0f}s'.format(
validation_time // 60, validation_time % 60))
writer.add_scalar('Validation Loss', val_loss, epoch)
writer.add_scalar('Validation Accuracy', val_acc, epoch)
# deep copy the model
is_best = val_acc > best_acc
if is_best:
best_acc = val_acc
best_model_wts = model.state_dict()
save_checkpoint(save_dir, {
'epoch': epoch,
'best_acc': best_acc,
'state_dict': model.state_dict(),
# 'optimizer': optimizer.state_dict(),
}, is_best)
writer.export_scalars_to_json(save_dir + "/all_scalars.json")
valid_error = 1.0 - val_acc / 100.0
if type(scheduler) == CustomReduceLROnPlateau:
scheduler.step(valid_error, epoch=epoch)
if scheduler.shouldStopTraining():
print("Stop training as no improvement in accuracy - no of unconstrainedBadEopchs: {0} > {1}".format(
scheduler.unconstrainedBadEpochs, scheduler.maxPatienceToStopTraining))
# Pdb().set_trace()
break
else:
scheduler.step()
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
# load best model weights
model.load_state_dict(best_model_wts)
# export scalar data to JSON for external processing
writer.export_scalars_to_json(save_dir + "/all_scalars.json")
writer.close()
return model
def save_checkpoint(save_dir, state, is_best):
savepath = save_dir + '/' + 'checkpoint.pth.tar'
torch.save(state, savepath)
if is_best:
shutil.copyfile(savepath, save_dir + '/' + 'model_best.pth.tar')
def test_model(model, dataloader, itoa, outputfile, use_gpu=False):
model.eval() # Set model to evaluate mode
example_count = 0
test_begin = time.time()
outputs = []
# Iterate over data.
for questions, images, image_ids, answers, ques_ids in dataloader:
if use_gpu:
questions, images, image_ids, answers = questions.cuda(
), images.cuda(), image_ids.cuda(), answers.cuda()
questions, images, answers = Variable(questions).transpose(
0, 1), Variable(images), Variable(answers)
# zero grad
ans_scores = model(images, questions, image_ids)
_, preds = torch.max(ans_scores, 1)
outputs.extend([{'question_id': ques_ids[i], 'answer': itoa[str(
preds.data[i])]} for i in range(ques_ids.size(0))])
if example_count % 100 == 0:
print('(Example Count: {})'.format(example_count))
# statistics
example_count += answers.size(0)
json.dump(outputs, open(outputfile, 'w'))
print('(Example Count: {})'.format(example_count))
test_time = time.time() - test_begin
print('Test Time: {:.0f}m {:.0f}s'.format(test_time // 60, test_time % 60))