<p>图像分类是根据图像的语义信息将不同类别图像区分开来,是计算机视觉中重要的基本问题</p>
<p>猫狗分类属于图像分类中的粗粒度分类问题</p>
<p><img alt="" class="blockcode" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-c5b65b1018bfa0865e6fabf4a425c0ec"></p>
<p>实践总体过程和步骤如下图</p>
<p><img alt="" class="blockcode" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8bd8707094571f45a0941924868ae2d6"></p>
<p><strong>首先导入必要的包</strong></p>
<p>paddle.fluid--->PaddlePaddle深度学习框架</p>
<p>os------------->python的模块,可使用该模块对操作系统进行操作</p>
<p>In[1]</p>
<pre class="blockcode"><code>#导入需要的包
import paddle as paddle
import paddle.fluid as fluid
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import os</code></pre>
<h1><strong>Step1:准备数据</strong></h1>
<p><strong>(1)数据集介绍</strong></p>
<p>我们使用CIFAR10数据集。CIFAR10数据集包含60,000张32x32的彩色图片,10个类别,每个类包含6,000张。其中50,000张图片作为训练集,10000张作为验证集。这次我们只对其中的猫和狗两类进行预测。</p>
<p><img alt="" class="blockcode" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-891613efc3f6ecdcec2a95d8b2827724"></p>
<p><strong>(2)train_reader和test_reader</strong></p>
<p>paddle.dataset.cifar.train10()和test10()分别获取cifar训练集和测试集</p>
<p>paddle.reader.shuffle()表示每次缓存BUF_SIZE个数据项,并进行打乱</p>
<p>paddle.batch()表示每BATCH_SIZE组成一个batch</p>
<p><strong>(3)数据集下载</strong></p>
<p>由于本次实践的数据集稍微比较大,以防出现不好下载的问题,为了提高效率,可以用下面的代码进行数据集的下载。</p>
<pre class="blockcode"><code>
!mkdir -p /home/aistudio/.cache/paddle/dataset/cifar/
!wget "http://ai-atest.bj.bcebos.com/cifar-10-python.tar.gz" -O cifar-10-python.tar.gz
!mv cifar-10-python.tar.gz /home/aistudio/.cache/paddle/dataset/cifar/
</code></pre>
<p>In[2]</p>
<pre class="blockcode"><code>!mkdir -p /home/aistudio/.cache/paddle/dataset/cifar/
!wget "http://ai-atest.bj.bcebos.com/cifar-10-python.tar.gz" -O cifar-10-python.tar.gz
!mv cifar-10-python.tar.gz /home/aistudio/.cache/paddle/dataset/cifar/
!ls -a /home/aistudio/.cache/paddle/dataset/cifar/</code></pre>
<pre class="blockcode"><code class="language-html">--2019-06-12 23:04:56-- http://ai-atest.bj.bcebos.com/cifar-10-python.tar.gz
Resolving ai-atest.bj.bcebos.com (ai-atest.bj.bcebos.com)... 100.64.253.37, 100.64.253.38
Connecting to ai-atest.bj.bcebos.com (ai-atest.bj.bcebos.com)|100.64.253.37|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 170498071 (163M) [application/x-gzip]
Saving to: ‘cifar-10-python.tar.gz’
cifar-10-python.tar 100%[===================>] 162.60M 99.7MB/s in 1.6s
2019-06-12 23:04:58 (99.7 MB/s) - ‘cifar-10-python.tar.gz’ saved [170498071/170498071]
. .. cifar-10-python.tar.gz
</code></pre>
<p>In[3]</p>
<pre class="blockcode"><code>BATCH_SIZE = 128
#用于训练的数据提供器
train_reader = paddle.batch(
paddle.reader.shuffle(paddle.dataset.cifar.train10(),
buf_size=128*100),
batch_size=BATCH_SIZE)
#用于测试的数据提供器
test_reader = paddle.batch(
paddle.dataset.cifar.test10(),
batch_size=BATCH_SIZE) </code></pre>
<pre class="blockcode"><code class="language-html"> </code></pre>
<h1><strong>Step2.网络配置</strong></h1>
<p><strong>(1)网络搭建</strong></p>
<p>*** <strong>CNN网络模型</strong></p>
<p>在CNN模型中,卷积神经网络能够更好的利用图像的结构信息。下面定义了一个较简单的卷积神经网络。显示了其结构:输入的二维图像,先经过三次卷积层、池化层和Batchnorm,再经过全连接层,最后使用softmax分类作为输出层。</p>
<p><img alt="" class="blockcode" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-d6e842ba28935b02c920076ed1e8ff1e"></p>
<p><strong>池化</strong>是非线性下采样的一种形式,主要作用是通过减少网络的参数来减小计算量,并且能够在一定程度上控制过拟合。通常在卷积层的后面会加上一个池化层。paddlepaddle池化默认为最大池化。是用不重叠的矩形框将输入层分成不同的区域,对于每个矩形框的数取最大值作为输出</p>
<p><strong>Batchnorm</strong>顾名思义是对每batch个数据同时做一个norm。作用就是在深度神经网络训练过程中使得每一层神经网络的输入保持相同分布的。</p>
<p>In[4]</p>
<pre class="blockcode"><code>def convolutional_neural_network(img):
# 第一个卷积-池化层
conv_pool_1 = fluid.nets.simple_img_conv_pool(
input=img, # 输入图像
filter_size=5, # 滤波器的大小
|
|