【657】深度学习模型预测单张图片
直接用 predict 函数,batch_size 设置为 1
代码如下:
# 一个图片预测
from PIL import Image
import numpy as np
from tensorflow.keras.preprocessing.image import load_img
# 图片转为矩阵输入数据
def img2x(img_path):
img = Image.open(img_path)
x = np.array(img) / 255
x = np.expand_dims(x, axis=0)
return x
img_name = input("Input image name: ")
load_img("../models_Res-U-Net/alex_google_map/" + img_name + ".png").show()
img_path = "../models_Res-U-Net/alex_google_map/" + img_name + ".png"
x = img2x(img_path)
x = x[:,:,:,:3]
# 直接通过模型预测,得到输出数据
y = model.predict(x, batch_size=1)[0]
# 对于概率数据进行判断得到二值图
mask = (y > 0.5).astype('uint8')
img = keras.preprocessing.image.array_to_img(mask)
img.show()
效果如下:


浙公网安备 33010602011771号