-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain.py
135 lines (105 loc) · 5.32 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
from __future__ import print_function, division
import torch.nn.parallel
import torch.optim
import time
import numpy as np
from test import test
from utils.utils import init_model_and_dataset, adjust_learning_rate, AverageMeter, accuracy
from utils.tb_visualizer import Logger
def train(ckpt, depth, num_epochs, batch_size):
num_workers = 0
lr = 5e-4
momentum = 0
weight_decay = 0
directory = 'data/'
start_epoch = 0
start_loss = 0
print_freq = 100
checkpoint_interval = 1
evaluation_interval = 1
logger = Logger('./logs')
model, train_dataset, val_dataset, criterion_grid, optimizer = init_model_and_dataset(depth, directory, lr,
weight_decay, momentum)
val_dataset.evaluate()
# load the pretrained network
if ckpt is not None:
checkpoint = torch.load(ckpt)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
start_epoch = checkpoint['epoch']
start_loss = checkpoint['loss']
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=True)
for epoch in range(start_epoch, num_epochs):
adjust_learning_rate(optimizer, epoch, lr)
# train for one epoch
batch_time = AverageMeter()
data_time = AverageMeter()
train_loss = AverageMeter()
train_recall = AverageMeter()
train_precision = np.array([AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter()])
train_loss.update(start_loss)
# switch to train mode
model.train()
end = time.time()
for data_idx, data in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)
input = data['image'].float().cuda()
grid = data['grid'].float().cuda()
corners = data['corners']
# compute output
output = model(input).split(input.shape[0], dim=0)
loss = sum(i*criterion_grid(o, grid) for i, o in enumerate(output))
# measure accuracy and record loss
accuracy(corners=corners, output=output[-1].data, target=grid, global_recall=train_recall,
global_precision=train_precision)
train_loss.update(loss.item())
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if data_idx % print_freq == 0 and data_idx != 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Loss.avg: {loss.avg:.4f}\t'
'Recall(%): {top1:.3f}\t'
'Precision num. corners (%): ({top2:.3f}, {top3:.3f}, {top4:.3f}, {top5:.3f})\t'.format(
epoch, data_idx, len(train_loader), loss=train_loss,
top1=train_recall.avg * 100, top2=train_precision[0].avg * 100, top3=train_precision[1].avg * 100,
top4=train_precision[2].avg * 100, top5=train_precision[3].avg * 100))
if epoch % evaluation_interval == 0:
# evaluate on validation set
print('Train set: ')
t_recall, t_precision = test(train_loader, model)
print('Validation set: ')
e_recall, e_precision = test(val_loader, model)
# 1. Log scalar values (scalar summary)
info = {'Train Loss': train_loss.avg, 'Train Recall': t_recall, 'Train Precision 1': t_precision[0],
'Train Precision 2': t_precision[1], 'Train Precision 3': t_precision[2],
'Train Precision 4': t_precision[3], 'Validation Recall': e_recall,
'Validation Precision 1': e_precision[0], 'Validation Precision 2': e_precision[1],
'Validation Precision 3': e_precision[2], 'Validation Precision 4': e_precision[3]}
for tag, value in info.items():
logger.scalar_summary(tag, value, epoch)
# 2. Log values and gradients of the parameters (histogram summary)
for tag, value in model.named_parameters():
tag = tag.replace('.', '/')
logger.histo_summary(tag, value.data.cpu().numpy(), epoch)
logger.histo_summary(tag + '/grad', value.grad.data.cpu().numpy(), epoch)
# 3. Log training images (image summary)
info = {'images': input.view(-1, 495, 495).cpu().numpy()}
for tag, images in info.items():
logger.image_summary(tag, images, epoch)
# remember best acc and save checkpoint
if epoch % checkpoint_interval == 0:
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': train_loss.avg
}, "checkpoints/hg_ckpt_{0}.pth".format(epoch))