Libtorch: torch::tensor from cv::cuda::GpuMat will cause segment fault

username: dancedpipi
cooked: <p>I found if use cv::cuda::GpuMat to create tensor, it will cause a segmentfault.<br>
my code:</p>
<pre><code class="lang-auto">cv::Mat image = cv::imread(argv[1], cv::IMREAD_COLOR);
cv::cuda::GpuMat image_device;
cv::cuda::GpuMat image_float_device;
image_device.upload(image);
image_device.convertTo(image_float_device, CV_32FC3);
at::Tensor input_tensor = torch::CUDA(torch::kFloat32).tensorFromBlob(img_float_device.data, {img_float_device.rows, img_float_device.cols, 3});
std::cout &lt;&lt; "all finished!" &lt;&lt; std::endl;
return 0;
</code></pre>
<p>The sentence “all finished!” could be print.<br>
But it will cause a segment fault, I guess maybe due to the tensor destructor ?<br>
How could I fix the problem?<br>
Appreciate for any help~</p>

username: dancedpipi
cooked: <p>anyone could help~!!!</p>

username: dambo
cooked: <p>See this:</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>

<p>I see no performance gain when using GpuMat vs using Mat.</p>

username: dancedpipi
cooked: <p><a class="mention" href="/u/dambo">@dambo</a>  we use cv::cuda::resize, it save some time.<br>
And we run model in GPU, Therefore, it is most appropriate to generate tensor directly from the GPU, eliminating the operation  “tensor.to(device)”.</p>
<p>From your answer, it seems that it does not explain why segmentfault.<br>
Can you explain more in detail?</p>

username: dambo
cooked: <p>But did my code also cause a segmentation fault?</p>

username: solarflarefx
cooked: <p><a class="mention" href="/u/dancedpipi">@dancedpipi</a> What was your ultimate result?  Were  you able to fix your problem?</p>
<p><a class="mention" href="/u/dambo">@dambo</a> Am I also misunderstanding the from_blob?  I was also under the impression that using the GpuMat should be more efficient than Mat when dealing with large datasets because you are avoiding Cpu to Gpu copies?  Am I incorrect here?  Would from_blob perform a copy within the GPU or would it change the way the data is referenced?</p>