Keras卷积神经网络

基于Keras的卷积神经网络

参考:

Keras官方文档

Keras中文文档

环境:

操作系统:Ubuntu 18.04

CUDA: 10.0

TensorFLow: 1.13

Keras: 2.3.1

Keras构建卷积神经网络

  1. 使用Keras构建卷积神经网络, 并将训练模型保存.
  2. 加载模型, 用于预测新图片
构建网络
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# -*- coding:utf-8 -*-

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

import keras.layers
import numpy as np
import matplotlib.pyplot as plt

path = "/home/wyundi/Project/Git/MachineLearning/Keras/Tutorial/mnist"

# 获取数据集 
x_train = np.load(path + "/data/x_train.npy")
y_train = np.load(path + "/data/y_train.npy")
x_test = np.load(path + "/data/x_test.npy")
y_test = np.load(path + "/data/y_test.npy")

print(x_train.shape, y_train.shape)

# 显示图片
'''
plt.figure()
plt.imshow(x_train[20])
plt.show()
'''

# channles_last
img_rows, img_cols = x_train.shape[1], x_train.shape[2]

if K.image_data_format() == 'channels_first':
    ori_shape = (1, img_rows, img_cols)
else:
    ori_shape = (img_rows, img_cols, 1)

# 数据预处理
x_train = x_train.reshape((x_train.shape[0],)+ori_shape)
x_test = x_test.reshape((x_test.shape[0],)+ori_shape)

x_train, x_test = x_train / 255.0, x_test / 255.0

# one-hot编码
classes = 10
y_train = np_utils.to_categorical(y_train, classes)
y_test = np_utils.to_categorical(y_test, classes)

# 设置模型参数
filters = 32
kernel_size = (3,3)
pool_size = (2,2)
epochs = 5
batch_size = 128

# 构建模型
model = Sequential([
    # 第一层卷积
    Conv2D(filters, kernel_size=kernel_size, input_shape=ori_shape, activation='relu'),
    MaxPooling2D(pool_size=pool_size),
    # 第二层卷积
    Conv2D(filters//2, kernel_size=kernel_size, activation='relu'),
    MaxPooling2D(pool_size=pool_size),
    Dropout(0.5),
    # 全连接层
    Flatten(),
    Dense(128, activation='relu'),
    Dropout(0.5),
    # 输出层
    Dense(classes),
    Activation('softmax')
])

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

# 训练并验证模型
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.05)

loss, acc = model.evaluate(x_test, y_test, verbose=1)
print("Loss: ", loss)
print("Accuracy: ", acc)

# 模型结构
model.summary()

# 保存模型
model.save(path + "/models/mnist_CNN.h5") 
save_path = path + "/models/mnist_CNN.h5"
print("Save path: ", save_path)
返回结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Using TensorFlow backend.
(60000, 28, 28) (60000,)
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
2020-01-17 19:17:00.826600: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-01-17 19:17:00.847100: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2893305000 Hz
2020-01-17 19:17:00.847476: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x2a03750 executing computations on platform Host. Devices:
2020-01-17 19:17:00.847508: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
Train on 57000 samples, validate on 3000 samples
Epoch 1/5
57000/57000 [==============================] - 28s 489us/step - loss: 0.6047 - accuracy: 0.8037 - val_loss: 0.0893 - val_accuracy: 0.9780
Epoch 2/5
57000/57000 [==============================] - 27s 471us/step - loss: 0.2332 - accuracy: 0.9293 - val_loss: 0.0611 - val_accuracy: 0.9847
Epoch 3/5
57000/57000 [==============================] - 27s 469us/step - loss: 0.1834 - accuracy: 0.9438 - val_loss: 0.0522 - val_accuracy: 0.9880
Epoch 4/5
57000/57000 [==============================] - 26s 447us/step - loss: 0.1543 - accuracy: 0.9521 - val_loss: 0.0459 - val_accuracy: 0.9873
Epoch 5/5
57000/57000 [==============================] - 27s 474us/step - loss: 0.1381 - accuracy: 0.9571 - val_loss: 0.0412 - val_accuracy: 0.9903
10000/10000 [==============================] - 1s 143us/step
Loss:  0.042725288298050876
Accuracy:  0.9861999750137329
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 11, 11, 16)        4624      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 5, 5, 16)          0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 5, 5, 16)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 400)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               51328     
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1290      
_________________________________________________________________
activation_1 (Activation)    (None, 10)                0         
=================================================================
Total params: 57,562
Trainable params: 57,562
Non-trainable params: 0
_________________________________________________________________
Save path:  /home/wyundi/Project/Git/MachineLearning/Keras/Tutorial/mnist/models/mnist_CNN.h5
使用保存的模型预测新图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from keras import backend as K
from keras.models import load_model

import numpy as np
import matplotlib.pyplot as plt

path = "/home/wyundi/Project/Git/MachineLearning/Keras/Tutorial/mnist"

# 获取数据集 
x_train = np.load(path + "/data/x_train.npy")
y_train = np.load(path + "/data/y_train.npy")
x_test = np.load(path + "/data/x_test.npy")
y_test = np.load(path + "/data/y_test.npy")

# 显示图片

plt.figure()
plt.imshow(x_train[20])
# plt.show()

# channles_last
img_rows, img_cols = x_train.shape[1], x_train.shape[2]

if K.image_data_format() == 'channels_first':
    ori_shape = (1, img_rows, img_cols)
else:
    ori_shape = (img_rows, img_cols, 1)

# 数据预处理
x_train = x_train.reshape((x_train.shape[0],)+ori_shape)
x_test = x_test.reshape((x_test.shape[0],)+ori_shape)

x_train, x_test = x_train / 255.0, x_test / 255.0

print(x_train.shape, y_train.shape)

# 加载模型
model = load_model(path + "/models/mnist_CNN.h5")

model.summary()

# 整理图片尺寸
x_pri = x_test[0]
x_pri = x_pri.reshape((1,) + ori_shape)
print(x_pri.shape)

# 预测
print("Predict: ", np.argmax(model.predict(x_pri)))
print("Label: ", y_test[0])
预测结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Using TensorFlow backend.
(60000, 28, 28, 1) (60000,)
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2020-01-17 19:22:16.737457: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-01-17 19:22:16.759050: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2893305000 Hz
2020-01-17 19:22:16.759443: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x25a9620 executing computations on platform Host. Devices:
2020-01-17 19:22:16.759475: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 11, 11, 16)        4624      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 5, 5, 16)          0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 5, 5, 16)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 400)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               51328     
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1290      
_________________________________________________________________
activation_1 (Activation)    (None, 10)                0         
=================================================================
Total params: 57,562
Trainable params: 57,562
Non-trainable params: 0
_________________________________________________________________
(1, 28, 28, 1)
Predict:  7
Label:  7