2
2
import tensorflow as tf
3
3
import math
4
4
import sys
5
+ import time
5
6
import datetime
7
+ import os
6
8
9
+ os .environ ['TF_CPP_MIN_LOG_LEVEL' ] = '2'
7
10
8
11
class NISTHelper ():
9
12
def __init__ (self , train_img , train_label , test_img , test_label ):
@@ -132,19 +135,20 @@ def main():
132
135
133
136
# MODEL
134
137
# filter size=(4,4); channels=1; filters=16; shape=?x32x32x32
135
- convo_1 = convolutional_layer (x , shape = [4 , 4 , 1 , 16 ], name = "Convolutional_1" )
138
+ convo_1 = convolutional_layer (x , shape = [4 , 4 , 1 , 32 ], name = "Convolutional_1" )
136
139
convo_1_pooling = max_pool_2by2 (convo_1 ) # shape=?x16x16x32
137
140
138
141
# filter size=(4,4); channels=16; filters=32; shape=?x16x16x64
139
- convo_2 = convolutional_layer (convo_1_pooling , shape = [4 , 4 , 16 , 32 ], name = "Convolutional_2" )
142
+ convo_2 = convolutional_layer (convo_1_pooling , shape = [4 , 4 , 32 , 64 ], name = "Convolutional_2" )
140
143
convo_2_pooling = max_pool_2by2 (convo_2 ) # shape=?x8x8x64
141
-
144
+ convo_2_flat = tf .reshape (convo_2_pooling , [- 1 , 8 * 8 * 64 ])
145
+
142
146
# filter size=(4,4); channels=32; filters=64; shape=?x8x8x32
143
- convo_3 = convolutional_layer (convo_2_pooling , shape = [4 , 4 , 32 , 64 ], name = "Convolutional_3" )
144
- convo_3_pooling = max_pool_2by2 (convo_3 ) # shape=4x4x32
145
- convo_3_flat = tf .reshape (convo_3_pooling , [- 1 , 4 * 4 * 64 ]) # Flatten convolutional layer
147
+ # convo_3 = convolutional_layer(convo_2_pooling, shape=[4, 4, 32, 64], name="Convolutional_3")
148
+ # convo_3_pooling = max_pool_2by2(convo_3) # shape=4x4x32
149
+ # convo_3_flat = tf.reshape(convo_3_pooling, [-1, 4 * 4 * 64]) # Flatten convolutional layer
146
150
147
- full_layer_one = normal_full_layer (convo_3_flat , 512 , tf .nn .relu , name = "Normal_Layer_1" )
151
+ full_layer_one = normal_full_layer (convo_2_flat , 1024 , tf .nn .relu , name = "Normal_Layer_1" )
148
152
with tf .name_scope ("dropout" ):
149
153
hold_prob = tf .placeholder (tf .float32 )
150
154
tf .summary .scalar ("dropout_keep_probability" , hold_prob )
@@ -154,11 +158,10 @@ def main():
154
158
with tf .name_scope ("cross_entropy" ):
155
159
with tf .name_scope ("total" ):
156
160
# Calculate cross-entropy
157
- cross_entropy = tf .reduce_mean (tf .nn .softmax_cross_entropy_with_logits (labels = y_true , logits = y_pred ))
161
+ cross_entropy = tf .reduce_mean (tf .nn .softmax_cross_entropy_with_logits_v2 (labels = y_true , logits = y_pred ))
158
162
tf .summary .scalar ("cross_entropy" , cross_entropy )
159
163
with tf .name_scope ("train" ):
160
- optimizer = tf .train .AdamOptimizer (learning_rate = 0.002 ) # Optimizer
161
- train = optimizer .minimize (cross_entropy )
164
+ train = tf .train .AdamOptimizer (learning_rate = 0.001 ).minimize (cross_entropy )
162
165
with tf .name_scope ("accuracy" ):
163
166
with tf .name_scope ("correct_predictions" ):
164
167
correct_predictions = tf .equal (tf .argmax (y_pred , 1 ), tf .argmax (y_true , 1 )) # use argmax to get the index
@@ -172,7 +175,7 @@ def main():
172
175
merged = tf .summary .merge_all ()
173
176
init = tf .global_variables_initializer ()
174
177
saver = tf .train .Saver ()
175
-
178
+ epoch_start = 0
176
179
with tf .Session () as sess :
177
180
train_writer = tf .summary .FileWriter ("log/train" , sess .graph )
178
181
test_writer = tf .summary .FileWriter ("log/test" )
@@ -193,9 +196,13 @@ def main():
193
196
summary , accuracy = sess .run ([merged , acc ], feed_dict = {x : batch [0 ], y_true : batch [1 ], hold_prob : 1 })
194
197
test_writer .add_summary (summary , i ) # Save the results of test batch
195
198
saver .save (sess , "log/model.ckpt" , i ) # Save model
196
- log ("Step: " + str (i ) + "; Accuracy: " + str (accuracy ))
199
+ if i > 0 :
200
+ log ("Step: " + str (i ) + "; Accuracy: " + str (accuracy ) + "; Time (200 Steps): " + str ((time .time () - epoch_start )))
201
+ else :
202
+ log ("Step: " + str (i ) + "; Accuracy: " + str (accuracy ) + ";" )
203
+ epoch_start = time .time ()
197
204
log ("Finished training." )
198
- model_path = "models/32x32_3conv_16_32_64_1norm_512 .ckpt"
205
+ model_path = "models/32x32_2conv_32_64_1norm_1024 .ckpt"
199
206
saver .save (sess , model_path ) # Save final model
200
207
log ("Model saved in " + model_path )
201
208
0 commit comments