How to covert torch::Tensor to cv::Mat

username: julianyang
cooked: <p>I use a libtorch c++ API to do a prediction task.<br>
The model is  a pretrained-model.<br>
input data is a image, and output data is also a image too.</p>
<p>I use this model according to test demo which is written by python:</p>
<p>input_image = Image.open(filename).convert(“RGB”)</p>
<h1>resize image, keep aspect ratio</h1>
<p>h = input_image.size[0]<br>
w = input_image.size[1]<br>
ratio = h <em>1.0 / w<br>
if ratio &gt; 1:<br>
h = opt.load_size<br>
w = int(h</em>1.0/ratio)<br>
else:<br>
w = opt.load_size<br>
h = int(w * ratio)<br>
input_image = input_image.resize((h, w), Image.BICUBIC)<br>
input_image = np.asarray(input_image)</p>
<h1>RGB -&gt; BGR</h1>
<p>input_image = input_image[:, :, [2, 1, 0]]<br>
input_image = transforms.ToTensor()(input_image).unsqueeze(0)</p>
<h1>preprocess, (-1, 1)</h1>
<p>input_image = -1 + 2 * input_image<br>
if opt.gpu &gt; -1:<br>
input_image = Variable(input_image, volatile=True).cuda()<br>
else:<br>
input_image = Variable(input_image, volatile=True).float()</p>
<h1>forward</h1>
<p>output_image = model(input_image)<br>
output_image = output_image[0]</p>
<h1>BGR -&gt; RGB</h1>
<p>output_image = output_image[[2, 1, 0], :, :]</p>
<h1>deprocess, (0, 1)</h1>
<p>output_image = output_image.data.cpu().float() * 0.5 + 0.5</p>
<p>I want to do predict in C++, but i have no idea to covert the output tensor to a cv::Mat.</p>
<p>My C++ code is like this, but it does not work.</p>
<p>// do predict.<br>
// cpu data.<br>
auto out_tensor = module-&gt;forward({img_tensor}).toTensor();</p>
<p>cv::Mat result_img(200, 200, CV_8UC3);<br>
out_tensor = out_tensor[0].permute({2, 1, 0});<br>
out_tensor = out_tensor.mul(255).clamp(0,255).to(torch::kU8);<br>
std::memcpy((void*)result_img.data, out_tensor.data_ptr(), sizeof(torch::kU8)*out_tensor.numel());</p>
<p>Is there anyone can help me?</p>

username: dambo
cooked: <p>See my reply here:</p><aside class="quote quote-modified" data-post="6" data-topic="47701">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dambo/48/29088_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/libtorch-c-convert-a-tensor-to-cv-mat-single-channel/47701/6">Libtorch C++ convert a Tensor to cv:Mat (single channel</a> <a class="badge-category__wrapper " href="/c/c/11"><span data-category-id="11" data-drop-close="true" class="badge-category " title="Topics related to the C++ Frontend, C++ API or C++ Extensions"><span class="badge-category__name">C++</span></span></a>
  </div>
  <blockquote>
    From cv2 to TORCH: 
 cv::cvtColor(frame, frame, CV_BGR2RGB);
 frame.convertTo(frame, CV_32FC3, 1.0f / 255.0f);
 auto input_tensor = torch::from_blob(frame.data, {1, frame_h, frame_w, kCHANNELS});
 input_tensor = input_tensor.permute({0, 3, 1, 2});

From TORCh to cv2: 
input_tensor = input_tensor.to(at::kCUDA);
                torch::Tensor out_tensor = module-&gt;forward({input_tensor}).toTensor();

                out_tensor = out_tensor.squeeze().detach().permute({1, 2, 0});
                out_t…
  </blockquote>
</aside>