TensorFlow训练CNN模型识别猫VS狗(总结)
学习TensorFlow的基础教程一般都会接触到入门实验--手写数字识别(MNIST),当我们学习完这个实验后就会想着能不能自己去做个一个CNN(卷积神经网络)模型来训练自己的图像集呢,于是基于此想法可以通过MNIST延伸加深TensorFlow的学习和理解。网上有很多图像分类的例子,做为新手我建议先去阅读别人的模型,然后在此基础上去修改,以为如果自己去从头开始
做的话一旦出错,你很难找出问题的解决办法,当然有问题可以激发你学习的动力,但一旦解决不了容易让你崩溃。接下来进入话题,猫狗识别,大致流程如下:
- 获取训练集及测试集(图像)
- 构建CNN模型
- 训练模型,优化参数
- 测试模型
以上是所有图像分类一致的处理过程
首先训练集及测试集,在互联网上搜索下载了一些猫和狗的图片,然后把这些图片读入到名称队列中,每次往队列中取一定数量的图片来训练
图1 训练集--猫
图2 训练集--狗
图像获取及处理过程为:首先把图像放置在文件夹 train_image 下面,train_image下面再放置两类训练集文件夹0和1,0代表猫,1代表狗,代码中的def get_files(file_path):函数返回的是file_path下面每一类的训练图像路径及标签值(image_list, label_list),代码中的def get_batches(image, label, resize_w, resize_h, batch_size, capacity):是通过训练图像的路径来加载训练图像到名称队列,每次获取一批次的训练图像到内存队列中,这个过程包含了图像裁剪,把图像处理成合适的尺寸,其中用到两个重要的函数:queue = tf.train.slice_input_producer([image, label]) 和 image_batch, label_batch = tf.train.batch([image, label], batch_size = batch_size,num_threads = 64, capacity = capacity),其中tf.train.batch是从内存队列中取出图像数据,每次取batch_size,关于这两个重要的函数,网上有说明可以参考:https://blog.csdn.net/dcrmg/article/details/79776876 和 http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/reading_data.html,下面贴出图像获取及处理相关代码:
def get_files(file_path):
class_train = []
label_train = []
for train_class in os.listdir(file_path):
for pic_name in os.listdir(file_path + train_class):
class_train.append(file_path + train_class + '/' + pic_name)
label_train.append(train_class)
temp = np.array([class_train, label_train])
temp = temp.transpose()
np.random.shuffle(temp)
image_list = list(temp[:,0])
label_list = list(temp[:,1])
# class is 1 2 3 4 5
label_list = [int(i) for i in label_list]
return image_list, label_list
def get_batches(image, label, resize_w, resize_h, batch_size, capacity):
image = tf.cast(image, tf.string)
label = tf.cast(label, tf.int64)
queue = tf.train.slice_input_producer([image, label])
label = queue[1]
image_temp = tf.read_file(queue[0])
image = tf.image.decode_jpeg(image_temp, channels = 3)
#resize image
image = tf.image.resize_image_with_crop_or_pad(image, resize_w, resize_h)
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)
images_batch = tf.cast(image_batch, tf.float32)
labels_batch = tf.reshape(label_batch, [batch_size])
return images_batch, labels_batch
接下来是模型构建,卷积层+全连接层,最后将得分通过softmax进行分类,得到每一类的概率
def inference(images, batch_size, n_classes):
#
# conv1
#
with tf.variable_scope('conv1') as scope:
weights = tf.Variable(tf.truncated_normal(shape=[3, 3, 3, 64], stddev=1.0, dtype=tf.float32),
name='weights', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[64]),
name='biases', dtype=tf.float32)
conv = tf.nn.conv2d(images, weights, strides=[1, 1, 1, 1], padding='SAME')
pre_activation = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(pre_activation, name=scope.name)
# pooling1
#
with tf.variable_scope('pooling1_lrn') as scope:
pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pooling1')
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1')
# conv2
with tf.variable_scope('conv2') as scope:
weights = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 16], stddev=0.1, dtype=tf.float32),
name='weights', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[16]),
name='biases', dtype=tf.float32)
conv = tf.nn.conv2d(norm1, weights, strides=[1, 1, 1, 1], padding='SAME')
pre_activation = tf.nn.bias_add(conv, biases)
conv2 = tf.nn.relu(pre_activation, name='conv2')
# pooling2
with tf.variable_scope('pooling2_lrn') as scope:
norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2')
pool2 = tf.nn.max_pool(norm2, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='SAME', name='pooling2')
# fc3
with tf.variable_scope('local3') as scope:
reshape = tf.reshape(pool2, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value
weights = tf.Variable(tf.truncated_normal(shape=[dim, 128], stddev=0.005, dtype=tf.float32),
name='weights', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[128]),
name='biases', dtype=tf.float32)
local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name)
# fc4
with tf.variable_scope('local4') as scope:
weights = tf.Variable(tf.truncated_normal(shape=[128, 128], stddev=0.005, dtype=tf.float32),
name='weights', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[128]),
name='biases', dtype=tf.float32)
local4 = tf.nn.relu(tf.matmul(local3, weights) + biases, name='local4')
# dropout
# with tf.variable_scope('dropout') as scope:
# drop_out = tf.nn.dropout(local4, 0.8)
with tf.variable_scope('softmax_linear') as scope:
weights = tf.Variable(tf.truncated_normal(shape=[128, n_classes], stddev=0.005, dtype=tf.float32),
name='softmax_linear', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[n_classes]),
name='biases', dtype=tf.float32)
softmax_linear = tf.add(tf.matmul(local4, weights), biases, name='softmax_linear')
return softmax_linear
# -----------------------------------------------------------------------------
# cal loss
def losses(logits, labels):
with tf.variable_scope('loss') as scope:
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels,
name='xentropy_per_example')
loss = tf.reduce_mean(cross_entropy, name='loss')
tf.summary.scalar(scope.name + '/loss', loss)
return loss
# --------------------------------------------------------------------------
# loss
# loss learning_rate
# train_op
def trainning(loss, learning_rate):
with tf.name_scope('optimizer'):
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
global_step = tf.Variable(0, name='global_step', trainable=False)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
# -----------------------------------------------------------------------
def evaluation(logits, labels):
with tf.variable_scope('accuracy') as scope:
correct = tf.nn.in_top_k(logits, labels, 1)
correct = tf.cast(correct, tf.float16)
accuracy = tf.reduce_mean(correct)
tf.summary.scalar(scope.name + '/accuracy', accuracy)
return accuracy
接下来是训练过程,
train,train_label = get_files('/home/jyf/jyf/python/PeopleRecong/train_image/')
train_batch, train_label_batch = get_batches(train, train_label, 64, 64, 10, 20)
train_logits = model.inference(train_batch, 10, 2)
train_loss = model.losses(train_logits, train_label_batch)
train_op = model.trainning(train_loss, 0.001)
train_acc = model.evaluation(train_logits, train_label_batch)
summary_op = tf.summary.merge_all()
sess = tf.Session()
train_writer = tf.summary.FileWriter(LOG_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(1000):
if coord.should_stop():
break
_, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])
if step % 10 == 0:
print('Step %d, train loss=%.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc))
summary_str = sess.run(summary_op)
train_writer.add_summary(summary_str, step)
if (step + 1) == 1000:
checkpoint_path = os.path.join(CHECK_POINT_DIR, 'model_ckpt')
saver.save(sess, checkpoint_path, global_step=step)
except tf.errors.OutOfRangeError:
print ('Done training')
finally:
coord.request_stop()
coord.join(threads)
接下来是测试过程,加载模型,输出测试结果
CHECK_POINT_DIR = '/home/jyf/jyf/python/PeopleRecong/modelsave'
def evaluate_one_image(image_array):
with tf.Graph().as_default():
image = tf.cast(image_array, tf.float32)
image = tf.image.per_image_standardization(image)
image = tf.reshape(image, [1, 64,64,3])
logit = model.inference(image, 1, 2)
logit = tf.nn.softmax(logit)
x = tf.placeholder(tf.float32, shape=[64,64,3])
saver = tf.train.Saver()
with tf.Session() as sess:
print ('Reading checkpoints...')
ckpt = tf.train.get_checkpoint_state(CHECK_POINT_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)
print (prediction)
if max_index == 0:
result = ('this is cat rate: %.6f, result prediction is [%s]' %(prediction[:,0],','.join(str(i) for i in prediction[0])))
else:
result = ('this is dog rate: %.6f, result prediction is [%s]' %(prediction[:,1],','.join(str(i) for i in prediction[0])))
return result
if __name__ == '__main__':
image = Image.open('/home/jyf/jyf/python/PeopleRecong/4.jpg')
plt.imshow(image)
plt.show()
image = image.resize([64,64])
image = np.array(image)
print evaluate_one_image(image)
测试结果:
训练过程的损失值曲线:
训练精度曲线图如下:
实验的完整代码已经上传到CSDN下载:https://download.csdn.net/download/jiangyingfeng/10610662