接下来将会以 MNIST 数据集为例,使用卷积层和池化层,实现一个卷积神经网络来进行手写数字识别,并输出卷积和池化效果。
MNIST 数据集可以从 THE MNIST DATABASE of handwritten digits 的网站直接下载。 网址:http://yann.lecun.com/exdb/mnist/
train-images-idx3-ubyte.gz: 训练集图片 train-labels-idx1-ubyte.gz: 训练集列标 t10k-images-idx3-ubyte.gz: 测试集图片 t10k-labels-idx1-ubyte.gz: 测试集列标
TensorFlow 有加载 MNIST 数据库相关的模块,可以在程序运行时直接加载。
代码如下:
代码的输出依次是:
1.单个手写数字图片的维度: (784,)
2.转化为二维矩阵之后的打印结果: (28, 28)
3.使用 matplotlib 输出为图片
tf.nn.conv2d(x, W, strides, padding=’SAME’) 针对输入的 4 维数据 x 计算 2 维卷积。
参数 x: 4 维张量,每一个维度分别是 batch,in_height,in_height,in_channels。 [batch, in_height, in_width, in_channels]
灰度图像只有 2 维来表示每一个像素的值,彩色图像每一个像素点有 3 通道的 RGB 值,所以一个彩色图片转化成张量后是 3 维的,分别是长度,宽度,颜色通道数。又因为每一次训练都是训练都是输入很多张图片,所以,多个 3 维张量组合在一起变成了 4 维张量。
参数 w: 过滤器,因为是二维卷积,所以它的维度是:
[filter_height, filter_width, in_channels, out_channels]
与参数 x 对应,前 3 个参数分别是对应 x 的 filter_height, filter_width, in_channels,最后一个参数是过滤器的输出通道数量。
参数 strides: 1 维长度为 4 的张量,对应参数 x 的 4 个维度上的步长。
参数 padding: 边缘填充方式,主要是 “SAME”, “VALID”,一般使用 “SAME”。
TensorFlow max_pool 函数介绍:
tf.nn.max_pool(x, ksize, strides ,padding)
参数 x: 和 conv2d 的参数 x 相同,是一个 4 维张量,每一个维度分别代表 batch,in_height,in_height,in_channels。
参数 ksize: 池化核的大小,是一个 1 维长度为 4 的张量,对应参数 x 的 4 个维度上的池化大小。
参数 strides: 1 维长度为 4 的张量,对应参数 x 的 4 个维度上的步长。
参数 padding: 边缘填充方式,主要是 “SAME”, “VALID”,一般使用 “SAME”。
接下来将会使用 TensorFlow 实现以下结构的卷积神经网络:
卷积神经网络定义:
训练过程输出
https://www.cs.toronto.edu/~kriz/cifar.html
https://github.com/tensorflow/models/tree/master/research/object_detection
主要参考对象:
Image Recognition https://tensorflow.google.cn/tutorials/image_recognition
https://www.tensorflow.org/tutorials/deep_cnn
ImageNet Classification with Deep Convolutional Neural Networks http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks
Convolutional Neural Networks http://cs231n.github.io/convolutional-networks/ Deep learning http://neuralnetworksanddeeplearning.com/chap6.html
Convolutional neural network https://en.wikipedia.org/wiki/Convolutional_neural_network
Comparison of Normal Neural network
https://leonardoaraujosantos.gitbooks.io/artificial-inteligence/content/convolutional_neural_networks.html
Convolutional Neural Networks (LeNet)
http://deeplearning.net/tutorial/lenet.html#sparse-connectivity
Convolutional neural networks from scratch
http://gluon.mxnet.io/chapter04_convolutional-neural-networks/cnn-scratch.html
卷积神经网络
http://prors.readthedocs.io/zh_CN/latest/2ndPart/Chapter8.SceneClassification/ConvNet.html
ImageNet Classification with Deep Convolutional Neural Networks
https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有