"unfold" command does not work as expected with LibTorch

username: sanyu_kimura
cooked: <p>I want to cut image into small patches.<br>
but “unfold” command does not work as expected.</p>
<p>How can I cut image into small patches.</p>
<p>I run the code below by using Pytorch 1.7.1 respectively with cuda 11.1.</p>
<p>Thank you.</p>
<pre><code>#include &lt;iostream&gt;
#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/core.hpp&gt;
#include &lt;opencv2/highgui/highgui.hpp&gt;

std::string type2str(int type) {
    std::string r;
    uchar depth = type &amp; CV_MAT_DEPTH_MASK;
    uchar chans = 1 + (type &gt;&gt; CV_CN_SHIFT);
    using std::string;
    typedef std::pair&lt;string, float&gt; Prediction;
    switch (depth) {
    case CV_8U:  r = "8U"; break;
    case CV_8S:  r = "8S"; break;
    case CV_16U: r = "16U"; break;
    case CV_16S: r = "16S"; break;
    case CV_32S: r = "32S"; break;
    case CV_32F: r = "32F"; break;
    case CV_64F: r = "64F"; break;
    default:     r = "User"; break;
    }
   r += "C";
   r += (chans + '0');
   return r;
}

torch::Tensor MattoTensor(cv::Mat img) {
    cv::Mat image;
    cv::cvtColor(img, image, cv::COLOR_BGR2RGB);
    std::vector&lt;int64_t&gt;shape = { 3, img.rows, img.cols };
    torch::Tensor img_tensor = torch::from_blob(img.data, at::IntList(shape), at::ScalarType::Byte).mul(-1);
    return img_tensor;
}
int saveImageTensor(at::Tensor pic,std::string name) {
    auto output = pic.clone().permute({ 1, 2, 0 });
    output = output.detach().mul(255).clamp(0, 255);
    output = output.to(torch::kU8).to(torch::kCPU);
    auto sizes = output.sizes();
    cv::Mat mat{ cv::Size{static_cast&lt;int&gt;(sizes[1]) ,
                     static_cast&lt;int&gt;(sizes[0]) },
            CV_8UC(static_cast&lt;int&gt;(sizes[2])),
            output.data_ptr() };
    cv::imwrite("C:/Users/Tens" + name + ".png", mat);
}
int main()
{
    const at::Tensor imageSample = MattoTensor(cv::imread("C:/Users/cat.jpg", cv::IMREAD_COLOR)).unsqueeze(0);
    std::cout &lt;&lt; imageSample.sizes() &lt;&lt; std::endl;
    const auto tensorpat = imageSample.unfold(2, 128, 64).unfold(3, 128, 64);
    std::cout &lt;&lt; tensorpat.sizes() &lt;&lt; std::endl;
    auto tensorpermute = tensorpat.permute({ 0,2, 3, 1, 4, 5 });
    std::cout &lt;&lt; tensorpermute.sizes() &lt;&lt; std::endl;
    auto tensorpershape = tensorpermute.reshape({ -1, 3, 128, 128 });
    std::cout &lt;&lt; tensorpershape.sizes() &lt;&lt; std::endl;
    saveImageTensor(tensorpershape[2], "_" + std::to_string(2));
}
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/9/69420e48970c140dfeac6bc0031d17455fb2d4e6.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/69420e48970c140dfeac6bc0031d17455fb2d4e6" title="cat"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/69420e48970c140dfeac6bc0031d17455fb2d4e6_2_333x500.jpeg" alt="cat" data-base62-sha1="f19EOrS97O7heToPpcVdkID2nlk" width="333" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/69420e48970c140dfeac6bc0031d17455fb2d4e6_2_333x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/69420e48970c140dfeac6bc0031d17455fb2d4e6_2_499x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/9/69420e48970c140dfeac6bc0031d17455fb2d4e6.jpeg 2x" data-dominant-color="92919F"><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">cat</span><span class="informations">547×821 59.2 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>
input picture<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/9/8/98b8982189df6061b887946e229963f7a8bd6374.png" alt="Tens_102" data-base62-sha1="lN2497MnzDZL6amEH07OnlFIWWM" width="128" height="128"><br>
output picture<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/6/8/684bda0264ea7c21e670efc49e39920bea0bd2fb.png" alt="patch - コピー" data-base62-sha1="eSEaqFtrB4fT79xi9cJSpcqnE71" width="128" height="128"><br>
correct output picture</p>

username: sanyu_kimura
cooked: <p>I miss “MattoTensor” and “saveImageTensor” .</p>
<p>sorry</p>