本次我们将会使用直接使用tensorflow中自带的模型对cifar10数据集进行分类、识别,通过前面的代码书写,我们了解了怎样构架一个卷积神经网络,但是大多数情况都是可以直接使用模型,然后微调参数来实现的。
下面我们通过cifar10来熟悉,正式训练一个神经网络的过程:
cifar数据集下载地址 http://www.cs.toronto.edu/~kriz/cifar.html
tf官方示例代码 https://github.com/tensorflow/models/tree/master/tutorials/image/cifar10
同时也将第一次使用tensorboard
一、数据集分析
下载完成数据集后解压后得到这样几个文件
通过前面的学习 我们大致也知道了 meta文件类似于一个索引 记录了每个类别的英文名称 而在我们上次训练的ckpt文件中记录的是模型的地址 而下面几个文件顾名思义 我们也知道 是5个批次的训练集 和一个测试集 在这个数据集中通过阅读readme后 我们知道这个数据集 一个样本是3073个字节组成 其中第一个字节代表分类
二、使用tf训练cifar10数据集
在前面的minist数据集和猫狗大战的数据集中 我们其实都是没有过多的涉及到数据增强这个概念的 在深度学习中 数据量越大对于模型的训练样本就会是越优秀 我们需要使用数据增强的方法 来获得更多不同的数据集 常见的数据增强方法
旋转 | 反射变换(Rotation/reflection): 随机旋转图像一定角度; 改变图像内容的朝向;
翻转变换(flip): 沿着水平或者垂直方向翻转图像;
缩放变换(zoom): 按照一定的比例放大或者缩小图像;
平移变换(shift): 在图像平面上对图像以一定方式进行平移;
可以采用随机或人为定义的方式指定平移范围和平移步长, 沿水平或竖直方向进行平移. 改变图像内容的位置;
尺度变换(scale): 对图像按照指定的尺度因子, 进行放大或缩小; 或者参照SIFT特征提取思想, 利用指定的尺度因子对图像滤波构造尺度空间. 改变图像内容的大小或模糊程度;
对比度变换(contrast): 在图像的HSV颜色空间,改变饱和度S和V亮度分量,保持色调H不变. 对每个像素的S和V分量进行指数运算(指数因子在0.25到4之间), 增加光照变化;
噪声扰动(noise): 对图像的每个像素RGB进行随机扰动, 常用的噪声模式是椒盐噪声和高斯噪声;
颜色变化:在图像通道上添加随机扰动。
输入图像随机选择一块区域涂黑,参考《Random Erasing Data Augmentation》
但是值得注意的是数据增强的过程中不能改变数据的原有标签 例如minist中 6 和 9 总之数据增强能够增强模型的泛化能力 避免过拟合 (过拟合类似于学习的过于呆板 不能够变通 导致的预测结果不尽人意)
下载实例代码后我们可以看到这样几个文件
打开input文件可以看到:
这段代码的意义就是在于对于数据增强 通过以下的几步:
1. reshaped_image 将原始图片从32 * 32 剪裁至 24 * 24 而这个剪裁的方式是随机的可能是图像的任何一个位置
2.对图片进行水平翻转 但是翻转概率为百分之五十
3.将对比度和亮度做出随机改变
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Routine for decoding the CIFAR-10 binary file format."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import tensorflow_datasets as tfds
# Process images of this size. Note that this differs from the original CIFAR
# image size of 32 x 32. If one alters this number, then the entire model
# architecture will change and any model would need to be retrained.
IMAGE_SIZE = 24
# Global constants describing the CIFAR-10 data set.
NUM_CLASSES = 10
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 50000
NUM_EXAMPLES_PER_EPOCH_FOR_EVAL = 10000
def _get_images_labels(batch_size, split, distords=False):
"""Returns Dataset for given split."""
dataset = tfds.load(name='cifar10', split=split)
scope = 'data_augmentation' if distords else 'input'
with tf.name_scope(scope):
dataset = dataset.map(DataPreprocessor(distords), num_parallel_calls=10)
# Dataset is small enough to be fully loaded on memory:
dataset = dataset.prefetch(-1)
dataset = dataset.repeat().batch(batch_size)
iterator = dataset.make_one_shot_iterator()
images_labels = iterat