如何在pytorch c++ api中为模型提供一批帧?
How to give a batch of frames to the model in pytorch c++ api?
在 PyTorch C++ 前端 api 的帮助下,我编写了一段代码以在 C++ 中加载 pytorch 模型。我想通过使用 module->forward(batch_frames)
为 C++ 中的预训练模型提供一批帧。但它可以通过单个输入转发。
如何为模型提供一批输入?
下面是我要给的部分代码:
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++中的一个函数来拼接图像并制作了一批图像。然后将 batch 转换为 torch::tensor 并使用 batch 喂养模型。部分代码如下:
// 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();
注意把{}放在forward-pass函数中!
您可以使用多种方式在 libtorch 中创建一批张量。
以下是其中一种方式:
使用 Torch::TensorList:
由于 torch::TensorList
是 ArrayRef
,您不能直接向其添加任何张量,因此首先创建一个张量向量,然后使用该向量创建您的 TensorList
:
// two already processed tensor!
auto tensor1 = torch::randn({ 1, 3, 32, 32 });
auto tensor2 = torch::randn({ 1, 3, 32, 32 });
// using a tensor list
std::vector<torch::Tensor> tensor_vec{ tensor1, tensor2 };
torch::TensorList tensor_list{ tensor_vec};
auto batch_of_tensors = torch::cat(tensor_lst);
auto out = module->forward({batch_of_tensors}).toTensor();
或者你可以这样做:
auto batch_of_tensors = torch::cat({ tensor_vec});
或
auto batch_of_tensors = torch::cat({ tensor1, tensor2});
在 PyTorch C++ 前端 api 的帮助下,我编写了一段代码以在 C++ 中加载 pytorch 模型。我想通过使用 module->forward(batch_frames)
为 C++ 中的预训练模型提供一批帧。但它可以通过单个输入转发。
如何为模型提供一批输入?
下面是我要给的部分代码:
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++中的一个函数来拼接图像并制作了一批图像。然后将 batch 转换为 torch::tensor 并使用 batch 喂养模型。部分代码如下:
// 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();
注意把{}放在forward-pass函数中!
您可以使用多种方式在 libtorch 中创建一批张量。
以下是其中一种方式:
使用 Torch::TensorList:
由于 torch::TensorList
是 ArrayRef
,您不能直接向其添加任何张量,因此首先创建一个张量向量,然后使用该向量创建您的 TensorList
:
// two already processed tensor!
auto tensor1 = torch::randn({ 1, 3, 32, 32 });
auto tensor2 = torch::randn({ 1, 3, 32, 32 });
// using a tensor list
std::vector<torch::Tensor> tensor_vec{ tensor1, tensor2 };
torch::TensorList tensor_list{ tensor_vec};
auto batch_of_tensors = torch::cat(tensor_lst);
auto out = module->forward({batch_of_tensors}).toTensor();
或者你可以这样做:
auto batch_of_tensors = torch::cat({ tensor_vec});
或
auto batch_of_tensors = torch::cat({ tensor1, tensor2});