import os
import numpy as np
import tensorflow as tf
import input_data
import model
N_CLASSES = 2 #数据集分两类
IMG_W = 64 # 图片的高
IMG_H = 64 # 图片的宽
BATCH_SIZE = 16
CAPACITY = 1000
MAX_STEP = 10000 # 学习的步长
learning_rate = 0.0001 # 学习率
def run_training():
# you need to change the directories to yours.
train_dir = '/Users/Desktop/cd/cd/Far_1/' #主要说下这个文件夹里边的图片 分成两类 一类是带image的图片名称, 一类是不带。。 图片的名称叫什么都行,学习特征两类,多类,都可以,需要自行修改代码。我是参考识别猫和狗的代码。。
logs_train_dir = '/Users/Desktop/cd/cd/logs' #生成的日志文件,数据集和tensorflow学习的效率,可以使用 tensorbord进行查看
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()