Ask Your Question

Revision history [back]

different result dnn in python and C++

Hi All,

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/raw/master/my_model.pb

platform windows7 64 python 3.6 opencv 3.3.0.10

model = tf_model_path
net = cv2.dnn.readNetFromTensorflow(model)
x_myTest_list=[]
for filename in glob.iglob("D:\\test\\*.bmp", recursive=True):
    input_img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    cv2.imshow("input_img", input_img)
    cv2.waitKey(1)
    x_myTest_list.append(input_img)
for img in x_myTest_list:
    img_input = cv2.dnn.blobFromImage(img, 0.00390625,(80,120))
    #print(img_input.shape)
    net.setInput(img_input, "conv2d_1_input")
    prob = net.forward("dense_2/Softmax")
    print(prob)

output:

[[ 9.99999642e-01 3.71539727e-07]]

[[ 9.99998093e-01 1.88101569e-06]]

[[ 9.99998927e-01 1.06129596e-06]].......


platform same (windows7 64 ), msvc2015, opencv 3.3.1

for (int i = 0; i < imageFiles.size(); i++)
{
    Mat img = imread(imageFiles[i], 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");
        prob = net.forward("dense_2/Softmax");output
        PrintfMat(prob, "prob");
    }
    catch (cv::Exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
        system("pause");
    }
}

output:

Net Outputs(1):

dense_2/Softmax

0.53188 0.46812

0.54745 0.45255

0.57105 0.42895...

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/raw/master/test.bmp ]

[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/raw/master/my_model.pb

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)
x_myTest_list=[]
for filename in glob.iglob("D:\\test\\*.bmp", recursive=True):
    
img = cv2.imread("D:\\test.bmp", cv2.IMREAD_GRAYSCALE)
#print(img)
#print(img.shape)
input_img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    cv2.imshow("input_img", input_img)
    cv2.waitKey(1)
    x_myTest_list.append(input_img)
for img in x_myTest_list:
    img_input = cv2.dnn.blobFromImage(img, 0.00390625,(80,120))
    #print(img_input.shape)
    net.setInput(img_input, #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)

[[ 9.99999642e-01 3.71539727e-07]]

[[ 9.99998093e-01 1.88101569e-06]]

[[ 9.99998927e-01 1.06129596e-06]].......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 i y = 0; i y < imageFiles.size(); i++)
{
    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(imageFiles[i], 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");
    "conv2d_1_input");        //set the network input
    prob = net.forward("dense_2/Softmax");output
    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

0.53188 0.46812

0.54745 0.45255

0.57105 0.42895...

prob

0.45581 0.54419