@@ -148,86 +148,31 @@ def read_and_decode(filename, is_train=None):
148
148
# prepare data in cpu
149
149
x_train_ , y_train_ = read_and_decode ("train.cifar10" , True )
150
150
x_test_ , y_test_ = read_and_decode ("test.cifar10" , False )
151
-
152
- x_train_batch , y_train_batch = tf .train .shuffle_batch (
153
- [x_train_ , y_train_ ], batch_size = batch_size , capacity = 2000 , min_after_dequeue = 1000 , num_threads = 32
154
- ) # set the number of threads here
151
+ # set the number of threads here
152
+ x_train_batch , y_train_batch = tf .train .shuffle_batch ([x_train_ , y_train_ ], \
153
+ batch_size = batch_size , capacity = 2000 , min_after_dequeue = 1000 , num_threads = 32 )
155
154
# for testing, uses batch instead of shuffle_batch
156
- x_test_batch , y_test_batch = tf .train .batch (
157
- [x_test_ , y_test_ ], batch_size = batch_size , capacity = 50000 , num_threads = 32
158
- )
155
+ x_test_batch , y_test_batch = tf .train .batch ([x_test_ , y_test_ ], \
156
+ batch_size = batch_size , capacity = 50000 , num_threads = 32 )
159
157
160
158
def model (x_crop , y_ , reuse ):
161
159
""" For more simplified CNN APIs, check tensorlayer.org """
162
- W_init = tf .truncated_normal_initializer (stddev = 5e-2 )
163
- W_init2 = tf .truncated_normal_initializer (stddev = 0.04 )
164
- b_init2 = tf .constant_initializer (value = 0.1 )
165
160
with tf .variable_scope ("model" , reuse = reuse ):
166
161
net = tl .layers .InputLayer (x_crop , name = 'input' )
167
- net = tl .layers .Conv2d (net , 64 , (5 , 5 ), (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , W_init = W_init , name = 'cnn1' )
162
+ net = tl .layers .Conv2d (net , 64 , (5 , 5 ), (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'cnn1' )
168
163
net = tl .layers .SignLayer (net )
169
164
net = tl .layers .MaxPool2d (net , (3 , 3 ), (2 , 2 ), padding = 'SAME' , name = 'pool1' )
170
- net = tl .layers .LocalResponseNormLayer (
171
- net , depth_radius = 4 , bias = 1.0 , alpha = 0.001 / 9.0 , beta = 0.75 , name = 'norm1'
172
- )
173
- net = tl .layers .BinaryConv2d (
174
- net , 64 , (5 , 5 ), (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , W_init = W_init , name = 'cnn2'
175
- )
176
- net = tl .layers .LocalResponseNormLayer (
177
- net , depth_radius = 4 , bias = 1.0 , alpha = 0.001 / 9.0 , beta = 0.75 , name = 'norm2'
178
- )
165
+ net = tl .layers .LocalResponseNormLayer (net , 4 , 1.0 , 0.001 / 9.0 , 0.75 , name = 'norm1' )
166
+ net = tl .layers .BinaryConv2d (net , 64 , (5 , 5 ), (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'cnn2' )
167
+ net = tl .layers .LocalResponseNormLayer (net , 4 , 1.0 , 0.001 / 9.0 , 0.75 , name = 'norm2' )
179
168
net = tl .layers .MaxPool2d (net , (3 , 3 ), (2 , 2 ), padding = 'SAME' , name = 'pool2' )
180
- net = tl .layers .FlattenLayer (net , name = 'flatten' ) # output: (batch_size, 2304)
169
+ net = tl .layers .FlattenLayer (net , name = 'flatten' )
181
170
net = tl .layers .SignLayer (net )
182
- net = tl .layers .BinaryDenseLayer (
183
- net , n_units = 384 , act = tf .nn .relu , W_init = W_init2 , b_init = b_init2 , name = 'd1relu'
184
- ) # output: (batch_size, 384)
171
+ net = tl .layers .BinaryDenseLayer (net , 384 , act = tf .nn .relu , name = 'd1relu' )
185
172
net = tl .layers .SignLayer (net )
186
- net = tl .layers .BinaryDenseLayer (
187
- net , n_units = 192 , act = tf .nn .relu , W_init = W_init2 , b_init = b_init2 , name = 'd2relu'
188
- ) # output: (batch_size, 192)
189
- net = tl .layers .DenseLayer (
190
- net , n_units = 10 , act = tf .identity , W_init = W_init2 , name = 'output'
191
- ) # output: (batch_size, 10)
192
- y = net .outputs
193
-
194
- ce = tl .cost .cross_entropy (y , y_ , name = 'cost' )
195
- # L2 for the MLP, without this, the accuracy will be reduced by 15%.
196
- L2 = 0
197
- for p in tl .layers .get_variables_with_name ('relu/W' , True , True ):
198
- L2 += tf .contrib .layers .l2_regularizer (0.004 )(p )
199
- cost = ce + L2
173
+ net = tl .layers .BinaryDenseLayer (net , 192 , act = tf .nn .relu , name = 'd2relu' )
174
+ net = tl .layers .DenseLayer (net , 10 , act = tf .identity , name = 'output' )
200
175
201
- # correct_prediction = tf.equal(tf.argmax(tf.nn.softmax(y), 1), y_)
202
- correct_prediction = tf .equal (tf .cast (tf .argmax (y , 1 ), tf .int32 ), y_ )
203
- acc = tf .reduce_mean (tf .cast (correct_prediction , tf .float32 ))
204
-
205
- return net , cost , acc
206
-
207
- def model_batch_norm (x_crop , y_ , reuse , is_train ):
208
- """ Batch normalization should be placed before rectifier. """
209
- W_init = tf .truncated_normal_initializer (stddev = 5e-2 )
210
- W_init2 = tf .truncated_normal_initializer (stddev = 0.04 )
211
- b_init2 = tf .constant_initializer (value = 0.1 )
212
- with tf .variable_scope ("model" , reuse = reuse ):
213
- net = InputLayer (x_crop , name = 'input' )
214
-
215
- net = tl .layers .Conv2d (net , 64 , (5 , 5 ), (1 , 1 ), padding = 'SAME' , W_init = W_init , b_init = None , name = 'cnn1' )
216
- net = tl .layers .BatchNormLayer (net , is_train , act = tf .nn .relu , name = 'batch1' )
217
- net = tl .layers .MaxPool2d (net , (3 , 3 ), (2 , 2 ), padding = 'SAME' , name = 'pool1' )
218
- net = tl .layers .Conv2d (net , 64 , (5 , 5 ), (1 , 1 ), padding = 'SAME' , W_init = W_init , b_init = None , name = 'cnn2' )
219
- net = tl .layers .BatchNormLayer (net , is_train , act = tf .nn .relu , name = 'batch2' )
220
- net = tl .layers .MaxPool2d (net , (3 , 3 ), (2 , 2 ), padding = 'SAME' , name = 'pool2' )
221
- net = tl .layers .FlattenLayer (net , name = 'flatten' ) # output: (batch_size, 2304)
222
- net = tl .layers .DenseLayer (
223
- net , n_units = 384 , act = tf .nn .relu , W_init = W_init2 , b_init = b_init2 , name = 'd1relu'
224
- ) # output: (batch_size, 384)
225
- net = tl .layers .DenseLayer (
226
- net , n_units = 192 , act = tf .nn .relu , W_init = W_init2 , b_init = b_init2 , name = 'd2relu'
227
- ) # output: (batch_size, 192)
228
- net = tl .layers .DenseLayer (
229
- net , n_units = 10 , act = tf .identity , W_init = W_init2 , name = 'output'
230
- ) # output: (batch_size, 10)
231
176
y = net .outputs
232
177
233
178
ce = tl .cost .cross_entropy (y , y_ , name = 'cost' )
@@ -237,6 +182,7 @@ def model_batch_norm(x_crop, y_, reuse, is_train):
237
182
L2 += tf .contrib .layers .l2_regularizer (0.004 )(p )
238
183
cost = ce + L2
239
184
185
+ # correct_prediction = tf.equal(tf.argmax(tf.nn.softmax(y), 1), y_)
240
186
correct_prediction = tf .equal (tf .cast (tf .argmax (y , 1 ), tf .int32 ), y_ )
241
187
acc = tf .reduce_mean (tf .cast (correct_prediction , tf .float32 ))
242
188
@@ -249,12 +195,8 @@ def model_batch_norm(x_crop, y_, reuse, is_train):
249
195
# cost, acc, network = model(x_crop, y_, None)
250
196
251
197
with tf .device ('/gpu:0' ): # <-- remove it if you don't have GPU
252
- ## using local response normalization
253
198
network , cost , acc , = model (x_train_batch , y_train_batch , False )
254
199
_ , cost_test , acc_test = model (x_test_batch , y_test_batch , True )
255
- ## you may want to try batch normalization
256
- # network, cost, acc, = model_batch_norm(x_train_batch, y_train_batch, None, is_train=True)
257
- # _, cost_test, acc_test = model_batch_norm(x_test_batch, y_test_batch, True, is_train=False)
258
200
259
201
## train
260
202
n_epoch = 50000
@@ -297,10 +239,8 @@ def model_batch_norm(x_crop, y_, reuse, is_train):
297
239
n_batch += 1
298
240
299
241
if epoch + 1 == 1 or (epoch + 1 ) % print_freq == 0 :
300
- print (
301
- "Epoch %d : Step %d-%d of %d took %fs" %
302
- (epoch , step , step + n_step_epoch , n_step , time .time () - start_time )
303
- )
242
+ print ("Epoch %d : Step %d-%d of %d took %fs" % \
243
+ (epoch , step , step + n_step_epoch , n_step , time .time () - start_time ))
304
244
print (" train loss: %f" % (train_loss / n_batch ))
305
245
print (" train acc: %f" % (train_acc / n_batch ))
306
246
0 commit comments