Cannot obtain same output tensor values in C++ side

sercan: <p>I try to make predictions for a 4-class classification project in C++ side using libtorch 1.4. However, I cannot obtain same predictions compared to Python side. Firstly, I obtain same input tensor values just before prediction. When I compare the output tensor values, I noticed that they are different. You can find those values in that picture:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/3/93f9ffae93722cba195d68d3db17dae21ba670fc.png" data-download-href="https://discuss.pytorch.org/uploads/default/93f9ffae93722cba195d68d3db17dae21ba670fc" title="Output_tensor_values_Python_and_C++"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/93f9ffae93722cba195d68d3db17dae21ba670fc_2_690x315.png" alt="Output_tensor_values_Python_and_C++" data-base62-sha1="l73O62HRCOpZeR07Gw8N9UOYSpm" width="690" height="315" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/93f9ffae93722cba195d68d3db17dae21ba670fc_2_690x315.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/93f9ffae93722cba195d68d3db17dae21ba670fc_2_1035x472.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/93f9ffae93722cba195d68d3db17dae21ba670fc_2_1380x630.png 2x" data-dominant-color="E8E8E8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Output_tensor_values_Python_and_C++</span><span class="informations">1883×861 114 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Left side includes Python output tensor values and the prediction results for each input picture.<br>
Right side includes C++ output tensor values and the prediction results for each input picture.</p>
<p>Could you offer a solution to obtain same output tensor values and prediction results?</p>

ptrblck: <p>Did you make sure to set the model to evaluation mode via <code>model.eval()</code> before executing the forward pass?<br>
Also, I assume you’ve checked the inputs already or are you using any (random) preprocessing steps?</p>

sercan: <p>I noticed that I used opencv functions to apply normalization using this code:</p>
<pre><code class="lang-auto">subtract(image, Scalar(0.485, 0.456, 0.406), temp);
divide(temp, Scalar(0.229, 0.224, 0.225), image);
</code></pre>
<p>This operation changes only the first channel and does not change other channels. For this reason, in fact, input tensor values are different. I applied normalization directly on tensor values writing this code:</p>
<pre><code>	tensor_image = tensor_image.permute({ 2,0,1 });//chw
	tensor_image = tensor_image.toType(torch::kFloat);
	tensor_image = tensor_image.div(255.0);
	//normalize
	tensor_image[0] = tensor_image[0].sub_(0.485).div_(0.229);
	tensor_image[1] = tensor_image[1].sub_(0.456).div_(0.224);
	tensor_image[2] = tensor_image[2].sub_(0.406).div_(0.225);
</code></pre>
<p>Thus, I obtained the same input tensor values compared to Python side. After prediction, I obtained the same output tensor values. My problem solved.</p>