zoukankan      html  css  js  c++  java
  • 深度学习 python 脚本实现 keras mninst 数字识别 预测端 code

    
    
    import numpy
    import skimage.io
    import matplotlib.pyplot as plt
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.layers import Dropout
    from keras.layers import Flatten
    from keras.layers.convolutional import Conv2D
    from keras.layers.convolutional import MaxPooling2D
    from 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 matrix
    img3 = 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-keras
    modelTrained = load_model('D:\works\jetBrians\PycharmProjects\tryPicture\my_model.h5')
    
    # we should get a correct answer is  2
    predict = modelTrained.predict(img3, verbose=0)
    #list of predicted labels and their probabilities
    print(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:applicationsAnaconda3python.exe D:/works/jetBrians/PycharmProjects/tryPicture/showPicture/ShowPicture.py
    Using TensorFlow backend.

    2018-03-08 20:43:29.102800: W C: f_jenkinshomeworkspace el-winMwindowsPY36 ensorflowcoreplatformcpu_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: f_jenkinshomeworkspace el-winMwindowsPY36 ensorflowcoreplatformcpu_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







  • 相关阅读:
    About Spring
    execle导入后 数据 无刷新 显示在 输入页面
    前端小神龟 -- 分页导航栏
    如何在mysql命令窗口获取到程序正在执行的sql语句
    div+css(ul li)实现图片上文字下列表布局
    C#异常之(已有打开的与此 Command 相关联,已有打开的与此命令相关联的 DataReader,必须首先将它关闭。)
    台灣VR內容產業聯盟_技術推廣中心
    2014新的一年到来,祝大家新年快乐
    matlab 提取文件路径名称,用于实现遍历读取文件(我的目的是遍历图像)
    无监督学习一些算法的简要概括(一)-稀疏自编码
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501187.html
Copyright © 2011-2022 走看看