卷积神经网络:LeNet-5

2021SC@SDUSC

LeNet-5卷积神经网络的整体框架:
LeNet-5卷积神经网络的整体框架
特征映射:一幅图在经过卷积操作后得到结果称为特征图。

LeNet-5共有8层,包含输入层,每层都包含可训练参数;每个层有多个特征映射(,每个特征映射通过一种卷积核(或者叫滤波器)提取输入的一种特征,然后每个特征映射有多个神经元。C层代表的是卷积层,通过卷积操作,可以使原信号特征增强,并且降低噪音。 S层是一个降采样层,利用图像局部相关性的原理,对图像进行子抽样,可以减少数据处理量同时保留有用信息。

计算公式:输入图像的大小为nxn,卷积核的大小为mxm,步长为s, 为输入图像两端填补p个零(zero padding),那么卷积操作之后输出的大小为(n - m + 2p) / s + 1。

1. LeNet-5结构:

1.1 First Layer
LeNet-5的输入是一个32×32灰度图像,它通过第一个卷积层,带有6个特征图或过滤器,大小为5x5,步长为1。 图像的尺寸从32x32x1变为28x28x6。
在这里插入图片描述
1.2Second Layer
LeNet-5应用平均池化层或分采样层,过滤器大小为2x2,步长为2。 得到的图像维数将减少到14x14x6。
在这里插入图片描述
1.3Third Layer
第二个卷积层,16个特征图大小为5x5,步幅为1。 在该层中,16个feature map中只有10个连接到上一层的6个feature map,如下图所示。
在这里插入图片描述
1.4Fourth Layer
第四层(S4)也是平均池化层,过滤器大小为2x2,步幅为2。 这一层和第二层(S2)一样,除了它有16个特征映射,所以输出将减少到5x5x16。
在这里插入图片描述
1.5Fifth Layer
第五层(C5)是一个全连接的卷积层,有120个特征映射,每个特征映射的大小为1×1。 C5中的120个单元每一个都连接到第四层S4中的所有400个节点(5x5x16)。
在这里插入图片描述
1.6Sixth Layer
第六层是84个单元的全连接层(F6)。
在这里插入图片描述
1.7Output Layer
最后,有一个全连接的输出层y,有10个可能的值,对应于从0到9的数字。
在这里插入图片描述

2. LeNet-5的总结如下

在这里插入图片描述

3.LeNet-5 代码实现

import pandas as pd
import numpy as np
np.random.seed(1337) # for reproducibility

from keras import backend as K
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils

# 输入图片
img_rows, img_cols = 28, 28

batch_size = 128 # 在每个优化步骤中使用的图像数量  
nb_classes = 10 # 每位数一个类
nb_epoch = 70 # 使用整个数据进行学习的次数  

# 读取训练和测试数据集
train = pd.read_csv("../input/train.csv").values
test  = pd.read_csv("../input/test.csv").values


# Check Keras backend
if(K.image_dim_ordering()=="th"):
    # Reshape the data to be used by a Theano CNN. 
    # (nb_of_samples, nb_of_color_channels, img_width, img_heigh)
    X_train = train[:, 1:].reshape(train.shape[0], 1, img_rows, img_cols)
    X_test = test.reshape(test.shape[0], 1, img_rows, img_cols)
    in_shape = (1, img_rows, img_cols)
else:
    # Reshape the data to be used by a Tensorflow CNN. 
    # (nb_of_samples, img_width, img_heigh, nb_of_color_channels)
    X_train = train[:, 1:].reshape(train.shape[0], img_rows, img_cols, 1)
    X_test = test.reshape(test.shape[0], img_rows, img_cols, 1)
    in_shape = (img_rows, img_cols, 1)
y_train = train[:, 0] # 第一个数据是label(已经从X_train中删除)  

# 转换成 [0;1] 之间的浮点数
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

#将类向量转换为二进制类矩阵
Y_train = np_utils.to_categorical(y_train, nb_classes)

#显示形状检查
print('X_train shape:', X_train.shape)
print('Y_train shape:', Y_train.shape)
print('X_test shape:', X_test.shape)

model = Sequential()

model.add(Convolution2D(12, 5, 5, activation = 'relu', input_shape=in_shape, init='he_normal'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Convolution2D(25, 5, 5, activation = 'relu', init='he_normal'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(180, activation = 'relu', init='he_normal'))
model.add(Dropout(0.5))
model.add(Dense(100, activation = 'relu', init='he_normal'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation = 'softmax', init='he_normal')) #最后一层,每个类有一个输出

model.compile(loss='categorical_crossentropy', optimizer='adamax', metrics=["accuracy"])

# 让模型学习
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1)

# 预测X_test的标签
yPred = model.predict_classes(X_test)

# Save prediction
np.savetxt('mnist-pred.csv', np.c_[range(1,len(yPred)+1),yPred], delimiter=',', header = 'ImageId,Label', comments = '', fmt='%d')
  • 6
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值