Dear Community,
I’m experimenting with the C++ API from pytorch. When loading an Image with opencv, I get an error when calling the modules forward function. Using the same logic but initializing as torch::tensor works perfectly. Can someone tell me what I’m doing wrong?
I think the code explains it best. I posted the code everyone can try below:
This is the torch-only version that does work:
int test_torch_model_torch_dummy_data() {
torch::jit::script::Module module = torch::jit::load("/path/to/traced/model.pt");
std::cout << "model loaded." << std::endl;
at::TensorOptions options(at::ScalarType::Float);
torch::Tensor tensor_image = torch::zeros({ 1, 3, 224, 224 }, options);
std::cout << tensor_image.sizes() << std::endl;
std::vector<torch::jit::IValue> inputs;
inputs.emplace_back(tensor_image);
std::cout << "input shape torch" << tensor_image.sizes() << std::endl;
auto ret = module.forward(inputs).toTensor();
std::cout << "works using torch dummy data.";
return 0;
}
This is the opencv-function that does not work:
int test_torch_model_cv_dummy_data() {
torch::jit::script::Module module = torch::jit::load("path/to/traced_model.pt");
std::cout << "model loaded." << std::endl;
auto bgr = cv::Mat(cv::Size(224, 224), CV_8UC3);
std::vector<int64_t> sizes = { 1, 224, 224, 3 };
at::TensorOptions options(at::ScalarType::Float);
at::Tensor tensor_image = torch::from_blob(bgr.data, at::IntList(sizes), options);
auto tensor_image_permuted = tensor_image.permute({ 0, 3, 1, 2 });
std::cout << "input shape cv data" << tensor_image_permuted.sizes() << std::endl;
std::vector<torch::jit::IValue> inputs;
inputs.emplace_back(tensor_image);
module.forward(inputs).toTensor();
std::cout << "works using cv dummy data " << std::endl;
}
void main() {
test_torch_model_torch_dummy_data();
test_torch_model_cv_dummy_data();
}