Move input tensor to cuda will cause 'Segmentation fault'

If I use opencv to read an image (1248x384 ), convert it to tensor and move it onto cuda in python , it won’t have problems.However , when I do the same thing in c++ libtorch , it will report Segmentation fault.Furthermore , I find it’s the moving onto cuda process that causes the Segmentation fault.Is there anyone know why or how to solve the problem?

It’s hard to tell with the information you give and no code.

One common pitfall: if you use from_blob to get the tensor from OpenCV, you do not own the memory, i.e. you need to call the copying(! mind you) to cuda before the OpenCV memory goes out of scope / keep the memory in scope until you have made your first copy.

Best regards

Thomas

2 Likes

Sorry , I forgot to post the code. Do you mean that I should use gpuMat to load my image onto gpu firstly and then calculate it ?

/pytorch 1.2/

#include “torch/script.h”
#include “torch/torch.h”
#include <opencv2/opencv.hpp>
#include

#include
#include

using namespace std;
using namespace cv;

int main()
{
int height = 1248;
int width = 384;

Mat img = imread("/media/usr515/ed65d8df-d015-4269-90bc-ac3cccb109a8/dsq/zz/stereo/train/2011_09_26_drive_0002_sync/image_02/data/0000000007.png");   
Mat temImg;
resize(img,temImg,Size(height,width),0,0,INTER_LINEAR);
cvtColor(temImg, temImg, CV_RGB2BGR);    
temImg.convertTo(temImg,CV_32FC3,1.0);
torch::Tensor tensor_image = torch::from_blob(temImg.data, {temImg.rows, temImg.cols , 3}, torch::kFloat);        
tensor_image = tensor_image.permute({2,0,1});
tensor_image = tensor_image.unsqueeze(0);
tensor_image = tensor_image.cuda();


Mat img_1 = imread("/media/usr515/ed65d8df-d015-4269-90bc-ac3cccb109a8/dsq/zz/stereo/train/2011_09_26_drive_0002_sync/image_03/data/0000000007.png");   
Mat temImg_1;
resize(img_1,temImg_1,Size(width,height),0,0,INTER_LINEAR);    
cvtColor(temImg_1, temImg_1, CV_RGB2BGR);
temImg_1.convertTo(temImg_1,CV_32FC3,1.0);
torch::Tensor tensor_image_1 = torch::from_blob(temImg_1.data, {temImg_1.rows, temImg_1.cols , 3}, torch::kFloat);        
tensor_image_1 = tensor_image_1.permute({2,0,1});
tensor_image_1 = tensor_image_1.unsqueeze(0);
tensor_image_1 = tensor_image_1.cuda();

torch::jit::script::Module module = torch::jit::load("/media/usr515/ed65d8df-d015-4269-90bc-ac3cccb109a8/dsq/zz/stereo/psmnet_256_1.pt" , torch::kCUDA);


torch::Tensor result = module.forward({tensor_image , tensor_image_1}).toTensor();

cout << result << endl;

return 1;

}

I know what you mean.It works! Thank you for solving my problem.

@savior What was the ultimate solution you came to? Did you use the from_blob method with a GpuMat?