def train(): batch_size = TRAIN_BATCH_SIZE num_labels = 10 path = utility.generate_data_path(TRAINING_DATA_SIZE, utility.Data.STREET, label=False) train_images = utility.load_data(path) path = utility.generate_data_path(TRAINING_DATA_SIZE, utility.Data.STREET, label=True) train_labels = utility.load_data(path, label=True) path = utility.generate_data_path(utility.Size.TEST, utility.Data.STREET, label=False) test_images = utility.load_data(path) path = utility.generate_data_path(utility.Size.TEST, utility.Data.STREET, label=True) test_labels = utility.load_data(path, label=True) path = utility.generate_data_path(utility.Size.VALIDATION, utility.Data.STREET, label=False) validation_images = utility.load_data(path) path = utility.generate_data_path(utility.Size.VALIDATION, utility.Data.STREET, label=True) validation_labels = utility.load_data(path, label=True) train_size = train_images.shape[0] total_batch = int(train_size / batch_size) # Boolean for MODE of train or test is_training = tf.placeholder(tf.bool, name='MODE') # tf input x = tf.placeholder(tf.float32, [None, 32, 32, 3]) y_ = tf.placeholder(tf.float32, [None, 10]) #answer y = cnn_model.CNN(x) # Get loss of model with tf.name_scope("LOSS"): loss = cnn_model.loss( y, y_, ) # Define optimizer with tf.name_scope("ADAM"): # Optimizer: set up a variable that's incremented once per batch and # controls the learning rate decay. batch = tf.Variable(0) learning_rate = tf.train.exponential_decay( 1e-4, # Base learning rate. batch * batch_size, # Current index into the dataset. train_size, # Decay step. 0.95, # Decay rate. staircase=True) # Use simple momentum for the optimization. train_step = tf.train.AdamOptimizer(learning_rate).minimize( loss, global_step=batch) # Get accuracy of model with tf.name_scope("ACC"): correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # Create a summary to monitor learning_rate tensor tf.summary.scalar('learning_rate', learning_rate) # Create a summary to monitor accuracy tensor tf.summary.scalar('acc', accuracy) # Merge all summaries into a single op merged_summary_op = tf.summary.merge_all() # Add ops to save and restore all the variables saver = tf.train.Saver() sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer(), feed_dict={is_training: True}) # op to write logs to Tensorboard summary_writer = tf.summary.FileWriter(LOGS_DIRECTORY, graph=tf.get_default_graph()) max_acc = 0. start_time = time.time() # Loop for epoch for epoch in range(training_epochs): # Random shuffling [test_images, test_labels] = utility.shuffle_data(test_images, test_labels) [train_images, train_labels] = utility.shuffle_data(train_images, train_labels) # Loop over all batches for i in range(total_batch): # Compute the offset of the current minibatch in the data. offset = (i * batch_size) % (train_size) batch_xs = train_images[offset:(offset + batch_size), :] batch_ys = train_labels[offset:(offset + batch_size), :] # Run optimization op (backprop), loss op (to get loss value) # and summary nodes _, train_accuracy, summary = sess.run( [train_step, accuracy, merged_summary_op], feed_dict={ x: batch_xs, y_: batch_ys, is_training: True }) # Write logs at every iteration summary_writer.add_summary(summary, epoch * total_batch + i) # Display logs if i % display_step == 0: print( "Epoch:", '%04d,' % (epoch + 1), "batch_index %4d/%4d, training accuracy %.5f" % (i, total_batch, train_accuracy)) # Get accuracy for validation data if i % validation_step == 0: # Calculate accuracy validation_accuracy = sess.run(accuracy, feed_dict={ x: validation_images, y_: validation_labels, is_training: False }) print( "Epoch:", '%04d,' % (epoch + 1), "batch_index %4d/%4d, validation accuracy %.5f" % (i, total_batch, validation_accuracy)) # Save the current model if the maximum accuracy is updated if validation_accuracy > max_acc: max_acc = validation_accuracy save_path = saver.save(sess, MODEL_DIRECTORY) print("Model updated and saved in file: %s" % save_path) print("Optimization Finished!") print("--- %s seconds ---" % (time.time() - start_time)) # Restore variables from disk saver.restore(sess, MODEL_DIRECTORY) # TESTING test_size = test_labels.shape[0] batch_size = TEST_BATCH_SIZE total_batch = int(test_size / batch_size) acc_buffer = [] for i in range(total_batch): offset = (i * batch_size) % (test_size) batch_xs = test_images[offset:(offset + batch_size), :] batch_ys = test_labels[offset:(offset + batch_size), :] y_final = sess.run(y, feed_dict={ x: batch_xs, y_: batch_ys, is_training: False }) correct_prediction = numpy.equal(numpy.argmax(y_final, 1), numpy.argmax(batch_ys, 1)) acc_buffer.append(numpy.sum(correct_prediction) / batch_size) print("test accuracy for the stored model: %g" % numpy.mean(acc_buffer))
def train(dataset): """Train this network model. Args: datasets: contain two elements: a tensor for feature,shape is [batch,height,width,channels] , and a tensor for label, shape is [batch, label_num]. Return: """ global GRID_FEATURE_SHAPE global FLAGS with tf.Graph().as_default(): global_step = tf.Variable(2, trainable=False) """Build network.""" x = tf.placeholder(tf.float32, shape=[None, GRID_FEATURE_SHAPE[0]\ , GRID_FEATURE_SHAPE[1], GRID_FEATURE_SHAPE[2]]) # [batch,heigh,width,depth] y_ = tf.placeholder(tf.float32, shape=[None]) # [batch] keep_prob = tf.placeholder(tf.float32, shape=[]) logits = cnn_model.inference(x, keep_prob) """get variable""" # For Debug """build loss function and optimizer.""" loss = cnn_model.loss(logits, y_) train_step = tf.train.AdamOptimizer(1e-4).minimize(loss) """build accuracy function""" # for column #pdb.set_trace() prediction = tf.equal(tf.argmax(logits, 1), tf.cast(y_, tf.int64)) accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32)) """train and save new model.""" with tf.Session() as sess: sess.run(tf.initialize_all_variables()) """init saver and load old model.""" saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(CFLAGS.checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: print 'Find old model %s, and try to restore it.' % ( ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) """training""" print 'Begin trainning....' for i in xrange(global_step.eval(), 10000): batch = dataset.next_batch(FLAGS.batch_size) #tmp = sess.run(logits,feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5}) #pdb.set_trace() train_step.run(feed_dict={ x: batch[0], y_: batch[1], keep_prob: 0.5 }) # show train accuracy if (i + 1) % 10 == 0: train_accuracy = accuracy.eval(feed_dict={x:batch[0]\ , y_:batch[1], keep_prob:1.0}) print "step %d, trainning accuracy %g" % (i, train_accuracy) # save checkpoint if (i + 1) % CFLAGS.checkpoint_steps == 0: sess.run(tf.assign(global_step, i + 1)) saver.save(sess, CFLAGS.checkpoint_dir + CFLAGS.checkpoint_file \ , global_step=global_step) print 'End trainning...'
def train(): batch_size = FLAGS.batch_size crop_size = FLAGS.crop_size with tf.Graph().as_default(): global_step = tf.Variable(0, name='global_step', trainable=False) # input train_input = CNNTrainInput(FLAGS.data_file) images = tf.placeholder(tf.float32, shape=[batch_size, crop_size, crop_size, 1], name='image') labels = tf.placeholder(tf.int64, shape=[batch_size], name='label') # Display the training images in the visualizer. image_slices = tf.slice(images, [0, 0, 0, 0], [int(batch_size), crop_size, crop_size, 1], name='central_slice') tf.summary.image('image_slices', image_slices, max_outputs=10) # inference logits = cnn_model.inference(images) # calculate accuracy and error rate prediction = tf.argmax(logits, 1) correct_prediction = tf.equal(prediction, labels) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) error_rate = 1 - accuracy tf.summary.scalar('accuracy', accuracy) tf.summary.scalar('error_rate', error_rate) # train to minimize loss loss = cnn_model.loss(logits, labels) lr = tf.placeholder(tf.float64, name='leaning_rate') tf.summary.scalar('learning_rate', lr) train_op = cnn_model.train(loss, lr, global_step) # Add ops to save and restore all the variables. saver = tf.train.Saver() # run graph in session with tf.Session() as sess: init = tf.global_variables_initializer( ) # create an operation initializes all the variables sess.run(init) merged = tf.summary.merge_all() writer = tf.summary.FileWriter('%s' % FLAGS.train_dir, sess.graph) if FLAGS.load_ckpt: ckpt_file = '%s/model.ckpt-%d' % \ (FLAGS.train_dir, FLAGS.ckpt_step) print('restore sess with %s' % ckpt_file) saver.restore(sess, ckpt_file) start = time.time() for step in range(FLAGS.max_steps): batch_images, batch_labels = train_input.next_batch( batch_size=batch_size) if FLAGS.shuffle: idx = np.arange(batch_size) np.random.shuffle(idx) batch_images = batch_images[idx] batch_labels = batch_labels[idx] lr_value = FLAGS.learning_rate * pow( FLAGS.decay_factor, (step / FLAGS.decay_steps)) _, err, g_step, loss_value, summary = sess.run( [train_op, error_rate, global_step, loss, merged], feed_dict={ labels: batch_labels, images: batch_images, lr: lr_value, }) if step % FLAGS.log_frequency == 0 and step != 0: end = time.time() duration = end - start examples_per_sec = FLAGS.log_frequency * FLAGS.batch_size / duration sec_per_batch = float(duration / FLAGS.log_frequency) format_str = ( '%s: step %d, loss = %.2f, err = %.4f (%.1f examples/sec; %.3f ' 'sec/batch)') print(format_str % (datetime.now(), g_step, loss_value, err, examples_per_sec, sec_per_batch)) writer.add_summary(summary, g_step) # Save the variables to disk. saver.save(sess, '%s/model.ckpt' % FLAGS.train_dir, global_step=g_step) start = end
#........ This part will used to get training data for each epoch during training init_count = 0 num_epochs = 100 batch_size = 40 numiter = 125 ne = 0 valacc = [] #Create session feed_images = tf.placeholder(tf.float32, shape=(None, 96, 96, 3)) feed_labels = tf.placeholder(tf.float32, shape=(None, )) aug_img = tf.placeholder(tf.float32, shape=(96, 96, 3)) logits = nn_model(feed_images, training=True) cost = loss(logits, feed_labels) opt_mom = optimizer(lr=0.01) opt = opt_mom.minimize(cost) acc = accuracy(logits, feed_labels) sess = tf.Session() sess.run(tf.global_variables_initializer()) img_scale_crop = tf.random_crop( tf.image.resize_images(aug_img, get_new_size()), [96, 96, 3]) img_rand_flip_lr = tf.image.random_flip_left_right(aug_img) img_rand_flip_ud = tf.image.random_flip_up_down(aug_img)