def fill_feed_dict(data_set, MFCCfeature_pl, labels_pl): """Fills the feed_dict for training the given step. A feed_dict takes the form of: feed_dict = { <placeholder>: <tensor of values to be passed for placeholder>, .... } Args: data_set: The set of MFCCfeatures and labels, from input_data.read_data_sets() MFCCfeature_pl: The MFCCfeature placeholder, from placeholder_inputs(). labels_pl: The labels placeholder, from placeholder_inputs(). Returns: feed_dict: The feed dictionary mapping from placeholders to values. """ # Create the feed_dict for the placeholders filled with the next # `batch size` examples. MFCCfeature_feed = input_data.next_batch(data_set[0], FLAGS.batch_size) labels_feed = input_data.next_batch(data_set[1], FLAGS.batch_size) # MFCCfeatures_feed = input_data.next_batch(data_set[0],1000) # labels_feed = input_data.next_batch(data_set[1],1000) feed_dict = { MFCCfeature_pl: MFCCfeature_feed, labels_pl: labels_feed, } return feed_dict
def main(_): # train raw_data and label train_data, train_label = input_data.get_files(WORK_DIRECTORY) train_batch, train_label_batch = input_data.next_batch( train_data, train_label) # define train op train_logits = model.inference(train_batch, N_LABELS, BATCH_SIZE) train_loss = model.losses(train_logits, train_label_batch) train_op = model.optimization(train_loss, LEARNING_RATE) train_acc = model.evaluation(train_logits, train_label_batch) # start logs summary_op = tf.summary.merge_all() # Save logs saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) # writen to logs train_writer = tf.summary.FileWriter(LOGS_DIRECTORY, sess.graph) # queue monitor coord = tf.train.Coordinator() # threads = tf.train.start_queue_runners(sess=sess, coord=coord) # train try: for step in np.arange(MAX_STEP): if coord.should_stop(): break # start op node _, loss, accuracy = sess.run([train_op, train_loss, train_acc]) # print and write to logs. if step % 2 == 0: print( f"Step [{step}/{MAX_STEP}] Loss {loss} Accuracy {accuracy * 100.0:.2f}%" ) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step == MAX_STEP: # Save logs checkpoint_path = os.path.join(LOGS_DIRECTORY, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) break print(f"Model saved! Global step = {step}") except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() sess.close()
def test(): weights = { 'wc1': tf.Variable(tf.random_normal([3, 3, 6, 64])), 'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])), 'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])), 'wd1': tf.Variable(tf.random_normal([32 * 32 * 256, 1024])), 'wd2': tf.Variable(tf.random_normal([1024, 1024])), 'out': tf.Variable(tf.random_normal([1024, n_class])) } biases = { 'bc1': tf.Variable(tf.random_normal([64])), 'bc2': tf.Variable(tf.random_normal([128])), 'bc3': tf.Variable(tf.random_normal([256])), 'bd1': tf.Variable(tf.random_normal([1024])), 'bd2': tf.Variable(tf.random_normal([1024])), 'out': tf.Variable(tf.random_normal([n_class])) } pred = alex_net(x, weights, biases, keep_prob) correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) saver = tf.train.Saver() init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) saver.restore(sess, model_path) batch_index = 0 batch_size = 100 iter_add = 0 while iter_add == 0: print batch_index batch_xs, batch_ys, batch_index, iter_add = input_data.next_batch( "test", batch_size, batch_index) acc = sess.run(accuracy, feed_dict={ x: batch_xs, y: batch_ys, keep_prob: 1. }) print "Test Accuracy= " + "{:.5f}".format(acc)
def train(): weights = { 'wc1': tf.Variable(tf.random_normal([3, 3, 6, 64])), 'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])), 'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])), 'wd1': tf.Variable(tf.random_normal([32 * 32 * 256, 1024])), 'wd2': tf.Variable(tf.random_normal([1024, 1024])), 'out': tf.Variable(tf.random_normal([1024, n_class])) } biases = { 'bc1': tf.Variable(tf.random_normal([64])), 'bc2': tf.Variable(tf.random_normal([128])), 'bc3': tf.Variable(tf.random_normal([256])), 'bd1': tf.Variable(tf.random_normal([1024])), 'bd2': tf.Variable(tf.random_normal([1024])), 'out': tf.Variable(tf.random_normal([n_class])) } #construct model pred = alex_net(x, weights, biases, keep_prob) cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y)) optimizer = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(cost) correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) saver = tf.train.Saver() init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) saver.restore(sess, model_path) step = 1 batch_index = 0 batch_size = 32 iter_index = 0 best_acc = 0 while iter_index < training_iters: print "step: " + str(step) batch_xs, batch_ys, batch_index, iter_add = input_data.next_batch( "train", batch_size, batch_index) iter_index += iter_add sess.run(optimizer, feed_dict={ x: batch_xs, y: batch_ys, keep_prob: dropout }) if step % display_step == 0: acc = sess.run(accuracy, feed_dict={ x: batch_xs, y: batch_ys, keep_prob: 1. }) loss = sess.run(cost, feed_dict={ x: batch_xs, y: batch_ys, keep_prob: 1. }) print "Iter " + str( iter_index) + ", Minibatch Loss= " + "{:.6f}".format( loss) + ", Training Accuracy= " + "{:.5f}".format(acc) if acc > best_acc: saver.save(sess, model_path) step += 1 print "Optimization Finished!"
}) init = tf.global_variables_initializer() config = tf.ConfigProto() config.gpu_options.allow_growth = True # In[8]: with tf.Session(config=config) as sess: sess.run(init) saver.restore(sess, "model/model.ckpt") for i in range(0, epoch): print("epoch:" + str(i) + " !!!!!!!!!!!!") step = 0 now_at = 0 while step * batch_size < training_iters: X, Y = input_data.next_batch( img_dir_path='mpii_human_pose_v1\\output_images\\', index_path='train_data\\new_data.json', img_height=480, img_width=640, batch_size=batch_size) sess.run(optimizer, feed_dict={x: X, y: Y, keep_prob: dropout}) loss = sess.run(cost, feed_dict={x: X, y: Y, keep_prob: 1.}) if step % display_step == 0: print("Iter " + str(step * batch_size) + ", Minibatch Loss=" + "{:.6f}".format(loss)) step += 1 now_at += batch_size save_path = saver.save(sess, "model/model_new.ckpt") print("done!")