facebookresearch / facebookresearch/detectron2

Error encountered while using libtorch for batch inference

Open
#5,187 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
34.7k
Forks
7.9k
PR merge metrics
No merged PRs in 30d

Description

## Instructions To Reproduce the 🐛 Bug:
1. batch inference code:

int main() {

torch::jit::script::Module module = load_model();

auto device = (*begin(module.buffers())).device();
module.to(device);
module.eval();

torch::Tensor imgs_in = torch::empty({ 0 }).to(device);

string image_path = "D:/VsProjects/d2_deploy/test_pic";
vector src_name;
glob(image_path, src_name);
int batchSize = 2;

for (int i = 0; i < src_name.size(); i++)
{

cv::Mat input_img = cv::imread(src_name[i], cv::IMREAD_COLOR);
resize(input_img, input_img, Size(500, 500));
input_img.convertTo(input_img, CV_32FC3, 1.0f / 127.5f, -1);

const int height = input_img.rows;
const int width = input_img.cols;
const int channels = 3;

auto img_tensor =
torch::from_blob(input_img.data, {1, height, width, channels }).to(device);
cout << "img_tensor define" << endl;
// HWC to CHW
img_tensor =
img_tensor.permute({0, 3, 1, 2 }).contiguous();

imgs_in = torch::cat({ imgs_in,img_tensor }, 0);
imgs_in.print();

if ((i+1)%batchSize==0)
{
vector batchInputs;
batchInputs.emplace_back(imgs_in);
auto outputs = module.forward({imgs_in});
batchInputs.clear();
}
}
return 0;
}

2 . one picture inference code:

c10::IValue get_scripting_inputs(cv::Mat& img, c10::Device device) {
const int height = img.rows;
const int width = img.cols;
const int channels = 3;

auto img_tensor =
torch::from_blob(img.data, {height, width, channels}, torch::kUInt8);
cout << "img_tensor define" << endl;
// HWC to CHW
img_tensor =
img_tensor.to(device, torch::kFloat).permute({2, 0, 1}).contiguous();
cout << "------------------tensor to device:" << device << endl;
c10::Dict dic = c10::Dict();
dic.insert("image", img_tensor);
cout << "------------get_scripting_inputs finish------" << endl;
return std::make_tuple(dic);
}
int main() {

torch::jit::script::Module module = load_model();

auto device = (*begin(module.buffers())).device();

string image_path = "D:/VsProjects/d2_deploy/test_pic";
vector src_name;
glob(image_path, src_name);

for (int i = 0; i (getTickCount());
auto outputs = module.forward({ inputs });
time0 = ((double)getTickCount() - time0) / getTickFrequency();
cout << "use time is " << time0 << endl;

}
return 0;
}

## Expected behavior:

The code for single image inference runs normally,but Error encountered while using code for batch inference

the error is appeared when executing the forward function " auto outputs = module.forward({ inputs });":
0x00007FFEE6F1CF19 处(位于 torchscript_mask_rcnn.exe 中)有未经处理的异常: Microsoft C++ 异常: c10::Error,位于内存位置 0x000000B8A5CFE2B0 处。

## Environment:
win10+libtorch2.1.1+cuda12.1+pytorch2.1.1+opencv4.8

Contributor guide

Open the contributing guide

Research direction

Start with the provided batch inference code and capture the full c10::Error raised at module.forward({imgs_in}) under the stated Win10, libtorch 2.1.1, CUDA 12.1, PyTorch 2.1.1, and OpenCV 4.8 environment. Compare the batch tensor construction with the working single-image path; done means batch inference completes without the exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, pytorch
Domain
computer-vision, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.