【Tensorflow】给图识物

【Tensorflow】给图识物

目标:输入一张手写图片,神经网络自动识别出值,输出识别结果

前向传播执行应用

predict(输入特征,batch_size=整数)  # 给定输入特征,经过预测,得到前向传播计算结果

实现预测仅需3步:

  1. 复现模型,实现前向传播
model = tf.keras.models.Sequential([
			tf.keras.layers.Flatten(),
			tf.keras.layers.Dense(128,activation='relu',
			tf.keras.layers.Dense(10,activation='softmax')])
  1. 加载参数
model.load_weights(model_save_path)
  1. 预测结果
result = model.predict(x_predict)

完整代码

from PIL import Image
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

# 模型参数存储路径及名称
model_save_path = './checkpoint/mnist.ckpt'
# 使用Sequential搭建神经网络,先将28x28的灰度图像拉平为一维向量,使用flatten
# 然后定义两层神经元,第一层有128个神经元,激活函数使用relu
# 第二层神经元是输出层,因为手写数字识别共有10种输出,因此设置10个神经元,使用softmax函数输出概率
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])
# 加载已有的最优参数,完善模型model
model.load_weights(model_save_path)
# 输入预测图片数量
preNum = int(input("input the number of test pictures:"))
# 循环输入图片路径,img = Image.open(ImgPath)打开的图片是PIL类型,默认RGB,自带reseize函数。
# 将PIL类型转化为numpy类型:im = numpy.array(img)才能看到shape属性,是(height, width, channel)数组,channel的通道数据是RGB。
for i in range(preNum):
    image_path = input("the path of test picture:")
    img = Image.open(image_path)
	# plt.imread()用于读取一张图片,将图像数据变成数组array如果是灰度图:返回(M,N)形状的数组,M表示高度,N表示宽度。
	# 如果是RGB图像,返回(M, N, 3) 形状的数组,M表示高度,N表示宽度。
	# 如果是RGBA图像,返回(M, N, 4) 形状的数组,M表示高度,N表示宽度。
	# 此外,PNG 图像以浮点数组 (0-1) 的形式返回,所有其他格式都作为 int 型数组返回,位深由具体图像决定。
    image = plt.imread(image_path)
    plt.set_cmap('gray')
    # plt.imshow(X, interpolation=None) 画出X
    # X:图像数据 
    # (M, N):标量数据的图像,灰度图  
    # (M, N, 3):RGB图像  
    # (M, N, 4):RGBA图像
    plt.imshow(image)
	# 图像压缩到28x28
    img = img.resize((28, 28), Image.ANTIALIAS)
    # 彩色图像转灰度图像 L (8-bit pixels, black and white),并转为数组
    img_arr = np.array(img.convert('L'))
	# 将28x28的图像像素点转为高对比度的黑白图片,保存了图片基本信息的同时滤去了背景噪声
    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
    # 增加一个维度,把28行28列的二维数据转为28行28列的三维数据
    x_predict = img_arr[tf.newaxis, ...]
    # 送入predict预测
    result = model.predict(x_predict)
    # 把最大的概率值的索引值赋给pred
    pred = tf.argmax(result, axis=1)

    print('\n')
    # 返回预测结果
    tf.print(pred)

    plt.pause(1)
    plt.close()

预测结果

在这里插入图片描述
会把5识别为3,其他的都识别正常。

猜你喜欢

转载自blog.csdn.net/qq_45746168/article/details/128073667