在tensorflow学习系列(一)里面介绍数据的构建,模型的构建,以及训练,在这个进阶阶段要将模型的训练和测试放在一起进行,下面就看看代码吧,小伙伴们参考的话可以和前面的代码进行比较一下看看有何变化。
补充一个kaggle的官网:https://www.kaggle.com/ 里面可以下载到数据,如果你下载不了,下面是我百度云的:http://pan.baidu.com/s/1slqHc3j
input_data.py
import tensorflow as tf
import numpy as np
import os
import math
#%%
# you need to change this to your data directory
train_dir = '/home/ccf/Study/tensorflow/My-TensorFlow-tutorials-master/02_cats_vs_dogs/data/train/'
**def get_files(file_dir,ratio):**
'''
Args:
file_dir: file directory
Returns:
list of images and labels
'''
cats = []
label_cats = []
dogs = []
label_dogs = []
for file in os.listdir(file_dir):
name = file.split('.')#这里file.split()函数内的参数py3和py2不同py3:name = file.split(seq='.');py2:name = file.split('.')
if name[0]=='cat':
cats.append(file_dir + file)
label_cats.append(0)
else:
dogs.append(file_dir + file)
label_dogs.append(1)
print('There are %d cats\nThere are %d dogs' %(len(cats), len(dogs)))
image_list = np.hstack((cats, dogs))
label_list = np.hstack((label_cats, label_dogs))
temp = np.array([image_list, label_list])
temp = temp.transpose()
np.random.shuffle(temp)
all_image_list =temp[:, 0]
all_label_list = temp[:, 1]
n_sample=int(len(all_label_list)) # 25000
n_val=int(math.ceil(n_sample*ratio)) #
n_train=int(n_sample-n_val)
tra_images=all_image_list[0:n_train]
tra_labels=all_label_list[0:n_train]
tra_labels=[int(float(i)) for i in tra_labels]
val_images=all_image_list[n_train:]
val_labels=all_label_list[n_train:]
val_labels=[int(float(i)) for i in val_labels]
#label_list = [int(i) for i in label_list]
return tra_images,tra_labels,val_images,val_labels
#%%
def get_batch(image, label, image_W, image_H, batch_size, capacity):#capacity队列中最大容纳数据的个数
'''
Args:
image: list type
label: list type
image_W: image width
image_H: image height
batch_size: batch size
capacity: the maximum elements in queue
Returns:
image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32
label_batch: 1D tensor [batch_size], dtype=tf.int32
'''
image = tf.cast(image, tf.string)
label = tf.cast(label, tf.int32)
# make an input queue
input_queue = tf.train.slice_input_producer([image, label])
label = input_queue[1]
image_contents = tf.read_file(input_queue[0])
image = tf.image.decode_jpeg(image_contents, channels=3)
######################################
# data argumentation should go to here
######################################
image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)
# if you want to test the generated batches of images, you might want to comment the following line.
image = tf.image.per_image_standardization(image)
image_batch, label_batch = tf.train.batch([image, label],
batch_size= batch_size,
num_threads= 64,
capacity = capacity)
#you can also use shuffle_batch
# image_batch, label_batch = tf.train.shuffle_batch([image,label],
# batch_size=BATCH_SIZE,
# num_threads=64,
# capacity=CAPACITY,
# min_after_dequeue=CAPACITY-1)
label_batch = tf.reshape(label_batch, [batch_size])
image_batch = tf.cast(image_batch, tf.float32)
return image_batch, label_batch
#%% TEST
# To test the generated batches of images
# When training the model, DO comment the following codes
# import matplotlib.pyplot as plt
#
# BATCH_SIZE = 2
# CAPACITY = 256
# IMG_W = 208
# IMG_H = 208
#
# train_dir = '/home/ccf/Study/tensorflow/My-TensorFlow-tutorials-master/01_cats_vs_dogs/data/train/'
#
# image_list, label_list = get_files(train_dir)
# image_batch, label_batch = get_batch(image_list, label_list, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
#
# with tf.Session() as sess:
# i = 0
# coord = tf.train.Coordinator()
# threads = tf.train.start_queue_runners(coord=coord)
#
# try:
# while not coord.should_stop() and i<1:
#
# img, label = sess.run([image_batch, label_batch])
#
# # just test one batch
# for j in np.arange(BATCH_SIZE):
# print('label: %d' %label[j])
# plt.imshow(img[j,:,:,:])
# plt.show()
# i+=1
#
# except tf.errors.OutOfRangeError:
# print('done!')
# finally:
# coord.request_stop()
# coord.join(threads)
#
看加粗的部分
model.py和前面的模型没有变就补贴上去了
training.py
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 = 64
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.000
ratio = 0.2
# %%
def run_training():
# you need to change the directories to yours.
train_dir = '/home/ccf/Study/tensorflow/My-TensorFlow-tutorials-master/02_cats_vs_dogs/data/train/'
logs_train_dir = '/home/ccf/Study/tensorflow/My-TensorFlow-tutorials-master/02_cats_vs_dogs/logs/train/'
logs_val_dir = '/home/ccf/Study/tensorflow/My-TensorFlow-tutorials-master/02_cats_vs_dogs/logs/val/'
train, train_label, val, val_label = input_data.get_files(train_dir, ratio)
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)
logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
loss = model.losses(logits, train_label_batch)
train_op = model.trainning(loss, learning_rate)
acc = model.evaluation(logits, train_label_batch)
# 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)
# val_logits = model.inference(val_batch, BATCH_SIZE, N_CLASSES)
# val_loss = model.losses(val_logits, val_label_batch)
# val_op = model.trainning(val_loss, learning_rate)
# val_acc = model.evaluation(val_logits, val_label_batch)
x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
y_ = tf.placeholder(tf.int16, shape=[BATCH_SIZE])
with tf.Session() as sess:
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
summary_op = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
val_writer = tf.summary.FileWriter(logs_val_dir, sess.graph)
try:
for step in np.arange(MAX_STEP):
if coord.should_stop():
break
tra_images, tra_labels = sess.run([train_batch, train_label_batch])
_, tra_loss, tra_acc = sess.run([train_op, loss, acc], feed_dict={x: tra_images, y_: tra_labels})
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 % 200 == 0 or (step + 1) == MAX_STEP:
val_images, val_labels = sess.run([val_batch, val_label_batch])
val_loss, val_acc = sess.run([loss, acc], feed_dict={x: val_images, y_: val_labels})
print('Step %d, val loss = %.2f, val accuracy = %.2f%%' % (step, val_loss, val_acc * 100.0))
summary_str = sess.run(summary_op)
val_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()
# %% Evaluate one image
# when training, comment the following codes.
#
# from PIL import Image
# import matplotlib.pyplot as plt
#
# def get_one_image(train):
# '''Randomly pick one image from training data
# Return: ndarray
# '''
# n = len(train)
# ind = np.random.randint(0, n)
# img_dir = train[ind]
#
# image = Image.open(img_dir)
# plt.imshow(image)
# image = image.resize([208, 208])
# image = np.array(image)
# return image
#
# def evaluate_one_image():
# '''Test one image against the saved models and parameters
# '''
#
# # you need to change the directories to yours.
# train_dir = '/home/ccf/Study/tensorflow/My-TensorFlow-tutorials-master/01_cats_vs_dogs/data/train/'
# train, train_label = input_data.get_files(train_dir)
# image_array = get_one_image(train)
#
# with tf.Graph().as_default():
# BATCH_SIZE = 1
# N_CLASSES = 2
#
# image = tf.cast(image_array, tf.float32)
# image = tf.image.per_image_standardization(image)
# image = tf.reshape(image, [1, 208, 208, 3])
# logit = model.inference(image, BATCH_SIZE, N_CLASSES)
#
# logit = tf.nn.softmax(logit)
#
# x = tf.placeholder(tf.float32, shape=[208, 208, 3])
#
# # you need to change the directories to yours.
# logs_train_dir = '/home/ccf/Study/tensorflow/My-TensorFlow-tutorials-master/01_cats_vs_dogs/logs/train/'
#
# saver = tf.train.Saver()
#
# with tf.Session() as sess:
#
# print("Reading checkpoints...")
# ckpt = tf.train.get_checkpoint_state(logs_train_dir)
# if ckpt and ckpt.model_checkpoint_path:
# global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
# saver.restore(sess, ckpt.model_checkpoint_path)
# print('Loading success, global_step is %s' % global_step)
# else:
# print('No checkpoint file found')
#
# prediction = sess.run(logit, feed_dict={x: image_array})
# max_index = np.argmax(prediction)
# if max_index==0:
# print('This is a cat with possibility %.6f' %prediction[:, 0])
# else:
# print('This is a dog with possibility %.6f' %prediction[:, 1])
#
# evaluate_one_image()
# %%
这个修改的有点多。