CNN-识别CIFAR-10–建立模型

1.导入所需模块

from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation,Flatten
from keras.layers import Conv2D,MaxPool2D,ZeroPadding2D
2.建立keras的Sequential模型
建立一个Sequential线性堆叠模型

model = Sequential()
3.建立卷积层1和池化层1
输入图像大小为32×32会产生32个图像,卷积运算并不会改变图像大小,所以图像大小仍然是32×32,卷积运算的效果很类似滤镜效果,可以提取图像的特征.

model.add(Conv2D(filters=32,kernel_size=(3,3),input_shape=(32,32,3),activation='relu',padding='same'))
filters=32设置随机产生32个滤镜
kernel_size=(3,3)滤镜大小为3×3
input_shape=(32,32,3)第1.2维代表输入的图像形状大小为32×32,第三维因为是彩色所以代表RGB三原色
activation=’relu’设置RELU激活函数
padding=’same’ 图像大小不变
加入DROPOUT避免过度拟合
以下程序代码把dropout加入模型中,Dropout的功能是每次迭代时,会随机的在神经网络中放弃一定比例的神经元,避免过度拟合

model.add(Dropout(rate=0.25))
建立池化层1
执行第一次采样压缩,将32×32缩小为16×16

model.add(MaxPool2D(pool_size=(2,2)))
4.建立卷积层2和池化层2
建立卷积层2
执行第2次卷积运算:将原本的32个图像转换为64个图像

model.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu',padding='same'))
加入DROPOUT避免过度拟合

model.add(Dropout(0.25))
建立池化层2
执行第二次缩减采样,将16×16的图像转换为8×8

model.add(MaxPool2D(pool_size=(2,2)))
5.建立神经网络
建立平坦层

model.add(Flatten())
model.add(Dropout(rate=0.25))
将前面建立的池化层2的64个8×8的图像转换为一维向量,长度是64x8x8=4096,也就是4096个float数,正好对应4096个神经元,且加入Dropout,避免过度拟合
建立隐藏层
共1024个神经元,且加入dropout

model.add(Dense(1024,activation='relu'))
model.add(Dropout(rate=0.25))
建立输出层
最后建立输出层,共10个神经元,对应10个图像类别,并且使用softmax激活函数进行转换,softmax激活函数可以将神经元的输出转换为预测每一个图像类别的概率

model.add(Dense(10,activation='softmax'))
6.查看模型摘要
print(model.summary())
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d_1 (Conv2D)            (None, 32, 32, 32)        896
_________________________________________________________________
dropout_1 (Dropout)          (None, 32, 32, 32)        0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 32)        0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 16, 16, 64)        18496
_________________________________________________________________
dropout_2 (Dropout)          (None, 16, 16, 64)        0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 8, 8, 64)          0
_________________________________________________________________
flatten_1 (Flatten)          (None, 4096)              0
_________________________________________________________________
dropout_3 (Dropout)          (None, 4096)              0
_________________________________________________________________
dense_1 (Dense)              (None, 1024)              4195328
_________________________________________________________________
dropout_4 (Dropout)          (None, 1024)              0
_________________________________________________________________
dense_2 (Dense)              (None, 10)                10250
=================================================================
Total params: 4,224,970
Trainable params: 4,224,970
Non-trainable params: 0
_________________________________________________________________
None