-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
178 lines (147 loc) · 6.28 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
"""
@author: autocyz
@contact: [email protected]
@file: train.py
@function: train model
@time: 19-04-15
"""
import os
import time
import numpy as np
import torch
import torchvision
from tensorboardX import SummaryWriter
from torch.optim.lr_scheduler import StepLR
from torch.utils.data.dataloader import DataLoader
from models.CornerNet import CornerNet, aeloss
from params import params
from sample.voc import VOC
from sample.cf import CF
from utils.torch_utils import save_params, get_lr
from utils.log import Logger
total_iter = 0
def train(train_loader, net, criterion, optimizer, epoch, writer, use_gpu=True, loader_info=''):
"""
train model
Args:
train_loader: dataloader
net: network
criterion: loss function
optimizer:
epoch:
writer: summary writer
loader_info:
Returns:
"""
time4 = 0
net.train()
for i, (img, tl_heatmap, br_heatmap, tl_tags, br_tags, tag_masks) in enumerate(train_loader):
time4_last = time4
time0 = time.time()
if use_gpu:
img = img.cuda()
tl_heatmap = tl_heatmap.cuda()
br_heatmap = br_heatmap.cuda()
tl_tags = tl_tags.cuda()
br_tags = br_tags.cuda()
tag_masks = tag_masks.cuda()
time1 = time.time()
# predict is a list [tl_heat, br_heat, tl_tag, br_tag, ...]
predict = net(*[img, tl_tags, br_tags])
time2 = time.time()
loss = criterion(predict, [tl_heatmap, br_heatmap, tag_masks])
time3 = time.time()
optimizer.zero_grad()
loss.backward()
# loss_paf.backward()
optimizer.step()
time4 = time.time()
# writer some train information
global total_iter
total_iter += 1
writer.add_scalar('train_loss', loss.item(), total_iter)
if total_iter % 5 == 0:
writer.add_image('0_img', img[0].cpu())
tl_heatmap = tl_heatmap[0].cpu()
br_heatmap = br_heatmap[0].cpu()
tl_heatmap_predict = predict[-4][0].cpu()
br_heatmap_predict = predict[-3][0].cpu()
writer.add_image('1_tl_heatmap',
torchvision.utils.make_grid([tl_heatmap, tl_heatmap_predict,
br_heatmap, br_heatmap_predict],
normalize=True, padding=10,
pad_value=1))
print('Epoch [{:03d}/{:03d}]\tStep [{}/{} {:5d}]\tLr [{}]'
'\tloss {:.4f}\n'
'T_preprocess:{:.5f} T_forward:{:.5f} T_loss: {:.5f} T_backward:{:.5f}'.
format(epoch, params['epoch_num'], i, len(train_loader), total_iter, get_lr(optimizer),
loss.item(),
time0 - time4_last, time2 - time1, time3 - time2, time4 - time3))
def eval(test_loader, net, criterion, epoch, writer, use_gpu=True):
net.eval()
val_loss = 0.
with torch.no_grad():
for i, (img, tl_heatmap, br_heatmap, tl_tags, br_tags, tag_masks) in enumerate(test_loader):
if use_gpu:
img = img.cuda()
tl_heatmap = tl_heatmap.cuda()
br_heatmap = br_heatmap.cuda()
tl_tags = tl_tags.cuda()
br_tags = br_tags.cuda()
tag_masks = tag_masks.cuda()
predict = net(*[img, tl_tags, br_tags])
loss = criterion(predict, [tl_heatmap, br_heatmap, tag_masks])
val_loss += loss
print('Eval [{}/{}], epoch [{}]: current loss:{} calculate_loss:{}'.format(
i, len(test_loader), epoch, loss.item(), val_loss.item()))
val_loss = val_loss.item() / len(test_loader)
writer.add_scalar('val_loss', val_loss, epoch)
return val_loss
if __name__ == "__main__":
root_dir = "/home/cyz/data/dataset/voc/VOCdevkit/VOC2012"
date = params['date']
if not os.path.exists('./result/logdir/' + date):
os.mkdir('./result/logdir/' + date)
logger = Logger("log", './result/logdir/' + date).get_logger()
print('loading trainset')
trainset = VOC(root_dir, "train", logger=logger)
valset = VOC(root_dir, "val", logger=logger)
train_loader = DataLoader(trainset, batch_size=params['batch_size'],
shuffle=True, num_workers=params['num_workers'])
val_loader = DataLoader(valset, batch_size=params['batch_size'],
shuffle=True, num_workers=params['num_workers'])
print("loading over")
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
net = CornerNet()
if params['pretrain_model']:
print("loading pre_trained model :", params['pretrain_model'])
params['has_checkpoint'] = True
net.load_state_dict(torch.load(params['pretrain_model']))
print("loading over")
if params['use_gpu']:
net = net.cuda()
# optimizer = torch.optim.Adam(net.parameters(), lr=params['learning_rate'],
# weight_decay=params['weight_decay'])
optimizer = torch.optim.SGD(net.parameters(), lr=params['learning_rate'],
weight_decay=params['weight_decay'])
lr_scheduler = StepLR(optimizer, step_size=params['step_size'], gamma=0.1)
criterion = aeloss
writer = SummaryWriter(log_dir='./result/logdir/' + date)
save_model_path = os.path.join('./result/checkpoint/', date)
if not os.path.exists(save_model_path):
os.mkdir(save_model_path)
params['train_sample_nums'] = len(trainset)
params['test_sample_nums'] = len(valset)
params['train_iter_nums'] = len(train_loader)
params['test_iter_nums'] = len(val_loader)
save_params(save_model_path, 'parameter', params)
best_loss = np.inf
for epoch in range(params['epoch_num']):
lr_scheduler.step()
train(train_loader, net, criterion, optimizer, epoch, writer, use_gpu=params['use_gpu'])
val_loss = eval(val_loader, net, criterion, epoch, writer, use_gpu=params['use_gpu'])
print('epoch [{}] val_loss [{:.4f}]'.format(epoch, val_loss))
if val_loss < best_loss:
best_loss = val_loss
torch.save(net.state_dict(), os.path.join(save_model_path, 'epoch_{}_{:.3f}.cpkt'.format(epoch, val_loss)))