I have two functions, one to load image data to tensor and one to convert back the tensor into the image form.
at::Tensor image2tensor(std::string image_path){
    cv::Mat image = cv::imread(image_path); 
    image.convertTo(image,CV_32FC3);
    std::vector<int64_t> sizes = {1, 3, image.rows, image.cols};
    c10::TensorOptions options = torch::TensorOptions().dtype(torch::kFloat32);
    at::Tensor tensor_image = torch::from_blob(image.data, at::IntList(sizes), options);
    tensor_image = tensor_image/tensor_image.max();
    return tensor_image;
}

cv::Mat tensor2image(at::Tensor tensor){
    tensor = tensor.cpu().squeeze().detach().permute({1, 2, 0});
    tensor = tensor.mul(255).toType(torch::kU8).clamp(0, 255);
    cv::Mat resultImg(tensor.size(0), tensor.size(1),CV_8UC3);
    std::memcpy((void *) resultImg.data, tensor.data_ptr(), sizeof(torch::kU8) * tensor.numel());
    return resultImg;
}

However the reconstructed image looks garbage. How does from_blob maps a mat to tensor? as I feel tensor2mat is exact copy of python code and works well.