HelloWorld of Keras

mnist_cnn

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
# -*- coding: utf8 -*-

# "Hello World" of Keras in image classification task

from __future__ import print_function # 在Python2的代码中可以使用Python3的print函数,即需要加上括号
import keras
from keras.datasets import mnist
from keras.models import Sequential # 序列模型,可以封装各种网络层: 卷积层、池化层、全连接层、dropout等
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K # Keras是一个高层API,后端使用TensorFlow或者Theano

batch_size = 128
num_classes = 10
epochs = 12 # 12个epoch足够训练好模型

img_rows, img_cols = 28, 28 # mnist dataset中image的大小

# 如果第一次跑,会通过url方式从amazon下载mnist.npz;如果已经下载过了,那么直接加载本地mnist.npz
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 根据不同的后端,将图片转换为不同的格式,TensorFlow是channel_last
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) # 通道为1
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

# 将每个pixel的类型从integer转为float32,使除法计算更加精确
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

# 将0-255范围的pixel值转化到0-1
x_train /= 255
x_test /= 255

# 如果后端是TensorFlow
print('x_train shape:', x_train.shape) # 输出(60000, 28, 28, 1)
print(x_train.shape[0], 'train samples') # 输出60000
print(x_test.shape[0], 'test samples') # 输出10000

# 每个y_train或者y_test本来是一个整数表示每一类,现转换为one-hot encoding
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# 实例化一个序列模型
model = Sequential()
# Conv1
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
# Conv2
model.add(Conv2D(64, (3, 3), activation='relu'))
# Pool1
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
# 将maxpooling后的64个activation map 全部拉伸为一个向量
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
# 用softmax函数将logits转为每个类别的概率
model.add(Dense(10, activation='softmax'))

# 设置损失函数,优化器,度量标准
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])

# 训练模型
# verbose=1 展示详细的训练过程
# verbose=0 只显示最后结果,不展示过程
# verbose=2 每个epoch输出一条信息,不展示详细的训练过程
# validation_data会在每个每个epoch结束后计算当前模型的准确率,validation_data不参与训练
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=2,
validation_data=(x_test, y_test))

# 在测试集上测试模型效果
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test Accuracy:', score[1])

训练过程

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
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Train on 60000 samples, validate on 10000 samples

Epoch 1/12
- 248s - loss: 0.2778 - acc: 0.9140 - val_loss: 0.0612 - val_acc: 0.9800
Epoch 2/12
- 219s - loss: 0.0982 - acc: 0.9708 - val_loss: 0.0433 - val_acc: 0.9857
Epoch 3/12
- 212s - loss: 0.0766 - acc: 0.9776 - val_loss: 0.0354 - val_acc: 0.9876
Epoch 4/12
- 226s - loss: 0.0653 - acc: 0.9806 - val_loss: 0.0342 - val_acc: 0.9879
Epoch 5/12
- 221s - loss: 0.0573 - acc: 0.9828 - val_loss: 0.0307 - val_acc: 0.9896
Epoch 6/12
- 230s - loss: 0.0539 - acc: 0.9834 - val_loss: 0.0286 - val_acc: 0.9907
Epoch 7/12
- 224s - loss: 0.0490 - acc: 0.9850 - val_loss: 0.0280 - val_acc: 0.9901
Epoch 8/12
- 229s - loss: 0.0475 - acc: 0.9856 - val_loss: 0.0300 - val_acc: 0.9904
Epoch 9/12
- 249s - loss: 0.0465 - acc: 0.9862 - val_loss: 0.0286 - val_acc: 0.9906
Epoch 10/12
- 257s - loss: 0.0423 - acc: 0.9866 - val_loss: 0.0261 - val_acc: 0.9910
Epoch 11/12
- 243s - loss: 0.0393 - acc: 0.9883 - val_loss: 0.0313 - val_acc: 0.9903
Epoch 12/12
- 277s - loss: 0.0413 - acc: 0.9877 - val_loss: 0.0266 - val_acc: 0.9907
Test loss: 0.026646837043244158
Test Accuracy: 0.9907

Process finished with exit code 0

如果是第一次运行,会从 https://s3.amazonaws.com/img-datasets/mnist.npz 下载数据集,Windows会存放在c://Users//<User name>//.keras/datasets下面;Linux会存放在home/.keras/datasets下面。

cifar10_cnn.py

1
2


训练过程


参考

Donate comment here