-
Notifications
You must be signed in to change notification settings - Fork 2
/
VGG.py
348 lines (271 loc) · 14.7 KB
/
VGG.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
import tensorflow as tf
import numpy as np
IMG_W = 224
IMG_H = 224
CHANNELS = 3
# BGR format
MEAN = [103.939, 116.779, 123.68]
VGG16_WEIGHTS = "data/VGG/vgg16.npy"
VGG19_WEIGHTS = "data/VGG/vgg19.npy"
def generate_VGG16(weights_file=VGG16_WEIGHTS,
scope="VGG16_factory",
remove_top=False,
input_shape=(1, IMG_W, IMG_H, CHANNELS),
input_tensor=None,
apply_preprocess=True):
weights = np.load(weights_file, encoding='latin1').item()
model = {}
# create a tf.Variable() or use a specific tensor
# TODO: create a tf.Variable() or a tf.placeholder() ?
if input_tensor is not None :
model['input'] = input_tensor
else:
with tf.name_scope('input'):
model['input']= tf.Variable(np.zeros(input_shape), dtype = 'float32')
# pre-processing step (RGB->BGR + mean subtraction)
if apply_preprocess:
with tf.name_scope("preprocessing"):
red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=model['input'])
bgr = tf.concat(axis=3, values=[blue - MEAN[0], green - MEAN[1], red - MEAN[2], ])
model['preprocess'] = bgr
else:
model['preprocess'] = model['input']
if isinstance(scope, str):
# the scope is a string, we can create a new scope, named by 'scope' (str).
# this scope will be returned for reuse (variable sharing)
with tf.variable_scope(scope) as new_scope:
model['conv1_1'] = _conv2d_relu(model['preprocess'], 'conv1_1', weights)
model['conv1_2'] = _conv2d_relu(model['conv1_1'], 'conv1_2', weights)
model['pool_1'] = _pool(model['conv1_2'], 'pool_1')
model['conv2_1'] = _conv2d_relu(model['pool_1'], 'conv2_1', weights)
model['conv2_2'] = _conv2d_relu(model['conv2_1'], 'conv2_2', weights)
model['pool_2'] = _pool(model['conv2_2'], 'pool_2')
model['conv3_1'] = _conv2d_relu(model['pool_2'], 'conv3_1', weights)
model['conv3_2'] = _conv2d_relu(model['conv3_1'], 'conv3_2', weights)
model['conv3_3'] = _conv2d_relu(model['conv3_2'], 'conv3_3', weights)
model['pool_3'] = _pool(model['conv3_3'], 'pool_3')
model['conv4_1'] = _conv2d_relu(model['pool_3'], 'conv4_1', weights)
model['conv4_2'] = _conv2d_relu(model['conv4_1'], 'conv4_2', weights)
model['conv4_3'] = _conv2d_relu(model['conv4_2'], 'conv4_3', weights)
model['pool_4'] = _pool(model['conv4_3'], 'pool_4')
model['conv5_1'] = _conv2d_relu(model['pool_4'], 'conv5_1', weights)
model['conv5_2'] = _conv2d_relu(model['conv5_1'], 'conv5_2', weights)
model['conv5_3'] = _conv2d_relu(model['conv5_2'], 'conv5_3', weights)
model['pool_5'] = _pool(model['conv5_3'], 'pool_5')
if remove_top:
return model, new_scope
model['flatten'] = _flatten(model['pool_5'])
model['fc6'] = _fc_relu(model['flatten'], 'fc6', weights)
model['fc7'] = _fc_relu(model['fc6'], 'fc7', weights)
model['fc8'] = _fc_linear(model['fc7'], 'fc8', weights)
model['prob'] = _prob(model['fc8'], 'prob')
return model, new_scope
elif isinstance(scope, tf.VariableScope):
# the scope is a tf.VariableScope
with tf.variable_scope(scope, reuse=True):
model['conv1_1'] = _conv2d_relu(model['preprocess'], 'conv1_1', weights, reuse_scope=True)
model['conv1_2'] = _conv2d_relu(model['conv1_1'], 'conv1_2', weights, reuse_scope=True)
model['pool_1'] = _pool(model['conv1_2'], 'pool_1')
model['conv2_1'] = _conv2d_relu(model['pool_1'], 'conv2_1', weights, reuse_scope=True)
model['conv2_2'] = _conv2d_relu(model['conv2_1'], 'conv2_2', weights, reuse_scope=True)
model['pool_2'] = _pool(model['conv2_2'], 'pool_2')
model['conv3_1'] = _conv2d_relu(model['pool_2'], 'conv3_1', weights, reuse_scope=True)
model['conv3_2'] = _conv2d_relu(model['conv3_1'], 'conv3_2', weights, reuse_scope=True)
model['conv3_3'] = _conv2d_relu(model['conv3_2'], 'conv3_3', weights, reuse_scope=True)
model['pool_3'] = _pool(model['conv3_3'], 'pool_3')
model['conv4_1'] = _conv2d_relu(model['pool_3'], 'conv4_1', weights, reuse_scope=True)
model['conv4_2'] = _conv2d_relu(model['conv4_1'], 'conv4_2', weights, reuse_scope=True)
model['conv4_3'] = _conv2d_relu(model['conv4_2'], 'conv4_3', weights, reuse_scope=True)
model['pool_4'] = _pool(model['conv4_3'], 'pool_4')
model['conv5_1'] = _conv2d_relu(model['pool_4'], 'conv5_1', weights, reuse_scope=True)
model['conv5_2'] = _conv2d_relu(model['conv5_1'], 'conv5_2', weights, reuse_scope=True)
model['conv5_3'] = _conv2d_relu(model['conv5_2'], 'conv5_3', weights, reuse_scope=True)
model['pool_5'] = _pool(model['conv5_3'], 'pool_5')
if remove_top:
return model, scope
# TODO : issue when creating a first scope with 'remove_top=True' and the second has 'remove_top=False'
model['flatten'] = _flatten(model['pool_5'])
model['fc6'] = _fc_relu(model['flatten'], 'fc6', weights, reuse_scope=True)
model['fc7'] = _fc_relu(model['fc6'], 'fc7', weights, reuse_scope=True)
model['fc8'] = _fc_linear(model['fc7'], 'fc8', weights, reuse_scope=True)
model['prob'] = _prob(model['fc8'], 'prob')
return model, scope
else:
print("Invalid scope")
exit(-1)
def generate_VGG19(weights_file=VGG19_WEIGHTS,
scope="VGG19_factory",
remove_top=False,
input_shape=(1, IMG_W, IMG_H, CHANNELS),
input_tensor=None,
apply_preprocess=True):
weights = np.load(weights_file, encoding='latin1').item()
model = {}
# create a tf.Variable() or use a specific tensor
if input_tensor is not None:
model['input'] = input_tensor
else:
with tf.name_scope('input'):
model['input'] = tf.Variable(np.zeros(input_shape), dtype='float32')
# pre-processing step (RGB->BGR + mean subtraction)
if apply_preprocess:
with tf.name_scope("preprocessing"):
red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=model['input'])
bgr = tf.concat(axis=3, values=[blue - MEAN[0], green - MEAN[1], red - MEAN[2], ])
model['preprocess'] = bgr
else:
model['preprocess'] = model['input']
if isinstance(scope, str):
with tf.variable_scope(scope) as new_scope:
model['conv1_1'] = _conv2d_relu(model['preprocess'], 'conv1_1', weights)
model['conv1_2'] = _conv2d_relu(model['conv1_1'], 'conv1_2', weights)
model['pool_1'] = _pool(model['conv1_2'], 'pool_1')
model['conv2_1'] = _conv2d_relu(model['pool_1'], 'conv2_1', weights)
model['conv2_2'] = _conv2d_relu(model['conv2_1'], 'conv2_2', weights)
model['pool_2'] = _pool(model['conv2_2'], 'pool_2')
model['conv3_1'] = _conv2d_relu(model['pool_2'], 'conv3_1', weights)
model['conv3_2'] = _conv2d_relu(model['conv3_1'], 'conv3_2', weights)
model['conv3_3'] = _conv2d_relu(model['conv3_2'], 'conv3_3', weights)
model['conv3_4'] = _conv2d_relu(model['conv3_3'], 'conv3_4', weights)
model['pool_3'] = _pool(model['conv3_4'], 'pool_3')
model['conv4_1'] = _conv2d_relu(model['pool_3'], 'conv4_1', weights)
model['conv4_2'] = _conv2d_relu(model['conv4_1'], 'conv4_2', weights)
model['conv4_3'] = _conv2d_relu(model['conv4_2'], 'conv4_3', weights)
model['conv4_4'] = _conv2d_relu(model['conv4_3'], 'conv4_4', weights)
model['pool_4'] = _pool(model['conv4_4'], 'pool_4')
model['conv5_1'] = _conv2d_relu(model['pool_4'], 'conv5_1', weights)
model['conv5_2'] = _conv2d_relu(model['conv5_1'], 'conv5_2', weights)
model['conv5_3'] = _conv2d_relu(model['conv5_2'], 'conv5_3', weights)
model['conv5_4'] = _conv2d_relu(model['conv5_3'], 'conv5_4', weights)
model['pool_5'] = _pool(model['conv5_3'], 'pool_5')
if remove_top:
return model, new_scope
model['flatten'] = _flatten(model['pool_5'])
model['fc6'] = _fc_relu(model['flatten'], 'fc6', weights)
model['fc7'] = _fc_relu(model['fc6'], 'fc7', weights)
model['fc8'] = _fc_linear(model['fc7'], 'fc8', weights)
model['prob'] = _prob(model['fc8'], 'prob')
return model, new_scope
elif isinstance(scope, tf.VariableScope):
with tf.variable_scope(scope, reuse=True):
model['conv1_1'] = _conv2d_relu(model['preprocess'], 'conv1_1', weights)
model['conv1_2'] = _conv2d_relu(model['conv1_1'], 'conv1_2', weights)
model['pool_1'] = _pool(model['conv1_2'], 'pool_1')
model['conv2_1'] = _conv2d_relu(model['pool_1'], 'conv2_1', weights)
model['conv2_2'] = _conv2d_relu(model['conv2_1'], 'conv2_2', weights)
model['pool_2'] = _pool(model['conv2_2'], 'pool_2')
model['conv3_1'] = _conv2d_relu(model['pool_2'], 'conv3_1', weights)
model['conv3_2'] = _conv2d_relu(model['conv3_1'], 'conv3_2', weights)
model['conv3_3'] = _conv2d_relu(model['conv3_2'], 'conv3_3', weights)
model['conv3_4'] = _conv2d_relu(model['conv3_3'], 'conv3_4', weights)
model['pool_3'] = _pool(model['conv3_4'], 'pool_3')
model['conv4_1'] = _conv2d_relu(model['pool_3'], 'conv4_1', weights)
model['conv4_2'] = _conv2d_relu(model['conv4_1'], 'conv4_2', weights)
model['conv4_3'] = _conv2d_relu(model['conv4_2'], 'conv4_3', weights)
model['conv4_4'] = _conv2d_relu(model['conv4_3'], 'conv4_4', weights)
model['pool_4'] = _pool(model['conv4_4'], 'pool_4')
model['conv5_1'] = _conv2d_relu(model['pool_4'], 'conv5_1', weights)
model['conv5_2'] = _conv2d_relu(model['conv5_1'], 'conv5_2', weights)
model['conv5_3'] = _conv2d_relu(model['conv5_2'], 'conv5_3', weights)
model['conv5_4'] = _conv2d_relu(model['conv5_3'], 'conv5_4', weights)
model['pool_5'] = _pool(model['conv5_3'], 'pool_5')
if remove_top:
return model, scope
model['flatten'] = _flatten(model['pool_5'])
model['fc6'] = _fc_relu(model['flatten'], 'fc6', weights)
model['fc7'] = _fc_relu(model['fc6'], 'fc7', weights)
model['fc8'] = _fc_linear(model['fc7'], 'fc8', weights)
model['prob'] = _prob(model['fc8'], 'prob')
return model, scope
else:
print("Invalid scope")
exit(-1)
def _get_weights(layer_name, weights):
"""
Load weights with name 'layer_name'
weights[layer_name][0] : W (kernel conv or matrix)
weights[layer_name][1] : b (bias vector)
"""
W = weights[layer_name][0]
b = weights[layer_name][1]
return W, b
def _conv2d_relu(prev_layer, layer_name, weights, reuse_scope=False):
"""
Return the Conv2D + RELU layer using the weights, biases from the VGG
model at 'layer'.
"""
with tf.name_scope(layer_name):
if reuse_scope is False:
w_np, b_np = _get_weights(layer_name, weights)
with tf.variable_scope(layer_name):
w = tf.get_variable('W', shape=tuple(w_np.shape),
dtype=w_np.dtype, trainable=False,
initializer=tf.constant_initializer(w_np))
b = tf.get_variable('b', shape=tuple(b_np.shape),
dtype=b_np.dtype, trainable=False,
initializer=tf.constant_initializer(b_np))
else:
with tf.variable_scope(layer_name, reuse=True):
w = tf.get_variable('W')
b = tf.get_variable('b')
conv = tf.nn.conv2d(prev_layer, filter=w, strides=[1, 1, 1, 1], padding='SAME')
out = tf.nn.bias_add(conv, b)
acti = tf.nn.relu(out, name=layer_name)
return acti
def _pool(prev_layer, layer_name):
"""
Return the MaxPooling layer.
"""
with tf.name_scope(layer_name):
return tf.nn.max_pool(prev_layer, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def _flatten(prev_layer):
"""
Reshape layer, flatten operation.
"""
with tf.name_scope('flatten'):
shape = int(np.prod(prev_layer.get_shape()[1:]))
return tf.reshape(prev_layer, [-1, shape])
def _fc_relu(prev_layer, layer_name, weights, reuse_scope=False):
"""
Return the Dense/Fully Connected + ReLU layer using the weights, biases from the VGG model
"""
with tf.name_scope(layer_name):
if reuse_scope is False:
w_np, b_np = _get_weights(layer_name, weights)
with tf.variable_scope(layer_name):
w = tf.get_variable('W', shape=tuple(w_np.shape),
dtype=w_np.dtype, trainable=False,
initializer=tf.constant_initializer(w_np))
b = tf.get_variable('b', shape=tuple(b_np.shape),
dtype=b_np.dtype, trainable=False,
initializer=tf.constant_initializer(b_np))
else:
with tf.variable_scope(layer_name, reuse=True):
w = tf.get_variable('W')
b = tf.get_variable('b')
return tf.nn.relu(tf.nn.bias_add(tf.matmul(prev_layer, w), b))
def _fc_linear(prev_layer, layer_name, weights, reuse_scope=False):
"""
Return the Dense/Fully Connected layer using the weights, biases from the VGG model
"""
with tf.name_scope(layer_name):
if reuse_scope is False:
w_np, b_np = _get_weights(layer_name, weights)
with tf.variable_scope(layer_name):
w = tf.get_variable('W', shape=tuple(w_np.shape),
dtype=w_np.dtype, trainable=False,
initializer=tf.constant_initializer(w_np))
b = tf.get_variable('b', shape=tuple(b_np.shape),
dtype=b_np.dtype, trainable=False,
initializer=tf.constant_initializer(b_np))
else:
with tf.variable_scope(layer_name, reuse=True):
w = tf.get_variable('W')
b = tf.get_variable('b')
return tf.nn.bias_add(tf.matmul(prev_layer, w), b)
def _prob(prev_layer, layer_name):
"""
Returns the softmax.
"""
with tf.name_scope(layer_name):
return tf.nn.softmax(prev_layer)