[置顶] [5]深度学习和Keras----一个图像识别的简单Demo

标签: AIKeras深度学习图像识别
2073人阅读 评论(1) 收藏 举报
分类:

在第三篇文章中。笔者给出了如何用Keras的神经网络训练一个线性函数,在这篇文章,笔者给出如何用Keras的神经网络,对mnist图库中的60000张手写的1~9的数字进行识别的一个例子。本代码是参考自莫烦视频中的代码,但是加了一个改进,把其中的一个测试图片的二进制已经其对应的标签打印出来了。这样大家有一个更直观的印象。

具体代码如下:

import numpy as np
np.random.seed(1337)
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Activation
from keras.optimizers import RMSprop

# Download the mnist to the path ~/.keras/datasets/ if it is the first time to be called
#XShape(6000 28X28),y shape(10,000,)
(X_train,y_train),(X_test,y_test)=mnist.load_data()

print(X_test[0])
print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
print(y_test[0])
#Data pre-processing
X_train=X_train.reshape(X_train.shape[0],-1)/255 #Normalize
X_test=X_test.reshape(X_test.shape[0],-1)/255 #Normalize
y_train=np_utils.to_categorical(y_train,10)
y_test=np_utils.to_categorical(y_test,10)


#Other way to build your neural net
model=Sequential([
        Dense(32,input_dim=784),
        Activation('relu'),
        Dense(10),
        Activation('softmax')
        ])

#Other way to define optimizer
rmsprop=RMSprop(lr=0.001,rho=0.9,epsilon=1e-08,decay=0.0)
#model.compile(optimizer='rmsprop')
model.compile(
        optimizer='rmsprop',
        loss='categorical_crossentropy',
        metrics=['accuracy']
        )

print('Training..............')
#Other way to train the model
model.fit(X_train,y_train,epochs=2,batch_size=32)

print('\nTesting..............')
loss,accuracy=model.evaluate(X_test,y_test)
print('\nTest lost:',loss)
print('Test accuracy',accuracy)

输出结果如下:

ubuntu@keras:~/keraslearn$ python3 2ImageClassify.py
Using TensorFlow backend.
Training..............
Epoch 1/2
2017-06-04 14:53:27.941940: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-04 14:53:27.941986: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-04 14:53:27.941993: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-06-04 14:53:27.941997: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-04 14:53:27.942002: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
60000/60000 [==============================] - 3s - loss: 0.3439 - acc: 0.9045
Epoch 2/2
60000/60000 [==============================] - 3s - loss: 0.1953 - acc: 0.9437


Testing..............
 9856/10000 [============================>.] - ETA: 0s
Test lost: 0.174390699773
Test accuracy 0.9505
ubuntu@keras:~/keraslearn$ sudo vi 2ImageClassify.py
ubuntu@keras:~/keraslearn$ python3 2ImageClassify.py
Using TensorFlow backend.
[[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0  84 185 159 151  60  36   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0 222 254 254 254 254 241 198 198 198 198 198 198
  198 198 170  52   0   0   0   0   0   0]
 [  0   0   0   0   0   0  67 114  72 114 163 227 254 225 254 254 254 250
  229 254 254 140   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0  17  66  14  67  67  67  59
   21 236 254 106   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   83 253 209  18   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  22
  233 255  83   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 129
  254 238  44   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  59 249
  254  62   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 133 254
  187   5   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   9 205 248
   58   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 126 254 182
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0  75 251 240  57
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0  19 221 254 166   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   3 203 254 219  35   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0  38 254 254  77   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0  31 224 254 115   1   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0 133 254 254  52   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0  61 242 254 254  52   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0 121 254 254 219  40   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0 121 254 207  18   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]]
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
7
Training..............
Epoch 1/2
2017-06-04 14:55:54.309716: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-04 14:55:54.309750: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-04 14:55:54.309756: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-06-04 14:55:54.309761: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-04 14:55:54.309765: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
60000/60000 [==============================] - 3s - loss: 0.3430 - acc: 0.9050
Epoch 2/2
60000/60000 [==============================] - 3s - loss: 0.1944 - acc: 0.9441


Testing..............
 8992/10000 [=========================>....] - ETA: 0s
Test lost: 0.173723190146
Test accuracy 0.9514


@其中reshape的作用是把一个图片的28X28个像素的2维数组,转成一个一维度的数组。

reshape(X_train.shape[0],-1)/255

@np_utils.to_categorical方法是把1~9的数字,转成一个二进制数组中来表示。

比如, 7 被np_utils.to_categorical作用后,就会变成:

[ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]

7的数字对应的部分就变成了1,其他的部分变成了0.


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:385977次
    • 积分:6349
    • 等级:
    • 排名:第4081名
    • 原创:228篇
    • 转载:0篇
    • 译文:5篇
    • 评论:157条
    博客专栏