Ask Your Question
0

different result dnn in python and C++

asked 2017-11-20 06:01:47 -0500

shawnLee103 gravatar image

updated 2017-11-20 23:16:30 -0500

Hi All,

[ I update my code to test easily ]

[upload my test img: https://github.com/shawnlee103/mytest... ]

[thanks ]

I trained a model in Python. But i want to use the model by C++.

different result dnn in python and C++.

someone could help? thanks

model file https://github.com/shawnlee103/mytest...

platform windows7 64 python 3.6 opencv 3.3.0.10

import cv2
tf_model_path= "D:\\my_model.pb"
model = tf_model_path
net = cv2.dnn.readNetFromTensorflow(model)

img = cv2.imread("D:\\test.bmp", cv2.IMREAD_GRAYSCALE)
#print(img)
#print(img.shape)
input_img = cv2.dnn.blobFromImage(img, 0.00390625,(80,120))
#print(input_img)
print("input_img.shape",input_img.shape)
net.setInput(input_img, "conv2d_1_input")
prob = net.forward("dense_2/Softmax")
print(prob)

output:

input_img.shape (1, 1, 120, 80)

[[ 0.89525342 0.10474654]]


platform same (windows7 64 ), msvc2015, opencv 3.3.1

void PrintfMat(cv::Mat srcImg, string title)
{
    printf("%s\n", title.c_str());
for (int y = 0; y < srcImg.rows; y++)
{
    for (int x = 0; x < srcImg.cols; x++)
    {
        if (srcImg.type() == 0)
            printf("%02d ", srcImg.at<uchar>(y, x));
        else if (srcImg.type() == 5)
            printf("%02.5f ", srcImg.at<float>(y, x));
    }
    printf("\n");
}
}
int main(int argc, char **argv)
{
CV_TRACE_FUNCTION();
String modelBin = "D:\\my_model.pb";
Net net;
try {
    net = dnn::readNetFromTensorflow(modelBin);
}
catch (cv::Exception& e) {
    std::cerr << "Exception: " << e.what() << std::endl;
    if (net.empty())
    {
        std::cerr << "Can't load network by using the mode file: " << std::endl;
        std::cerr << modelBin << std::endl;
        exit(-1);
    }
}
Mat img = imread("D:\\test.bmp", CV_LOAD_IMAGE_GRAYSCALE);
Mat inputBlob = dnn::blobFromImage(img, 0.00390625f, Size(80, 120), Scalar(), false);
Mat prob;
try {
    CV_TRACE_REGION("forward");
    net.setInput(inputBlob, "conv2d_1_input");        //set the network input
    prob = net.forward("dense_2/Softmax");                          //compute output
    PrintfMat(prob, "prob");
}
catch (cv::Exception& e) {
    std::cerr << "Exception: " << e.what() << std::endl;
    system("pause");
}
}

output:

Net Outputs(1):

dense_2/Softmax

prob

0.45581 0.54419


edit retag flag offensive close merge delete

Comments

hi there. it happens quite often lately, that ppl post duplicates, while in the moderation queue. it's probably not your fault, but if you remember anything weird on the website, while doing so, let us know, please !

berak gravatar imageberak ( 2017-11-20 06:11:12 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-11-20 06:16:05 -0500

berak gravatar image

updated 2017-11-20 09:19:08 -0500

try cv2.dnn.blobFromImage(img, 0.00390625,(80,120), swapRB=False)

(since this IS different between your c++ and python code)

edit flag offensive delete link more

Comments

making the openCV version conistant will be fine. I will close the question if i can.

shawnLee103 gravatar imageshawnLee103 ( 2017-11-23 19:03:51 -0500 )edit

I agree that C++ and Python should have consistent behaviour :)

StevenPuttemans gravatar imageStevenPuttemans ( 2017-11-24 02:48:26 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-11-20 06:01:47 -0500

Seen: 474 times

Last updated: Nov 23 '17