facebookresearch / detectron2

Detectron2 is a platform for object detection, segmentation and other visual recognition tasks.
https://detectron2.readthedocs.io/en/latest/
Apache License 2.0
30k stars 7.41k forks source link

Error when converting model using export_onnx() #1700

Closed r5sb closed 4 years ago

r5sb commented 4 years ago

Instructions To Reproduce the Issue:

Check https://stackoverflow.com/help/minimal-reproducible-example for how to ask good questions. Simplify the steps to reproduce the issue using suggestions from the above link, and provide them below:

  1. full code you wrote or full changes you made (git diff)
    
    import torch
    import torchvision
    from detectron2.modeling import build_model
    from detectron2 import model_zoo
    from detectron2.config import get_cfg
    from detectron2.structures import BoxMode
    import pdb
    from detectron2.checkpoint.detection_checkpoint import DetectionCheckpointer
    from detectron2.export import add_export_config, export_onnx_model, Caffe2Tracer
    from detectron2.data import DatasetCatalog, MetadataCatalog, build_detection_test_loader, build_detection_train_loader
    from detectron2.data.datasets import register_coco_instances

def register_data(): register_coco_instances("val_data", {}, 'coco_mod/val.json', 'coco_mod/val')

def setup_cfg(backbone): cfg = get_cfg() cfg.merge_from_file(model_zoo.get_config_file('Misc/cascade_mask_rcnn_R_50_FPN_3x.yaml')) cfg.DATALOADER.NUM_WORKERS = 0 cfg.MODEL.WEIGHTS = 'model_0.pth' cfg.MODEL.ROI_HEADS.NUM_CLASSES = 5 cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 cfg.DATASETS.TEST = ('val_data',) cfg.MODEL.DEVICE = "cpu" cfg.freeze() return cfg

if name == 'main': register_data() cfg = setup_cfg(0) model = build_model(cfg) DetectionCheckpointer(model).resume_or_load(cfg.MODEL.WEIGHTS) data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0]) first_batch = next(iter(data_loader)) tracer = Caffe2Tracer(cfg, model, first_batch)

script_model = tracer.export_torchscript()

script_model.save("model.ts")

onnx_model = tracer.export_onnx() onnx.save(onnx_model, "cascade_rcnn.onnx")

2. what exact command you run:

python3 export_model.py. This is the script above to convert .pth model to onnx. I am following the method in caffe2_converter.py

3. __full logs__ you observed:

File "/usr/local/lib/python3.6/dist-packages/mock/mock.py", line 1411, in get_original "%s does not have the attribute %r" % (target, name) AttributeError: <class 'torch.nn.modules.container.ModuleList'> does not have the attribute 'inference'


## Expected behaviour:

I feel this is similar to issue #1143 I am not sure what the "does not have attribute 'inference'" is pointing to. I have tried running model.eval() before converting but that doesn't seem to work either. I have also tried using export_torchscript() to get the torchscript model format but get the same error. The model was trained on GPU but I have put cfg.MODEL.DEVICE = "cpu" for inference. Could that be a problem? Or is the way I am passing sample data incorrect? I have followed how it is done in caffe2_converter.py. I would appreciate any help. Thanks!

## Environment:

sys.platform linux Python 3.6.9 (default, Apr 18 2020, 01:56:04) [GCC 8.4.0] numpy 1.16.3 detectron2 0.1.3 @/home/ubuntu/detectron2/detectron2 Compiler GCC 7.5 CUDA compiler CUDA 11.0 detectron2 arch flags sm_70 DETECTRON2_ENV_MODULE PyTorch 1.5.1 @/home/ubuntu/.local/lib/python3.6/site-packages/torch PyTorch debug build False GPU available True GPU 0 Tesla V100-SXM2-16GB CUDA_HOME /usr/local/cuda Pillow 7.1.2 torchvision 0.6.1 @/home/ubuntu/.local/lib/python3.6/site-packages/torchvision torchvision arch flags sm_35, sm_50, sm_60, sm_70, sm_75 fvcore 0.1.1.post20200622 cv2 4.2.0


PyTorch built with:

ppwwyyxx commented 4 years ago

Exporting a Cascade R-CNN is not supported. Duplicate of #988