@@ -175,6 +175,59 @@ def conv_layers(net_in):
175175 return network
176176
177177
178+ def conv_layers_simple_api (net_in ):
179+ with tf .name_scope ('preprocess' ) as scope :
180+ """
181+ Notice that we include a preprocessing layer that takes the RGB image
182+ with pixels values in the range of 0-255 and subtracts the mean image
183+ values (calculated over the entire ImageNet training set).
184+ """
185+ mean = tf .constant ([123.68 , 116.779 , 103.939 ], dtype = tf .float32 , shape = [1 , 1 , 1 , 3 ], name = 'img_mean' )
186+ net_in .outputs = net_in .outputs - mean
187+ """ conv1 """
188+ network = tl .layers .Conv2d (net_in , n_filter = 64 , filter_size = (3 , 3 ),
189+ 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 ),
191+ 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 ),
193+ padding = 'SAME' , name = 'pool1' )
194+ """ conv2 """
195+ network = tl .layers .Conv2d (network , n_filter = 128 , filter_size = (3 , 3 ),
196+ 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 ),
198+ 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 ),
200+ padding = 'SAME' , name = 'pool2' )
201+ """ conv3 """
202+ network = tl .layers .Conv2d (network , n_filter = 256 , filter_size = (3 , 3 ),
203+ 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 ),
205+ 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 ),
207+ 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 ),
209+ padding = 'SAME' , name = 'pool3' )
210+ """ conv4 """
211+ network = tl .layers .Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
212+ 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 ),
214+ 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 ),
216+ 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 ),
218+ padding = 'SAME' , name = 'pool4' )
219+ """ conv5 """
220+ network = tl .layers .Conv2d (network , n_filter = 512 , filter_size = (3 , 3 ),
221+ 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 ),
223+ 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 ),
225+ 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 ),
227+ padding = 'SAME' , name = 'pool5' )
228+ return network
229+
230+
178231def fc_layers (net ):
179232 network = tl .layers .FlattenLayer (net , name = 'flatten' )
180233 network = tl .layers .DenseLayer (network , n_units = 4096 ,
@@ -194,17 +247,18 @@ def fc_layers(net):
194247x = tf .placeholder (tf .float32 , [None , 224 , 224 , 3 ])
195248y_ = tf .placeholder (tf .int32 , shape = [None , ], name = 'y_' )
196249
197- net_in = tl .layers .InputLayer (x , name = 'input_layer' )
198- net_cnn = conv_layers (net_in )
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
199253network = fc_layers (net_cnn )
200254
201255y = network .outputs
202256probs = tf .nn .softmax (y )
203- y_op = tf .argmax (tf .nn .softmax (y ), 1 )
204- cost = tl .cost .cross_entropy (y , y_ , name = 'cost' )
205-
206- correct_prediction = tf .equal (tf .cast (tf .argmax (y , 1 ), tf .float32 ), tf .cast (y_ , tf .float32 ))
207- acc = tf .reduce_mean (tf .cast (correct_prediction , tf .float32 ))
257+ # y_op = tf.argmax(tf.nn.softmax(y), 1)
258+ # cost = tl.cost.cross_entropy(y, y_, name='cost')
259+ #
260+ # correct_prediction = tf.equal(tf.cast(tf.argmax(y, 1), tf.float32), tf.cast(y_, tf.float32))
261+ # acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
208262
209263# sess.run(tf.initialize_all_variables())
210264tl .layers .initialize_global_variables (sess )
0 commit comments