# 使用单张图片测试
# 预览测试图片
import cv2
import numpy as np
path = "/home/aistudio/dog-2.png"
pictu = Image.open(path)
plt.imshow(pictu)
img_test = cv2.imread(path)
img_test = np.array(img_test)
img_test_flt = img_test.flatten()
with fluid.dygraph.guard():
# 读取模型
# 参数为保存模型参数的文件地址
model_dict, _ = fluid.load_dygraph('catornocat')
# 加载模型参数
model.load_dict(model_dict)
#评估模式
model.eval()
images = img_test_flt.astype(np.float32)
# 将numpy数据转为飞桨动态图variable形式
image = fluid.dygraph.to_variable(images)
# 前向计算
predict = model(image)
# 统计预测结果
# 将预测结果转为numpy数据类型
predict = predict.numpy()
predict = np.argmax(predict)
print("predict:({})".format(predict))
name = ["non-cat","cat"]
aa = name[int(predict)]
print(
"\n"
+ ", 1 means it's a cat picture, 0 means not "
+ "\nYou predict that it's a "
+ aa
+ " picture. \nCongrats!"
)
print("end")
paddlepaddle第三章猫分类任务导入个人图片进行预测
于 2023-04-29 13:11:13 首次发布