facebookresearch / facebookresearch/detectron2
C++ API Issue using faster_rcnn_R_50_FPN_3x model
- Dominant language
- Python
- Stars
- 34.7k
- Forks
- 7.9k
- PR merge metrics
- No merged PRs in 30d
Description
## Instructions To Reproduce the 🐛 Bug:
Using export_model.py, I created a scripted export of a model I trained. I will attach the model. Exact command used was:
python /mnt/f/wsl/detectron2/tools/deploy/export_model.py --config-file /mnt/f/detectron2/configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml \
--output ./export_output_script_cuda --export-method scripting --format torchscript --sample-image /mnt/f/Publaynet/train/PMC449870_00000.jpg \
MODEL.WEIGHTS /mnt/f/torch_output/model_final.pth \
MODEL.DEVICE cuda \
MODEL.ROI_HEADS.NUM_CLASSES 5
The model works great in Python with PyTorch.
I then used the small bit of code here to load the model with the C++ API.
#include
#include
int main(int argc, const char* argv[]) {
bool test = torch::cuda::cudnn_is_available();
bool test2 = torch::hasMAGMA();
int num = torch::cuda::device_count();
assert(test && test2 && num == 1);
if (argc != 2) {
fprintf(stderr, "usage: testapplication \n");
return -1;
}
fprintf(stderr, "Loading model...\n");
torch::jit::FusionStrategy strat = { {torch::jit::FusionBehavior::DYNAMIC, 1} };
torch::jit::setFusionStrategy(strat);
torch::autograd::AutoGradMode guard(false);
auto module = torch::jit::load(argv[1]);
assert(module.buffers().size() > 0);
fprintf(stderr, "Model loaded successfully\n");
return 0;
}
Running this bit of code with the scripted model created from export_model.py throws an exception.
In the above code we make it into auto module = torch::jit::load(argv[1]);
Stepping through in debug mode, problem occurs in
libtorch-win-shared-with-deps-debug-1.11.0+cu113\libtorch\include\torch\csrc\jit\frontend\sugared_value.h
Line 471 labeled below:
for (const std::string& method_name : method_names_) {
if (auto class_type = self_->type()->cast()) {
Function& method = class_type->getMethod(method_name);
try {
471 --> method.ensure_defined();
} catch (const RecursiveMethodCallError&) {
throw ErrorReport(loc)
<< " method '" << method.name() << "' is called recursively. "
<< "Recursive calls are not supported";
}
schemas.push_back(&method.getSchema());
} else if (auto interface_type = self_->type()->cast()) {
schemas.push_back(interface_type->getMethod(method_name));
} else {
TORCH_INTERNAL_ASSERT(
false, "method constructed that is not a class or interface");
}
}
The method_name at the time of this is "predict_proposals"
Function& method is method = {name_={atoms_={ size=7 } qualifiedName_="__torch__.detectron2.modeling.proposal_generator.rpn.RPN.predict_proposals" ...} ...}
and __vfptr member of interest is valued as
[5] = 0x00007ffd3c928ef0 {torch_cpu.dll!torch::jit::GraphFunction::ensure_defined(void)}
Unhandled exception at 0x00007FFE0386474C in TorchProject_gpu2.exe: Microsoft C++ exception: torch::jit::ErrorReport at memory location 0x000000E4DBD800D0.
You can try your own faster_rcnn_R_50_FPN_3x model or I will try to attach mine.
## Expected behavior:
No exception thrown.
## Environment:
Running in windows 11. Visual Studio. Making used of TorchProject template. I have made sure to install the proper versions of libtorch and cuda. I included assert testing in the above code. Question is, should a faster_rcnn_R_50_FPN_3x model work fine with the C++ API?
Model is too large to attach. Download from
https://drive.google.com/file/d/1ZJHjQKAIdyxG2XvWqcTSknwxa-FEagRH/view?usp=sharing
Contributor guide
Assessment
This issue has not been assessed yet.