I use a libtorch c++ API to do a prediction task.
The model is  a pretrained-model.
input data is a image, and output data is also a image too.
I use this model according to test demo which is written by python:
input_image = Image.open(filename).convert(“RGB”)
resize image, keep aspect ratio
h = input_image.size[0]
w = input_image.size[1]
ratio = h 1.0 / w
if ratio > 1:
h = opt.load_size
w = int(h1.0/ratio)
else:
w = opt.load_size
h = int(w * ratio)
input_image = input_image.resize((h, w), Image.BICUBIC)
input_image = np.asarray(input_image)
RGB -> BGR
input_image = input_image[:, :, [2, 1, 0]]
input_image = transforms.ToTensor()(input_image).unsqueeze(0)
preprocess, (-1, 1)
input_image = -1 + 2 * input_image
if opt.gpu > -1:
input_image = Variable(input_image, volatile=True).cuda()
else:
input_image = Variable(input_image, volatile=True).float()
forward
output_image = model(input_image)
output_image = output_image[0]
BGR -> RGB
output_image = output_image[[2, 1, 0], :, :]
deprocess, (0, 1)
output_image = output_image.data.cpu().float() * 0.5 + 0.5
I want to do predict in C++, but i have no idea to covert the output tensor to a cv::Mat.
My C++ code is like this, but it does not work.
// do predict.
// cpu data.
auto out_tensor = module->forward({img_tensor}).toTensor();
cv::Mat result_img(200, 200, CV_8UC3);
out_tensor = out_tensor[0].permute({2, 1, 0});
out_tensor = out_tensor.mul(255).clamp(0,255).to(torch::kU8);
std::memcpy((void*)result_img.data, out_tensor.data_ptr(), sizeof(torch::kU8)*out_tensor.numel());
Is there anyone can help me?