tensorflowを利用して自分の画像データを訓練する(4)--ニューラルネットワーク訓練

3136 ワード

1  . 説明
前回のブログでtensorflowを利用して自分の画像データを訓練した(3)では、今回の訓練のモデルを構築し、次にネットワーク訓練を開始し、訓練後のネットワークパラメータを保存してテスト時に使用する.
二.プログラミングの実現
#======================================================================
#    
import os
import numpy as np
import tensorflow as tf
import input_data
import model

#    
N_CLASSES = 4  #husky,jiwawa,poodle,qiutian
IMG_W = 64   # resize  ,         
IMG_H = 64
BATCH_SIZE =20
CAPACITY = 200
MAX_STEP = 200 #     10K
learning_rate = 0.0001 #     0.0001

#    batch
train_dir = 'E:/Re_train/image_data/inputdata'   #         
logs_train_dir = 'E:/Re_train/image_data/inputdata'    #logs    
#logs_test_dir =  'E:/Re_train/image_data/test'        #logs    

#train, train_label = input_data.get_files(train_dir)
train, train_label, val, val_label = input_data.get_files(train_dir, 0.3)
#       
train_batch,train_label_batch = input_data.get_batch(train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
#       
val_batch, val_label_batch = input_data.get_batch(val, val_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)

#      
test_logits = model.inference(val_batch, BATCH_SIZE, N_CLASSES)
test_loss = model.losses(test_logits, val_label_batch)        
test_acc = model.evaluation(test_logits, val_label_batch)

#   log    
summary_op = tf.summary.merge_all() 

#      
sess = tf.Session()  
#    writer  log  
train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) 
#val_writer = tf.summary.FileWriter(logs_test_dir, sess.graph) 
#    saver         
saver = tf.train.Saver()
#       
sess.run(tf.global_variables_initializer())  
#    
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

#  batch   
try:
    #  MAX_STEP    ,    batch
    for step in np.arange(MAX_STEP):
        if coord.should_stop():
            break
        #        ,    ,   train_logits       ?
        _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])
        
        #  50        loss  acc,    log,  writer   
        if step % 10  == 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)
        #  100 ,          
        if (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()
    
#========================================================================

今回の訓練は300回、ratioは0.3、学習率は0.001、ロット処理量は20である.10回ホールごとにトレーニング結果を確認し、最後のトレーニングが完了したら、トレーニングデータをlogs_に保存します.train_dirのmodel.ckptという名前のファイルにあります.