C++ convert signed float to tensor?

I have only one last question here:

I have a cv::Mat which is CV_32S format contains signed float with negative values. (there is no other way to store negative values in opencv)

When I convert this Mat to tensor:

at::Tensor tensor_image = torch::from_blob(in_img.data, {1, in_img.rows, in_img.cols, 3}, torch::kByte);
  tensor_image = tensor_image.permute({0, 3, 1, 2}).to(torch::kFloat32).to(device_type);
  cout << "tensor_image: " << tensor_image << endl;

The result is totally wrong!! All negative values are gone!! This is totally different in python input model tensor values…

Anybody knows how to convert a cv::Mat with CV_32S type into a tensor and does not effect values store in it???

I think “signed float” is the source of your trouble, OpenCV’s types are Unsigned ints, Signed ints and Floats of various precision.
So using torch::kLong for the from_blob would seem to be the natural way. You’d run into trouble for CV_8S because PyTorch doesn’t have them, but here, just avoid the kByte that corresponds to CV_8U.

Best regards


thanks!! it worked!!!