Cannot obtain same output tensor values in C++ side
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:



Left side includes Python output tensor values and the prediction results for each input picture.

Right side includes C++ output tensor values and the prediction results for each input picture.

Could you offer a solution to obtain same output tensor values and prediction results?

I noticed that I used opencv functions to apply normalization using this code:

subtract(image, Scalar(0.485, 0.456, 0.406), temp);
divide(temp, Scalar(0.229, 0.224, 0.225), image);


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:

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);


Thus, I obtained the same input tensor values compared to Python side. After prediction, I obtained the same output tensor values. My problem solved.
