Different results in c++inference when compared to python inference

username: asgardian
cooked: <p>Hi,<br>
I used the below code for c++ implementation and the results are completely different with the python results.<br>
Please provide suggestions.</p>
<p><span class="hashtag">#include</span> &lt;torch/script.h&gt; // One-stop header.</p>
<p><span class="hashtag">#include</span><br>
<span class="hashtag">#include</span><br>
<span class="hashtag">#include</span> &lt;opencv2/opencv.hpp&gt;</p>
<p><span class="hashtag">#include</span></p>
<p>std::string image_path = “/home/Desktop/libtorch-cxx11-abi-shared-with-deps-1.9.0+cpu/libtorch/example-app-FAN/crop_FRAME0020.jpg”;</p>
<p>int main(int argc, const char* argv[]) {<br>
if (argc != 2) {<br>
std::cerr &lt;&lt; “usage: example-app \n”;<br>
return -1;<br>
}</p>
<p>torch::jit::script::Module module;<br>
try {<br>
// Deserialize the ScriptModule from a file using torch::jit::load().<br>
module = torch::jit::load(argv[1]);<br>
}<br>
catch (const c10::Error&amp; e) {<br>
std::cerr &lt;&lt; “error loading the model\n”;<br>
return -1;<br>
}</p>
<p>std::cout &lt;&lt; “ok\n”;<br>
// Create a vector of inputs.<br>
std::vectortorch::jit::IValue inputs;<br>
inputs.push_back(torch::ones({1, 3, 256, 256}));</p>
<p>//Input image<br>
auto image = cv::imread(image_path, cv::ImreadModes::IMREAD_COLOR);<br>
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);</p>
<p>// Convert to Tensor<br>
torch::Tensor tensor_image = torch::from_blob(image.data,{ image.rows, image.cols,3 }, torch::kByte); /<br>
tensor_image = tensor_image.permute({ 2,0,1 });<br>
tensor_image = tensor_image.toType(torch::kFloat);<br>
tensor_image = tensor_image.div(255);<br>
tensor_image = tensor_image.unsqueeze(0);</p>
<p>// Execute the model and turn its output into a tensor.</p>
<p>auto output = module.forward({tensor_image}) ;<br>
std::cout&lt;&lt;output&lt;&lt;"\n";</p>
<p>}<br>
The model inference in python returns a lis of values.After using this code,I am getting o/p like “Columns 10 to 18 0.2104 0.1923 0.2044” etc.I dont have any idea why it’s like that.Hi,<br>
I used the below code for c++ implementation and the results are completely different with the python results.<br>
Please provide suggestions.</p>
<p><span class="hashtag">#include</span> &lt;torch/script.h&gt; // One-stop header.</p>
<p><span class="hashtag">#include</span><br>
<span class="hashtag">#include</span><br>
<span class="hashtag">#include</span> &lt;opencv2/opencv.hpp&gt;</p>
<p><span class="hashtag">#include</span></p>
<p>std::string image_path = “/home/Desktop/libtorch-cxx11-abi-shared-with-deps-1.9.0+cpu/libtorch/example-app-FAN/crop_FRAME0020.jpg”;</p>
<p>int main(int argc, const char* argv[]) {<br>
if (argc != 2) {<br>
std::cerr &lt;&lt; “usage: example-app \n”;<br>
return -1;<br>
}</p>
<p>torch::jit::script::Module module;<br>
try {<br>
// Deserialize the ScriptModule from a file using torch::jit::load().<br>
module = torch::jit::load(argv[1]);<br>
}<br>
catch (const c10::Error&amp; e) {<br>
std::cerr &lt;&lt; “error loading the model\n”;<br>
return -1;<br>
}</p>
<p>std::cout &lt;&lt; “ok\n”;<br>
// Create a vector of inputs.<br>
std::vectortorch::jit::IValue inputs;<br>
inputs.push_back(torch::ones({1, 3, 256, 256}));</p>
<p>//Input image<br>
auto image = cv::imread(image_path, cv::ImreadModes::IMREAD_COLOR);<br>
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);</p>
<p>// Convert to Tensor<br>
torch::Tensor tensor_image = torch::from_blob(image.data,{ image.rows, image.cols,3 }, torch::kByte); /<br>
tensor_image = tensor_image.permute({ 2,0,1 });<br>
tensor_image = tensor_image.toType(torch::kFloat);<br>
tensor_image = tensor_image.div(255);<br>
tensor_image = tensor_image.unsqueeze(0);</p>
<p>// Execute the model and turn its output into a tensor.</p>
<p>auto output = module.forward({tensor_image}) ;<br>
std::cout&lt;&lt;output&lt;&lt;"\n";</p>
<p>}<br>
The model inference in python returns a lis of values.After using this code,I am getting o/p like “Columns 10 to 18 0.2104 0.1923 0.2044” etc.I dont have any idea why it’s like that.</p>