Here https://stackoverflow.com/a/49817506/1277317 there is an example of how to use a convolution network in OpenCV. But this example is in Python. How to do the same in C++? Namely, how to do this in C++:
net = cv.dnn.readNetFromTensorflow('model.pb')
net.setInput(inp.transpose(0, 3, 1, 2))
cv_out = net.forward()
?
And how to create Mat for the setInput function for an image size: 60x162x1? I use float for the data just like in the python example. Now I have this code and it gives incorrect results:
Net net = readNet("e://xor.pb");
float x0[60][162];
for(int i=0;i<60;i++)
{
for(int j=0;j<162;j++)
{
x0[i][j]=0;
}
}
x0[5][59]=0.5;
x0[5][60]=1;
x0[5][61]=1;
x0[5][62]=0.5;
Mat aaa = cv::Mat(60,162, CV_32F, x0);
Mat inputBlob = dnn::blobFromImage(aaa, 1.0, Size(60,162));
net.setInput(inputBlob , "conv2d_input");
Mat prob = net.forward("activation_2/Softmax");
for(int i=0;i<prob.cols;i++)
{
qDebug()<<i<<prob.at<float>(0,i);
}
In openCV almost all functions are designed to work with 3D matrices. So the easiest way for me to work with CV_32F 4D matrices is to work with them directly. The following code works correctly and quickly:
Net net = readNet("e://xor.pb");
const int sizes[] = {1,1,60,162};
Mat tenz = Mat::zeros(4, sizes, CV_32F);
float* dataB=(float*)tenz.data;
int x=1;
int y=2;
dataB[y*tenz.size[2]+x]=0.5f;
x=1;
y=3;
dataB[y*tenz.size[2]+x]=1.0f;
try
{
net.setInput(tenz , "input_layer_my_input_1");
Mat prob = net.forward("output_layer_my/MatMul");
}
catch( cv::Exception& e )
{
const char* err_msg = e.what();
qDebug()<<"err_msg"<<err_msg;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.