貓狗大戰(3)訓練模型

參考博客:github.com/kevin28520/M

tensorflow 實戰 貓狗大戰(一)訓練自己的數據 - CSDN博客

import osimport numpy as npimport tensorflow as tfimport input_dataimport modelN_CLASSES = 2IMG_W = 208 # resize the image, if the input image is too large, training will be very slow.IMG_H = 208BATCH_SIZE = 16CAPACITY = 2000MAX_STEP = 10000 # with current parameters, it is suggested to use MAX_STEP>10klearning_rate = 0.0001 # with current parameters, it is suggested to use learning rate<0.0001def run_training(): # you need to change the directories to yours. train_dir = J:\移動硬碟\數據集\train\ logs_train_dir = J:\移動硬碟\數據集\logs\ train, train_label = input_data.get_files(train_dir) train_batch, train_label_batch = input_data.get_batch(train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) train_loss = model.losses(train_logits, train_label_batch) train_op = model.trainning(train_loss, learning_rate) train__acc = model.evaluation(train_logits, train_label_batch) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) if step % 50 == 0: print(Step %d, train loss = %.2f, train accuracy = %.2f%% %(step, tra_loss, tra_acc*100.0)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, model.ckpt) saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print(Done training -- epoch limit reached) finally: coord.request_stop() coord.join(threads) sess.close()run_training()

推薦閱讀:

使用Tensorflow C++ API自定義操作
單純形演算法的原理和示例實現
人性本惡,機器本善?
Machine Learning in Action機器學習實戰——勘誤
主動學習中的經驗風險最小化

TAG:模型 | 機器學習 | 建模 |