1. 程式人生 > >[Kaggle] dogs-vs-cats之模型訓練

[Kaggle] dogs-vs-cats之模型訓練

naconda lob flow 技術分享 prot merge efault int app

上一步建立好模型之後,現在就可以訓練模型了。

主要代碼如下:

import sys
#將當期路徑加入系統path中
sys.path.append("E:\\CODE\\Anaconda\\tensorflow\\Kaggle\\My-TensorFlow-tutorials-master\\01 cats vs dogs\\")

import os
import numpy as np
import tensorflow as tf
import input_data
import model

#%%

N_CLASSES = 2 #類別數
IMG_W = 208  # resize the image, if the input image is too large, training will be very slow.
IMG_H = 208 BATCH_SIZE = 16 CAPACITY = 2000 #隊列中元素個數 MAX_STEP = 10000 #最大叠代次數 with current parameters, it is suggested to use MAX_STEP>10k learning_rate = 0.0001 # with current parameters, it is suggested to use learning rate<0.0001 #%% def run_training(): # you need to change the directories to yours.
#train_dir = ‘/home/kevin/tensorflow/cats_vs_dogs/data/train/‘#數據存放路徑 train_dir = E:\\data\\Dog_Cat\\train\\ #logs_train_dir = ‘/home/kevin/tensorflow/cats_vs_dogs/logs/train/‘#存放訓練參數,模型等 logs_train_dir = "E:\\CODE\\Anaconda\\tensorflow\\Kaggle\\My-TensorFlow-tutorials-master\\01 cats vs dogs\\"
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)#獲取loss 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)#把summary保存到路徑中 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()

一些函數說明如下:

1)tf.summary.merge_all

作用:Merges all summaries collected in the default graph.

2)tf.summary.FileWriter

作用:Writes Summary protocol buffers to event files.

3)tf.train.Saver

作用:保存和恢復變量。

舉例:

saver.save(sess, ‘my-model‘, global_step=0)

==> filename: ‘my-model-0‘
...
saver.save(sess, ‘my-model‘, global_step=1000)

==> filename: ‘my-model-1000‘ 

4)add_summary

作用:Writes Summary protocol buffers to event files.

程序運行後,控制臺輸出如下:

技術分享

訓練期間,也可以使用tensorboard查看模型訓練情況。

可以使用如下命令打開tensorboard。

tensorboard --logdir=log文件路徑

log文件路徑即為程序中設置的logs_train_dir。

啟動tensorboard之後,打開瀏覽器,輸入對應網址,即可查看訓練情況。

整體解碼如下圖:

技術分享

loss與step的關系如下(兩條曲線的原因是訓練了兩次,一次叠代了10000步,另一次叠代了15000步):

技術分享

也可以選擇查看模型:

技術分享

說明:

代碼來自:https://github.com/kevin28520/My-TensorFlow-tutorials,略有修改

函數作用主要參考tensorflow官網。https://www.tensorflow.org/versions/master/api_docs/

[Kaggle] dogs-vs-cats之模型訓練