深度學習 python 指令碼實現 keras mninst 數字識別 預測端 code

來源:互聯網
上載者:User
import numpyimport skimage.ioimport matplotlib.pyplot as pltfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.layers import Dropoutfrom keras.layers import Flattenfrom keras.layers.convolutional import Conv2Dfrom keras.layers.convolutional import MaxPooling2Dfrom keras.models import load_model#if the picture is bigger than 28*28 will get below error#ValueError: cannot reshape array of size 775440 into shape (1,28,28,1)image = 'D:\\sthself\\ml\\reshape7.jpg'img2 = skimage.io.imread(image,as_grey=True)skimage.io.imshow(img2)plt.show()#img3 is a matriximg3 = numpy.reshape(img2,(1,28,28,1)).astype('float32')print(img3)# rebuild the model  ,do we need to add the layer ?  AttributeError: 'Sequential' object has no attribute 'load_model'#If you stored the complete model, not only the weights, in the HDF5 file, then it is as simple as#from keras.models import load_model#model = load_model('model.h5')# examples https://stackoverflow.com/questions/35074549/how-to-load-a-model-from-an-hdf5-file-in-kerasmodelTrained = load_model('D:\\works\\jetBrians\\PycharmProjects\\tryPicture\\my_model.h5')# we should get a correct answer is  2predict = modelTrained.predict(img3, verbose=0)#list of predicted labels and their probabilitiesprint(predict[0])#[ 0.04785086  0.02547075  0.06954221  0.03620625  0.01439319  0.03016909   0.03120618  0.00815302  0.70513636  0.03187207]# AttributeError: 'Sequential' object has no attribute 'prediect_classes'result = modelTrained.predict_proba(img3,batch_size=1, verbose=0)print(result)print("tensorflow hello word is done")

同事幫忙寫的數字


我自己寫的數字



程式列印log

重點說明: 我們自己的圖片應該是黑底白字 才能被識別

D:\applications\Anaconda3\python.exe D:/works/jetBrians/PycharmProjects/tryPicture/showPicture/ShowPicture.py
Using TensorFlow backend.

2018-03-08 20:43:29.102800: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\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.
2018-03-08 20:43:29.102800: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\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.
[  1.17482814e-05   1.08457927e-03   2.43252050e-02   3.06303948e-02
   1.07244858e-04   1.54377140e-05   1.01265108e-07   9.38272536e-01
   4.20123106e-04   5.13266213e-03]
[[  1.17482814e-05   1.08457927e-03   2.43252050e-02   3.06303948e-02
    1.07244858e-04   1.54377140e-05   1.01265108e-07   9.38272536e-01
    4.20123106e-04   5.13266213e-03]]
tensorflow hello word is done

Process finished with exit code 0







相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.