import tensorflow as tf
import numpy as np
import math
import time
import cifar10
import cifar10_input
"""
Created on Tue Nov 27 17:31:35 2018
@author: zhen
"""
max_steps = 1000
# 下载cifar10数据集的默认路径
batch_size = 128
data_dir = "C:/Users/zhen/.spyder-py3/cifar/cifar-10/cifar-10-batches/cifar-10-batches-bin" def variable_with_weight_losses(shape, stddev, wl):
# 定义初始化weights的函数
var = tf.Variable(tf.truncated_normal(shape, stddev=stddev))
if wl is not None:
weight_loss = tf.multiply(tf.nn.l2_loss(var), wl, name='weight_loss')
tf.add_to_collection("losses", weight_loss)
return var # 下载数据
cifar10.maybe_download_and_extract()
# 加载训练数据
images_train, labels_train = cifar10_input.distorted_inputs(data_dir=data_dir, batch_size=batch_size)
# 生成测试数据
images_test, labels_test = cifar10_input.inputs(eval_data=True, data_dir=data_dir, batch_size=batch_size) image_holder = tf.placeholder(tf.float32, [batch_size, 24, 24, 3])
label_holder = tf.placeholder(tf.int32, [batch_size]) # 设置第一层卷积层
weight_1 = variable_with_weight_losses(shape=[5, 5, 3, 64], stddev=5e-2, wl=0.0)
kernel_1 = tf.nn.conv2d(image_holder, filter=weight_1, strides=[1, 1, 1, 1], padding='SAME')
bias_1 = tf.Variable(tf.constant(0.0, shape=[64]))
# 卷积
conv_1 = tf.nn.relu(tf.nn.bias_add(kernel_1, bias_1))
# 池化
pool_1 = tf.nn.max_pool(conv_1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
norm_1 = tf.nn.lrn(pool_1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75) # 设置第二层卷积层
weight_2 = variable_with_weight_losses(shape=[5, 5, 64, 64], stddev=5e-2, wl=0.0)
kernel_2 = tf.nn.conv2d(norm_1, weight_2, [1, 1, 1, 1], padding='SAME')
bias_2 = tf.Variable(tf.constant(0.1, shape=[64])) conv_2 = tf.nn.relu(tf.nn.bias_add(kernel_2, bias_2))
norm_2 = tf.nn.lrn(conv_2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
pool_2 = tf.nn.max_pool(norm_2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME') # 全连接层
reshape = tf.reshape(pool_2, [batch_size, -1])
dim = reshape.get_shape()[1].value weight_3 = variable_with_weight_losses(shape=[dim, 384], stddev=0.04, wl=0.004)
bias_3 = tf.Variable(tf.constant(0.1, shape=[384]))
local_3 = tf.nn.relu(tf.matmul(reshape, weight_3) + bias_3) # 第二层全连接层
weight_4 = variable_with_weight_losses(shape=[384, 192], stddev=0.04, wl=0.004)
bias_4 = tf.Variable(tf.constant(0.1, shape=[192]))
local_4 = tf.nn.relu(tf.matmul(local_3, weight_4) + bias_4) # 结果层
weight_5 = variable_with_weight_losses(shape=[192, 10], stddev=1/192.0, wl=0.0)
bias_5 = tf.Variable(tf.constant(0.0, shape=[10]))
logits = tf.add(tf.matmul(local_4, weight_5), bias_5) def loss(logits, labels):
labels = tf.cast(labels, tf.int64)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits,
labels=labels,
name="cross_entropy_per_example"
)
cross_entropy_mean = tf.reduce_mean(cross_entropy, name="cross_entropy")
tf.add_to_collection("losses", cross_entropy_mean)
return tf.add_n(tf.get_collection("losses"), name="total_loss") loss = loss(logits=logits, labels=label_holder)
train_op = tf.train.AdamOptimizer(1e-3).minimize(loss)
top_k_op = tf.nn.in_top_k(logits, label_holder, 1)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
tf.train.start_queue_runners() # 训练
for step in range(max_steps):
start_time = time.time()
image_batch, label_batch = sess.run([images_train, labels_train])
_, loss_value = sess.run([train_op, loss], feed_dict={image_holder: image_batch, label_holder: label_batch})
duration = time.time() - start_time if step % 10 == 0:
examples_per_sec = batch_size / duration
sec_per_batch = float(duration) format_str = "step %d, loss =%.2f (%.1f examples/sec; %.3f sec/batch"
print(format_str % (step, loss_value, examples_per_sec, sec_per_batch)) # 评估模型
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:
image_batch, label_batch = sess.run([images_test, labels_test])
predictions = sess.run([top_k_op], feed_dict={image_holder: image_batch, label_holder: label_batch})
true_count += np.sum(predictions)
step += 1 precision = true_count / total_sample_count
print("precision @ 1 = %.3f" % precision)

过程:

Filling queue with 20000 CIFAR images before starting to train. This will take a few minutes.
step 0, loss =4.68 (19.0 examples/sec; 6.734 sec/batch
step 10, loss =3.58 (62.1 examples/sec; 2.062 sec/batch
step 20, loss =3.09 (62.5 examples/sec; 2.047 sec/batch
step 30, loss =2.77 (62.5 examples/sec; 2.047 sec/batch
step 40, loss =2.48 (62.5 examples/sec; 2.047 sec/batch
step 50, loss =2.36 (62.5 examples/sec; 2.047 sec/batch
step 60, loss =2.13 (60.2 examples/sec; 2.125 sec/batch
step 70, loss =1.95 (63.0 examples/sec; 2.031 sec/batch
step 80, loss =2.01 (62.1 examples/sec; 2.062 sec/batch
step 90, loss =1.90 (63.5 examples/sec; 2.016 sec/batch
step 100, loss =1.93 (62.5 examples/sec; 2.047 sec/batch
step 110, loss =1.96 (62.1 examples/sec; 2.062 sec/batch
step 120, loss =1.92 (62.3 examples/sec; 2.055 sec/batch
step 130, loss =1.81 (63.5 examples/sec; 2.016 sec/batch
step 140, loss =1.86 (59.8 examples/sec; 2.141 sec/batch
step 150, loss =1.88 (64.0 examples/sec; 2.000 sec/batch
step 160, loss =1.87 (62.5 examples/sec; 2.047 sec/batch
step 170, loss =1.73 (49.6 examples/sec; 2.578 sec/batch
step 180, loss =1.86 (62.1 examples/sec; 2.062 sec/batch
step 190, loss =1.71 (62.5 examples/sec; 2.047 sec/batch
step 200, loss =1.63 (63.0 examples/sec; 2.031 sec/batch
step 210, loss =1.63 (63.5 examples/sec; 2.016 sec/batch
step 220, loss =1.67 (62.1 examples/sec; 2.063 sec/batch
step 230, loss =1.72 (62.5 examples/sec; 2.047 sec/batch
step 240, loss =1.76 (62.1 examples/sec; 2.062 sec/batch
step 250, loss =1.67 (61.6 examples/sec; 2.078 sec/batch
step 260, loss =1.67 (62.5 examples/sec; 2.047 sec/batch
step 270, loss =1.59 (63.0 examples/sec; 2.031 sec/batch
step 280, loss =1.55 (62.5 examples/sec; 2.047 sec/batch
step 290, loss =1.64 (62.5 examples/sec; 2.047 sec/batch
step 300, loss =1.63 (62.1 examples/sec; 2.062 sec/batch
step 310, loss =1.49 (62.1 examples/sec; 2.062 sec/batch
step 320, loss =1.49 (62.5 examples/sec; 2.047 sec/batch
step 330, loss =1.61 (62.1 examples/sec; 2.062 sec/batch
step 340, loss =1.55 (61.1 examples/sec; 2.094 sec/batch
step 350, loss =1.63 (62.5 examples/sec; 2.047 sec/batch
step 360, loss =1.75 (61.6 examples/sec; 2.078 sec/batch
step 370, loss =1.54 (61.1 examples/sec; 2.094 sec/batch
step 380, loss =1.66 (61.6 examples/sec; 2.078 sec/batch
step 390, loss =1.66 (62.1 examples/sec; 2.062 sec/batch
step 400, loss =1.74 (62.1 examples/sec; 2.062 sec/batch
step 410, loss =1.60 (61.6 examples/sec; 2.078 sec/batch
step 420, loss =1.64 (62.5 examples/sec; 2.047 sec/batch
step 430, loss =1.59 (61.1 examples/sec; 2.094 sec/batch
step 440, loss =1.64 (59.8 examples/sec; 2.141 sec/batch
step 450, loss =1.67 (62.5 examples/sec; 2.047 sec/batch
step 460, loss =1.35 (60.7 examples/sec; 2.109 sec/batch
step 470, loss =1.45 (63.5 examples/sec; 2.016 sec/batch
step 480, loss =1.47 (62.5 examples/sec; 2.047 sec/batch
step 490, loss =1.37 (61.6 examples/sec; 2.078 sec/batch
step 500, loss =1.64 (63.0 examples/sec; 2.031 sec/batch
step 510, loss =1.58 (64.0 examples/sec; 2.000 sec/batch
step 520, loss =1.36 (63.5 examples/sec; 2.016 sec/batch
step 530, loss =1.30 (61.6 examples/sec; 2.078 sec/batch
step 540, loss =1.49 (62.5 examples/sec; 2.047 sec/batch
step 550, loss =1.46 (62.5 examples/sec; 2.047 sec/batch
step 560, loss =1.58 (63.0 examples/sec; 2.031 sec/batch
step 570, loss =1.46 (63.5 examples/sec; 2.016 sec/batch
step 580, loss =1.49 (64.5 examples/sec; 1.984 sec/batch
step 590, loss =1.30 (64.0 examples/sec; 2.000 sec/batch
step 600, loss =1.39 (64.5 examples/sec; 1.984 sec/batch
step 610, loss =1.62 (63.0 examples/sec; 2.031 sec/batch
step 620, loss =1.41 (62.1 examples/sec; 2.062 sec/batch
step 630, loss =1.29 (62.5 examples/sec; 2.047 sec/batch
step 640, loss =1.42 (63.5 examples/sec; 2.016 sec/batch
step 650, loss =1.36 (63.0 examples/sec; 2.031 sec/batch
step 660, loss =1.46 (63.5 examples/sec; 2.016 sec/batch
step 670, loss =1.26 (63.0 examples/sec; 2.031 sec/batch
step 680, loss =1.64 (62.1 examples/sec; 2.062 sec/batch
step 690, loss =1.39 (63.0 examples/sec; 2.031 sec/batch
step 700, loss =1.32 (61.6 examples/sec; 2.078 sec/batch
step 710, loss =1.36 (61.6 examples/sec; 2.078 sec/batch
step 720, loss =1.51 (62.1 examples/sec; 2.062 sec/batch
step 730, loss =1.48 (63.5 examples/sec; 2.016 sec/batch
step 740, loss =1.34 (61.1 examples/sec; 2.094 sec/batch
step 750, loss =1.44 (61.1 examples/sec; 2.094 sec/batch
step 760, loss =1.34 (60.7 examples/sec; 2.109 sec/batch
step 770, loss =1.46 (61.1 examples/sec; 2.094 sec/batch
step 780, loss =1.46 (60.7 examples/sec; 2.109 sec/batch
step 790, loss =1.42 (61.1 examples/sec; 2.094 sec/batch
step 800, loss =1.40 (63.0 examples/sec; 2.031 sec/batch
step 810, loss =1.46 (61.6 examples/sec; 2.078 sec/batch
step 820, loss =1.32 (62.1 examples/sec; 2.062 sec/batch
step 830, loss =1.46 (62.5 examples/sec; 2.047 sec/batch
step 840, loss =1.27 (64.0 examples/sec; 2.000 sec/batch
step 850, loss =1.38 (62.5 examples/sec; 2.047 sec/batch
step 860, loss =1.30 (63.0 examples/sec; 2.031 sec/batch
step 870, loss =1.18 (63.0 examples/sec; 2.031 sec/batch
step 880, loss =1.39 (62.5 examples/sec; 2.047 sec/batch
step 890, loss =1.17 (63.5 examples/sec; 2.016 sec/batch
step 900, loss =1.27 (62.1 examples/sec; 2.062 sec/batch
step 910, loss =1.38 (60.7 examples/sec; 2.109 sec/batch
step 920, loss =1.64 (60.2 examples/sec; 2.125 sec/batch
step 930, loss =1.45 (60.7 examples/sec; 2.109 sec/batch
step 940, loss =1.39 (61.6 examples/sec; 2.078 sec/batch
step 950, loss =1.40 (63.5 examples/sec; 2.016 sec/batch
step 960, loss =1.32 (62.1 examples/sec; 2.063 sec/batch
step 970, loss =1.32 (63.0 examples/sec; 2.031 sec/batch
step 980, loss =1.28 (61.6 examples/sec; 2.078 sec/batch
step 990, loss =1.20 (63.5 examples/sec; 2.016 sec/batch

结果:

分析:

  cifar10数据集比mnist数据集更完整也更复杂,基于cifar数据集进行10分类比mnist有更高的难度,整体的准确率和召回率都普遍偏低,但适当的增加迭代次数和卷积核的大小有助于提升准确度,大概能到80%,要想获得更高的准确度可以增加训练集的数量!

基于cifar10实现卷积神经网络图像识别的更多相关文章

  1. 基于Python的卷积神经网络和特征提取

    基于Python的卷积神经网络和特征提取 用户1737318发表于人工智能头条订阅 224 在这篇文章中: Lasagne 和 nolearn 加载MNIST数据集 ConvNet体系结构与训练 预测 ...

  2. 基于 SoC 的卷积神经网络车牌识别系统设计(0)摘要

    ​NOTES:现如今,芯片行业无比火热啊,无论是前景还是钱景,国家芯片战略的发布,公司四五十万的年薪,着实令人非常的向往,为了支持芯片设计者,集成了工作.科研.竞赛于一体的<基于 SoC 的卷积 ...

  3. 基于 SoC 的卷积神经网络车牌识别系统设计(1)概述

    NOTES: 这是第三届全国大学生集成电路创新创业大赛 - Arm 杯 - 片上系统设计挑战赛(本人指导的一个比赛).主要划分为以下的 Top5 重点.难点.亮点.热点以及创新点:1.通过 Arm C ...

  4. 深度学习基础-基于Numpy的卷积神经网络(CNN)实现

    本文是深度学习入门: 基于Python的实现.神经网络与深度学习(NNDL)以及动手学深度学习的读书笔记.本文将介绍基于Numpy的卷积神经网络(Convolutional Networks,CNN) ...

  5. visualization of filters keras 基于Keras的卷积神经网络(CNN)可视化

    https://adeshpande3.github.io/adeshpande3.github.io/ https://blog.csdn.net/weiwei9363/article/detail ...

  6. 普适注意力:用于机器翻译的2D卷积神经网络,显著优于编码器-解码器架构

    现有的当前最佳机器翻译系统都是基于编码器-解码器架构的,二者都有注意力机制,但现有的注意力机制建模能力有限.本文提出了一种替代方法,这种方法依赖于跨越两个序列的单个 2D 卷积神经网络.该网络的每一层 ...

  7. tensorflow学习笔记——图像识别与卷积神经网络

    无论是之前学习的MNIST数据集还是Cifar数据集,相比真实环境下的图像识别问题,有两个最大的问题,一是现实生活中的图片分辨率要远高于32*32,而且图像的分辨率也不会是固定的.二是现实生活中的物体 ...

  8. 优化基于FPGA的深度卷积神经网络的加速器设计

    英文论文链接:http://cadlab.cs.ucla.edu/~cong/slides/fpga2015_chen.pdf 翻译:卜居 转载请注明出处:http://blog.csdn.net/k ...

  9. 基于MTCNN多任务级联卷积神经网络进行的人脸识别 世纪晟人脸检测

    神经网络和深度学习目前为处理图像识别的许多问题提供了最佳解决方案,而基于MTCNN(多任务级联卷积神经网络)的人脸检测算法也解决了传统算法对环境要求高.人脸要求高.检测耗时高的弊端. 基于MTCNN多 ...

随机推荐

  1. Pulsar Consumer实现介绍

    Pulsar-Consumer “Pulsar is a distributed pub-sub messaging platform with a very flexible messaging m ...

  2. 纸上谈兵: 堆 (heap)

    纸上谈兵: 堆 (heap)   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 堆(heap)又被为优先队列(priority ...

  3. mysql中外键的特点

    mysql中外键的特点简单描述: 1.要求在从表中设置外键关系: 2.从表的外键列的类型和主表的关联列的类型要求一致或兼容,名称无要求: 3.主表的关联列必须是一个key(一般是主键或唯一键): 4. ...

  4. WebService SOAP

    <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http:// ...

  5. 如何像Python高手(Pythonista)一样编程

    最近在网上看到一篇介绍Pythonic编程的文章:Code Like a Pythonista: Idiomatic Python,其实作者在2006的PyCon会议后就写了这篇文章,写这篇文章的主要 ...

  6. man sm-notify(sm-notify命令中文手册)

    本人译作集合:http://www.cnblogs.com/f-ck-need-u/p/7048359.html sm-notify命令是用来发送重启通知信息给NFS对端的,在锁状态恢复过程中起着至关 ...

  7. mybatis逆向工程(MyBatis Generator)

    mybatis逆向工程(MyBatis Generator) 1. 什么是mybatis逆向工程 mybatis官方为了提高开发效率,提高自动对单表生成sql,包括 :mapper.xml.mappe ...

  8. Redis学习笔记(3)-XShell连接CentOSMini,并安装Redis

    使用XShell远程连接CentOSMini 点击download下载XShell5.0. 下载之后安装.配置XShell. 配置XShell前的准备 打开VM,启动CentOSMini.CentOS ...

  9. Asp.net连接数据库的配置方法

    1.Sqlserver数据库连接 <connectionStrings> <add name="Conn" connectionString="serv ...

  10. WPF Grid布局

    本节讲述布局,顺带加点样式给大家看看~单纯学布局,肯定是枯燥的~哈哈 那如上界面,该如何设计呢? 1.一些布局元素经常用到.Grid StackPanel Canvas WrapPanel等.如上这种 ...