-
Notifications
You must be signed in to change notification settings - Fork 5
/
train.py
209 lines (174 loc) · 6.11 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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import _init_paths
import os
import torch
import torch.utils.model_zoo as model_zoo
from torch.nn.parameter import Parameter
import numpy as np
from datetime import datetime
import cPickle as pkl
import network
from wsddn import WSDDN
from utils.timer import Timer
import roi_data_layer.roidb as rdl_roidb
from roi_data_layer.layer import RoIDataLayer
from datasets.factory import get_imdb
from fast_rcnn.config import cfg, cfg_from_file
import visdom
from logger import *
from test import test_net
try:
from termcolor import cprint
except ImportError:
cprint = None
def log_print(text, color=None, on_color=None, attrs=None):
if cprint is not None:
cprint(text, color=color, on_color=on_color, attrs=attrs)
else:
print(text)
# hyper-parameters
# ------------
imdb_name = 'voc_2007_trainval'
imdb_test_name = 'voc_2007_test'
cfg_file = 'experiments/cfgs/wsddn.yml'
pretrained_model = 'data/pretrained_model/alexnet_imagenet.npy'
output_dir = 'models/saved_model'
visualize = True
vis_interval = 5000
start_step = 0
end_step = 50000
lr_decay_steps = {150000}
lr_decay = 1./10
rand_seed = 1024
_DEBUG = False
use_tensorboard = True
use_visdom = True
log_grads = False
remove_all_log = False # remove all historical experiments in TensorBoard
exp_name = None # the previous experiment name in TensorBoard
# ------------
if rand_seed is not None:
np.random.seed(rand_seed)
# load config file and get hyperparameters
cfg_from_file(cfg_file)
lr = cfg.TRAIN.LEARNING_RATE
momentum = cfg.TRAIN.MOMENTUM
weight_decay = cfg.TRAIN.WEIGHT_DECAY
disp_interval = cfg.TRAIN.DISPLAY
log_interval = cfg.TRAIN.LOG_IMAGE_ITERS
imdb_test = get_imdb(imdb_test_name)
#imdb_test.competition_mode(on=True)
# load imdb and create data later
imdb = get_imdb(imdb_name)
rdl_roidb.prepare_roidb(imdb)
roidb = imdb.roidb
data_layer = RoIDataLayer(roidb, imdb.num_classes)
# Create network and initialize
net = WSDDN(classes=imdb.classes, debug=_DEBUG)
network.weights_normal_init(net, dev=0.001)
if os.path.exists('pretrained_alexnet.pkl'):
pret_net = pkl.load(open('pretrained_alexnet.pkl','r'))
else:
pret_net = model_zoo.load_url('https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth')
pkl.dump(pret_net, open('pretrained_alexnet.pkl','wb'), pkl.HIGHEST_PROTOCOL)
own_state = net.state_dict()
for name, param in pret_net.items():
if name not in own_state:
continue
if isinstance(param, Parameter):
param = param.data
try:
own_state[name].copy_(param)
print('Copied {}'.format(name))
except:
print('Did not find {}'.format(name))
continue
# Move model to GPU and set train mode
net.cuda()
net.train()
# Create optimizer for network parameters
params = list(net.parameters())
optimizer = torch.optim.SGD(params[2:], lr=lr,
momentum=momentum, weight_decay=weight_decay)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
vis = visdom.Visdom(port='8097')
logger = Logger('./mylog',name='WSDDN')
# training
train_loss = 0
tp, tf, fg, bg = 0., 0., 0, 0
step_cnt = 0
re_cnt = False
t = Timer()
t.tic()
for step in range(start_step, end_step+1):
# get one batch
blobs = data_layer.forward()
im_data = blobs['data']
rois = blobs['rois']
im_info = blobs['im_info']
gt_vec = blobs['labels']
#gt_boxes = blobs['gt_boxes']
# forward
net(im_data, rois, im_info, gt_vec)
loss = net.loss
train_loss += loss.data[0]
step_cnt += 1
# backward pass and update
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Log to screen
if step % disp_interval == 0:
duration = t.toc(average=False)
fps = step_cnt / duration
log_text = 'step %d, image: %s, loss: %.4f, fps: %.2f (%.2fs per batch), lr: %.9f, momen: %.4f, wt_dec: %.6f' % (
step, blobs['im_name'], train_loss / step_cnt, fps, 1./fps, lr, momentum, weight_decay)
log_print(log_text, color='green', attrs=['bold'])
re_cnt = True
#TODO: evaluate the model every N iterations (N defined in handout)
if step%5000==0 and step>0:
net.eval()
aps = test_net('WSDDN_TEST', net, imdb_test, 300, thresh=1e-4, visualize=True, logger = logger, step = step)
net.train()
#TODO: Perform all visualizations here
#You can define other interval variable if you want (this is just an
#example)
#The intervals for different things are defined in the handout
#TODO: Create required visualizations
if use_tensorboard:
if visualize and step%500==0:
logger.scalar_summary('train/loss', loss, step)
if visualize and step%2000==0:
logger.model_param_histo_summary(net, step)
if visualize and step%5000==0 and step>0:
logger.scalar_summary('test/mAP', np.mean(aps), step)
for myclass in range(imdb.num_classes):
logger.scalar_summary('test/mAP/class_{}'.format(imdb.classes[myclass]), aps[myclass], step)
if use_visdom:
if visualize and step%500==0:
if step == 0:
vis_loss = vis.line(X=np.array([step]), Y=np.array([loss.data[0]]), opts=dict(title='loss'))
else:
vis_loss = vis.line(X=np.array([step]), Y=np.array([loss.data[0]]), win = vis_loss, update='append', opts=dict(title='loss'))
if visualize and step%5000==0 and step>0:
if step == 5000:
vis_aps = vis.line(X=np.array([step]), Y=np.array([np.mean(aps)]), opts=dict(title='mAP'))
else:
vis_aps = vis.line(X=np.array([step]), Y=np.array([np.mean(aps)]), win=vis_aps, update='append', opts=dict(title='mAP'))
# Save model occasionally
if (step % cfg.TRAIN.SNAPSHOT_ITERS == 0) and step > 0:
save_name = os.path.join(output_dir, '{}_{}.h5'.format(cfg.TRAIN.SNAPSHOT_PREFIX,step))
network.save_net(save_name, net)
print('Saved model to {}'.format(save_name))
if step in lr_decay_steps:
lr *= lr_decay
optimizer = torch.optim.SGD(params, lr=lr, momentum=momentum, weight_decay=weight_decay)
if re_cnt:
tp, tf, fg, bg = 0., 0., 0, 0
train_loss = 0
step_cnt = 0
t.tic()
re_cnt = False