• Tensorflow2.0笔记28——应用程序,给图识物


    Tensorflow2.0笔记

    本博客为Tensorflow2.0学习笔记,感谢北京大学微电子学院曹建老师

    2.6 应用程序,给图识物

    1.给图识物

    输入一张手写数字图片:

    image-20210623204734996

    神经网络自动识别出值:6

    手写十个数,正确率 90%以上合格。

    2.前向传播执行应用

    predict(输入数据, batch_size=整数) 返回前向传播计算结果

    注:predict 参数详解。(1)x:输入数据,Numpy 数组(或者 Numpy 数组的列表,如果模型有多个输出);(2)batch_size:整数,由于 GPU 的特性,batch_size最好选用 8,16,32,64……,如果未指定,默认为 32;(3)verbose: 日志显示模式,0 或 1;(4)steps: 声明预测结束之前的总步数(批次样本),默认值 None; (5)返回:预测的 Numpy 数组(或数组列表)。

    from PIL import Image
    import numpy as np
    import tensorflow as tf
    
    model_save_path = './checkpoint/mnist.ckpt'
    
    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(10, activation='softmax')])
        
    model.load_weights(model_save_path)
    
    preNum = int(input("input the number of test pictures:"))
    
    for i in range(preNum):
        image_path = input("the path of test picture:")
        img = Image.open(image_path)
        img = img.resize((28, 28), Image.ANTIALIAS)
        img_arr = np.array(img.convert('L'))
    
        img_arr = 255 - img_arr
                    
        img_arr = img_arr / 255.0
        print("img_arr:",img_arr.shape)
        x_predict = img_arr[tf.newaxis, ...]
        print("x_predict:",x_predict.shape)
        result = model.predict(x_predict)
        
        pred = tf.argmax(result, axis=1)
        
        print('
    ')
        tf.print(pred)
    

    ​ 注:1、输出结果 pred 是张量,需要用 tf.print,print 打印出来是 tf.Tensor([1], shape=(1,), dtype=int64);2、去掉二值化,出现无法收敛问题,需要对数据集进行归一化。

    image-20210623205328736

    from PIL import Image
    import numpy as np
    import tensorflow as tf
    
    model_save_path = './checkpoint/mnist.ckpt'
    
    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(10, activation='softmax')])
    
    model.load_weights(model_save_path)
    
    preNum = int(input("input the number of test pictures:"))
    
    for i in range(preNum):
        image_path = input("the path of test picture:")
        img = Image.open(image_path)
        img = img.resize((28, 28), Image.ANTIALIAS)
        img_arr = np.array(img.convert('L'))
    
        for i in range(28):
            for j in range(28):
                if img_arr[i][j] < 200:
                    img_arr[i][j] = 255
                else:
                    img_arr[i][j] = 0
    
        img_arr = img_arr / 255.0
        x_predict = img_arr[tf.newaxis, ...]
        result = model.predict(x_predict)
    
        pred = tf.argmax(result, axis=1)
    
        print('
    ')
        tf.print(pred)
    

    image-20210623205441664

  • 相关阅读:
    洛谷 P1875 佳佳的魔法药水
    洛谷 P4822 [BJWC2012]冻结
    洛谷 P6175 无向图的最小环问题
    洛谷 P1312 Mayan游戏
    洛谷 P1311 选择客栈
    洛谷 T150024 矩形面积并(扫描线)
    洛谷 P1311 选择客栈
    洛谷 P1514 引水入城
    洛谷 P1310 表达式的值
    求和(团队题目)
  • 原文地址:https://www.cnblogs.com/wind-and-sky/p/14924840.html
Copyright © 2020-2023  润新知