猫狗大战分类TensorFlow实战分享
点击上方“机器学习爱好者社区”
选择“星标”公众号,重磅干货,第一时间送达
Cats vs. Dogs(猫狗大战)是Kaggle大数据竞赛某一年的一道赛题,利用给定的数据集,用算法实现猫和狗的识别。数据集可以从Kaggle官网上下载,即https://www.kaggle.com/c/dogs-vs-cats。数据集由训练数据和测试数据组成,训练数据包含猫和狗各12500张图片,测试数据包含12500张猫和狗的图片。
首先在Pycharm上新建Cats_vs_Dogs工程,工程目录结构为:
data文件夹下包含test和train两个子文件夹,分别用于存放测试数据和训练数据。logs文件夹用于存放我们训练时的模型结构以及训练参数。input_data.py负责实现读取数据,生成批次(batch)。model.py负责实现我们的神经网络模型。training.py负责实现模型的训练以及评估。
接下来分成数据读取、模型构造、模型训练、测试模型四个部分来讲。
训练数据的读取(input_data.py)
首先需要引入如下模块:
import tensorflow as tf
import numpy as np
import os因为我们需要获取test目录下的文件,所以要导入os模块。
# 获取文件路径和标签,file_dir是文件夹路径,该函数返回乱序后的图片和标签
def get_files(file_dir):
cats = []
label_cats = []
dogs = []
label_dogs = []
for file in os.listdir(file_dir): # 载入数据路径并写入标签值
name = file.split(sep='.')
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)
image_list = list(temp[:, 0])
label_list = list(temp[:, 1])
label_list = [int(i) for i in label_list]
return image_list, label_list
函数get_files的功能是获取给定路径file_dir下的所有的训练数据(包括图片和标签),以list的形式返回。由于训练数据前12500张是猫,后12500张是狗,如果直接按这个顺序训练,训练效果可能会受影响(猜测的),所以需要将顺序打乱。因为图片和标签是一一对应的,所以要整合到一起乱序。
这里先用np.hstack方法将猫和狗图片和标签整合到一起,得到image_list和label_list,hstack((a,b))的功能是将a和b以水平的方式连接,比如原来cats和dogs是长度为12500的向量,执行了hstack(cats, dogs)后,image_list的长度为25000,同理label_list的长度也为25000。接着将一一对应的image_list和label_list再合并一次。temp的大小是2 * 25000,经过转置(变成25000 * 2),然后使用np.random.shuffle方法进行乱序。
最后从temp中分别取出乱序后的image_list和label_list列向量,作为函数的返回值。这里要注意,因为label_list里面的数据类型是字符串类型,所以加上label_list = [int(i) for i in label_list]这么一行将其转为int类型。
# 生成相同大小的批次,参数capacity队列容量,返回值是图像和标签的batch
def get_batch(image, label, image_W, image_H, batch_size, capacity):
# 将python.list类型转换成tf能够识别的格式
image = tf.cast(image, tf.string)
label = tf.cast(label, tf.int32)
input_queue = tf.train.slice_input_producer([image, label]) # 生成队列
image_contents = tf.read_file(input_queue[0])
label = input_queue[1]
image = tf.image.decode_jpeg(image_contents, channels=3)
# 统一图片大小
image = tf.image.resize_images(image, [image_H, image_W], \
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
image = tf.cast(image, tf.float32)
image_batch, label_batch = tf.train.batch([image, label], batch_size=batch_size, \
num_threads=64, capacity=capacity)
# label_batch = tf.reshape(label_batch, [batch_size])
return image_batch, label_batch
函数get_batch用于将图片分批次,因为一次性将所有25000张图片载入内存不现实也不必要,所以将图片分成不同批次进行训练。对于把训练数据集设置成一个个batch,其解释为:如果损失函数是非凸的话,整个训练样本尽管算的动,可能会卡在局部最优解上;分批训练表示全样本的抽样实现,也就是相当于人为地引入了修正梯度上的采样噪声,使得一路不同,找别路的方法,更有可能搜索到全局最优解。这里传入的image和label参数就是函数get_files返回的image_list和label_list,是python中的list类型,所以需要将其转为TensorFlow可以识别的tensor格式。
这里使用队列来获取数据,因为队列操作牵扯到线程,这里引用了一张图解释:

我认为大体上可以这么理解:每次训练时,从队列中取一个batch送到网络进行训练,然后又有新的图片从训练库中注入队列,这样循环往复。队列相当于起到了训练库到网络模型间数据管道的作用,训练数据通过队列送入网络。
我们使用slice_input_producer来建立一个队列,将image和label放入一个list中当做参数传给该函数,然后从队列中取得image和label。要注意,用read_file读取图片之后,要按照图片格式进行解码。本例程中训练数据是jpg格式的,所以使用decode_jpeg解码器,如果是其他格式,就要用其他解码器。注意decode出来的数据类型是uint8,之后模型卷积层里面conv2d要求输入数据为float32类型,所以需要进行类型转换。
因为训练库中图片大小是不一样的,所以还需要将图片裁剪成相同大小(img_W和img_H)。有些程序员使用resize_image_with_crop_or_pad方法来裁剪图片,这种方法是从图像中心向四周裁剪,如果图片超过规定尺寸,最后只会剩中间区域的一部分,可能一只狗只剩下躯干,头都不见了,用这样的图片训练结果肯定会受到影响。所以这里稍微改动了一下,使用resize_images对图像进行缩放,而不是裁剪,采用NEAREST_NEIGHBOR插值方法。
然后用tf.train.batch方法获取batch,还有一种方法是tf.train.shuffle_batch,因为之前已经乱序过了,这里用普通的batch函数。
最后将得到的image_batch和label_batch返回,image_batch是一个4D的tensor,即[batch, width, height, channels],label_batch是一个1D的tensor,即[batch]。
可以用下面的代码测试获取图片是否成功,因为之前将图片转为float32了,因此这里imshow出来的图片色彩会有点奇怪,因为本来imshow是显示uint8类型的数据(灰度值在uint8类型下是0至255,转为float32后会超出这个范围,所以色彩有点奇怪),不过这不影响后面模型的训练:
import matplotlib.pyplot as plt
BATCH_SIZE = 2
CAPACITY = 256
IMG_W = 208
IMG_H = 208
train_dir = "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])
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)
以下仿照TensorFlow的官方例程cifar-10的网络结构来编写的,就是两个卷积层(每个卷积层后加一个池化层),两个全连接层,最后使用softmax输出分类结果:
import tensorflow as tf
def inference(images, batch_size, n_classes):
# conv1, shape = [kernel_size, kernel_size, channels, kernel_numbers]
with tf.variable_scope("conv1") as scope:
weights = tf.get_variable("weights", shape=[3, 3, 3, 16], dtype=tf.float32, \
initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32))
biases = tf.get_variable("biases", shape=[16], dtype=tf.float32, initializer=tf.constant_initializer(0.1))
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="conv1")
with tf.variable_scope("pooling1_lrn") as scope: # pool1 && norm1
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')
with tf.variable_scope("conv2") as scope: # conv2
weights = tf.get_variable("weights", shape=[3, 3, 16, 16], dtype=tf.float32, \
initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32))
biases = tf.get_variable("biases", shape=[16], dtype=tf.float32, initializer=tf.constant_initializer(0.1))
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")
with tf.variable_scope("pooling2_lrn") as scope: # pool2 && norm2
pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding="SAME", name="pooling2")
norm2 = tf.nn.lrn(pool2, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2')
with tf.variable_scope("fc1") as scope: # full-connect1
reshape = tf.reshape(norm2, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value
weights = tf.get_variable("weights", shape=[dim, 128], dtype=tf.float32, \
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable("biases", shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(0.1))
fc1 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name="fc1")
with tf.variable_scope("fc2") as scope: # full_connect2
weights = tf.get_variable("weights", shape=[128, 128], dtype=tf.float32, \
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable("biases", shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(0.1))
fc2 = tf.nn.relu(tf.matmul(fc1, weights) + biases, name="fc2")
with tf.variable_scope("softmax_linear") as scope: # softmax
weights = tf.get_variable("weights", shape=[128, n_classes], dtype=tf.float32, \
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable("biases", shape=[n_classes], dtype=tf.float32, initializer=tf.constant_initializer(0.1))
softmax_linear = tf.add(tf.matmul(fc2, weights), biases, name="softmax_linear")
return softmax_linear
发现程序里面有很多with tf.variable_scope("name")的语句,这其实是TensorFlow中的变量作用域机制,目的是有效便捷地管理需要的变量。变量作用域机制在TensorFlow中主要由两部分组成:
tf.get_variable(<name>, <shape>, <initializer>):创建一个变量。tf.variable_scope(<scope_name>):指定命名空间。
如果需要共享变量,需要通过reuse_variables方法来指定。
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
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
函数losses用于计算训练过程中的loss,这里输入参数logtis是函数inference的输出,代表图片对猫和狗的预测概率,labels则是图片对应的标签。
通过在程序中设置断点,查看logtis的值,结果如下图所示,一个数值代表属于猫的概率,一个数值代表属于狗的概率,两者的和为1:

函数tf.nn.sparse_sotfmax_cross_entropy_with_logtis是将稀疏表示的label与输出层计算出来结果做对比。然后因为训练的时候是16张图片一个batch,所以再用tf.reduce_mean求一下平均值,就得到了这个batch的平均loss。对于training(loss, learning_rate),loss是训练的loss,learning_rate是学习率,使用AdamOptimizer优化器来使loss朝着变小的方向优化。evaluation(logits, labels)的功能是在训练过程中实时监测验证数据的准确率,达到反映训练效果的作用。
·合作、交流请关注:公众号「机器学习爱好者社区」(ML_shequ)
·转载请添加微信:yimudeguo
