sizeof(torch::kF32)=1 and sizeof(torch::kU8)=1?

:question: Questions and Help

  • libtorch 1.2 CU100 or libtorch 1.4 cpu

When I try to convert the the torch::Tensor to cv::Mat , I use the following code:

std::memcpy((void *), score_link.data_ptr(), sizeof(torch::kF32) * score_text.numel())

The tensor type is CPUFloatType and I use sizeof(torch::kF32) to get the tensor data, but failed, and I notices that sizeof(torch::kF32) and sizeof(torch::kU8) are all equal to 1 , which was not usual, since sizeif(float) should be 4 and sizeof(char) should be 1.

I change the code sizeof(torch::kF32) to 4 and get the right cv::Mat.

My question is why sizeof(torch::kF32)=1 and sizeof(torch::kU8)=1 ?

That’s because torch::kF32 is a constant indicating a type, not a C++ type. I think you want elementSize(torch::kF32), which gives you the element size.

Best regards


torch::elementSize(torch::kF32) = 4 works.