首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >模型训练好后 预测 numpy图片

模型训练好后 预测 numpy图片

作者头像
用户6021899
发布2022-11-18 14:10:12
发布2022-11-18 14:10:12
5180
举报

神经网络训练好后,预测若干张图片(实际上是numpy 数组),可将numpy 数组转换成 size 为 (batch,channels, height, width), 类型为float 的 tersor 后,直接预测。不必有实体图片文件,不必建数据集。

注意要手动将数据归一化,mean值和标准差 与 训练集的 transforms 的归一化参数一致。

代码语言:javascript
复制
if __name__ == "__main__":
    model_path = "diecode_mode.pth"

    #device = "cuda" if torch.cuda.is_available  else "cpu"  # Get cpu or gpu device for training.
    device ="cpu"
    net = NeuralNetwork() # .to(device)
    net.load_state_dict(torch.load(model_path))

    images = np.random.random((4,1,50,36)) # batch, channels, height, width
    mean, std = 0.5, 0.5
    images = (images-mean) / std  # 对应 transforms.Normalize()

    # .to(torch.float) :权重默认都是浮点数,所以input也必须是浮点数
    # 否则报错 RuntimeError: expected scalar type Double but found Float
    images = torch.from_numpy(images).to(torch.float)

    #print(images)
    with torch.no_grad():
        outputs = net(images)
        _, predictions = torch.max(outputs, 1)
        print(predictions)
        print([classes[x] for x in predictions])
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-10-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档