matlab手写数字识别cnn,Machine Learning (CNN): 识别手写数字

机器学习入门 Machine Learning Tutorial I: 图像 Images

1. 手写数字识别 Handwritten Digits Recognition

附上对应英文,有助于日后更深层次的学习;若对内容有任何疑问,欢迎留言讨论 : )

内容简介

通过训练简单的CNN模型 (Convolutional Neural Networks 卷积神经网络)来识别MNIST数据集中的手写数字,精确度高于99%。

环境准备

Python 3.6

pandas 0.24.2

tensorflow 1.13.1 / keras 2.2.4

1. 导入模块 (Import module)

import pandas as pd

from keras import datasets, layers, models # 或 from tensorflow.keras import datasets, layers, models

2. 下载及处理MNIST数据集 (Download and prepare the MNIST dataset)

数据来源:keras.datasets.minist

training data: 60000 images, 28x28 pixels for each image

testing data: 10000 images, 28x28 pixels for each image

图像示例:

后附数据视觉化(Data visualization)代码

1f739f186e47

image.png

下载数据集:

(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()

处理数据集:

train_images = train_images.reshape((60000, 28, 28, 1))

test_images = test_images.reshape((10000, 28, 28, 1))

train_images, test_images = train_images / 255.0, test_images / 255.0 # 将像素值标准化为至0-1区间 (原为0-255)

数据视觉化(如果你想看看图片):

def visualize(X, label):

# Convert train datset to (num_images, img_rows, img_cols) format

X = X.reshape(X.shape[0], 28, 28)

for i in range(0, 9):

plt.subplot(330+(i+1))

plt.imshow(X[i], cmap=plt.get_cmap('gray'))

plt.title(label[i])

plt.show()

visualize(train_images, train_labels)

3. 建立模型 (Build the model)

model = models.Sequential()

model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28,28,1)))

model.add(layers.MaxPool2D(2, 2))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

model.add(layers.MaxPool2D(2, 2))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

model.add(layers.Flatten())

model.add(layers.Dense(64, activation='relu'))

model.add(layers.Dense(10, activation='softmax'))

4. 编译及训练模型 (Compile and train the model)

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

model.fit(train_images, train_labels, epochs=5)

5. 评估模型 (Evaluate the model)

test_loss, test_acc = model.evaluate(test_images, test_labels)

print("Test Accuracy: ", test_acc)

准确率 Accuracy: 99.16%

1f739f186e47

image.png

6. 输出预测结果 (Output the predictions)

predictions = model.predict_classes(test_images, verbose=0)

submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)),

"Label": predictions})

submissions.to_csv("predictions_cnn.csv", index=False, header=True)

即可在当前文件夹中看到predictions_cnn.csv文件,内含预测结果。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值