13059l 2023-03-05 21:27 采纳率: 70.6%
浏览 59

opencv读取onnx模型出错


void Resnet::recognition(cv::Mat& srcImg, cv::dnn::Net& net) {


    cv::Mat blob;
    cv::Mat netInPutImage = srcImg.clone();
    netInPutImage.resize(netWidth, netHeight);
    cv::dnn::blobFromImage(netInPutImage, blob, 1 / 255.0, cv::Size(256,256), cv::Scalar(0, 0, 0), true, false);
    net.setInput(netInPutImage);

    cv::Mat prob = net.forward();
    cv::Point classIdPoint;
    double confidence;
    minMaxLoc(prob.reshape(1, 1), 0, &confidence, 0, &classIdPoint);
    int classId = classIdPoint.x;

    std::string label = cv::format("Predicted Class : %s, confidence : %.3f", (className[classId].c_str()), confidence);


    cv::putText(srcImg, label, cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 0.6, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);

    cv::imshow("Classification Output", srcImg);
    cv::waitKey(0);
}
这是识别的代码,

img


这是模型导出的代码。batch是32,分成了19类也就是32*19.

img


这是报错
下面是我的onnx导出代码

import torch
import torch.nn
import onnx
 
model = torch.load('./baocunmoxing/faceall.pt')
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))#使用cuda
model = model.to(device)
model.eval()

input_name = 'input'
output_name = 'output'
batch_size = '32'
x = torch.randn(32,3,256,256,requires_grad=True)
x = x.to(device)
torch.onnx.export(model, 
                      x, 
                      "face.onnx",
                      opset_version=11,
                      input_names=[input_name],
                      output_names=[output_name]
                      )

  • 写回答

1条回答 默认 最新

  • 「已注销」 2023-03-05 22:30
    关注

    你把读取过程和报错都发给我看一下

    评论

报告相同问题?

问题事件

  • 创建了问题 3月5日

悬赏问题

  • ¥15 Windows Script Host 无法找到脚本文件"C:\ProgramData\Player800\Cotrl.vbs”
  • ¥15 matlab自定义损失函数
  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图