-
Notifications
You must be signed in to change notification settings - Fork 2
/
vgg_feature_extractor.py
570 lines (481 loc) · 28.8 KB
/
vgg_feature_extractor.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import numpy as np
import cv2
import tensorflow as tf
from feature_extractor import FeatureExtractor
import display
from conv_reg_config import TrainDataCfg
VGG_MODEL_PATH = TrainDataCfg.VGG_MODEL_PATH
VGG_MEAN = TrainDataCfg.VGG_MEAN
class VggExtractor(FeatureExtractor):
def __init__(self):
super(VggExtractor, self).__init__()
self._feature_width = 0
self._feature_height = 0
self._channel_num = 64
self._resolution = 4
self._feature_mean = TrainDataCfg.VGG_FEATURE_MEAN
self._feature_std = TrainDataCfg.VGG_FEATURE_STD
self._feature_offset = None
self._feature_scale = None
self._graph = None
self._session = None
self._input_holder = None
self._output_feature = None
self._output_feature_after_pca = None
self._use_pca = True
self.pca = None
self._load_data()
def _build_network(self, input_height, input_width):
pass
def _build_pca_network(self):
with self._graph.as_default():
_output_channel = self._output_feature.shape.dims[-1]
self._pca_mean = tf.Variable(tf.zeros([1, 1, 1, _output_channel]), trainable=False)
self._pca_vector = tf.Variable(tf.zeros([1, 1, _output_channel, self._channel_num]),
trainable=False,
expected_shape=[1, 1, _output_channel, self._channel_num])
_sub_mean = self._output_feature - self._pca_mean
self._output_feature_after_pca = tf.nn.conv2d(_sub_mean, self._pca_vector, [1, 1, 1, 1], padding='SAME')
_pca_initializer = tf.variables_initializer([self._pca_mean, self._pca_vector])
self._session.run(_pca_initializer)
def _load_data(self):
pass
def extract_multiple_features(self, input_images):
# assert len(input_images) > 0
# input_width = input_images[0].shape[1]
# input_height = input_images[0].shape[0]
#
# if input_height != self._feature_height or input_width != self._feature_width:
# self._build_network(input_height, input_width)
#
# _merge_list = []
# for image in input_images:
# _merge_list.append(image[np.newaxis, :, :, :])
# merged = np.concatenate(_merge_list, axis=0)
# feed_dict = {self._input_holder: merged}
# output_feature = self._session.run(self._output_feature, feed_dict=feed_dict)
# return output_feature
assert len(input_images) > 0
input_width = input_images[0].shape[1]
input_height = input_images[0].shape[0]
if input_height != self._feature_height or input_width != self._feature_width:
self._build_network(input_height, input_width)
self._build_pca_network()
self.pca = None
self._feature_offset = None
self._feature_scale = None
_merge_list = []
for image in input_images:
_merge_list.append(image[np.newaxis, :, :, :])
merged = np.concatenate(_merge_list, axis=0)
if not self._use_pca:
output_features = self._session.run(self._output_feature, feed_dict={self._input_holder: merged})
else:
if not self.pca:
_org_features = self._session.run(self._output_feature, feed_dict={self._input_holder: merged})
# _temp_save_path = './tmp/conv_feature.npy'
# np.save(_temp_save_path, _org_features)
self.pca = FeatureReduction(_org_features[0], self._channel_num)
self._session.run(self._pca_mean.assign(self.pca.mean.reshape((1, 1, 1, -1))))
self._session.run(self._pca_vector.assign(self.pca.eigen_vecs.T.reshape((1, 1, -1, self._channel_num))))
output_features = self._session.run(self._output_feature_after_pca,
feed_dict={self._output_feature: _org_features})
else:
output_features = self._session.run(self._output_feature_after_pca,
feed_dict={self._input_holder: merged})
# if self._use_pca:
# assert self._channel_num < output_features.shape[3]
# if not self.pca:
# self.pca = FeatureReduction(output_features[0], self._channel_num)
# self._pca_mean.assign(self.pca.mean.reshape((1, 1, 1, self._channel_num)))
# self._pca_vector.assign(self.pca.eigen_vecs.reshape((1,1,-1,self._channel_num)))
#
# re_features = self.pca.project(output_features)
# else:
# assert self._channel_num == output_features.shape[3]
# re_features = output_features
if not self._feature_offset or not self._feature_scale:
_mean = np.mean(output_features)
_std = np.std(output_features)
self._feature_offset = self._feature_mean - _mean
self._feature_scale = self._feature_std / _std
# normalized_feature = self._feature_scale * (output_features-self._feature_offset)
# _mean = np.mean(normalized_feature)
# _std = np.std(normalized_feature)
# print('\tfeatures mean:{:.6e}, std:{:.6e}'.format(_mean, _std))
# hist, bin_edges = np.histogram(normalized_feature, bins=100)
# display.show_histogram(hist, bin_edges)
return output_features
class VggL1Extractor(VggExtractor):
def __init__(self):
super(VggL1Extractor, self).__init__()
self._use_pca = False
def _build_network(self, input_height, input_width):
assert not input_height % self._resolution and not input_width % self._resolution
if self._session:
self._session.close()
self._graph = tf.Graph()
self._feature_height = input_height
self._feature_width = input_width
print('Starting building the network for h={:d} w={:d}'.format(input_height, input_width))
with self._graph.as_default():
_input_shape = (None, input_height, input_width, 3)
self._input_holder = tf.placeholder(tf.float32, shape=_input_shape)
_mean = tf.Variable(VGG_MEAN, trainable=False)
_sub_mean = self._input_holder - _mean
_conv_11_w = tf.Variable(self._conv_data_11_weights)
_conv_11_b = tf.Variable(self._conv_data_11_bias)
_conv_11_output = tf.nn.conv2d(_sub_mean, _conv_11_w, [1, 1, 1, 1], padding='SAME') + _conv_11_b
_conv_11_act = tf.nn.relu(_conv_11_output)
_max_pool_11_output = tf.nn.max_pool(_conv_11_act, (1, 2, 2, 1), (1, 2, 2, 1), padding='SAME')
_conv_12_w = tf.Variable(self._conv_data_12_weights)
_conv_12_b = tf.Variable(self._conv_data_12_bias)
_conv_12_output = tf.nn.conv2d(_max_pool_11_output, _conv_12_w, [1, 1, 1, 1], padding='SAME') + _conv_12_b
_conv_12_act = tf.nn.relu(_conv_12_output)
_max_pool_12_output = tf.nn.max_pool(_conv_12_act, (1, 2, 2, 1), (1, 2, 2, 1), padding='SAME')
self._output_feature = _max_pool_12_output
self._session = tf.Session(graph=self._graph)
self._session.run(tf.global_variables_initializer())
def _load_data(self):
with np.load(VGG_MODEL_PATH) as npz_file:
self._conv_data_11_weights = npz_file['conv1_1/weights']
self._conv_data_11_bias = npz_file['conv1_1/biases']
self._conv_data_12_weights = npz_file['conv1_2/weights']
self._conv_data_12_bias = npz_file['conv1_2/biases']
print('VggL1 parameters loaded successfully!')
# def extract_feature(self, input_image):
# input_width = input_image.shape[1]
# input_height = input_image.shape[0]
# assert input_image.shape[2] == 3
# if input_height != self._feature_height or input_width != self._feature_width:
# self._build_network(input_height, input_width)
# assert self._session
# feed_dict = {self._input_holder: input_image[np.newaxis,:,:,:]}
# output_feature = self._session.run(self._output_feature, feed_dict=feed_dict)
# return output_feature[0,:,:,:]
class FeatureReduction(object):
def __init__(self, image_feature, max_components):
assert image_feature.ndim == 3
feature = np.reshape(image_feature, (-1, image_feature.shape[2]))
_mean = np.mean(feature, axis=0, keepdims=True)
self.mean, self.eigen_vecs = cv2.PCACompute(feature, _mean, maxComponents=max_components)
print('\tPCA computed!')
def project(self, images_features):
assert images_features.ndim == 4
data = np.reshape(images_features, (-1, images_features.shape[3]))
coeffs = cv2.PCAProject(data, self.mean, self.eigen_vecs)
re_shape = list(images_features.shape)
re_shape[3] = len(self.eigen_vecs)
re_features = np.reshape(coeffs, re_shape)
return re_features
class VggL2Extractor(VggExtractor):
def __init__(self):
super(VggL2Extractor, self).__init__()
def _build_network(self, input_height, input_width):
assert not input_height % self._resolution and not input_width % self._resolution
if self._session:
self._session.close()
self._graph = tf.Graph()
self._feature_height = input_height
self._feature_width = input_width
print('Starting building the network for h={:d} w={:d}'.format(input_height, input_width))
with self._graph.as_default():
_input_shape = (None, input_height, input_width, 3)
self._input_holder = tf.placeholder(tf.float32, shape=_input_shape)
_mean = tf.Variable(VGG_MEAN, trainable=False)
_sub_mean = self._input_holder - _mean
_conv_11_w = tf.Variable(self._conv_data_11_weights)
_conv_11_b = tf.Variable(self._conv_data_11_bias)
_conv_11_output = tf.nn.conv2d(_sub_mean, _conv_11_w, [1, 1, 1, 1], padding='SAME') + _conv_11_b
_conv_11_act = tf.nn.relu(_conv_11_output)
_conv_12_w = tf.Variable(self._conv_data_12_weights)
_conv_12_b = tf.Variable(self._conv_data_12_bias)
_conv_12_output = tf.nn.conv2d(_conv_11_act, _conv_12_w, [1, 1, 1, 1], padding='SAME') + _conv_12_b
_conv_12_act = tf.nn.relu(_conv_12_output)
_max_pool_12_output = tf.nn.max_pool(_conv_12_act, (1, 2, 2, 1), (1, 2, 2, 1), padding='SAME')
_conv_21_w = tf.Variable(self._conv_data_21_weights)
_conv_21_b = tf.Variable(self._conv_data_21_bias)
_conv_21_output = tf.nn.conv2d(_max_pool_12_output, _conv_21_w, (1, 1, 1, 1), padding='SAME') + _conv_21_b
_conv_21_act = tf.nn.relu(_conv_21_output)
_conv_22_w = tf.Variable(self._conv_data_22_weights)
_conv_22_b = tf.Variable(self._conv_data_22_bias)
_conv_22_output = tf.nn.conv2d(_conv_21_act, _conv_22_w, (1, 1, 1, 1), padding='SAME') + _conv_22_b
_conv_22_act = tf.nn.relu(_conv_22_output)
_max_pool_22_output = tf.nn.max_pool(_conv_22_act, (1, 2, 2, 1), (1, 2, 2, 1,), padding='SAME')
self._output_feature = _max_pool_22_output
self._session = tf.Session(graph=self._graph)
self._session.run(tf.global_variables_initializer())
def _load_data(self):
with np.load(VGG_MODEL_PATH) as npz_file:
self._conv_data_11_weights = npz_file['conv1_1/weights']
self._conv_data_11_bias = npz_file['conv1_1/biases']
self._conv_data_12_weights = npz_file['conv1_2/weights']
self._conv_data_12_bias = npz_file['conv1_2/biases']
self._conv_data_21_weights = npz_file['conv2_1/weights']
self._conv_data_21_bias = npz_file['conv2_1/biases']
self._conv_data_22_weights = npz_file['conv2_2/weights']
self._conv_data_22_bias = npz_file['conv2_2/biases']
print('VggL2 parameters loaded successfully!')
class VggL3Extractor(VggExtractor):
def __init__(self):
super(VggL3Extractor, self).__init__()
def _build_network(self, input_height, input_width):
assert not input_height % self._resolution and not input_width % self._resolution
if self._session:
self._session.close()
self._graph = tf.Graph()
self._feature_height = input_height
self._feature_width = input_width
print('Starting building the network for h={:d} w={:d}'.format(input_height, input_width))
with self._graph.as_default():
_input_shape = (None, input_height, input_width, 3)
self._input_holder = tf.placeholder(tf.float32, shape=_input_shape)
_mean = tf.Variable(VGG_MEAN, trainable=False)
_sub_mean = self._input_holder - _mean
_conv_11_w = tf.Variable(self._conv_data_11_weights)
_conv_11_b = tf.Variable(self._conv_data_11_bias)
_conv_11_output = tf.nn.conv2d(_sub_mean, _conv_11_w, [1, 1, 1, 1], padding='SAME') + _conv_11_b
_conv_11_act = tf.nn.relu(_conv_11_output)
_conv_12_w = tf.Variable(self._conv_data_12_weights)
_conv_12_b = tf.Variable(self._conv_data_12_bias)
_conv_12_output = tf.nn.conv2d(_conv_11_act, _conv_12_w, [1, 1, 1, 1], padding='SAME') + _conv_12_b
_conv_12_act = tf.nn.relu(_conv_12_output)
_max_pool_12_output = tf.nn.max_pool(_conv_12_act, (1, 2, 2, 1), (1, 2, 2, 1), padding='SAME')
_conv_21_w = tf.Variable(self._conv_data_21_weights)
_conv_21_b = tf.Variable(self._conv_data_21_bias)
_conv_21_output = tf.nn.conv2d(_max_pool_12_output, _conv_21_w, (1, 1, 1, 1), padding='SAME') + _conv_21_b
_conv_21_act = tf.nn.relu(_conv_21_output)
_conv_22_w = tf.Variable(self._conv_data_22_weights)
_conv_22_b = tf.Variable(self._conv_data_22_bias)
_conv_22_output = tf.nn.conv2d(_conv_21_act, _conv_22_w, (1, 1, 1, 1), padding='SAME') + _conv_22_b
_conv_22_act = tf.nn.relu(_conv_22_output)
_max_pool_22_output = tf.nn.max_pool(_conv_22_act, (1, 2, 2, 1), (1, 2, 2, 1,), padding='SAME')
_conv_31_w = tf.Variable(self._conv_data_31_weights)
_conv_31_b = tf.Variable(self._conv_data_31_bias)
_conv_31_output = tf.nn.conv2d(_max_pool_22_output, _conv_31_w, (1, 1, 1, 1), padding='SAME') + \
_conv_31_b
_conv_31_act = tf.nn.relu(_conv_31_output)
_conv_32_w = tf.Variable(self._conv_data_32_weights)
_conv_32_b = tf.Variable(self._conv_data_31_bias)
_conv_32_output = tf.nn.conv2d(_conv_31_act, _conv_32_w, (1, 1, 1, 1), padding='SAME') + _conv_32_b
_conv_32_act = tf.nn.relu(_conv_32_output)
_conv_33_w = tf.Variable(self._conv_data_33_weights)
_conv_33_b = tf.Variable(self._conv_data_33_bias)
_conv_33_output = tf.nn.conv2d(_conv_32_act, _conv_33_w, (1, 1, 1, 1), padding='SAME') + _conv_33_b
_conv_33_act = tf.nn.relu(_conv_33_output)
self._output_feature = _conv_33_act
self._session = tf.Session(graph=self._graph)
self._session.run(tf.global_variables_initializer())
def _load_data(self):
with np.load(VGG_MODEL_PATH) as npz_file:
self._conv_data_11_weights = npz_file['conv1_1/weights']
self._conv_data_11_bias = npz_file['conv1_1/biases']
self._conv_data_12_weights = npz_file['conv1_2/weights']
self._conv_data_12_bias = npz_file['conv1_2/biases']
self._conv_data_21_weights = npz_file['conv2_1/weights']
self._conv_data_21_bias = npz_file['conv2_1/biases']
self._conv_data_22_weights = npz_file['conv2_2/weights']
self._conv_data_22_bias = npz_file['conv2_2/biases']
self._conv_data_31_weights = npz_file['conv3_1/weights']
self._conv_data_31_bias = npz_file['conv3_1/biases']
self._conv_data_32_weights = npz_file['conv3_2/weights']
self._conv_data_32_bias = npz_file['conv3_2/biases']
self._conv_data_33_weights = npz_file['conv3_3/weights']
self._conv_data_33_bias = npz_file['conv3_3/biases']
print('VggL3 parameters loaded successfully!')
class VggL4Extractor(VggExtractor):
def __init__(self):
super(VggL4Extractor, self).__init__()
def _build_network(self, input_height, input_width):
assert not input_height % self._resolution and not input_width % self._resolution
if self._session:
self._session.close()
self._graph = tf.Graph()
self._feature_height = input_height
self._feature_width = input_width
print('Starting building the network for h={:d} w={:d}'.format(input_height, input_width))
with self._graph.as_default():
_input_shape = (None, input_height, input_width, 3)
self._input_holder = tf.placeholder(tf.float32, shape=_input_shape)
_mean = tf.Variable(VGG_MEAN, trainable=False)
_sub_mean = self._input_holder - _mean
_conv_11_w = tf.Variable(self._conv_data_11_weights)
_conv_11_b = tf.Variable(self._conv_data_11_bias)
_conv_11_output = tf.nn.conv2d(_sub_mean, _conv_11_w, [1, 1, 1, 1], padding='SAME') + _conv_11_b
_conv_11_act = tf.nn.relu(_conv_11_output)
_conv_12_w = tf.Variable(self._conv_data_12_weights)
_conv_12_b = tf.Variable(self._conv_data_12_bias)
_conv_12_output = tf.nn.conv2d(_conv_11_act, _conv_12_w, [1, 1, 1, 1], padding='SAME') + _conv_12_b
_conv_12_act = tf.nn.relu(_conv_12_output)
_max_pool_12_output = tf.nn.max_pool(_conv_12_act, (1, 2, 2, 1), (1, 2, 2, 1), padding='SAME')
_conv_21_w = tf.Variable(self._conv_data_21_weights)
_conv_21_b = tf.Variable(self._conv_data_21_bias)
_conv_21_output = tf.nn.conv2d(_max_pool_12_output, _conv_21_w, (1, 1, 1, 1), padding='SAME') + _conv_21_b
_conv_21_act = tf.nn.relu(_conv_21_output)
_conv_22_w = tf.Variable(self._conv_data_22_weights)
_conv_22_b = tf.Variable(self._conv_data_22_bias)
_conv_22_output = tf.nn.conv2d(_conv_21_act, _conv_22_w, (1, 1, 1, 1), padding='SAME') + _conv_22_b
_conv_22_act = tf.nn.relu(_conv_22_output)
_max_pool_22_output = tf.nn.max_pool(_conv_22_act, (1, 2, 2, 1), (1, 2, 2, 1,), padding='SAME')
_conv_31_w = tf.Variable(self._conv_data_31_weights)
_conv_31_b = tf.Variable(self._conv_data_31_bias)
_conv_31_output = tf.nn.conv2d(_max_pool_22_output, _conv_31_w, (1, 1, 1, 1), padding='SAME') + \
_conv_31_b
_conv_31_act = tf.nn.relu(_conv_31_output)
_conv_32_w = tf.Variable(self._conv_data_32_weights)
_conv_32_b = tf.Variable(self._conv_data_31_bias)
_conv_32_output = tf.nn.conv2d(_conv_31_act, _conv_32_w, (1, 1, 1, 1), padding='SAME') + _conv_32_b
_conv_32_act = tf.nn.relu(_conv_32_output)
_conv_33_w = tf.Variable(self._conv_data_33_weights)
_conv_33_b = tf.Variable(self._conv_data_33_bias)
_conv_33_output = tf.nn.conv2d(_conv_32_act, _conv_33_w, (1, 1, 1, 1), padding='SAME') + _conv_33_b
_conv_33_act = tf.nn.relu(_conv_33_output)
_conv_41_w = tf.Variable(self._conv_data_41_weights)
_conv_41_b = tf.Variable(self._conv_data_41_bias)
_conv_41_output = tf.nn.conv2d(_conv_33_act, _conv_41_w, (1, 1, 1, 1), padding='SAME') + \
_conv_41_b
_conv_41_act = tf.nn.relu(_conv_41_output)
_conv_42_w = tf.Variable(self._conv_data_42_weights)
_conv_42_b = tf.Variable(self._conv_data_42_bias)
_conv_42_output = tf.nn.conv2d(_conv_41_act, _conv_42_w, (1,1,1,1), padding='SAME') + _conv_42_b
_conv_42_act = tf.nn.relu(_conv_42_output)
_conv_43_w = tf.Variable(self._conv_data_43_weights)
_conv_43_b = tf.Variable(self._conv_data_43_bias)
_conv_43_output = tf.nn.conv2d(_conv_42_act, _conv_43_w, (1,1,1,1), padding='SAME') + _conv_43_b
_conv_43_act = tf.nn.relu(_conv_43_output)
self._output_feature = _conv_43_act
self._session = tf.Session(graph=self._graph)
self._session.run(tf.global_variables_initializer())
def _load_data(self):
with np.load(VGG_MODEL_PATH) as npz_file:
self._conv_data_11_weights = npz_file['conv1_1/weights']
self._conv_data_11_bias = npz_file['conv1_1/biases']
self._conv_data_12_weights = npz_file['conv1_2/weights']
self._conv_data_12_bias = npz_file['conv1_2/biases']
self._conv_data_21_weights = npz_file['conv2_1/weights']
self._conv_data_21_bias = npz_file['conv2_1/biases']
self._conv_data_22_weights = npz_file['conv2_2/weights']
self._conv_data_22_bias = npz_file['conv2_2/biases']
self._conv_data_31_weights = npz_file['conv3_1/weights']
self._conv_data_31_bias = npz_file['conv3_1/biases']
self._conv_data_32_weights = npz_file['conv3_2/weights']
self._conv_data_32_bias = npz_file['conv3_2/biases']
self._conv_data_33_weights = npz_file['conv3_3/weights']
self._conv_data_33_bias = npz_file['conv3_3/biases']
self._conv_data_41_weights = npz_file['conv4_1/weights']
self._conv_data_41_bias = npz_file['conv4_1/biases']
self._conv_data_42_weights = npz_file['conv4_2/weights']
self._conv_data_42_bias = npz_file['conv4_2/biases']
self._conv_data_43_weights = npz_file['conv4_3/weights']
self._conv_data_43_bias = npz_file['conv4_3/biases']
print('VggL4 parameters loaded successfully!')
class VggL5Extractor(VggExtractor):
def __init__(self):
super(VggL5Extractor, self).__init__()
def _build_network(self, input_height, input_width):
assert not input_height % self._resolution and not input_width % self._resolution
if self._session:
self._session.close()
self._graph = tf.Graph()
self._feature_height = input_height
self._feature_width = input_width
print('Starting building the network for h={:d} w={:d}'.format(input_height, input_width))
with self._graph.as_default():
_input_shape = (None, input_height, input_width, 3)
self._input_holder = tf.placeholder(tf.float32, shape=_input_shape)
_mean = tf.Variable(VGG_MEAN, trainable=False)
_sub_mean = self._input_holder - _mean
_conv_11_w = tf.Variable(self._conv_data_11_weights)
_conv_11_b = tf.Variable(self._conv_data_11_bias)
_conv_11_output = tf.nn.conv2d(_sub_mean, _conv_11_w, [1, 1, 1, 1], padding='SAME') + _conv_11_b
_conv_11_act = tf.nn.relu(_conv_11_output)
_conv_12_w = tf.Variable(self._conv_data_12_weights)
_conv_12_b = tf.Variable(self._conv_data_12_bias)
_conv_12_output = tf.nn.conv2d(_conv_11_act, _conv_12_w, [1, 1, 1, 1], padding='SAME') + _conv_12_b
_conv_12_act = tf.nn.relu(_conv_12_output)
_max_pool_12_output = tf.nn.max_pool(_conv_12_act, (1, 2, 2, 1), (1, 2, 2, 1), padding='SAME')
_conv_21_w = tf.Variable(self._conv_data_21_weights)
_conv_21_b = tf.Variable(self._conv_data_21_bias)
_conv_21_output = tf.nn.conv2d(_max_pool_12_output, _conv_21_w, (1, 1, 1, 1), padding='SAME') + _conv_21_b
_conv_21_act = tf.nn.relu(_conv_21_output)
_conv_22_w = tf.Variable(self._conv_data_22_weights)
_conv_22_b = tf.Variable(self._conv_data_22_bias)
_conv_22_output = tf.nn.conv2d(_conv_21_act, _conv_22_w, (1, 1, 1, 1), padding='SAME') + _conv_22_b
_conv_22_act = tf.nn.relu(_conv_22_output)
_max_pool_22_output = tf.nn.max_pool(_conv_22_act, (1, 2, 2, 1), (1, 2, 2, 1,), padding='SAME')
_conv_31_w = tf.Variable(self._conv_data_31_weights)
_conv_31_b = tf.Variable(self._conv_data_31_bias)
_conv_31_output = tf.nn.conv2d(_max_pool_22_output, _conv_31_w, (1, 1, 1, 1), padding='SAME') + \
_conv_31_b
_conv_31_act = tf.nn.relu(_conv_31_output)
_conv_32_w = tf.Variable(self._conv_data_32_weights)
_conv_32_b = tf.Variable(self._conv_data_31_bias)
_conv_32_output = tf.nn.conv2d(_conv_31_act, _conv_32_w, (1, 1, 1, 1), padding='SAME') + _conv_32_b
_conv_32_act = tf.nn.relu(_conv_32_output)
_conv_33_w = tf.Variable(self._conv_data_33_weights)
_conv_33_b = tf.Variable(self._conv_data_33_bias)
_conv_33_output = tf.nn.conv2d(_conv_32_act, _conv_33_w, (1, 1, 1, 1), padding='SAME') + _conv_33_b
_conv_33_act = tf.nn.relu(_conv_33_output)
_conv_41_w = tf.Variable(self._conv_data_41_weights)
_conv_41_b = tf.Variable(self._conv_data_41_bias)
_conv_41_output = tf.nn.conv2d(_conv_33_act, _conv_41_w, (1, 1, 1, 1), padding='SAME') + \
_conv_41_b
_conv_41_act = tf.nn.relu(_conv_41_output)
_conv_42_w = tf.Variable(self._conv_data_42_weights)
_conv_42_b = tf.Variable(self._conv_data_42_bias)
_conv_42_output = tf.nn.conv2d(_conv_41_act, _conv_42_w, (1, 1, 1, 1), padding='SAME') + _conv_42_b
_conv_42_act = tf.nn.relu(_conv_42_output)
_conv_43_w = tf.Variable(self._conv_data_43_weights)
_conv_43_b = tf.Variable(self._conv_data_43_bias)
_conv_43_output = tf.nn.conv2d(_conv_42_act, _conv_43_w, (1, 1, 1, 1), padding='SAME') + _conv_43_b
_conv_43_act = tf.nn.relu(_conv_43_output)
_conv_51_w = tf.Variable(self._conv_data_51_weights)
_conv_51_b = tf.Variable(self._conv_data_51_bias)
_conv_51_output = tf.nn.conv2d(_conv_43_act, _conv_51_w, (1,1,1,1), padding='SAME') + _conv_51_b
_conv_51_act = tf.nn.relu(_conv_51_output)
_conv_52_w = tf.Variable(self._conv_data_52_weights)
_conv_52_b = tf.Variable(self._conv_data_52_bias)
_conv_52_output = tf.nn.conv2d(_conv_51_act, _conv_52_w, (1,1,1,1), padding='SAME') + _conv_52_b
_conv_52_act = tf.nn.relu(_conv_52_output)
_conv_53_w = tf.Variable(self._conv_data_53_weights)
_conv_53_b = tf.Variable(self._conv_data_53_bias)
_conv_53_output = tf.nn.conv2d(_conv_52_act, _conv_53_w, (1,1,1,1), padding='SAME') + _conv_53_b
_conv_53_act = tf.nn.relu(_conv_53_output)
self._output_feature = _conv_53_act
self._session = tf.Session(graph=self._graph)
self._session.run(tf.global_variables_initializer())
def _load_data(self):
with np.load(VGG_MODEL_PATH) as npz_file:
self._conv_data_11_weights = npz_file['conv1_1/weights']
self._conv_data_11_bias = npz_file['conv1_1/biases']
self._conv_data_12_weights = npz_file['conv1_2/weights']
self._conv_data_12_bias = npz_file['conv1_2/biases']
self._conv_data_21_weights = npz_file['conv2_1/weights']
self._conv_data_21_bias = npz_file['conv2_1/biases']
self._conv_data_22_weights = npz_file['conv2_2/weights']
self._conv_data_22_bias = npz_file['conv2_2/biases']
self._conv_data_31_weights = npz_file['conv3_1/weights']
self._conv_data_31_bias = npz_file['conv3_1/biases']
self._conv_data_32_weights = npz_file['conv3_2/weights']
self._conv_data_32_bias = npz_file['conv3_2/biases']
self._conv_data_33_weights = npz_file['conv3_3/weights']
self._conv_data_33_bias = npz_file['conv3_3/biases']
self._conv_data_41_weights = npz_file['conv4_1/weights']
self._conv_data_41_bias = npz_file['conv4_1/biases']
self._conv_data_42_weights = npz_file['conv4_2/weights']
self._conv_data_42_bias = npz_file['conv4_2/biases']
self._conv_data_43_weights = npz_file['conv4_3/weights']
self._conv_data_43_bias = npz_file['conv4_3/biases']
self._conv_data_51_weights = npz_file['conv5_1/weights']
self._conv_data_51_bias = npz_file['conv5_1/biases']
self._conv_data_52_weights = npz_file['conv5_2/weights']
self._conv_data_52_bias = npz_file['conv5_2/biases']
self._conv_data_53_weights = npz_file['conv5_3/weights']
self._conv_data_53_bias = npz_file['conv5_3/biases']
print('VggL5 parameters loaded successfully!')
def _test_load_data():
ext = VggL1Extractor()
# test_image = np.random.randint(0,255, (210, 30, 3), dtype=np.uint8)
# ext.extract_feature(test_image)
if __name__ == '__main__':
_test_load_data()