Converting sl::Mat on gpu to torch tensor using C++

Hi,
I want to convert retrieved pointcloud measure whish is a sl::Mat type on gpu to a Torch tensor on gpu. I couldn’t figure it out how to do that. In python, it is straighforward using numpy, however in C++ there is no numpy. To convert with using opencv, it needs to be compiled with Cuda. It must be possible to convert directly from sl::Mat to torch tensor without using opencv.
Has anyone done this before? Thanks

Hi @burak, welcome to the forums!

You should be able to access the sl::Mat GPU pointer using getPtr(sl::MEM::GPU).

Then, I’m not too knowledgeable about the Torch API, but this post hints that the torch::from_blob function could be useful.

I figure it out like this, hope someone finds it useful.

void to_tensor(sl::Mat & point_cloud , torch::Tensor & tensor) {
    std::vector<int64_t> sizes = {
                                static_cast<int64_t>(point_cloud.getChannels() - 1),
                                static_cast<int64_t>(point_cloud.getHeight()),
                                static_cast<int64_t>(point_cloud.getWidth())
                              };
    long long step = point_cloud.getStep();
    std::vector<int64_t> strides = {1 , step*static_cast<int64_t>(point_cloud.getChannels()), static_cast<int64_t>(point_cloud.getChannels())};
   
    tensor = torch::from_blob(point_cloud.getPtr<sl::uchar1>(MEM::GPU), sizes, strides, deleter,  torch::kCUDA);
}
3 Likes