Tensorflow 入门学习17.CNN 分辨CIFAR-10数据集

Tensorflow 入门学习17.CNN 分辨CIFAR-10数据集


一、介绍

CIFAR-10 是由神经网络的先驱和大师Hinton的两名学生:Alex Krizhevsky 和 Ilya Sutskever整理的一个基于现实物体,通过所拍摄的照片进行物体识别的数据集。这个数据集项目是为了推广和加速深度学习所创建的。
CIFAR-10的官网链接为:http://www.cs.toronto.edu/~kriz/cifar.html
CIFAR-10分类数据集为60000张32 * 32的彩色图片,总共有10个类别,其中50000张训练集,10000张测试集。

二、代码

本测试基于环境:tensorflow1.13 cuda10.0

1. 准备

下载 cifar10.py, cifar10_input.pygit地址

安装tensorflow-datasets

pip3 install tensorflow-datasets
pip3 install tools

2. 代码

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import tensorflow as tf
import numpy as np
import cifar10_input
import time
import math


def variable_with_weight_loss(shape, std, w1):
    var = tf.Variable(tf.truncated_normal(shape, stddev=std), dtype=tf.float32)
    # 使用tf.truncated_normal截断的正态分布,加上L2的loss,相当于做了一个L2的正则化处理
    # w1:控制L2 loss的大小,tf.nn.l2_loss函数计算weight的L2 loss
    if w1 is not None:
        weight_loss = tf.multiply(tf.nn.l2_loss(var), w1, name="weight_loss")
        tf.add_to_collection("losses", weight_loss)  #加入losses列表
    return var


def loss_func(logits, labels):
    labels = tf.cast(labels, tf.int32)
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels, name="cross_entropy_per_example")
    # softmax和cross entropy loss的计算合在一起
    cross_entropy_mean = tf.reduce_mean(tf.reduce_sum(cross_entropy))
    # 计算cross entropy 均值
    tf.add_to_collection("losses", cross_entropy_mean)
    # 把交叉熵均值加入到losses中
    return tf.add_n(tf.get_collection("losses"), name="total_loss")
    #将整体losses的collection中的全部loss求和,即最终的loss,有cross entropy loss,两个全连接层   中weight的L2 loss


# 设置最大迭代次数
max_steps = 10000
# 设置每次训练的数据大小
batch_size = 128
# 设置数据的存放目录
cifar10_dir = "./cifar-10-batches-bin"
# 训练集
# distored_inputs函数产生训练需要使用的数据,包括特征和其对应的label,返回已经封装好的tensor,每次执行都会生成一个batch_size的数量的样本
images_train, labels_train = cifar10_input.distorted_inputs(batch_size)
# 测试集
images_test, labels_test = cifar10_input.inputs(eval_data=True, batch_size=batch_size)
# 载入数据
image_holder = tf.placeholder(dtype=tf.float32, shape=[batch_size, 24, 24, 3])
# 裁剪后尺寸为24×24,彩色图像通道数为3
label_holder = tf.placeholder(dtype=tf.int32, shape=[batch_size])


# 卷积层 第1层
weight1 = variable_with_weight_loss(shape=[5, 5, 3, 64], std=5e-2, w1=0)
# 使用variable_with_weight_loss函数创建卷积核的参数并进行初始化。5×5的卷积和,3个通道,64个滤波器。weight1初始化函数的标准差为0.05,不进行正则wl(weight loss)设为0
kernel1 = tf.nn.conv2d(image_holder, weight1, [1, 1, 1, 1], padding="SAME")
# tf.nn.conv2d函数对输入进行卷积
bais1 = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[64]))
conv1 = tf.nn.relu(tf.nn.bias_add(kernel1, bais1))
# 最大池化层尺寸为3x3,步长为2x2
pool1 = tf.nn.max_pool(conv1, [1, 3, 3, 1], [1, 2, 2, 1], padding="SAME")
# LRN层模仿生物神经系统的侧抑制机制
norm1 = tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001 / 9, beta=0.75)


# 第二层
weight2 = variable_with_weight_loss(shape=[5, 5, 64, 64], std=5e-2, w1=0)
# 5×5的卷积和,第一个卷积层输出64个通道,64个滤波器
kernel2 = tf.nn.conv2d(norm1, weight2, [1, 1, 1, 1], padding="SAME")
bais2 = tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[64]))
# 初始化为0.1
conv2 = tf.nn.relu(tf.nn.bias_add(kernel2, bais2))
norm2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.01 / 9, beta=0.75)
pool2 = tf.nn.max_pool(norm2, [1, 3, 3, 1], [1, 2, 2, 1], padding="SAME")


# 全连接层第1层
reshape = tf.reshape(pool2, [batch_size, -1])  # 将数据变为1D数据
dim = reshape.get_shape()[1].value  # 获取维度
weight3 = variable_with_weight_loss([dim, 384], std=0.04, w1=0.004)
bais3 = tf.Variable(tf.constant(0.1, shape=[384], dtype=tf.float32))
local3 = tf.nn.relu(tf.matmul(reshape, weight3) + bais3)

# 第2、3层
weight4 = variable_with_weight_loss([384, 192], std=0.04, w1=0.004)
bais4 = tf.Variable(tf.constant(0.1, shape=[192], dtype=tf.float32))
local4 = tf.nn.relu(tf.matmul(local3, weight4) + bais4)

weight5 = variable_with_weight_loss([192, 10], std=1 / 192.0, w1=0)
bais5 = tf.Variable(tf.constant(0.0, shape=[10], dtype=tf.float32))
logits = tf.add(tf.matmul(local4, weight5), bais5)

# 数据准备
# 获取损失函数
loss = loss_func(logits, label_holder)
# 设置优化算法使得成本最小
train_step = tf.train.AdamOptimizer(1e-3).minimize(loss)
# 获取最高类的分类准确率,取top1作为衡量标准
top_k_op = tf.nn.in_top_k(logits, label_holder, 1)
# 创建会话
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
# 动线程,在图像数据增强队列例使用了16个线程进行加速。
tf.train.start_queue_runners()


# 训练过程
for step in range(max_steps):
        start_time = time.time()
        # 获得一个batch的训练数据
        images_batch, labels_batch = sess.run([images_train, labels_train])
        # 将batch的数据传入train_op和loss的计算
        _, loss_value = sess.run([train_step, loss], feed_dict={image_holder:images_batch, label_holder:labels_batch})
        # 获取计算时间
        duration = time.time() - start_time
        if step % 1000 == 0:
            # 计算每秒处理多少张图片
            per_images_second = batch_size / duration
            # 获取计算一个batch需要的时间
            sec_per_batch = float(duration)
            print("step:%d,duration:%.3f,per_images_second:%.2f,loss:%.3f" % (step, duration, per_images_second, loss_value))

# 计算准确率
num_examples = 10000
num_iter = int(math.ceil(num_examples / batch_size))
true_count = 0
total_sample_count = num_iter * batch_size
step = 0
while step < num_iter:
    images_batch, labels_batch = sess.run([images_test, labels_test])
    pred = sess.run([top_k_op], feed_dict={image_holder: images_batch, label_holder: labels_batch})
    true_count += np.sum(pred)
    step += 1
# 计算测试集的准确率
precision = true_count / total_sample_count
print("test accuracy:%.3f" % precision)

运行结果:

step:0,duration:1.353,per_images_second:94.59,loss:296.814
step:1000,duration:0.517,per_images_second:247.70,loss:148.668
step:2000,duration:0.523,per_images_second:244.51,loss:117.796
step:3000,duration:0.529,per_images_second:242.06,loss:111.743
step:4000,duration:0.524,per_images_second:244.45,loss:106.063
step:5000,duration:0.529,per_images_second:241.97,loss:112.751
step:6000,duration:0.524,per_images_second:244.48,loss:122.578
step:7000,duration:0.522,per_images_second:245.02,loss:89.198
step:8000,duration:0.522,per_images_second:245.21,loss:70.143
step:9000,duration:0.523,per_images_second:244.63,loss:82.841
test accuracy:0.853
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程圈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值