-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPredictor.py
413 lines (354 loc) · 15.1 KB
/
Predictor.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import logging
import mxnet as mx
import numpy as np
import cPickle
import os
import time
class CarReID_Predictor(object):
def __init__(self, prefix='', symbol=None, ctx=None, data_shape=None):
self.prefix = prefix
self.symbol = symbol
self.ctx = ctx
if self.ctx is None:
self.ctx = mx.cpu()
self.data_shape = data_shape
self.batchsize = data_shape[0]
self.arg_params = None
self.aux_params = None
self.executor = None
def get_params(self):
arg_names = self.symbol.list_arguments()
arg_shapes, _, aux_shapes = \
self.symbol.infer_shape(part1_data=self.data_shape,
part2_data=self.data_shape)
self.arg_params = {}
for name, shape in zip(arg_names, arg_shapes):
self.arg_params[name] = mx.nd.zeros(shape, self.ctx)
aux_names = self.symbol.list_auxiliary_states()
self.aux_params = {k: mx.nd.zeros(s, self.ctx) for k, s in zip(aux_names, aux_shapes)}
def set_params(self, whichone):
logging.info('loading checkpoint from %s-->%d...', self.prefix, whichone)
loadfunc = mx.model.load_checkpoint
_, update_params, aux_params = loadfunc(self.prefix, whichone)
for name in update_params:
self.arg_params[name][:] = update_params[name]
# print update_params[name].asnumpy()
for name in aux_params:
self.aux_params[name][:] = aux_params[name]
# print name, aux_params[name].asnumpy()
# exit()
return
def predict(self, data_query, data_set, whichone=None, logger=None):
if logger is not None:
logger.info('Start testing with %s', str(self.ctx))
self.get_params()
if whichone is not None:
self.set_params(whichone)
self.executor = self.symbol.bind(ctx=self.ctx, args=self.arg_params, grad_req='null', aux_states=self.aux_params)
# print self.executor.arg_dict['part1_data'], self.executor.arg_dict['part2_data']
# print self.arg_params['part1_data'], self.arg_params['part2_data']
# for av in self.aux_params:
# print av, self.aux_params[av].asnumpy()
# exit()
# begin training
data_query.reset()
for dquery in data_query:
# datainfo1 = dquery['sons'][0]
id1 = dquery['id']
for datainfo1 in dquery['sons']:
data1 = datainfo1['data'].reshape((1,)+datainfo1['data'].shape)
cmpfile = open('Result/cmp=%s=%s.list'%(id1, datainfo1['name']), 'w')
# d1s = np.mean(data1)
# print data1.shape, self.arg_params['part1_data'].asnumpy().shape
self.arg_params['part1_data'][:] = mx.nd.array(data1, self.ctx)
data_set.reset()
for dset in data_set:
id2 = dset['id']
for datainfo2 in dset['sons']:
data2 = datainfo2['data'].reshape((1,)+datainfo2['data'].shape)
# d2s = np.mean(data2)
self.arg_params['part2_data'][:] = mx.nd.array(data2, self.ctx)
self.executor.forward(is_train=False)
cmp_score = self.executor.outputs[0].asnumpy()[0, 0]
cmpfile.write('%s,%s,%f\n'%(id2, datainfo2['name'], cmp_score))
cmpfile.flush()
# print 'query:%s,%.3f,%d; dset:%s,%.3f,%d; %.3f'%(id1, d1s, data_query.cur_idx, id2, d2s, data_set.cur_idx, cmp_score)
print 'query:%s,%d; dset:%s,%d; %.3f'%(id1, data_query.cur_idx, id2, data_set.cur_idx, cmp_score)
cmpfile.close()
# exit()
class CarReID_Feature_Predictor(object):
def __init__(self, prefix='', symbol=None, ctx=None, data_shape=None):
self.prefix = prefix
self.symbol = symbol
self.ctx = ctx
if self.ctx is None:
self.ctx = mx.cpu()
self.data_shape = data_shape
self.batchsize = data_shape[0]
self.arg_params = None
self.aux_params = None
self.executor = None
def get_params(self):
arg_names = self.symbol.list_arguments()
arg_shapes, _, aux_shapes = \
self.symbol.infer_shape(part1_data=self.data_shape)
self.arg_params = {}
for name, shape in zip(arg_names, arg_shapes):
self.arg_params[name] = mx.nd.zeros(shape, self.ctx)
aux_names = self.symbol.list_auxiliary_states()
self.aux_params = {k: mx.nd.zeros(s, self.ctx) for k, s in zip(aux_names, aux_shapes)}
def set_params(self, whichone):
logging.info('loading checkpoint from %s-->%d...', self.prefix, whichone)
loadfunc = mx.model.load_checkpoint
_, update_params, aux_params = loadfunc(self.prefix, whichone)
for name in self.arg_params:
if name.endswith('weight') or name.endswith('bias') or name.endswith('gamma') or name.endswith('beta'):
self.arg_params[name][:] = update_params[name]
# print update_params[name].asnumpy()
for name in self.aux_params:
if name.endswith('moving_var') or name.endswith('moving_mean'):
self.aux_params[name][:] = aux_params[name]
# print name, aux_params[name].asnumpy()
# exit()
return
def predict(self, data_set, savepath, whichone=None, logger=None):
if logger is not None:
logger.info('Start testing with %s', str(self.ctx))
self.get_params()
if whichone is not None:
self.set_params(whichone)
self.executor = self.symbol.bind(ctx=self.ctx, args=self.arg_params, grad_req='null', aux_states=self.aux_params)
# begin training
data_set.reset()
for dquery in data_set:
id1 = dquery['id']
for datainfo1 in dquery['sons']:
data1 = datainfo1['data'].reshape((1,)+datainfo1['data'].shape)
self.arg_params['part1_data'][:] = mx.nd.array(data1, self.ctx)
self.executor.forward(is_train=False)
feature = self.executor.outputs[0].asnumpy()
print feature[0, 0]
idfolder = savepath + '/' + id1
if not os.path.exists(idfolder):
os.makedirs(idfolder)
featfn = idfolder + '/' + datainfo1['name'] + '.bin'
cPickle.dump(feature, open(featfn, 'wb'))
print 'saved feature:%d/%d, %s'%(data_set.cur_idx, data_set.datalen, featfn)
class CarReID_Compare_Predictor(object):
def __init__(self, prefix='', symbol=None, ctx=None, data_shape=None):
self.prefix = prefix
self.symbol = symbol
self.ctx = ctx
if self.ctx is None:
self.ctx = mx.cpu()
self.data_shape = data_shape
self.batchsize = data_shape[0]
self.arg_params = None
self.aux_params = None
self.executor = None
def get_params(self):
arg_names = self.symbol.list_arguments()
arg_shapes, _, aux_shapes = \
self.symbol.infer_shape(feature1_data=(1, self.data_shape[1]),
feature2_data=self.data_shape)
self.arg_params = {}
for name, shape in zip(arg_names, arg_shapes):
self.arg_params[name] = mx.nd.zeros(shape, self.ctx)
aux_names = self.symbol.list_auxiliary_states()
self.aux_params = {k: mx.nd.zeros(s, self.ctx) for k, s in zip(aux_names, aux_shapes)}
def set_params(self, whichone):
logging.info('loading checkpoint from %s-->%d...', self.prefix, whichone)
loadfunc = mx.model.load_checkpoint
_, update_params, aux_params = loadfunc(self.prefix, whichone)
for name in self.arg_params:
if name.endswith('weight') or name.endswith('bias') or name.endswith('gamma') or name.endswith('beta'):
self.arg_params[name][:] = update_params[name]
# print update_params[name].asnumpy()
for name in self.aux_params:
if name.endswith('moving_var') or name.endswith('moving_mean'):
self.aux_params[name][:] = aux_params[name]
# print name, aux_params[name].asnumpy()
# exit()
return
def predict(self, data_query, data_set, whichone=None, logger=None):
if logger is not None:
logger.info('Start Comparing with %s', str(self.ctx))
self.get_params()
if whichone is not None:
self.set_params(whichone)
self.executor = self.symbol.bind(ctx=self.ctx, args=self.arg_params, grad_req='null', aux_states=self.aux_params)
data_query.reset()
for dquery in data_query:
id1 = dquery['ids'][0]
data1 = dquery['data']
cmpfile = open('Result/cmp=%s=%s.list'%(id1, dquery['names'][0]), 'w')
print data1.shape, self.arg_params['feature1_data'].asnumpy().shape
self.arg_params['feature1_data'][:] = mx.nd.array(data1, self.ctx)
data_set.reset()
t0 = time.time()
for dset in data_set:
id2s = dset['ids']
data2 = dset['data']
self.arg_params['feature2_data'][:] = mx.nd.array(data2, self.ctx)
self.executor.forward(is_train=False)
cmp_scores = self.executor.outputs[0].asnumpy()
# print data_set.batchsize, data1.shape, data2.shape, np.sum(np.abs(data1 - data2)), np.sum(cmp_scores)
# print cmp_scores
# print data1[0, 0], data2[500, 0]
if True:
cmp_scores = np.sum(cmp_scores, axis=1)
# print data2.shape, cmp_scores
writestrs = ''
# t0_0 = time.time()
for bi in xrange(data_set.batchsize):
id2 = id2s[bi]
onename = dset['names'][bi]
cmp_score = cmp_scores[bi]
writestrs += '%s,%s,%f\n'%(id2, onename, cmp_score)
# print 'query:%s,%d; dset:%s,%d; %.3f'%(id1, data_query.cur_idx, id2, data_set.cur_idx*data_set.batchsize+bi, cmp_score)
# t1_0 = time.time()
cmpfile.write(writestrs)
cmpfile.flush()
cmpfile.close()
t1 = time.time()
print '%s, %d->time cost:%.3f s'%(id1, data_query.cur_idx, (t1-t0))
class CarReID_Softmax_Predictor(object):
def __init__(self, prefix='', symbol=None, ctx=None, data_shape=None):
self.prefix = prefix
self.symbol = symbol
self.ctx = ctx
if self.ctx is None:
self.ctx = mx.cpu()
self.data_shape = data_shape
self.batchsize = data_shape[0]
self.arg_params = None
self.aux_params = None
self.executor = None
def init_args(self, args):
for key in args:
arr = args[key]
if key.endswith('_weight'):
self.initializer(key, arr)
if key.endswith('_bias'):
arr[:] = 0.0
if key.endswith('_gamma'):
arr[:] = 1.0
if key.endswith('_beta'):
arr[:] = 0.0
if key.endswith('_init_c'):
arr[:] = 0.0
if key.endswith('_init_h'):
arr[:] = 0.0
def get_params(self):
arg_names = self.symbol.list_arguments()
arg_shapes, _, aux_shapes = \
self.symbol.infer_shape(data=self.data_shape)
self.arg_params = {}
for name, shape in zip(arg_names, arg_shapes):
self.arg_params[name] = mx.nd.zeros(shape, self.ctx)
aux_names = self.symbol.list_auxiliary_states()
self.aux_params = {k: mx.nd.zeros(s, self.ctx) for k, s in zip(aux_names, aux_shapes)}
def set_params(self, whichone):
logging.info('loading checkpoint from %s-->%d...', self.prefix, whichone)
loadfunc = mx.model.load_checkpoint
_, update_params, aux_params = loadfunc(self.prefix, whichone)
for name in update_params:
self.arg_params[name][:] = update_params[name]
# print update_params[name].asnumpy()
for name in aux_params:
self.aux_params[name][:] = aux_params[name]
# print name, aux_params[name].asnumpy()
# exit()
return
def predict(self, train_data, showperiod=100, whichone=None, logger=None):
if logger is not None:
logger.info('Start softmax predicting with %s', str(self.ctx))
savefunc = mx.model.save_checkpoint
self.get_params()
if whichone is not None:
self.set_params(whichone)
self.executor = self.symbol.bind(self.ctx, self.arg_params, grad_req='null', aux_states=self.aux_params)
# epoch_end_callback = mx.callback.do_checkpoint(self.prefix)
# begin training
accus = []
train_data.reset()
num_batches = train_data.num_batches
num_update = 0
for databatch in train_data:
num_update += 1
for k, v in databatch.data.items():
self.arg_params[k][:] = mx.nd.array(v, self.ctx)
for k, v in databatch.label.items():
self.arg_params[k][:] = mx.nd.array(v, self.ctx)
output_dict = {name: nd for name, nd in zip(self.symbol.list_outputs(), self.executor.outputs)}
self.executor.forward(is_train=False)
outval = output_dict['cls_output'].asnumpy()
label = databatch.label['label']
cls_predict = np.argmax(outval, axis=1)
accone = np.mean(cls_predict!=label)
accus.append(accone)
if num_update % showperiod == 0:
print num_update, 'errate_accu:', np.mean(accus)
accus = []
class CarReID_Compare_Predictor__(object):
def __init__(self, prefix='', symbol=None, ctx=None, data_shape=None):
self.prefix = prefix
self.symbol = symbol
self.ctx = ctx
if self.ctx is None:
self.ctx = mx.cpu()
self.data_shape = data_shape
self.batchsize = data_shape[0]
self.arg_params = None
self.aux_params = None
self.executor = None
def get_params(self):
arg_names = self.symbol.list_arguments()
arg_shapes, _, aux_shapes = \
self.symbol.infer_shape(feature1_data=self.data_shape,
feature2_data=self.data_shape)
self.arg_params = {}
for name, shape in zip(arg_names, arg_shapes):
self.arg_params[name] = mx.nd.zeros(shape, self.ctx)
aux_names = self.symbol.list_auxiliary_states()
self.aux_params = {k: mx.nd.zeros(s, self.ctx) for k, s in zip(aux_names, aux_shapes)}
def set_params(self, whichone):
logging.info('loading checkpoint from %s-->%d...', self.prefix, whichone)
loadfunc = mx.model.load_checkpoint
_, update_params, aux_params = loadfunc(self.prefix, whichone)
for name in self.arg_params:
if name.endswith('weight') or name.endswith('bias') or name.endswith('gamma') or name.endswith('beta'):
self.arg_params[name][:] = update_params[name]
# print update_params[name].asnumpy()
for name in self.aux_params:
if name.endswith('moving_var') or name.endswith('moving_mean'):
self.aux_params[name][:] = aux_params[name]
# print name, aux_params[name].asnumpy()
# exit()
return
def predict(self, data_query, data_set, whichone=None, logger=None):
if logger is not None:
logger.info('Start Comparing with %s', str(self.ctx))
self.get_params()
if whichone is not None:
self.set_params(whichone)
self.executor = self.symbol.bind(ctx=self.ctx, args=self.arg_params, grad_req='null', aux_states=self.aux_params)
data_query.reset()
for dquery in data_query:
id1 = dquery['id']
for datainfo1 in dquery['sons']:
data1 = datainfo1['data'].reshape((1,)+datainfo1['data'].shape)
cmpfile = open('Result/cmp=%s=%s.list'%(id1, datainfo1['name']), 'w')
self.arg_params['feature1_data'][:] = mx.nd.array(data1, self.ctx)
data_set.reset()
for dset in data_set:
id2 = dset['id']
for datainfo2 in dset['sons']:
data2 = datainfo2['data'].reshape((1,)+datainfo2['data'].shape)
self.arg_params['feature2_data'][:] = mx.nd.array(data2, self.ctx)
self.executor.forward(is_train=False)
cmp_score = self.executor.outputs[0].asnumpy()[0, 0]
cmpfile.write('%s,%s,%f\n'%(id2, datainfo2['name'], cmp_score))
cmpfile.flush()
print 'query:%s,%d; dset:%s,%d; %.3f'%(id1, data_query.cur_idx, id2, data_set.cur_idx, cmp_score)
cmpfile.close()