import torchvision.transforms.v2 as v2
...
self._input_tensor = v2.Compose(
[v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]
)(self._input_img)[None].to(self._device)
I want to translate this code to C++.
there seems to be no torchvision.transforms.compose functions in libtorch? I’m not sure about this.
my code is like
cv::Mat input_img = ...;
...
torch::Tensor input_tensor =
torch::from_blob(input_img.data,
{input_img.rows, input_img.cols, input_img.channels()},
torch::kUInt8)
.permute(
{2, 0, 1}) // the image format of cv::Mat is channel-last,
// transfer it to channel-first format of torch tensor
.to(torch::kFloat32)
.div(255.0F) // scale from [0, 255] to [0, 1]
.unsqueeze(0); // add a batch dimension
but it is much longer than using Compose
besides, I don’t know if there is a scale = True
equivalent in C++?
so I divided it by 255, but it is not strictly from 0 to 1, it is just in [0, 1]