综合实验---基于卷积神经网络的目标分类案例
文章目录
- 一、卷积神经网络(CNN)
- 1.1 卷积
- 1.2 前馈神经网络
- 1.3 卷积神经网络(CNN)的组成
- 输入层:
- 卷积层(包含有卷积核、卷积层参数、激励函数):
- 池化层:
- 全连接层:
- 输出层:
- 二、猫狗数据分类建模
- 2.1 猫狗图像预处理
- 2.2 猫狗分类的实例——基准模型
- 2.3 数据增强
- 3.4 dropout 层
一、卷积神经网络(CNN)
卷积神经网络(Convolutional Neural Networks, CNN)是一类包含卷积计算且具有深度结构的前馈神经网络(Feedforward Neural Networks),是深度学习(deep learning)的代表算法之一。
顾名思义,就是将卷积与前馈神经网络结合,所衍生出来的一种深度学习算法
1.1 卷积
1、简单定义:设 f(x),g(x)是R1上的两个可积函数,作积分:∫−∞∞f(τ)g(x−τ)dτ
称为函数 f与g的卷积,记为: f ( x ) ∗ g ( x ) = h ( x )
2、卷积与傅里叶变换有着密切的关系:两函数的傅里叶变换的乘积 = 它们卷积后的傅里叶变换(能简化傅里叶分析)
3、h(x)要比f(x)、g(x)更光滑:特别当 g(x)为具有紧致集的光滑函数, f(x)为局部可积时,它们的卷积h(x)也是光滑函数;利用这一性质,对于任意的可积函数 f ( x ) f(x) f(x),都可以简单地构造出一列逼近于f(x)的光滑函数列fs,这种方法称为函数的光滑化或正则化。
4、卷积的概念还可以推广到数列、测度以及广义函数上去。
1.2 前馈神经网络
前馈神经网络是一种最简单的神经网络,各神经元分层排列。每个神经元只与前一层的神经元相连。接收前一层的输出,并输出给下一层.各层间没有反馈。
其中每一层包含若干个神经元,各神经元可以接收前一层神经元的信号,并产生输出到下一层。第0层叫输入层,最后一层叫输出层,其他中间层叫做隐含层(或隐藏层、隐层),隐层可以是一层。也可以是多层。
一个典型的多层前馈神经网络如下图所示:
1.3 卷积神经网络(CNN)的组成
卷积神经网络是在前馈神经网络的隐藏层做的改变,它的隐藏层包括卷积层、池化层、全连接层三部分。
输入层:
卷积神经网络的输入层可以处理多维数据,由于卷积神经网络在计算机视觉领域应用较广,因此许多研究在介绍其结构时预先假设了三维输入数据,即平面上的二维像素点和 RGB 通道
卷积层(包含有卷积核、卷积层参数、激励函数):
使用卷积核进行特征提取和特征映射。
卷积核:类似于一个前馈神经网络的神经元,组成卷积核的每个元素都对应一个权重系数核一个偏差量,含义可类比视觉皮肤细胞的感受野
卷积层参数:包括卷积核大小、步长、填充,三者共同决定了卷积层输出特征图的尺寸,是卷积神经网络的超参数
激励函数:协助表达复杂的特征,类似于其它深度学习算法
池化层:
在卷积层进行特征提取后,输出的特征图会被传递至池化层进行特征选择和信息过滤。
池化层包含预设定的池化函数,其功能是将特征图中单个点的结果替换为其相邻区域的特征图统计量。
池化层选取池化区域与卷积核扫描特征图步骤相同,由池化大小、步长和填充控制。
最大池化——>它只是输出在区域中观察到的最大输入值。
均值池化——>它只是输出在区域中观察到的平均输入值。
两者最大区别在于卷积核的不同(池化是一种特殊的卷积过程)。
全连接层:
卷积神经网络中的全连接层等价于传统前馈神经网络中的隐含层(每个神经元与上一层的所有神经元相连)。
全连接层位于卷积神经网络隐含层的最后部分,并只向其它全连接层传递信号。
特征图在全连接层中会失去空间拓扑结构,被展开为向量并通过激励函数。
全连接层的作用则是对提取的特征进行非线性组合以得到输出,即全连接层本身不被期望具有特征提取能力,而是试图利用现有的高阶特征完成学习目标。
输出层:
卷积神经网络中输出层的上游通常是全连接层,因此其结构和工作原理与传统前馈神经网络中的输出层相同。
对于图像分类问题,输出层使用逻辑函数或归一化指数函数(softmax function)输出分类标签。
在物体识别(object detection)问题中,输出层可设计为输出物体的中心坐标、大小和分类。
在图像语义分割中,输出层直接输出每个像素的分类结果。
二、猫狗数据分类建模
数据集下载完毕后,解压缩,并放在一个没有中文路径下,如下图所示:
2.1 猫狗图像预处理
对猫狗图像进行分类,代码如下:
import os, shutil
# 原始目录所在的路径
original_dataset_dir = 'E:\\Cat_And_Dog\\train\\'
# 数据集分类后的目录
base_dir = 'E:\\Cat_And_Dog\\train1'
os.mkdir(base_dir)
# # 训练、验证、测试数据集的目录
train_dir = os.path.join(base_dir, 'train')
os.mkdir(train_dir)
validation_dir = os.path.join(base_dir, 'validation')
os.mkdir(validation_dir)
test_dir = os.path.join(base_dir, 'test')
os.mkdir(test_dir)
# 猫训练图片所在目录
train_cats_dir = os.path.join(train_dir, 'cats')
os.mkdir(train_cats_dir)
# 狗训练图片所在目录
train_dogs_dir = os.path.join(train_dir, 'dogs')
os.mkdir(train_dogs_dir)
# 猫验证图片所在目录
validation_cats_dir = os.path.join(validation_dir, 'cats')
os.mkdir(validation_cats_dir)
# 狗验证数据集所在目录
validation_dogs_dir = os.path.join(validation_dir, 'dogs')
os.mkdir(validation_dogs_dir)
# 猫测试数据集所在目录
test_cats_dir = os.path.join(test_dir, 'cats')
os.mkdir(test_cats_dir)
# 狗测试数据集所在目录
test_dogs_dir = os.path.join(test_dir, 'dogs')
os.mkdir(test_dogs_dir)
# 将前1000张猫图像复制到train_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(train_cats_dir, fname)
shutil.copyfile(src, dst)
# 将下500张猫图像复制到validation_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1000, 1500)]
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(validation_cats_dir, fname)
shutil.copyfile(src, dst)
# 将下500张猫图像复制到test_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1500, 2000)]
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(test_cats_dir, fname)
shutil.copyfile(src, dst)
# 将前1000张狗图像复制到train_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(train_dogs_dir, fname)
shutil.copyfile(src, dst)
# 将下500张狗图像复制到validation_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1000, 1500)]
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(validation_dogs_dir, fname)
shutil.copyfile(src, dst)
# 将下500张狗图像复制到test_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1500, 2000)]
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(test_dogs_dir, fname)
shutil.copyfile(src, dst)
查看分类后,对应目录下的图片数量:
#输出数据集对应目录下图片数量
print('total training cat images:', len(os.listdir(train_cats_dir)))
print('total training dog images:', len(os.listdir(train_dogs_dir)))
print('total validation cat images:', len(os.listdir(validation_cats_dir)))
print('total validation dog images:', len(os.listdir(validation_dogs_dir)))
print('total test cat images:', len(os.listdir(test_cats_dir)))
print('total test dog images:', len(os.listdir(test_dogs_dir)))
猫狗训练图片各 1000 张,验证图片各 500 张,测试图片各 500 张。
2.2 猫狗分类的实例——基准模型
第一步:构建网络模型:
#网络模型构建
from keras import layers
from keras import models
#keras的序贯模型
model = models.Sequential()
#卷积层,卷积核是3*3,激活函数relu
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(150, 150, 3)))
#最大池化层
model.add(layers.MaxPooling2D((2, 2)))
#卷积层,卷积核2*2,激活函数relu
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
#最大池化层
model.add(layers.MaxPooling2D((2, 2)))
#卷积层,卷积核是3*3,激活函数relu
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
#最大池化层
model.add(layers.MaxPooling2D((2, 2)))
#卷积层,卷积核是3*3,激活函数relu
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
#最大池化层
model.add(layers.MaxPooling2D((2, 2)))
#flatten层,用于将多维的输入一维化,用于卷积层和全连接层的过渡
model.add(layers.Flatten())
#全连接,激活函数relu
model.add(layers.Dense(512, activation='relu'))
#全连接,激活函数sigmoid
model.add(layers.Dense(1, activation='sigmoid'))
第二步:配置优化器:
loss:计算损失,这里用的是交叉熵损失
metrics:列表,包含评估模型在训练和测试时的性能的指标
from keras import optimizers
model.compile(loss='binary_crossentropy',
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=['acc'])
第三步:图片格式转化
所有图片(2000张)重设尺寸大小为 150x150 大小,并使用 ImageDataGenerator 工具将本地图片 .jpg 格式转化成 RGB 像素网格,再转化成浮点张量上传到网络上。
from keras.preprocessing.image import ImageDataGenerator
# 所有图像将按1/255重新缩放
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
# 这是目标目录
train_dir,
# 所有图像将调整为150x150
target_size=(150, 150),
batch_size=20,
# 因为我们使用二元交叉熵损失,我们需要二元标签
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(150, 150),
batch_size=20,
class_mode='binary')
输出结果:
#查看上面对于图片预处理的处理结果
for data_batch, labels_batch in train_generator:
print('data batch shape:', data_batch.shape)
print('labels batch shape:', labels_batch.shape)
break
输出结果如下:
第四步:开始训练模型
#模型训练过程
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=30,
validation_data=validation_generator,
validation_steps=50)
第五步:保存模型。
#保存训练得到的的模型
model.save('G:\\Cat_And_Dog\\kaggle\\cats_and_dogs_small_1.h5')
第六步:结果可视化
#对于模型进行评估,查看预测的准确性
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
2.3 数据增强
什么是数据增强?数据集增强主要是为了减少网络的过拟合现象,通过对训练图片进行变换可以得到泛化能力更强的网络,更好的适应应用场景。
图像数据生成器增强数据:
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
参数解释:
rotation_range:一个角度值(0-180),在这个范围内可以随机旋转图片
width_shift和height_shift: 范围(作为总宽度或高度的一部分),在其中可以随机地垂直或水平地转换图片
shear_range:用于随机应用剪切转换
zoom_range:用于在图片内部随机缩放
horizontal_flip:用于水平随机翻转一半的图像——当没有假设水平不对称时(例如真实世界的图片)
fill_mode:用于填充新创建像素的策略,它可以在旋转或宽度/高度移动之后出现
查看数据增强后的效果:
import matplotlib.pyplot as plt
# This is module with image preprocessing utilities
from keras.preprocessing import image
fnames = [os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir)]
# We pick one image to "augment"
img_path = fnames[3]
# Read the image and resize it
img = image.load_img(img_path, target_size=(150, 150))
# Convert it to a Numpy array with shape (150, 150, 3)
x = image.img_to_array(img)
# Reshape it to (1, 150, 150, 3)
x = x.reshape((1,) + x.shape)
# The .flow() command below generates batches of randomly transformed images.
# It will loop indefinitely, so we need to `break` the loop at some point!
i = 0
for batch in datagen.flow(x, batch_size=1):
plt.figure(i)
imgplot = plt.imshow(image.array_to_img(batch[0]))
i += 1
if i % 4 == 0:
break
plt.show()
结果如下
图片格式转化
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,)
# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
# This is the target directory
train_dir,
# All images will be resized to 150x150
target_size=(150, 150),
batch_size=32,
# Since we use binary_crossentropy loss, we need binary labels
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(150, 150),
batch_size=32,
class_mode='binary')
开始训练并保存结果。
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=100,
validation_data=validation_generator,
validation_steps=50)
model.save('E:\\Cat_And_Dog\\kaggle\\cats_and_dogs_small_2.h5')
结果如下:
结果可视化:
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
3.4 dropout 层
什么是dropout层?Dropout层在神经网络层当中是用来干嘛的呢?它是一种可以用于减少神经网络过拟合的结构
在数据增强的基础上,再添加一个 dropout 层。
#退出层
model.add(layers.Dropout(0.5))
再次训练模型,查看训练结果如下:
相比于只使用数据增强的效果来看,额外添加一层 dropout 层,仔细对比,可以发现训练曲线更加紧密地跟踪验证曲线,波动的幅度也降低了些。