-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrainConvolutionModel.py
90 lines (75 loc) · 2.47 KB
/
TrainConvolutionModel.py
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
# Copyright: Copyright (c) 2019
# Createdon: 2019年5月23日
# Author: ChrisKong
# Version: 1.0
# Title: 一个Python程序
import os
from keras import Sequential
from keras.datasets import mnist
from keras.layers import Activation, MaxPooling2D, Convolution2D, Flatten, Dense
from keras.utils import np_utils
def buildmodel():
model = Sequential()
model.add(Convolution2D(
batch_input_shape=(32, 1, 28, 28),
filters=32,
kernel_size=5,
strides=1,
padding='same', # Padding method
data_format='channels_first',
))
model.add(Activation('relu'))
model.add(MaxPooling2D(
pool_size=2,
strides=2,
padding='same', # Padding method
data_format='channels_first',
))
model.add(Convolution2D(
filters=64,
kernel_size=5,
strides=1,
padding='same', # Padding method
data_format='channels_first',
))
model.add(Activation('relu'))
model.add(MaxPooling2D(
pool_size=2,
strides=2,
padding='same', # Padding method
data_format='channels_first',
))
model.add(Flatten())
model.add(Dense(1024))
model.add(Dense(10))
model.add(Activation('softmax'))
return model
def train():
localpath = os.getcwd()
# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
(x_train, y_train), (x_test, y_test) = mnist.load_data(localpath + '/mnist/mnist.npz')
# data pre-processing
# X shape (60,000 28x28), y shape (10,000, )
x_train = x_train.reshape(-1, 1, 28, 28) / 255.
x_test = x_test.reshape(-1, 1, 28, 28) / 255. # normalize
# print(x_test.shape)
# print(x_test[1].shape)
# print(x_test[1].reshape(1, - 1, 28, 28).shape)
# exit()
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
model = buildmodel() # 导入模型结构
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5, batch_size=32)
# 评估模型
loss, accuracy = model.evaluate(x_test, y_test)
print('test loss', loss)
print('accuracy', accuracy)
# 保存模型
if not os.path.exists(localpath + '/model'):
os.mkdir(localpath + '/model')
model.save('./model/ConvolutionModel.h5') # HDF5文件,pip install h5py
if __name__ == '__main__':
train()