本项目参考:
https://www.bilibili.com/video/av31500120?t=4657
训练代码
# coding: utf-8
# Learning from Mofan and Mike G
# Recreated by Paprikatree
# Convolution NN Train import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Convolution2D, Activation, MaxPool2D, Flatten, Dense
from keras.optimizers import Adam
from keras.models import load_model nb_class = 10
nb_epoch = 4
batchsize = 128 '''
1st,准备参数
X_train: (0,255) --> (0,1) CNN中似乎没有必要?cnn自动转了吗?
设置时间函数测试一下两者对比。
小技巧:X_train /= 255.0 就可不用转换成浮点了???
'''
# Preparing your data mnist. MAC /.keras/datasets linux home ./keras/datasets
(X_train, Y_train), (X_test, Y_test) = mnist.load_data() # setup data shape
# (-1, 28, 28, 1) -1表示有默认个数据集,28*28是像素,1是1个通道
X_train = X_train.reshape(-1, 28, 28, 1) # tensorflow-channel last,while theano-channel first
X_test = X_test.reshape(-1, 28, 28, 1) X_train = X_train/255.000
X_test = X_test/255.000 # One-hot 6 --> [0,0,0,0,0,1,0,0,0]
Y_train = np_utils.to_categorical(Y_train, nb_class)
Y_test = np_utils.to_categorical(Y_test, nb_class) '''
2nd,设置模型
''' # setup model
model = Sequential() # 1st convolution layer # 滤波器要在28x28的图上横着走32次
model.add(Convolution2D(
filters=32, # 此处把filters写成了filter,找了半天。囧
kernel_size=[5, 5], # 滤波器是5x5大小的,可以是list列表,也可以是tuple元祖
padding='same', # padding也是一个窗口模式
input_shape=(28, 28, 1) # 定义输入的数据,必须是元组
))
model.add(Activation('relu'))
model.add(MaxPool2D(
pool_size=(2, 2), # 按照规则抓取特征,此处为在pool_size的2*2窗口下,strides = 2*2 跳两格再抓取。如 1 2 3 4 5 6...27 28 抓取1 2 ,跳过 3 4 抓取 5 6。
strides=(2, 2), # 相当于把图片缩小了。
padding="same",
)) # 2nd Conv2D layer
model.add(Convolution2D(
filters=64,
kernel_size=(5, 5),
padding='same',
))
model.add(Activation('relu'))
model.add(MaxPool2D(
pool_size=(2, 2), # 按照规则抓取特征,此处为在pool_size的2*2窗口下,strides = 2*2 跳两格再抓取。如 1 2 3 4 5 6...27 28 抓取1 2 ,跳过 3 4 抓取 5 6。
strides=(2, 2), # 相当于把图片缩小了。
padding="same",
)) # 讨论,卷积层数和最终结果关系。 # 1st Fully connected Dense,Dense 全连接层是hello world里面的内容
model.add(Flatten()) # 把卷积层里面的全部转换层一维数组
model.add(Dense(1024)) # Dense is output
model.add(Activation('relu')) # 1st Fully connected Dense,Dense 全连接层是hello world里面的内容
# 把卷积层里面的全部转换层一维数组
model.add(Dense(256)) # Dense is output
model.add(Activation('tanh')) # 2nd Fully connected Dense
model.add(Dense(10))
model.add(Activation('softmax')) '''
3rd 定义参数
'''
# Define Optimizer and setup Param
adam = Adam(lr=0.0001) # Adam实例化 # compile model
model.compile(
optimizer=adam, # optimizer='Adam'也是可以的,且默认lr=0.001,此处已经实例化为adam
loss='categorical_crossentropy',
metrics=['accuracy'],
) # Run network
model.fit(x=X_train, # 更多参数可以查看fit函数,alt+鼠标左键单击fit
y=Y_train,
epochs=nb_epoch,
batch_size=batchsize, # p=parameter, batch_size; v=var, batch size
verbose=1, # 显示模式
validation_data=(X_test, Y_test)
)
model.save('model_name.h5')
# evaluation = model.evaluate(X_test, Y_test) 现在用model.fit(validation_data)
# print(evaluation) 效果一样
测试代码:
# coding: utf-8
# Learning from Mofan and Mike G
# Recreated by Paprikatree
# Convolution NN Predict import numpy as np
from keras.models import load_model # ??
import matplotlib.pyplot as plt
import matplotlib.image as processimage # load trained model
model = load_model('model_name.h5') # 已经训练好了的模型,在根目录下,默认为model_name.h5 # 写一个来预测的类
class MainPredictImg(object): def __init__(self):
pass def pred(self, filename):
pred_img = processimage.imread(filename)
pred_img = np.array(pred_img)
pred_img = pred_img.reshape(-1, 28, 28, 1)
prediction = model.predict(pred_img)
final_prediction = [result.argmax() for result in prediction][0]
a = 0
for i in prediction[0]:
print(a)
print('Percent:{:.30%}'.format(i))
a = a+1
return final_prediction def main():
predict = MainPredictImg()
res = predict.pred('4.png')
print("your number is:-->", res) if __name__ == '__main__':
main()