at::kByte keeping negative values in float for inputs to model

I have a cv::Mat which is CV_32S type (also tried CV_8SC3), it stores both positive and negative values.

When convert it to tensor, the values are messed up:

cout << in_img << endl;

auto tensor_image = torch::from_blob(in_img.data, {1, in_img.rows, in_img.cols, 3}, at::kByte);

The in_img has negative values, while after print out tensor_image, the values were all totally different than in_img. It somehow seems to be scaled to 255 range

the negative values are gone. how do I keep the negative values? When I do this:

auto tensor_image = torch::from_blob(in_img.data, {1, in_img.rows, in_img.cols, 3}, at::kFloat);

and pass this to the forward of the model, I get seg fault :frowning:

but, when I originally do:

auto tensor_image = torch::from_blob(in_img.data, {1, in_img.rows, in_img.cols, 3}, at::kByte);
tensor_image = tensor_image.toType(at::kFloat);

it works as expected… which is perplexing to me :((

kByte is defined as uint8, so the valid range value is [0, 255], which explains why the values are clipped to this range.
The last approach should also yield values only in this range and should not contain any negative values.

I guess for CV_32S you could use kInt32 instead.

1 Like

Thanks a tonne @ptrblck it helped/solved it :slight_smile:

1 Like