我如何重塑Mat张量在c++中使用深度神经网络?



我想在c++应用程序中部署一个经过训练的深度神经网络。在阅读图像并使用blobFromImage(我使用opencv 4.4)函数后,我收到了blow错误,这表明我有张量的尺寸和形状问题。深度神经网络的输入为(h=150, w=100, channel=3)。blobFromImage函数是制作张量的唯一方法吗?我该如何解决这个问题?提前感谢。我的代码和错误。

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>

int main() {
std::vector< cv::Mat > outs;
std::cout << "LOAD DNN in CPP Project!" << std::endl;
cv::Mat image = cv::imread("example.png",1/*, cv::IMREAD_GRAYSCALE*/);
cv::dnn::Net net;
net = cv::dnn::readNetFromONNX("model.onnx");
cv::Mat blob;
cv::dnn::blobFromImage(image, blob, 1/255, cv::Size(100,150), cv::Scalar(0,0,0), false,false);
net.setInput(blob);
net.forward( outs, "output");
return 0;
}

,错误是:

global /home/hasa/opencv4.4/opencv-4.4.0/modules/dnn/src/dnn.cpp (3441) getLayerShapesRecursively OPENCV/DNN: [Convolution]:(model/vgg19/block1_conv1/BiasAdd:0):  getMemoryShapes() throws exception. inputs=1 outputs=0/1 blobs=2
[ERROR:0] global /home/hasa/opencv4.4/opencv-4.4.0/modules/dnn/src/dnn.cpp (3447) getLayerShapesRecursively     input[0] = [ 1 100 3 150 ]
[ERROR:0] global /home/hasa/opencv4.4/opencv-4.4.0/modules/dnn/src/dnn.cpp (3455) getLayerShapesRecursively     blobs[0] = CV_32FC1 [ 64 3 3 3 ]
[ERROR:0] global /home/hasa/opencv4.4/opencv-4.4.0/modules/dnn/src/dnn.cpp (3455) getLayerShapesRecursively     blobs[1] = CV_32FC1 [ 64 1 ]
[ERROR:0] global /home/hasa/opencv4.4/opencv-4.4.0/modules/dnn/src/dnn.cpp (3457) getLayerShapesRecursively Exception message: OpenCV(4.4.0) /home/hasa/opencv4.4/opencv- 4.4.0/modules/dnn/src/layers/convolution_layer.cpp:346: error: (-2:Unspecified error)  Number of input channels should be multiple of 3 but got 100 in function  'getMemoryShapes'
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.4.0) /home/hasa/opencv4.4/opencv-  4.4.0/modules/dnn/src/layers/convolution_layer.cpp:346: error:  (-2:Unspecified error) Number of input channels should be multiple of  3 but got 100 in function 'getMemoryShapes'

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

使用。onnx模型需要一些改变。

如果我加载tensorflow模型,下面的代码可以为我工作。

inputNet = cv::dnn::readNetFromTensorflow(pbFilePath);
// load image of rowsxcols = 100x150
cv::Mat img, imgn, blob;
img = cv::imread("1.jpg");
//cv::cvtColor(img, img, CV_GRAY2RGB);// convert gray to color image

// normalize image (if needed)
//img.convertTo(imgn, CV_32FC3);//float32, 3channels (depends on your model)
//imgn = (imgn-127.5)/128.0;//normalized crop (in rgb)

//extract feature vector
cv::dnn::blobFromImage(imgn, blob, 1.0, cv::Size(100, 150),0, false, false);
inputNet.setInput(blob);
cv::Mat feature_vector = inputNet.forward();

如果我加载。onnx模型,那么我需要像这样改变尺寸:

std::cout << blob.size << std::endl;// NCHW
blob = blob.reshape(1, {1, 100,150, 3});// NHWC
std::cout << blob.size << std::endl;
inputNet.setInput(blob);

相关内容

  • 没有找到相关文章

最新更新