Help with a tracing NeuralStyle transfer model to C++

dambo: <p>Dear all,<br>
As part of my small project (<a href="https://github.com/QuantScientist/PngTorch" rel="noopener nofollow ugc">https://github.com/QuantScientist/PngTorch</a>) I am using the trained models from here <a href="https://github.com/gnsmrky/pytorch-fast-neural-style-for-web" class="inline-onebox" rel="noopener nofollow ugc">GitHub - gnsmrky/pytorch-fast-neural-style-for-web: Make PyTorch fast-neural-style to run inference with ONNX.js in web browsers</a> which I used before with OpenCV in C++ without any issues.<br>
The full example to reproduce the issue is here:<br>
<a href="https://github.com/QuantScientist/PngTorch/blob/master/src/example002.cpp" rel="noopener nofollow ugc">https://github.com/QuantScientist/PngTorch/blob/master/src/example002.cpp</a></p>
<p>Since my project does not use OpenCV, I am using libpng and there are differences between the resulting image in PyTorch VS traced model in C++. The project is based on CMake and if you clone it all the dependencies are downloaded automatically for you.</p>
<p>For instance original image:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/a/5a65d40e400c5fc9b57b2206e067237106b869e3.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/5a65d40e400c5fc9b57b2206e067237106b869e3" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/a/5a65d40e400c5fc9b57b2206e067237106b869e3_2_500x500.jpeg" alt="image" data-base62-sha1="cTH8ACeptCoG3yJUGrVicbmYX3t" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/a/5a65d40e400c5fc9b57b2206e067237106b869e3_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/5/a/5a65d40e400c5fc9b57b2206e067237106b869e3_2_750x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/a/5a65d40e400c5fc9b57b2206e067237106b869e3_2_1000x1000.jpeg 2x" data-dominant-color="8B8078"><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">image</span><span class="informations">1080×1080 231 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>Original style transfer with the candy.pth:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/5/45416b86b007521b270921887338c3b43eb4bfa6.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/45416b86b007521b270921887338c3b43eb4bfa6" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/5/45416b86b007521b270921887338c3b43eb4bfa6_2_500x500.jpeg" alt="image" data-base62-sha1="9SF6E8wTbMezokEf9GW6lVICC1w" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/5/45416b86b007521b270921887338c3b43eb4bfa6_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/4/5/45416b86b007521b270921887338c3b43eb4bfa6_2_750x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/5/45416b86b007521b270921887338c3b43eb4bfa6_2_1000x1000.jpeg 2x" data-dominant-color="AB9F97"><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">image</span><span class="informations">1080×1080 652 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><br>
My style transfer with the traced candy.pt (<a href="https://github.com/QuantScientist/PngTorch/blob/master/resources/mosaic_cpp.pt" rel="noopener nofollow ugc">https://github.com/QuantScientist/PngTorch/blob/master/resources/mosaic_cpp.pt</a>):</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/6/a6adbb32d8ddb169a12029ef6cb85be284cf67c1.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/a6adbb32d8ddb169a12029ef6cb85be284cf67c1" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a6adbb32d8ddb169a12029ef6cb85be284cf67c1_2_500x500.jpeg" alt="image" data-base62-sha1="nMvtBMb9cd4OCwi5fHueLH2lStb" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a6adbb32d8ddb169a12029ef6cb85be284cf67c1_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a6adbb32d8ddb169a12029ef6cb85be284cf67c1_2_750x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a6adbb32d8ddb169a12029ef6cb85be284cf67c1_2_1000x1000.jpeg 2x" data-dominant-color="555F68"><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">image</span><span class="informations">1080×1080 1.33 MB</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>I am sure the issue is here in <a href="https://github.com/QuantScientist/PngTorch/blob/master/include/utils/vision_utils.hpp" rel="noopener nofollow ugc">https://github.com/QuantScientist/PngTorch/blob/master/include/utils/vision_utils.hpp</a> in these lines:</p>
<pre><code class="lang-auto">png::image&lt;png::rgb_pixel&gt; VisionUtils::torchToPng(torch::Tensor &amp;tensor_){
//    out_tensor = out_tensor.mul(255).clamp(0, 255).to(torch::kU8);
    torch::Tensor tensor = tensor_.squeeze().detach().cpu().permute({1, 2, 0});  // {C,H,W} ===&gt; {H,W,C}
    tensor = tensor.mul(255).clamp(0, 255).to(torch::kU8); **// HERE** 
//    tensor = tensor.mul(0.5).add(0.5).mul(255).clamp(0, 255).to(torch::kU8);

    size_t width = tensor.size(1);
    size_t height = tensor.size(0);
    auto pointer = tensor.data_ptr&lt;unsigned char&gt;();
    png::image&lt;png::rgb_pixel&gt; image(width, height);
    for (size_t j = 0; j &lt; height; j++){
        for (size_t i = 0; i &lt; width; i++){
            image[j][i].red = pointer[j * width * 3 + i * 3 + 0];
            image[j][i].green = pointer[j * width * 3 + i * 3 + 1];
            image[j][i].blue = pointer[j * width * 3 + i * 3 + 2];
        }
    }
    return image;
}
</code></pre>
<p>I did dozens of experiments, but it seems I am overlooking something in the conversion process.</p>
<p>Any help would be appreciated.</p>
<p>Thanks</p>

dambo: <p>Issue resolved. Updated code is here:<br>
</p><aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/QuantScientist/PngTorch" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars1.githubusercontent.com/u/18743986?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/QuantScientist/PngTorch" target="_blank" rel="nofollow noopener">QuantScientist/PngTorch</a></h3>

<p>An integration of the libpng /png++ libraries with the Libtorch C++ Deep Learning Library.  - QuantScientist/PngTorch</p>


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