I want to cut image into small patches.
but “unfold” command does not work as expected.
How can I cut image into small patches.
I run the code below by using Pytorch 1.7.1 respectively with cuda 11.1.
Thank you.
#include <iostream>
#include <torch/script.h>
#include <torch/torch.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
std::string type2str(int type) {
std::string r;
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> CV_CN_SHIFT);
using std::string;
typedef std::pair<string, float> 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<int64_t>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<int>(sizes[1]) ,
static_cast<int>(sizes[0]) },
CV_8UC(static_cast<int>(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 << imageSample.sizes() << std::endl;
const auto tensorpat = imageSample.unfold(2, 128, 64).unfold(3, 128, 64);
std::cout << tensorpat.sizes() << std::endl;
auto tensorpermute = tensorpat.permute({ 0,2, 3, 1, 4, 5 });
std::cout << tensorpermute.sizes() << std::endl;
auto tensorpershape = tensorpermute.reshape({ -1, 3, 128, 128 });
std::cout << tensorpershape.sizes() << std::endl;
saveImageTensor(tensorpershape[2], "_" + std::to_string(2));
}
input picture
output picture
correct output picture