# Open CV dnn module python and c++ outputs are different

**URL:** <https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493>\
**Category:** C++\
**Tags:** dnn\
**Created:** [March 30, 2021, 11:47pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493 "2021-03-30T23:47:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hayk\_Gasparyan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hayk_gasparyan/32/552_2.png) [@Hayk\_Gasparyan](https://forum.opencv.org/u/Hayk_Gasparyan)\
**Post date:** [March 30, 2021, 11:47pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/1 "2021-03-30T23:47:10Z")

</div>

I have tf model (DeepLabv3 plus with MobileNet V2 backbone). In python open CV(4.5.1) I can read model .pb and do inference which is done correct. Here is my code.

my\_im = cv2.imread(‘test.jpg’)  
net = cv2.dnn.readNetFromTensorflow(‘DeepLabSegm.pb’)  
inputs = cv2.dnn.blobFromImage(my\_im, size=(513, 513),scalefactor=1/255.0, mean=[-123.68/255, -116.779/255,-103.939/255], swapRB=True)  
net.setInput(inputs)

outputs = net.forward()

This works fine and gives me output segmentation map.

But in C++, I have the same code and it gives wrong output Mat. In response I have very large numbers which are wrong.

Here is my C++ code.

 ![Code](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/b/ba976b91cc47376ca90129fd1bb4664fe0717cd9.png)

From here after debugging, I see that in my response data I have very large numbers like 471316160… This is strange and I have no idea why in python I can run same functions but in c++ I have such output response.

Here is my .pb file. [Deeplabv3 model - Google Drive](https://drive.google.com/drive/folders/1Mw7jClL6vBk3yTVeZIukJiVCn48FV3Z5?usp=sharing)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 31, 2021, 12:35pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/2 "2021-03-31T12:35:58Z")

</div>

just for comparison – can you show your python code as well, please ?

---

<div class="post-metadata">

**Author:** ![Hayk\_Gasparyan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hayk_gasparyan/32/552_2.png) [@Hayk\_Gasparyan](https://forum.opencv.org/u/Hayk_Gasparyan)\
**Post date:** [March 31, 2021, 12:54pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/3 "2021-03-31T12:54:19Z")

</div>

@berak Here is my python code.

```python
my_im = cv2.imread(im_path)
mean_sub = [-123.68/255, -116.779/255,-103.939/255]

net = cv2.dnn.readNetFromTensorflow(model_path)
inputs = cv2.dnn.blobFromImage(my_im, size=(513,513),scalefactor=1/255, mean=mean_sub, swapRB=True)
net.setInput(inputs)
outputs = net.forward()

plt.imshow(np.argmax(outputs[0], axis=0))
plt.show()

```

I have checked that input blobs are the same for python and C++. So I think the problem is either in forward function or in parsing the response in C++.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 31, 2021, 1:12pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/4 "2021-03-31T13:12:23Z")

</div>

aaand your c++ code AS TEXT, not as a silly image, so ppl can actually try it ?

(please NEVER post screenshots of code or errors anywhere, please…)

---

<div class="post-metadata">

**Author:** ![Hayk\_Gasparyan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hayk_gasparyan/32/552_2.png) [@Hayk\_Gasparyan](https://forum.opencv.org/u/Hayk_Gasparyan)\
**Post date:** [March 31, 2021, 1:33pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/5 "2021-03-31T13:33:23Z")

</div>

@berak Ok, Sorry. Here is my C++ code also

```cpp
cv::Mat faceImg= cv::imread(facePath);
cv::dnn::Net net = cv::dnn::readNetFromTensorflow(MODEL_PATH);
cv::Mat blob;
cv::Scalar scMean(-123.68/255, -116.779/255, -103.939/255);
cv::dnn::blobFromImage(faceImg, blob, 1.0/255.0, cv::Size(IMAGE_SIZE, IMAGE_SIZE), scMean, true);
net.setInput(blob);
cv::Mat response = net.forward();

const int rows = response.size[2]; //image height
const int cols = response.size[3]; //The width of the image
const int chns = response.size[1]; //Number of image channels
cv::Mat maxCl(rows, cols, CV_8UC1);
cv::Mat maxVal(rows, cols, CV_32FC1);

for (int c = 0; c < chns; c++)
{
	for (int row = 0; row < rows; row++)
	{
		const float* ptrScore = response.ptr<float>(0, c, row);
		uchar* ptrMaxCl = maxCl.ptr<uchar>(row);
		float* ptrMaxVal = maxVal.ptr<float>(row);
		for (int col = 0; col < cols; col++)
		{
			if (ptrScore[col] > ptrMaxVal[col])
			{
				ptrMaxVal[col] = ptrScore[col];
				ptrMaxCl[col] = (uchar)c;
			}
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 31, 2021, 2:44pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/6 "2021-03-31T14:44:07Z")

</div>

> [@Hayk\_Gasparyan](#):
>
> `cv::Mat maxVal(rows, cols, CV_32FC1);`

should have initialized memory, since you compare it:

`cv::Mat maxVal(rows, cols, CV_32FC1, 0.0f);`

apart from that, sorry, i cannot reproduce the problem from c++, the values in the response look all reasonable.

if you really only have 2 classes / channels as output, you might simplify it to:

```
Mat ch1(IMAGE_SIZE, IMAGE_SIZE, CV_32F, response.ptr<float>(0,0)); // bg
Mat ch2(IMAGE_SIZE, IMAGE_SIZE, CV_32F, response.ptr<float>(0,1)); // fg

Mat res;
cv::compare(ch1, ch2, res, cv::CMP_LT);|

```

---

<div class="post-metadata">

**Author:** ![Hayk\_Gasparyan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hayk_gasparyan/32/552_2.png) [@Hayk\_Gasparyan](https://forum.opencv.org/u/Hayk_Gasparyan)\
**Post date:** [March 31, 2021, 3:03pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/7 "2021-03-31T15:03:01Z")

</div>

@berak  
Thanks for response, I have changed the code like yours, but for test image I only get background pixel predictions. If you test an image do you get reasonable result for background segmentation?

When I run this code after getting response,

for (int row = 0; row \< 512; row++) {  
const float\* ptrScore = response.ptr(0, 0, row);  
std::cout \<\< ptrScore \<\< std::endl;  
}  
I am getting numbers

00000171AF35B080  
00000171AF35B884  
00000171AF35C088  
00000171AF35C88C  
00000171AF35D090

What version of openCV do you use? May be the problem is in the version

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 31, 2021, 3:40pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/8 "2021-03-31T15:40:07Z")

</div>

> [@Hayk\_Gasparyan](#):
>
> std::cout \<\< ptrScore \<\< std::endl;

will print out the _address_, not the _value_, which would be:

```
std::cout << *ptrScore << std::endl; // "de-ref" the pointer !

```

* * *

using this input:  
 ![person](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/e/e29723ce5dfb713e70e94fa08053d502889b15e8.png)

i get:

 ![segm](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/af84c1c07a29e17cf10f1e85037a166174a9cb92.png)

> [@Hayk\_Gasparyan](#):
>
> What version of openCV do you use?

latest master (4.5.2-dev)

---

<div class="post-metadata">

**Author:** ![Hayk\_Gasparyan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hayk_gasparyan/32/552_2.png) [@Hayk\_Gasparyan](https://forum.opencv.org/u/Hayk_Gasparyan)\
**Post date:** [March 31, 2021, 3:43pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/9 "2021-03-31T15:43:54Z")

</div>

Ah, sorry…

with \*ptrScore I am getting values something like this 4.71316e+08… for channel 1,  
and -4.03595e+08 for channel 2… But in python I am getting values something like 2.5 or -1.7

Mine is 4.5.1. can update of version help?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 31, 2021, 3:45pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/10 "2021-03-31T15:45:54Z")

</div>

> [@Hayk\_Gasparyan](#):
>
> But in python I am getting values something like 2.5 or -1.7

i’m getting the same 2.5 or -1.7 values in c++

> [@Hayk\_Gasparyan](#):
>
> Mine is 4.5.1. can update of version help?

unlikely. problem is somewhere in your code

---

<div class="post-metadata">

**Author:** ![Hayk\_Gasparyan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hayk_gasparyan/32/552_2.png) [@Hayk\_Gasparyan](https://forum.opencv.org/u/Hayk_Gasparyan)\
**Post date:** [March 31, 2021, 3:46pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/11 "2021-03-31T15:46:59Z")

</div>

Strange… Can you send me your full code of c++ please?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 31, 2021, 3:50pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/12 "2021-03-31T15:50:16Z")

</div>

> <https://gist.github.com/berak/1c676b2f9c0ec0f40e70e5fbfe4f62df>

---

<div class="post-metadata">

**Author:** ![Hayk\_Gasparyan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hayk_gasparyan/32/552_2.png) [@Hayk\_Gasparyan](https://forum.opencv.org/u/Hayk_Gasparyan)\
**Post date:** [March 31, 2021, 4:21pm UTC](https://forum.opencv.org/t/open-cv-dnn-module-python-and-c-outputs-are-different/2493/13 "2021-03-31T16:21:46Z")

</div>

Thanks, I am using full copy of your code and the output is the same :). Are you trying this on Intel processor?
