基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别

文章目录

  • 一、环境配置
    • 1、安装Anaconda
    • 2、配置TensorFlow、Keras
  • 二、猫狗数据集分类建模
    • 3.1 猫狗图像预处理
    • 3.2 猫狗分类的实例——基准模型
      • 3.1 构建神经网络
      • 3.2 配置优化器
      • 3.3 图片格式转化
      • 3.4 训练模型
      • 3.5 保存模型
      • 3.6 可视化
  • 三、数据增强
  • 四、dropout 层
  • 五、参考资料

一、环境配置

1、安装Anaconda

具体可参考此博文

2、配置TensorFlow、Keras

创建虚拟环境:
输入下面命令

conda create -n tf1 python=3.6
#tf1是自己为创建虚拟环境取的名字,后面python的版本可以根据自己需求进行选择
  • 如果命令无效,可参考此博文
  • 如果提示:CondaSSLError: OpenSSL appears to be unavailable on this machine.可参考此博文

运行结果如下:
基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第1张图片

激活环境:

activate
conda activate tf1

安装 tensorflow、keras 库:
在新建的虚拟环境 tf1 内,使用以下命令安装两个库:

pip install tensorflow==1.14.0 -i “https://pypi.doubanio.com/simple/”
pip install keras==2.2.5 -i “https://pypi.doubanio.com/simple/”

结果如下:

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第2张图片

安装 nb_conda_kernels 包:

conda install nb_conda_kernels

安装 1.16.4 版本的 numpy:

pip install numpy==1.16.4 -i "https://pypi.doubanio.com/simple/"

结果如下:

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第3张图片
安装 pillow 库:

pip install pillow -i “https://pypi.doubanio.com/simple/

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第4张图片
安装matplotlib库:

pip install matplotlib -i “https://pypi.doubanio.com/simple/

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第5张图片

打开 Jupyter Notebook((tf1)环境下的):

点击【New】→【Python[tf1环境下的]】创建 python 文件:

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第6张图片

二、猫狗数据集分类建模

  • 猫狗图片数据集下载:https://pan.baidu.com/s/1f-MvZl7_J6DF7P9CGBY3SQ—提取码:ruyn
  • 数据集下载完毕后,解压缩,并放在一个没有中文路径下,如下图所示:
    基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第7张图片

3.1 猫狗图像预处理

  • 对猫狗图像进行分类,代码如下:
import os, shutil 
# 原始目录所在的路径
original_dataset_dir = 'D:\\Cat_And_Dog\\train\\'

# 数据集分类后的目录
base_dir = 'D:\\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)

运行效果如下:

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第8张图片

  • 查看分类后,对应目录下的图片数量:
#输出数据集对应目录下图片数量
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)))

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第9张图片
可从上图看出猫狗训练图片各 1000 张,验证图片各 500 张,测试图片各 500 张。

3.2 猫狗分类的实例——基准模型

3.1 构建神经网络

#网络模型构建
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'))
  • 查看模型各层参数情况:
#输出模型各层的参数状况
model.summary()

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第10张图片

3.2 配置优化器

  • loss:计算损失,这里用的是交叉熵损失
  • metrics:列表,包含评估模型在训练和测试时的性能的指标
from keras import optimizers

model.compile(loss='binary_crossentropy',
              optimizer=optimizers.RMSprop(lr=1e-4),
              metrics=['acc'])

3.3 图片格式转化

  • 所有图片(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')

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第11张图片

  • 查看上述图像预处理生成其中的输出
#查看上面对于图片预处理的处理结果
for data_batch, labels_batch in train_generator:
    print('data batch shape:', data_batch.shape)
    print('labels batch shape:', labels_batch.shape)
    break

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第12张图片

3.4 训练模型

#模型训练过程
history = model.fit_generator(
      train_generator,
      steps_per_epoch=100,
      epochs=30,
      validation_data=validation_generator,
      validation_steps=50)

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第13张图片

3.5 保存模型

#保存训练得到的的模型
model.save('D:\\Cat_And_Dog\\kaggle\\cats_and_dogs_small_1.h5')

3.6 可视化

#对于模型进行评估,查看预测的准确性
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()

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第14张图片

  • 训练结果如上图所示,很明显模型上来就过拟合了,主要原因是数据不够,或者说相对于数据量,模型过复杂(训练损失在第30个epoch就降为0了),训练精度随着时间线性增长,直到接近100%,而我们的验证精度停留在70-72%。我们的验证损失在5个epoch后达到最小,然后停止,而训练损失继续线性下降,直到接近0。

  • 这里先解释下什么是过拟合?
    举个例子:我们设计了一个模型来判断 一件物品是否为树叶。喂养这个模型的数据集中含有几张带有尖刺边缘的树叶。模型的设计者希望模型能满足每一个训练数据,模型就将尖刺边缘也纳入了参数中。当我们测试这个模型的泛化性能时,就会发现效果很差,因为模型钻牛角尖,它认为树叶必须带有尖刺边缘,所以它排除了所有没有带有尖刺边缘的树叶,但事实上,我们知道树叶并不一定带有尖刺边缘。

  • 过拟合常见解决方法:
    (1)在神经网络模型中,可使用权值衰减的方法,即每次迭代过程中以某个小因子降低每个权值。
    (2)选取合适的停止训练标准,使对机器的训练在合适的程度;
    (3)保留验证数据集,对训练成果进行验证;
    (4)获取额外数据进行交叉验证;
    (5)正则化,即在进行目标函数或代价函数优化时,在目标函数或代价函数后面加上一个正则项,一般有L1正则与L2正则等。

  • 不过接下来将使用一种新的方法,专门针对计算机视觉,在深度学习模型处理图像时几乎普遍使用——数据增强。

三、数据增强

数据集增强主要是为了减少网络的过拟合现象,通过对训练图片进行变换可以得到泛化能力更强的网络,更好的适应应用场景。

常用的数据增强方法有:
基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第15张图片
重新构建模型:

  • 我们重新建一个 .ipynb 文件,重新开始建模
  • 首先猫狗图像预处理,只不过这里将分类好的数据集放在 train2 文件夹中,其它的都一样
    基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第16张图片
  • 然后配置网络模型、构建优化器,然后进行数据增强,代码如下:

图像数据生成器增强数据

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')
  • 参数解释:
    基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第17张图片
  • 查看数据增强后的效果:
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()

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第18张图片
基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第19张图片

  • 图片格式转换
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')

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第20张图片

  • 可视化
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()

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第21张图片

  • 由于数据量的增加,对比基准模型,可以很明显的观察到曲线没有过度拟合了,训练曲线紧密地跟踪验证曲线,这也就是数据增强带来的影响,但是可以发现它的波动幅度还是比较大的。
  • 下面在此数据增强的基础上,再增加一层 dropout 层,再来训练看看。

四、dropout 层

什么是dropout层?

Dropout层在神经网络层当中是用来干嘛的呢?它是一种可以用于减少神经网络过拟合的结构,那么它具体是怎么实现的呢?
假设下图是我们用来训练的原始神经网络:
基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第22张图片
基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第23张图片

实现:
在构建网络模型时额外加入以下代码:

#退出层
model.add(layers.Dropout(0.5))

基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第24张图片
运行结果如下:
基于Tensorflow和Keras实现卷积神经网络CNN并进行猫狗识别_第25张图片

五、参考资料

https://blog.csdn.net/ssj925319/article/details/117787737

你可能感兴趣的:(tensorflow,keras,cnn)