Model has different outputs in different languages using OpenCV DNN

I’m feeding the same model with the same input (same image that pass by the same operations) parameters in C++ and Python, but they are giving me different outputs. I know the Python outputs is what I expect as an output. But why C++ is having this behavior?
My C++ code:

...

	cv::Mat img = imread("debugg.bmp");

	cv::dnn::Image2BlobParams params;
	params.scalefactor = 1.0;
	params.size = cv::Size2i::Size_(30, 30);
	params.mean = 0.0;
	params.ddepth = CV_32F;
	params.datalayout = cv::dnn::DNN_LAYOUT_NHWC;
	params.paddingmode = cv::dnn::DNN_PMODE_NULL;
	params.swapRB = false;

	cv::Mat imgFloat;
	img.convertTo(imgFloat, CV_32F);
	imgFloat /= 255.0f;

	cv::Mat blobPB;
	cv::dnn::blobFromImageWithParams(imgFloat, blobPB, params);
	
	modelPB.setInput(blobPB);
	cv::Mat outputPB = modelPB.forward();

Python code:

...
params = cv2.dnn.Image2BlobParams()

params.scalefactor = 1.0
params.size = (30, 30)
params.mean = 0.0
params.ddepth = cv2.CV_32F
params.datalayout = cv2.dnn.DNN_LAYOUT_NHWC
params.paddingmode = cv2.dnn.DNN_PMODE_NULL

img = cv2.imread("debugg.bmp")
img_float = img.astype(np.float32)
img_float /= 255

blobPB = cv2.dnn.blobFromImageWithParams(img_float, params)
modelopenCV.setInput(blobPB)

output = modelopenCV.forward()

check this post Image2BlobParams C++ and python - #11 by Zihao_Mu
issue
and PR

1 Like

long story short:

please try to fill in ALL 3 Scalar members, for both mean & scaleFactor, for now:

  • c++
    params.mean = Scalar(0,0,0);
    params.scaleFactor = Scalar(1,1,1);

  • python
    params.mean = (0,0,0);
    params.scaleFactor = (1,1,1);

1 Like

Solved! Thanks both of you guys, always so fast and attentive. @berak @laurent.berger