-
Notifications
You must be signed in to change notification settings - Fork 7
/
Train_Parallel_Combine.py
267 lines (208 loc) · 9.67 KB
/
Train_Parallel_Combine.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
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
import logging
import numpy as np
import mxnet as mx
from mxnet import metric
from DataIter import CarReID_Proxy_Mxnet_Iter
from DataIter import CarReID_Proxy_Mxnet_Iter2
from Module_Combine import Module_Info, Module_Combine
from MDL_PARAM import model2_proxy_nca_combine as proxy_nca_combine
class Proxy_Metric(metric.EvalMetric):
def __init__(self, saveperiod=1):
super(Proxy_Metric, self).__init__('proxy_metric')
print "hello metric init..."
self.num_inst = 0
self.sum_metric = 0.0
self.p_inst = 0
self.saveperiod=saveperiod
# def reset(self):
# pass
def update(self, labels, preds):
# print '=========%d========='%(self.p_inst)
self.p_inst += 1
if self.p_inst%self.saveperiod==0:
self.num_inst += 1
loss = preds[0].asnumpy().mean()
# print 'metric', loss
self.sum_metric += loss
def Do_Proxy_NCA_Train2():
print 'Proxy NCA Training...'
# set up logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
mod_context0 = [mx.gpu(1), mx.gpu(2), mx.gpu(3)]
mod_context1 = [mx.gpu(0)]
devicenum = len(mod_context0)
proxy_devicenum = len(mod_context1)
num_epoch = 10000
batch_size = 50*devicenum
show_period = 1000
assert(batch_size%devicenum==0)
bsz_per_device = batch_size / devicenum
proxy_bsz_per_device = batch_size / proxy_devicenum
print 'batch_size per device:', bsz_per_device
bucket_key = bsz_per_device
featdim = 128
proxy_num = 43928
clsnum = proxy_num
data_shape = (batch_size, 3, 299, 299)
proxy_yM_shape = (batch_size, proxy_num)
proxy_ZM_shape = (batch_size, proxy_num)
reid_feature_shape = (batch_size, featdim)
label_shape = dict(zip(['proxy_yM', 'proxy_ZM'], [proxy_yM_shape, proxy_ZM_shape]))
datafn = '/home/mingzhang/data/car_ReID_for_zhangming/data_each.list' #43928 calss number.
data_train = CarReID_Proxy_Mxnet_Iter(['data'], [data_shape], ['proxy_yM', 'proxy_ZM'], [proxy_yM_shape, proxy_ZM_shape], datafn, bucket_key)
dlr = 400000/batch_size
# dlr_steps = [dlr, dlr*2, dlr*3, dlr*4]
lr_start = 0.6*(10**-4)
lr_min = 10**-5
lr_reduce = 0.95
lr_stepnum = np.log(lr_min/lr_start)/np.log(lr_reduce)
lr_stepnum = np.int(np.ceil(lr_stepnum))
dlr_steps = [dlr*i for i in xrange(1, lr_stepnum+1)]
print 'lr_start:%.1e, lr_min:%.1e, lr_reduce:%.2f, lr_stepsnum:%d'%(lr_start, lr_min, lr_reduce, lr_stepnum)
print dlr_steps
lr_scheduler = mx.lr_scheduler.MultiFactorScheduler(dlr_steps, lr_reduce)
# lr_scheduler = mx.lr_scheduler.FactorScheduler(dlr, 0.9)
param_prefix = 'MDL_PARAM/params2_proxy_nca_combine/car_reid'
reid_feature, proxy_loss = proxy_nca_combine.CreateModel_Color_Combine(None, bsz_per_device, proxy_bsz_per_device, proxy_num, data_shape[2:])
optimizer_params={'learning_rate':lr_start,
'momentum':0.9,
'wd':0.0005,
'lr_scheduler':lr_scheduler,
'clip_gradient':None,
'rescale_grad': 1.0/batch_size}
mod_info0 = Module_Info(name='reid_feature', symbol=reid_feature,
data_names=['data'], data_shapes=[data_shape],
label_names=None, label_shapes=None,
inputs_need_grad=False,
optimizer='sgd',
optimizer_params=optimizer_params,
context=mod_context0)
mod_info1 = Module_Info(name='proxy_loss', symbol=proxy_loss,
data_names=['reid_feature'], data_shapes=[reid_feature_shape],
label_names=['proxy_yM', 'proxy_ZM'], label_shapes=[proxy_yM_shape, proxy_ZM_shape],
inputs_need_grad=True,
optimizer='sgd',
optimizer_params=optimizer_params,
context=mod_context1)
reid_model = Module_Combine(module_infos=[mod_info0, mod_info1])
proxy_metric = Proxy_Metric()
if True:
reid_model.bind(for_training=True)
reid_model.load_checkpoint(param_prefix, 1)
def norm_stat(d):
return mx.nd.norm(d)/np.sqrt(d.size)
mon = mx.mon.Monitor(1, norm_stat,
pattern='.*part1_fc1.*|.*proxy_Z_weight.*')
def batch_end_call(*args, **kwargs):
# print eval_metric.loss_list
epoch = args[0].epoch
nbatch = args[0].nbatch + 1
eval_metric = args[0].eval_metric
data_batch = args[0].locals['data_batch']
if nbatch%show_period==0:
reid_model.save_checkpoint(param_prefix, epoch%4)
batch_end_calls = [batch_end_call, mx.callback.Speedometer(batch_size, show_period/10)]
reid_model.fit(train_data=data_train, eval_metric=proxy_metric,
begin_epoch=50, num_epoch=num_epoch,
eval_end_callback=None,
batch_end_callback=batch_end_calls)
return
def Do_Proxy_NCA_Train3():
print 'Proxy NCA Training...'
# set up logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
mod_context0 = [mx.gpu(7), mx.gpu(6), mx.gpu(5)]
mod_context1 = [mx.gpu(4), mx.gpu(3), mx.gpu(2), mx.gpu(1), mx.gpu(0)]
devicenum = len(mod_context0)
proxy_devicenum = len(mod_context1)
num_epoch = 10000
batch_size = 50*devicenum
show_period = 1000
assert(batch_size%devicenum==0)
bsz_per_device = batch_size / devicenum
proxy_bsz_per_device = batch_size / proxy_devicenum
print 'batch_size per device:', bsz_per_device
bucket_key = bsz_per_device
featdim = 128
proxy_num = 196166#294255#548597
clsnum = proxy_num
data_shape = (batch_size, 3, 299, 299)
proxy_yM_shape = (batch_size, proxy_num)
proxy_ZM_shape = (batch_size, proxy_num)
reid_feature_shape = (batch_size, featdim)
label_shape = dict(zip(['proxy_yM', 'proxy_ZM'], [proxy_yM_shape, proxy_ZM_shape]))
proxyfn = 'proxy.bin'
datapath = '/home/mingzhang/data/ReID_origin/mingzhang/'
# datapath = '/mnt/sdc1/mingzhang/ReID_origin/mingzhang/'
# datafn_list = ['data_each_part1.list', 'data_each_part2.list', 'data_each_part3.list', 'data_each_part4.list', 'data_each_part5.list', 'data_each_part6.list', 'data_each_part7.list'] #43928 calss number.
# datafn_list = ['data_each_part1.list', 'data_each_part2.list', 'data_each_part3.list', 'data_each_part4.list'] #43928 calss number.
datafn_list = ['data_each_part1.list', 'data_each_part2.list', 'data_each_part3.list'] #43928 calss number.
for di in xrange(len(datafn_list)):
datafn_list[di] = datapath + datafn_list[di]
# datafn = '/home/mingzhang/data/car_ReID_for_zhangming/data_each.500.list'
# data_train = CarReID_Proxy2_Iter(['data'], [data_shape], ['proxy_yM', 'proxy_ZM'], [proxy_yM_shape, proxy_ZM_shape], datafn, bucket_key)
data_train = CarReID_Proxy_Mxnet_Iter2(['data'], [data_shape], ['proxy_yM', 'proxy_ZM'], [proxy_yM_shape, proxy_ZM_shape], datafn_list, bucket_key)
dlr = 400000/batch_size
# dlr_steps = [dlr, dlr*2, dlr*3, dlr*4]
lr_start = (10**-1)
lr_min = 10**-5
lr_reduce = 0.95
lr_stepnum = np.log(lr_min/lr_start)/np.log(lr_reduce)
lr_stepnum = np.int(np.ceil(lr_stepnum))
dlr_steps = [dlr*i for i in xrange(1, lr_stepnum+1)]
print 'lr_start:%.1e, lr_min:%.1e, lr_reduce:%.2f, lr_stepsnum:%d'%(lr_start, lr_min, lr_reduce, lr_stepnum)
print dlr_steps
lr_scheduler = mx.lr_scheduler.MultiFactorScheduler(dlr_steps, lr_reduce)
# lr_scheduler = mx.lr_scheduler.FactorScheduler(dlr, 0.9)
param_prefix = 'MDL_PARAM/params2_proxy_nca_combine/car_reid'
reid_feature, proxy_loss = proxy_nca_combine.CreateModel_Color_Combine(None, bsz_per_device, proxy_bsz_per_device, proxy_num, data_shape[2:])
optimizer_params={'learning_rate':lr_start,
'momentum':0.9,
'wd':0.0005,
'lr_scheduler':lr_scheduler,
'clip_gradient':None,
'rescale_grad': 1.0/batch_size}
mod_info0 = Module_Info(name='reid_feature', symbol=reid_feature,
data_names=['data'], data_shapes=[data_shape],
label_names=None, label_shapes=None,
inputs_need_grad=False,
optimizer='sgd',
optimizer_params=optimizer_params,
context=mod_context0)
mod_info1 = Module_Info(name='proxy_loss', symbol=proxy_loss,
data_names=['reid_feature'], data_shapes=[reid_feature_shape],
label_names=['proxy_yM', 'proxy_ZM'], label_shapes=[proxy_yM_shape, proxy_ZM_shape],
inputs_need_grad=True,
optimizer='sgd',
optimizer_params=optimizer_params,
context=mod_context1)
reid_model = Module_Combine(module_infos=[mod_info0, mod_info1])
proxy_metric = Proxy_Metric()
if True:
reid_model.bind(for_training=True)
reid_model.load_checkpoint(param_prefix, 1)
def norm_stat(d):
return mx.nd.norm(d)/np.sqrt(d.size)
mon = mx.mon.Monitor(1, norm_stat,
pattern='.*part1_fc1.*|.*proxy_Z_weight.*')
def batch_end_call(*args, **kwargs):
# print eval_metric.loss_list
epoch = args[0].epoch
nbatch = args[0].nbatch + 1
eval_metric = args[0].eval_metric
data_batch = args[0].locals['data_batch']
if nbatch%show_period==0:
reid_model.save_checkpoint(param_prefix, epoch%4)
batch_end_calls = [batch_end_call, mx.callback.Speedometer(batch_size, show_period/10)]
reid_model.fit(train_data=data_train, eval_metric=proxy_metric,
begin_epoch=10, num_epoch=num_epoch,
eval_end_callback=None, kvstore=None,
batch_end_callback=batch_end_calls)
return
if __name__=='__main__':
# Do_Train()
# Do_Proxy_NCA_Train()
# Do_Proxy_NCA_Train2()
Do_Proxy_NCA_Train3()