zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    WEB 文件上传
    solr 管理页面详解
    tomcat 修改端口
    solr 6.0 没有schema.xml未自动创建schema文件
    selenium 使用action进行鼠标,键盘操作
    Ubuntu1404安装eclipse(目的是为了运行python,当然java更可以)
    ubuntu1404安装
    如何在博客园中添加数学公式
    卷积cnn总结
    在Ubuntu1404的64bit版本下安装caffe
  • 原文地址:https://www.cnblogs.com/wind-and-sky/p/14924840.html
Copyright © 2011-2022 走看看