我编写了一段代码,借助PyTorch C ++前端api在C ++中加载pytorch模型。我想使用来为C ++中的预训练模型提供一批框架module->forward(batch_frames)
。但是它可以通过单个输入转发。
如何为模型提供一批输入?
我想要批处理的一部分代码如下所示:
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();
最后,我在c ++中使用了一个函数来连接图像并制作一批图像。然后将批处理转换为torch :: tensor并使用该批处理输入模型。下面给出了一部分代码:
// 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();
请注意,将{}放在前向传递函数中!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句