5050model_file_name = "./model_cifar10_tfrecord.ckpt"
5151resume = False # load model, resume from previous checkpoint?
5252
53- ## Download data, and convert to TFRecord format, see ```tutorial_tfrecord.py```
53+ # Download data, and convert to TFRecord format, see ```tutorial_tfrecord.py```
5454X_train , y_train , X_test , y_test = tl .files .load_cifar10_dataset (shape = (- 1 , 32 , 32 , 3 ), plotable = False )
5555
5656print ('X_train.shape' , X_train .shape ) # (50000, 32, 32, 3)
6161
6262
6363def data_to_tfrecord (images , labels , filename ):
64- """ Save data into TFRecord """
64+ """Save data into TFRecord. """
6565 if os .path .isfile (filename ):
6666 print ("%s exists" % filename )
6767 return
@@ -70,11 +70,11 @@ def data_to_tfrecord(images, labels, filename):
7070 writer = tf .python_io .TFRecordWriter (filename )
7171 for index , img in enumerate (images ):
7272 img_raw = img .tobytes ()
73- ## Visualize a image
73+ # Visualize a image
7474 # tl.visualize.frame(np.asarray(img, dtype=np.uint8), second=1, saveable=False, name='frame', fig_idx=1236)
7575 label = int (labels [index ])
7676 # print(label)
77- ## Convert the bytes back to image as follow:
77+ # Convert the bytes back to image as follow:
7878 # image = Image.frombytes('RGB', (32, 32), img_raw)
7979 # image = np.fromstring(img_raw, np.float32)
8080 # image = image.reshape([32, 32, 3])
@@ -92,7 +92,7 @@ def data_to_tfrecord(images, labels, filename):
9292
9393
9494def read_and_decode (filename , is_train = None ):
95- """ Return tensor to read from TFRecord """
95+ """Return tensor to read from TFRecord. """
9696 filename_queue = tf .train .string_input_producer ([filename ])
9797 reader = tf .TFRecordReader ()
9898 _ , serialized_example = reader .read (filename_queue )
@@ -136,7 +136,7 @@ def read_and_decode(filename, is_train=None):
136136 return img , label
137137
138138
139- ## Save data into TFRecord files
139+ # Save data into TFRecord files
140140data_to_tfrecord (images = X_train , labels = y_train , filename = "train.cifar10" )
141141data_to_tfrecord (images = X_test , labels = y_test , filename = "test.cifar10" )
142142
@@ -159,7 +159,7 @@ def read_and_decode(filename, is_train=None):
159159 )
160160
161161 def model (x_crop , y_ , reuse ):
162- """ For more simplified CNN APIs, check tensorlayer.org """
162+ """For more simplified CNN APIs, check tensorlayer.org. """
163163 with tf .variable_scope ("model" , reuse = reuse ):
164164 net = tl .layers .InputLayer (x_crop , name = 'input' )
165165 net = tl .layers .Conv2d (net , 64 , (5 , 5 ), (1 , 1 ), act = tf .nn .relu , padding = 'SAME' , name = 'cnn1' )
@@ -191,8 +191,8 @@ def model(x_crop, y_, reuse):
191191
192192 return net , cost , acc
193193
194- ## You can also use placeholder to feed_dict in data after using
195- ## val, l = sess.run([x_train_batch, y_train_batch]) to get the data
194+ # You can also use placeholder to feed_dict in data after using
195+ # val, l = sess.run([x_train_batch, y_train_batch]) to get the data
196196 # x_crop = tf.placeholder(tf.float32, shape=[batch_size, 24, 24, 3])
197197 # y_ = tf.placeholder(tf.int32, shape=[batch_size,])
198198 # cost, acc, network = model(x_crop, y_, None)
@@ -201,7 +201,7 @@ def model(x_crop, y_, reuse):
201201 network , cost , acc , = model (x_train_batch , y_train_batch , False )
202202 _ , cost_test , acc_test = model (x_test_batch , y_test_batch , True )
203203
204- ## train
204+ # train
205205 n_epoch = 50000
206206 learning_rate = 0.0001
207207 print_freq = 1
@@ -231,7 +231,7 @@ def model(x_crop, y_, reuse):
231231 start_time = time .time ()
232232 train_loss , train_acc , n_batch = 0 , 0 , 0
233233 for s in range (n_step_epoch ):
234- ## You can also use placeholder to feed_dict in data after using
234+ # You can also use placeholder to feed_dict in data after using
235235 # val, l = sess.run([x_train_batch, y_train_batch])
236236 # tl.visualize.images2d(val, second=3, saveable=False, name='batch', dtype=np.uint8, fig_idx=2020121)
237237 # err, ac, _ = sess.run([cost, acc, train_op], feed_dict={x_crop: val, y_: l})
0 commit comments