C++ output differs from python output

username: sq1988
cooked: <p>Pytorch1.0,Ubuntu16.04,Same model,Same transformation,but I got totally different result with python and cpp code.<br>
Anyone can help me out?Thx a lot.</p>
<p>#========python output======#<br>
tensor(9073.5586, device=‘cuda:0’, grad_fn=)<br>
#========c++ output======#<br>
count:2.185325</p>
<p>I put the code in Baidu Cloud<br>
link：<a href="https://pan.baidu.com/s/1iQVndobbaSHERpIpJ0gSiw" rel="nofollow noopener">https://pan.baidu.com/s/1iQVndobbaSHERpIpJ0gSiw</a><br>
code：sbdd</p>

username: arlecchino
cooked: <p>My ?Chinese? knowledge to see the sources is somewhat limited <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: mhubii
cooked: <p>Hi <a class="mention" href="/u/sq1988">@sq1988</a>,</p>
<p>I can’t download your files. Can you please explain your problem in more detail here?</p>

username: mhubii
cooked: <p>Just as a hint, you can format code by using triple quotation marks.</p>
<pre><code class="lang-cpp">#include &lt;something.h&gt;

int main() {

    return 0;
}
</code></pre>
<p>Can you please additionally add the python code?</p>

username: sq1988
cooked: <pre><code class="lang-auto">#include &lt;torch/script.h&gt;
#include &lt;torch/torch.h&gt;
#include &lt;opencv2/opencv.hpp&gt;
#include &lt;opencv2/imgproc/imgproc.hpp&gt;
#include &lt;opencv2/core.hpp&gt;
#include &lt;iostream&gt;
#include &lt;memory&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
 
int main(int argc, const char *argv[]) {
    torch::DeviceType device_type;
    device_type = torch::kCUDA;  //torch::kCUDA  and torch::kCPU
    torch::Device device(device_type, 0);
    
    std::shared_ptr&lt;torch::jit::script::Module&gt; module = torch::jit::load("model.pt");
     
    assert(module != nullptr);
    std::cout &lt;&lt; "load model ok\n";
    module-&gt;to(device);
 
    auto image = cv::imread("284193,c89b000bf74ce6e.jpg");
    cv::Mat image_transfomed;
    cv::resize(image, image_transfomed, cv::Size(1024, 720),cv::INTER_AREA);
    cv::cvtColor(image_transfomed, image_transfomed, cv::COLOR_BGR2RGB);
 
    torch::Tensor tensor_image = torch::from_blob(image_transfomed.data, {image_transfomed.rows, image_transfomed.cols,3},torch::kByte).to(device);//hwc
    //ToTensor
    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);
    //add batch dimension
    tensor_image = tensor_image.unsqueeze(0);
    std::vector&lt;torch::jit::IValue&gt; inputs;
    inputs.emplace_back(tensor_image);
 
    auto t = (double) cv::getTickCount();
    at::Tensor output = module-&gt;forward(inputs).toTensor().cpu();
    at::IntList sizes = output.sizes();//shape
    //get result
    auto count = output[0][0].sum();
    t = (double) cv::getTickCount() - t;
    printf("count:%f,execution time = %gs\n",count,t / cv::getTickFrequency());
    inputs.pop_back();
    return 0;
}
</code></pre>
<pre><code class="lang-auto">import PIL.Image as Image
import torch
from torchvision import transforms
torch.backends.cudnn.benchmark = True
import torch
 
transform=transforms.Compose([
                       transforms.Resize((720, 1024)),
                       transforms.ToTensor(),
                       transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
                   ])
 
org = Image.open("284193,c89b000bf74ce6e.jpg")
img = transform(org).cuda()
traced_script_module = torch.jit.load('model.pt')
output = traced_script_module(img.unsqueeze(0))
print(output.sum())
</code></pre>

username: sq1988
cooked: <p>Any suggestion will be appreciated.</p>

username: mhubii
cooked: <p>what are the sizes of your output? Are they the same for python and cpp? So maybe you are not summing over all elements.</p>

username: sq1988
cooked: <p>1,1,720,1024 for c++ and python.<br>
Can you give me your email address so I can send you the model?</p>

username: mhubii
cooked: <p>you could make your code publicly available at github or something similar, and I will have a look at it</p>

username: sq1988
cooked: <p>I upload the code and model in Dropbox.<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://cfl.dropboxstatic.com/static/images/favicon-vflUeLeeY.ico" class="site-icon" width="16" height="16">
      <a href="https://www.dropbox.com/sh/xjgiwj2vee57b37/AACQrCtCAGSF71YLaxY3U8L-a?dl=0" target="_blank" rel="nofollow noopener">Dropbox</a>
  </header>
  <article class="onebox-body">
    <img src="https://www.dropbox.com/static/images/spectrum-icons/generated/content/content-folder_dropbox-large.png" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://www.dropbox.com/sh/xjgiwj2vee57b37/AACQrCtCAGSF71YLaxY3U8L-a?dl=0" target="_blank" rel="nofollow noopener">test</a></h3>

<p>Shared with Dropbox</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>

username: sq1988
cooked: <p>I upload the code and model in Dropbox.</p>

username: mhubii
cooked: <p>ye sry, I will try to check whats the difference, but there is no cuda available to me atm. Do you have the same problem, when you run the code on a cpu? May you save the script module with device type cpu?</p>

username: zhenglei0102
cooked: <p>Did you use batch norm in your code, if so ,you should call “module-&gt;eval()” before calling “module-&gt;forward()”</p>