44
55import tensorflow as tf
66import tensorlayer as tl
7+ from tensorlayer .layers import *
78import os
89import sys
910import numpy as np
@@ -60,118 +61,46 @@ def conv_layers(net_in):
6061 mean = tf .constant ([123.68 , 116.779 , 103.939 ], dtype = tf .float32 , shape = [1 , 1 , 1 , 3 ], name = 'img_mean' )
6162 net_in .outputs = net_in .outputs - mean
6263 """ conv1 """
63- network = tl .layers .Conv2dLayer (net_in ,
64- act = tf .nn .relu ,
65- shape = [3 , 3 , 3 , 64 ], # 64 features for each 3x3 patch
66- strides = [1 , 1 , 1 , 1 ],
67- padding = 'SAME' ,
68- name = 'conv1_1' )
69- network = tl .layers .Conv2dLayer (network ,
70- act = tf .nn .relu ,
71- shape = [3 , 3 , 64 , 64 ], # 64 features for each 3x3 patch
72- strides = [1 , 1 , 1 , 1 ],
73- padding = 'SAME' ,
74- name = 'conv1_2' )
75- network = tl .layers .PoolLayer (network ,
76- ksize = [1 , 2 , 2 , 1 ],
77- strides = [1 , 2 , 2 , 1 ],
78- padding = 'SAME' ,
79- pool = tf .nn .max_pool ,
80- name = 'pool1' )
64+ network = Conv2dLayer (net_in , act = tf .nn .relu , shape = [3 , 3 , 3 , 64 ], # 64 features for each 3x3 patch
65+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv1_1' )
66+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 64 , 64 ], # 64 features for each 3x3 patch
67+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv1_2' )
68+ network = PoolLayer (network , ksize = [1 , 2 , 2 , 1 ], strides = [1 , 2 , 2 , 1 ],
69+ padding = 'SAME' , pool = tf .nn .max_pool , name = 'pool1' )
8170 """ conv2 """
82- network = tl .layers .Conv2dLayer (network ,
83- act = tf .nn .relu ,
84- shape = [3 , 3 , 64 , 128 ], # 128 features for each 3x3 patch
85- strides = [1 , 1 , 1 , 1 ],
86- padding = 'SAME' ,
87- name = 'conv2_1' )
88- network = tl .layers .Conv2dLayer (network ,
89- act = tf .nn .relu ,
90- shape = [3 , 3 , 128 , 128 ], # 128 features for each 3x3 patch
91- strides = [1 , 1 , 1 , 1 ],
92- padding = 'SAME' ,
93- name = 'conv2_2' )
94- network = tl .layers .PoolLayer (network ,
95- ksize = [1 , 2 , 2 , 1 ],
96- strides = [1 , 2 , 2 , 1 ],
97- padding = 'SAME' ,
98- pool = tf .nn .max_pool ,
99- name = 'pool2' )
71+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 64 , 128 ], # 128 features for each 3x3 patch
72+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv2_1' )
73+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 128 , 128 ], # 128 features for each 3x3 patch
74+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv2_2' )
75+ network = PoolLayer (network , ksize = [1 , 2 , 2 , 1 ], strides = [1 , 2 , 2 , 1 ],
76+ padding = 'SAME' , pool = tf .nn .max_pool , name = 'pool2' )
10077 """ conv3 """
101- network = tl .layers .Conv2dLayer (network ,
102- act = tf .nn .relu ,
103- shape = [3 , 3 , 128 , 256 ], # 256 features for each 3x3 patch
104- strides = [1 , 1 , 1 , 1 ],
105- padding = 'SAME' ,
106- name = 'conv3_1' )
107- network = tl .layers .Conv2dLayer (network ,
108- act = tf .nn .relu ,
109- shape = [3 , 3 , 256 , 256 ], # 256 features for each 3x3 patch
110- strides = [1 , 1 , 1 , 1 ],
111- padding = 'SAME' ,
112- name = 'conv3_2' )
113- network = tl .layers .Conv2dLayer (network ,
114- act = tf .nn .relu ,
115- shape = [3 , 3 , 256 , 256 ], # 256 features for each 3x3 patch
116- strides = [1 , 1 , 1 , 1 ],
117- padding = 'SAME' ,
118- name = 'conv3_3' )
119- network = tl .layers .PoolLayer (network ,
120- ksize = [1 , 2 , 2 , 1 ],
121- strides = [1 , 2 , 2 , 1 ],
122- padding = 'SAME' ,
123- pool = tf .nn .max_pool ,
124- name = 'pool3' )
78+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 128 , 256 ], # 256 features for each 3x3 patch
79+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv3_1' )
80+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 256 , 256 ], # 256 features for each 3x3 patch
81+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv3_2' )
82+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 256 , 256 ], # 256 features for each 3x3 patch
83+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv3_3' )
84+ network = PoolLayer (network , ksize = [1 , 2 , 2 , 1 ], strides = [1 , 2 , 2 , 1 ],
85+ padding = 'SAME' , pool = tf .nn .max_pool , name = 'pool3' )
12586 """ conv4 """
126- network = tl .layers .Conv2dLayer (network ,
127- act = tf .nn .relu ,
128- shape = [3 , 3 , 256 , 512 ], # 512 features for each 3x3 patch
129- strides = [1 , 1 , 1 , 1 ],
130- padding = 'SAME' ,
131- name = 'conv4_1' )
132- network = tl .layers .Conv2dLayer (network ,
133- act = tf .nn .relu ,
134- shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
135- strides = [1 , 1 , 1 , 1 ],
136- padding = 'SAME' ,
137- name = 'conv4_2' )
138- network = tl .layers .Conv2dLayer (network ,
139- act = tf .nn .relu ,
140- shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
141- strides = [1 , 1 , 1 , 1 ],
142- padding = 'SAME' ,
143- name = 'conv4_3' )
144- network = tl .layers .PoolLayer (network ,
145- ksize = [1 , 2 , 2 , 1 ],
146- strides = [1 , 2 , 2 , 1 ],
147- padding = 'SAME' ,
148- pool = tf .nn .max_pool ,
149- name = 'pool4' )
87+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 256 , 512 ], # 512 features for each 3x3 patch
88+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv4_1' )
89+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
90+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv4_2' )
91+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
92+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv4_3' )
93+ network = PoolLayer (network , ksize = [1 , 2 , 2 , 1 ], strides = [1 , 2 , 2 , 1 ],
94+ padding = 'SAME' , pool = tf .nn .max_pool , name = 'pool4' )
15095 """ conv5 """
151- network = tl .layers .Conv2dLayer (network ,
152- act = tf .nn .relu ,
153- shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
154- strides = [1 , 1 , 1 , 1 ],
155- padding = 'SAME' ,
156- name = 'conv5_1' )
157- network = tl .layers .Conv2dLayer (network ,
158- act = tf .nn .relu ,
159- shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
160- strides = [1 , 1 , 1 , 1 ],
161- padding = 'SAME' ,
162- name = 'conv5_2' )
163- network = tl .layers .Conv2dLayer (network ,
164- act = tf .nn .relu ,
165- shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
166- strides = [1 , 1 , 1 , 1 ],
167- padding = 'SAME' ,
168- name = 'conv5_3' )
169- network = tl .layers .PoolLayer (network ,
170- ksize = [1 , 2 , 2 , 1 ],
171- strides = [1 , 2 , 2 , 1 ],
172- padding = 'SAME' ,
173- pool = tf .nn .max_pool ,
174- name = 'pool5' )
96+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
97+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv5_1' )
98+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
99+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv5_2' )
100+ network = Conv2dLayer (network , act = tf .nn .relu , shape = [3 , 3 , 512 , 512 ], # 512 features for each 3x3 patch
101+ strides = [1 , 1 , 1 , 1 ], padding = 'SAME' , name = 'conv5_3' )
102+ network = PoolLayer (network , ksize = [1 , 2 , 2 , 1 ], strides = [1 , 2 , 2 , 1 ],
103+ padding = 'SAME' , pool = tf .nn .max_pool , name = 'pool5' )
175104 return network
176105
177106
@@ -185,82 +114,72 @@ def conv_layers_simple_api(net_in):
185114 mean = tf .constant ([123.68 , 116.779 , 103.939 ], dtype = tf .float32 , shape = [1 , 1 , 1 , 3 ], name = 'img_mean' )
186115 net_in .outputs = net_in .outputs - mean
187116 """ conv1 """
188- network = tl . layers . Conv2d (net_in , n_filter = 64 , filter_size = (3 , 3 ),
117+ network = Conv2d (net_in , n_filter = 64 , filter_size = (3 , 3 ),
189118 strides = (1 , 1 ), act = tf .nn .relu ,padding = 'SAME' , name = 'conv1_1' )
190- network = tl . layers . Conv2d (network , n_filter = 64 , filter_size = (3 , 3 ),
119+ network = Conv2d (network , n_filter = 64 , filter_size = (3 , 3 ),
191120 strides = (1 , 1 ), act = tf .nn .relu ,padding = 'SAME' , name = 'conv1_2' )
192- network = tl . layers . MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
121+ network = MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
193122 padding = 'SAME' , name = 'pool1' )
194123 """ conv2 """
195- network = tl . layers . Conv2d (network , n_filter = 128 , filter_size = (3 , 3 ),
124+ network = Conv2d (network , n_filter = 128 , filter_size = (3 , 3 ),
196125 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv2_1' )
197- network = tl . layers . Conv2d (network ,n_filter = 128 , filter_size = (3 , 3 ),
126+ network = Conv2d (network ,n_filter = 128 , filter_size = (3 , 3 ),
198127 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv2_2' )
199- network = tl . layers . MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
128+ network = MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
200129 padding = 'SAME' , name = 'pool2' )
201130 """ conv3 """
202- network = tl . layers . Conv2d (network , n_filter = 256 , filter_size = (3 , 3 ),
131+ network = Conv2d (network , n_filter = 256 , filter_size = (3 , 3 ),
203132 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv3_1' )
204- network = tl . layers . Conv2d (network , n_filter = 256 , filter_size = (3 , 3 ),
133+ network = Conv2d (network , n_filter = 256 , filter_size = (3 , 3 ),
205134 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv3_2' )
206- network = tl . layers . Conv2d (network , n_filter = 256 , filter_size = (3 , 3 ),
135+ network = Conv2d (network , n_filter = 256 , filter_size = (3 , 3 ),
207136 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv3_3' )
208- network = tl . layers . MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
137+ network = MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
209138 padding = 'SAME' , name = 'pool3' )
210139 """ conv4 """
211- network = tl . layers . Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
140+ network = Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
212141 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv4_1' )
213- network = tl . layers . Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
142+ network = Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
214143 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv4_2' )
215- network = tl . layers . Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
144+ network = Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
216145 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv4_3' )
217- network = tl . layers . MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
146+ network = MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
218147 padding = 'SAME' , name = 'pool4' )
219148 """ conv5 """
220- network = tl . layers . Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
149+ network = Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
221150 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv5_1' )
222- network = tl . layers . Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
151+ network = Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
223152 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv5_2' )
224- network = tl . layers . Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
153+ network = Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
225154 strides = (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'conv5_3' )
226- network = tl . layers . MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
155+ network = MaxPool2d (network , filter_size = (2 , 2 ), strides = (2 , 2 ),
227156 padding = 'SAME' , name = 'pool5' )
228157 return network
229158
230-
231159def fc_layers (net ):
232- network = tl .layers .FlattenLayer (net , name = 'flatten' )
233- network = tl .layers .DenseLayer (network , n_units = 4096 ,
234- act = tf .nn .relu ,
235- name = 'fc1_relu' )
236- network = tl .layers .DenseLayer (network , n_units = 4096 ,
237- act = tf .nn .relu ,
238- name = 'fc2_relu' )
239- network = tl .layers .DenseLayer (network , n_units = 1000 ,
240- act = tf .identity ,
241- name = 'fc3_relu' )
160+ network = FlattenLayer (net , name = 'flatten' )
161+ network = DenseLayer (network , n_units = 4096 , act = tf .nn .relu , name = 'fc1_relu' )
162+ network = DenseLayer (network , n_units = 4096 , act = tf .nn .relu , name = 'fc2_relu' )
163+ network = DenseLayer (network , n_units = 1000 , act = tf .identity , name = 'fc3_relu' )
242164 return network
243165
244-
245166sess = tf .InteractiveSession ()
246167
247168x = tf .placeholder (tf .float32 , [None , 224 , 224 , 3 ])
248- y_ = tf .placeholder (tf .int32 , shape = [None , ], name = 'y_' )
169+ # y_ = tf.placeholder(tf.int32, shape=[None, ], name='y_')
249170
250- net_in = tl . layers . InputLayer (x , name = 'input' )
251- net_cnn = conv_layers (net_in ) # professional CNN APIs
252- # net_cnn = conv_layers_simple_api(net_in) # simplified CNN APIs
171+ net_in = InputLayer (x , name = 'input' )
172+ # net_cnn = conv_layers(net_in) # professional CNN APIs
173+ net_cnn = conv_layers_simple_api (net_in ) # simplified CNN APIs
253174network = fc_layers (net_cnn )
254175
255176y = network .outputs
256177probs = tf .nn .softmax (y )
257178# y_op = tf.argmax(tf.nn.softmax(y), 1)
258179# cost = tl.cost.cross_entropy(y, y_, name='cost')
259- #
260180# correct_prediction = tf.equal(tf.cast(tf.argmax(y, 1), tf.float32), tf.cast(y_, tf.float32))
261181# acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
262182
263- # sess.run(tf.initialize_all_variables())
264183tl .layers .initialize_global_variables (sess )
265184network .print_params ()
266185network .print_layers ()
0 commit comments