python小练习(083):基于keras快速搭建神经网络(进阶篇)之手写数字自动识别
本帖最后由 jerryxjr1220 于 2017-4-19 14:12 编辑在上一期python小练习(082)的基础上,今天我们继续用keras来实现手写数字图片的自动识别。
同样的方法可以实现网站验证码的自动识别,前提是要收集足够多的可用于学习的验证码原始图片,并且这些图片需要事先分好类。
下面就让我们开始吧。
原始的手写数字图片,如这个样子:
经过机器自动学习以后:
~~~~Training~~~~
Epoch 1/2
32/60000 [..............................] - ETA: 84s - loss: 2.2966 - acc: 0.1562
352/60000 [..............................] - ETA: 16s - loss: 1.5836 - acc: 0.5341
608/60000 [..............................] - ETA: 15s - loss: 1.3376 - acc: 0.6266
960/60000 [..............................] - ETA: 12s - loss: 1.0848 - acc: 0.6917
1280/60000 [..............................] - ETA: 11s - loss: 0.9546 - acc: 0.7328
1536/60000 [..............................] - ETA: 12s - loss: 0.8830 - acc: 0.7546
...
...
...
59104/60000 [============================>.] - ETA: 0s - loss: 0.2233 - acc: 0.9352
59392/60000 [============================>.] - ETA: 0s - loss: 0.2227 - acc: 0.9353
59744/60000 [============================>.] - ETA: 0s - loss: 0.2219 - acc: 0.9355
60000/60000 [==============================] - 11s - loss: 0.2218 - acc: 0.9356
Epoch 2/2
32/60000 [..............................] - ETA: 18s - loss: 0.3342 - acc: 0.9062
192/60000 [..............................] - ETA: 19s - loss: 0.2109 - acc: 0.9323
352/60000 [..............................] - ETA: 19s - loss: 0.1708 - acc: 0.9489
640/60000 [..............................] - ETA: 15s - loss: 0.1391 - acc: 0.9563
992/60000 [..............................] - ETA: 13s - loss: 0.1309 - acc: 0.9617
1280/60000 [..............................] - ETA: 13s - loss: 0.1236 - acc: 0.9625
1632/60000 [..............................] - ETA: 12s - loss: 0.1256 - acc: 0.9626
...
...
...
59008/60000 [============================>.] - ETA: 0s - loss: 0.1009 - acc: 0.9709
59296/60000 [============================>.] - ETA: 0s - loss: 0.1012 - acc: 0.9709
59488/60000 [============================>.] - ETA: 0s - loss: 0.1011 - acc: 0.9709
59776/60000 [============================>.] - ETA: 0s - loss: 0.1010 - acc: 0.9710
60000/60000 [==============================] - 11s - loss: 0.1008 - acc: 0.9710
~~~~Evaluation~~~~
32/10000 [..............................] - ETA: 2s
1216/10000 [==>...........................] - ETA: 0s
1792/10000 [====>.........................] - ETA: 0s
2496/10000 [======>.......................] - ETA: 0s
3552/10000 [=========>....................] - ETA: 0s
4704/10000 [=============>................] - ETA: 0s
5888/10000 [================>.............] - ETA: 0s
6592/10000 [==================>...........] - ETA: 0s
7584/10000 [=====================>........] - ETA: 0s
8704/10000 [=========================>....] - ETA: 0s
9856/10000 [============================>.] - ETA: 0s
loss: 0.108989, accuracy: 0.970900
可以看到,经过机器学习以后,数字识别的准确率达到了惊人的97%(还包括手写图片中写得确实很差的数字)
为了能直观得看到自动分类识别的结果,我把每个图片的识别概率转换为了二维的坐标,用不同颜色进行了标注,这样看得更清楚。
源代码及讲解:
**** Hidden Message ***** 楼主好强 可怕! 学习一下 棒棒哒 kankan 看看 增加了把分类的结果用图形化显示,这样看得更直观,可以看到机器确实做到了分类识别。
图中有部分图像重叠,是因为原本的10维数据本降维处理成2维,造成了特征的丢失,实际上在10维的维度上来看应该还是分得比较开的。 图形转化部分的代码:
# coding:utf-8
import numpy as np
from matplotlib import pyplot as plt
from keras.models import load_model
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
X_train = x_train.reshape(60000, 784) / 255.0
X_test = x_test.reshape(10000, 784) / 255.0
Y_train = np.array([ * 10 for i in range(60000)])
for i in range(60000):
Y_train] = 1
Y_test = np.array([ * 10 for i in range(10000)])
for i in range(10000):
Y_test] = 1
model = load_model('mnist.h5')
predicts = model.predict(X_test) * 255
pred = []
for i in range(len(predicts)):
px, py = 0, 0
for j in range(10):
r = predicts
px += np.sin(3.1415926 * j / 5) * r
py += np.cos(3.1415926 * j / 5) * r
pred.append()
pred = np.array(pred)
plt.scatter(pred[:, 0], pred[:, 1], c=y_test)
plt.show() 。 good {:5_90:} 学习学习 谢谢分享~~~!!!! 朝圣了,大大太厉害了 厉害了 学习下keras 过来学习深度学习知识 哈哈