I've written a code to load the pytorch model in C++ with help of the PyTorch C++ Frontend api. I want to give a batch of frames to a pretrained model in the C++ by using module->forward(batch_frames)
. But it can forward through a single input.
How can I give a batch of inputs to the model?
A part of code that I want to give the batch is shown below:
cv::Mat frame;
vector<torch::jit::IValue> frame_batch;
// do some pre-processes on each frame and then add it to the frame_batch
//forward through the batch frames
torch::Tensor output = module->forward(frame_batch).toTensor();
Finally, I used a function in c++ to concatenate images and make a batch of images. Then convert the batch into the torch::tensor and feed the model using the batch. A part of code is given below:
// cat 2 or more images to make a batch
cv::Mat batch_image;
cv::vconcat(image_2, images_1, batch_image);
// do some pre-process on image
auto input_tensor_batch = torch::from_blob(batch_image.data, {size_of_batch, image_height, image_width, 3});
input_tensor_batch = input_tensor_batch.permute({0, 3, 1, 2});
//forward through the batch frames
torch::Tensor output = module->forward({input_tensor_batch}).toTensor();
Note that put the { } in the forward-pass function!