I have a problem getting the resulting image partitioned when i use c++ frontend,,,

username: Joseph_Ahn
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/e/7e535c42727846616498d7e619265a49139e089d.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/7e535c42727846616498d7e619265a49139e089d" title="res3"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/e/7e535c42727846616498d7e619265a49139e089d_2_500x500.jpeg" alt="res3" data-base62-sha1="i1wL7LNQsSMepY0HpxUAktohKPP" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/e/7e535c42727846616498d7e619265a49139e089d_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/7/e/7e535c42727846616498d7e619265a49139e089d_2_750x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/7/e/7e535c42727846616498d7e619265a49139e089d_2_1000x1000.jpeg 2x" data-dominant-color="863143"><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">res3</span><span class="informations">1042×1042 260 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>As shown above, I always get the resulting image divided into 9 equal parts.<br>
This is really annoying me. Why is this happening,?</p>
<pre><code class="lang-auto">torch::Tensor Mat_To_Tensor(Mat mat)
{
    std::cout &lt;&lt; "converting mat to tensor\n";

    mat.convertTo(mat, CV_32FC3, 1.0 / 255.0f);

    torch::Tensor tensor = torch::from_blob(mat.data, { mat.channels(), mat.rows, mat.cols }, torch::kFloat);
    
    tensor = tensor.unsqueeze_(0);
    cout &lt;&lt; "tensor sizes : " &lt;&lt; tensor.sizes() &lt;&lt; endl;
    tensor = tensor.to(torch::kCUDA);

    return tensor;
}

Mat Tensor_To_Mat(torch::Tensor tensor)
{
    std::cout &lt;&lt; "converting tensor to mat\n";

    tensor = tensor.to(torch::kCPU);
    tensor = tensor.squeeze(0).detach().permute({ 1, 2, 0 });
    tensor = tensor.mul(255).clamp(0, 255).to(torch::kU8);

    int64_t height = tensor.size(0);
    int64_t width = tensor.size(1);
    Mat mat(height, width, CV_8UC3);
    std::memcpy((void*)mat.data, tensor.data_ptr(), sizeof(torch::kU8) * tensor.numel());

    return mat.clone();
}

int main() {
    const string model_path = "../models/net_22.pt";
    const string img_path = "../data/input3.png";
    torch::jit::script::Module Net = torch::jit::load(model_path);
    Net.to(torch::kCUDA);

    Mat im = imread(img_path, IMREAD_COLOR);
    torch::Tensor tensor = Mat_To_Tensor(im);

    std::vector&lt;torch::jit::IValue&gt; inputs;
    inputs.push_back(tensor);

    torch::Tensor output = Net.forward(inputs).toTensor();
    output = torch::exp(tensor) - 1 + output;

    Mat res = Tensor_To_Mat(output);
    imwrite("../data/res3.png", res);

    return 0;
}
</code></pre>
<p>This is my code.<br>
net_22.pt is trained script model by pytorch.<br>
please help me,</p>

username: ptrblck
cooked: <p>OpenCV uses the channels-last memory format, so using:</p>
<pre><code class="lang-python">torch::Tensor tensor = torch::from_blob(mat.data, { mat.channels(), mat.rows, mat.cols }, torch::kFloat);
</code></pre>
<p>would create an interleaved tensor and you should either permute the OpenCV matrix or create the tensor in a channels-last format and <code>permute</code> it afterwards.</p>