-
Notifications
You must be signed in to change notification settings - Fork 0
/
VGG16.py
340 lines (318 loc) · 8.74 KB
/
VGG16.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
# -*- coding: utf-8 -*-
# 上采样之后得到的得分,然后通过argmax来得到最后的分类结果
import tensorflow as tf
import numpy as np
import gc
IMAGE_CHANNAL = 3
CONV1_1_SIZE = 3
CONV1_1_DEEP = 64
CONV1_2_SIZE = 3
CONV1_2_DEEP = 64
CONV2_1_SIZE = 3
CONV2_1_DEEP = 128
CONV2_2_SIZE = 3
CONV2_2_DEEP = 128
CONV3_1_SIZE = 3
CONV3_1_DEEP = 256
CONV3_2_SIZE = 3
CONV3_2_DEEP = 256
CONV3_3_SIZE = 1
CONV3_3_DEEP = 256
CONV4_1_SIZE = 3
CONV4_1_DEEP = 512
CONV4_2_SIZE = 3
CONV4_2_DEEP = 512
CONV4_3_SIZE = 1
CONV4_3_DEEP = 512
CONV5_1_SIZE = 3
CONV5_1_DEEP = 512
CONV5_2_SIZE = 3
CONV5_2_DEEP = 512
CONV5_3_SIZE = 1
CONV5_3_DEEP = 512
FC1_NODE = 4096
FC2_NODE = 4096
FC3_NODE = 2
FLAGS = tf.flags.FLAGS
tf.flags.DEFINE_integer("batch_size", "50", "batch size for training")
tf.flags.DEFINE_string("logs_dir", "logs/", "path to logs directory")
tf.flags.DEFINE_string("data_dir", "'/home/give/Documents/dataset/ADEChallengeData2016'", "path to dataset")
tf.flags.DEFINE_float("learning_rate", "1e-1", "Learning rate for Adam Optimizer")
tf.flags.DEFINE_string("model_dir", "Model_zoo/", "Path to vgg model mat")
tf.flags.DEFINE_bool('debug', "False", "Debug mode: True/ False")
tf.flags.DEFINE_string('mode', "train", "Mode train/ test/ visualize")
MAX_ITERATION = int(1e5 + 1)
DECAY_LEARNING_RATE = 0.1
def do_conv(name, weight_shape, bias_shape, input_tensor):
with tf.variable_scope(name):
weight = tf.get_variable(
'weight',
shape=weight_shape,
initializer=tf.contrib.layers.xavier_initializer()
)
bias = tf.get_variable(
'bias',
shape=bias_shape,
initializer=tf.constant_initializer(0.0)
)
conv = tf.nn.conv2d(
input_tensor,
weight,
strides=[1, 1, 1, 1],
padding='SAME',
)
layer = tf.nn.bias_add(conv, bias)
return tf.nn.relu(layer)
def do_full_connection(name, weight_shape, bias_shape, input_tensor):
with tf.variable_scope(name):
weight = tf.get_variable(
'weight',
shape=weight_shape,
initializer=tf.contrib.layers.xavier_initializer()
)
bias = tf.get_variable(
'bias',
shape=bias_shape,
initializer=tf.constant_initializer(0.0)
)
matmul = tf.nn.bias_add(tf.matmul(input_tensor, weight), bias)
return tf.nn.relu(matmul)
def inference(image, keep_prob):
with tf.variable_scope('inference'):
layer11 = do_conv(
'conv1_1',
weight_shape=[
CONV1_1_SIZE,
CONV1_1_SIZE,
IMAGE_CHANNAL,
CONV1_1_DEEP
],
bias_shape=[
CONV1_1_DEEP
],
input_tensor=image
)
print layer11.shape
layer12 = do_conv(
'conv1_2',
weight_shape=[
CONV1_2_SIZE,
CONV1_2_SIZE,
CONV1_1_DEEP,
CONV1_2_DEEP
],
bias_shape=[
CONV1_2_DEEP
],
input_tensor=layer11
)
print layer12.shape
with tf.variable_scope('pooling1'):
pooling1 = tf.nn.max_pool(
layer12,
strides=[1, 2, 2, 1],
padding='SAME',
ksize=[1, 2, 2, 1]
)
print pooling1.shape
layer21 = do_conv(
'conv2_1',
weight_shape=[
CONV2_1_SIZE,
CONV2_1_SIZE,
CONV1_2_DEEP,
CONV2_1_DEEP
],
bias_shape=[
CONV2_1_DEEP
],
input_tensor=pooling1
)
layer22 = do_conv(
'conv2_2',
weight_shape=[
CONV2_2_SIZE,
CONV2_2_SIZE,
CONV2_1_DEEP,
CONV2_2_DEEP
],
bias_shape=[
CONV2_2_DEEP
],
input_tensor=layer21
)
with tf.variable_scope('pooling2'):
pooling2 = tf.nn.max_pool(
layer22,
strides=[1, 2, 2, 1],
padding='SAME',
ksize=[1, 2, 2, 1]
)
print pooling2.shape
layer31 = do_conv(
'conv3_1',
weight_shape=[
CONV3_1_SIZE,
CONV3_1_SIZE,
CONV2_2_DEEP,
CONV3_1_DEEP
],
bias_shape=[
CONV3_1_DEEP
],
input_tensor=pooling2
)
layer32 = do_conv(
'conv3_2',
weight_shape=[
CONV3_2_SIZE,
CONV3_2_SIZE,
CONV3_1_DEEP,
CONV3_2_DEEP
],
bias_shape=[
CONV3_2_DEEP
],
input_tensor=layer31
)
layer33 = do_conv(
'conv3_3',
weight_shape=[
CONV3_3_SIZE,
CONV3_3_SIZE,
CONV3_2_DEEP,
CONV3_3_DEEP
],
bias_shape=[
CONV3_3_DEEP
],
input_tensor=layer32
)
with tf.variable_scope('pooling3'):
pooling3 = tf.nn.max_pool(
layer33,
strides=[1, 2, 2, 1],
padding='SAME',
ksize=[1, 2, 2, 1]
)
print pooling3.shape
layer41 = do_conv(
'conv4_1',
weight_shape=[
CONV4_1_SIZE,
CONV4_1_SIZE,
CONV3_3_DEEP,
CONV4_1_DEEP
],
bias_shape=[
CONV4_1_DEEP
],
input_tensor=pooling3
)
layer42 = do_conv(
'conv4_2',
weight_shape=[
CONV4_2_SIZE,
CONV4_2_SIZE,
CONV4_1_DEEP,
CONV4_2_DEEP
],
bias_shape=[
CONV4_2_DEEP
],
input_tensor=layer41
)
layer43 = do_conv(
'conv4_3',
weight_shape=[
CONV4_3_SIZE,
CONV4_3_SIZE,
CONV4_2_DEEP,
CONV4_3_DEEP
],
bias_shape=[
CONV4_3_DEEP
],
input_tensor=layer42
)
with tf.variable_scope('pooling4'):
pooling4 = tf.nn.max_pool(
layer43,
strides=[1, 2, 2, 1],
padding='SAME',
ksize=[1, 2, 2, 1]
)
print pooling4.shape
layer51 = do_conv(
'conv5_1',
weight_shape=[
CONV5_1_SIZE,
CONV5_1_SIZE,
CONV4_3_DEEP,
CONV5_1_DEEP
],
bias_shape=[
CONV5_1_DEEP
],
input_tensor=pooling4
)
layer52 = do_conv(
'conv5_2',
weight_shape=[
CONV5_2_SIZE,
CONV5_2_SIZE,
CONV5_1_DEEP,
CONV5_2_DEEP
],
bias_shape=[
CONV5_2_DEEP
],
input_tensor=layer51
)
layer53 = do_conv(
'conv5_3',
weight_shape=[
CONV5_3_SIZE,
CONV5_3_SIZE,
CONV5_2_DEEP,
CONV5_3_DEEP
],
bias_shape=[
CONV5_3_DEEP
],
input_tensor=layer52
)
with tf.variable_scope('pooling5'):
pooling5 = tf.nn.max_pool(
layer53,
strides=[1, 2, 2, 1],
padding='SAME',
ksize=[1, 2, 2, 1]
)
print pooling5.shape
shape = pooling5.get_shape().as_list()
nodes = shape[1] * shape[2] * shape[3]
reshaped = tf.reshape(
pooling5,
[
-1,
nodes
]
)
fc1_result = do_full_connection('full_connection1', [nodes, FC1_NODE], [FC1_NODE], reshaped)
fc2_result = do_full_connection('full_connection2', [FC1_NODE, FC2_NODE], [FC2_NODE], fc1_result)
fc3_result = do_full_connection('full_connection3', [FC2_NODE, FC3_NODE], [FC3_NODE], fc2_result)
return fc2_result, fc3_result
if __name__ == '__main__':
test_tensor = tf.placeholder(
tf.float32,
[
50,
224,
224,
3
],
name='input'
)
fc2_result, result = inference(test_tensor, None)
print result