Ask Your Question

shawnLee103's profile - activity

2020-09-20 04:50:45 -0600 received badge  Student (source)
2017-11-24 02:48:49 -0600 marked best answer different result dnn in python and C++

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


2017-11-23 19:03:51 -0600 answered a question different result dnn in python and C++

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

2017-11-20 23:16:30 -0600 received badge  Editor (source)
2017-11-20 23:16:30 -0600 edited question different result dnn in python and C++

different result dnn in python and C++ Hi All, [ I update my code to test easily ] [upload my test img: https://githu

2017-11-20 06:08:42 -0600 asked a question different result dnn in python and C++.

different result dnn in python and C++. Hi All, I trained a model in Python. But i want to use the model by C++. diffe

2017-11-20 06:08:41 -0600 asked a question different result dnn in python and C++

different result dnn in python and C++ Hi All, I trained a model in Python. But i want to use the model by C++. differ