avatar

深度学习入门(三)

卷积神经网络介绍

卷积层

三个参数

  1. ksize
    • 卷积核的大小 (n*n的卷积核采样)
  2. strides
    • 卷积核移动的跨度 (每次采样的步长)
  3. padding
    • 边缘填充 (使采样均匀)

非线性变换层

激活函数

池化层

使图像像素变小

  1. 最大池化
  2. 平均池化

全连接层

w*x + b

卷积神经网络玩转mnist数据集

数据预处理

1
2
x_train.shape, y_train.shape
((60000, 28, 28), (60000,))

keras要求输入的dataformate.(batch, height, weight, channel)
mnist数据集缺少channel这一维度,因此需要扩充维度,我们使用numpy的一个方法:

1
2
3
4
x_train = np.expand_dims(x_train, axis=-1)
x_test = np.expand_dims(x_test, axis=-1)
x_train.shape, x_test.shape
((60000, 28, 28, 1), (10000, 28, 28, 1))

可以看到现在完全符合keras的dataformate标准

搭建模型

根据卷积神经网络的结构:

  1. 卷积
  2. 池化
  3. 卷积
  4. 池化
  5. 全连接

卷积 + 池化

1
2
3
4
5
model = keras.Sequential()
model.add(layers.Conv2D(64, (3,3), activation='relu', input_shape=(28,28,1)))
model.add(layers.Conv2D(64, (3,3), activation='relu'))
model.add(layers.MaxPooling2D())
model.summary()

全连接

1
2
3
4
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, activation='softmax'))

model.summary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 26, 26, 64) 640
_________________________________________________________________
conv2d_2 (Conv2D) (None, 24, 24, 64) 36928
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 9216) 0
_________________________________________________________________
dense_1 (Dense) (None, 256) 2359552
_________________________________________________________________
dropout_1 (Dropout) (None, 256) 0
_________________________________________________________________
dense_2 (Dense) (None, 10) 2570
=================================================================
Total params: 2,399,690
Trainable params: 2,399,690
Non-trainable params: 0

编译,训练模型

1
2
3
4
5
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

model.fit(x_train, y_train, epochs=10, batch_size=512)

训练集测试结果

1
2
3
4
Epoch 9/10
60000/60000 [==============================] - 121s 2ms/step - loss: 0.0265 - accuracy: 0.9911
Epoch 10/10
60000/60000 [==============================] - 130s 2ms/step - loss: 0.0267 - accuracy: 0.9912

可以看到正确率轻松达到99%,看一下在测试集的表现吧

测试集测试结果

1
2
model.evaluate(x_test, y_test)
[0.04169193260611291, 0.9886999726295471]

由此可见,卷积神经网络非常适合图像识别。

文章作者: gh
文章链接: https://ghclub.top/posts/42681/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 GHBlog
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论