Skip to content

Unable to export PointRend model (from detectron2) to ONNX #69674

@MichalHek

Description

@MichalHek

Issue description

I'm trying to export PointRend model from FaceBooks AI's library detectron2 to onnx (by exporting to caffe2) according to their export instructions in detectron2 deployment tutorial but apparently they don't provide deployment support to their research projects (#2086 issue).

I tried to deploy it anyway, but struggling with issues that I cannot resolve.

@garymm

Code example

  • This is an example of an export script that works on MASK-RCNN:
import os
import torch
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.config import get_cfg
from detectron2 import model_zoo
from detectron2.data import build_detection_test_loader
from detectron2.evaluation import COCOEvaluator, inference_on_dataset, print_csv_format
from detectron2.export import add_export_config  # @thiagocrepaldi removed `export_caffe2_model`
from detectron2.modeling import build_model
from detectron2.utils.logger import setup_logger

# @thiagocrepaldi edit to support latest detectron2 master branch
try:
    from detectron2.export import export_caffe2_model
except ImportError:
    from detectron2.export import Caffe2Model, Caffe2Tracer
    def export_caffe2_model(cfg, model, inputs):
        return Caffe2Tracer(cfg, model, inputs).export_caffe2()

# Set cfg
cfg = get_cfg()
cfg.DATALOADER.NUM_WORKERS = 0
cfg = add_export_config(cfg)

cfg.merge_from_file('./configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml')
cfg.MODEL.WEIGHTS = "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl" 
cfg.MODEL.DEVICE = 'cpu'
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.freeze()

# create a torch model
torch_model = build_model(cfg)
DetectionCheckpointer(torch_model).resume_or_load(cfg.MODEL.WEIGHTS)

# get a sample data
data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0])
first_batch = next(iter(data_loader))

# convert and save caffe2 model
caffe2_model = export_caffe2_model(cfg, torch_model, first_batch)
caffe2_model.save_protobuf('onnx')
import os
import torch
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.config import get_cfg
from detectron2 import model_zoo
from detectron2.data import build_detection_test_loader
from detectron2.evaluation import COCOEvaluator, inference_on_dataset, print_csv_format
from detectron2.export import add_export_config  # @thiagocrepaldi removed `export_caffe2_model`
from detectron2.modeling import build_model
from detectron2.utils.logger import setup_logger
from projects.PointRend import point_rend

# @thiagocrepaldi edit to support latest detectron2 master branch
try:
    from detectron2.export import export_caffe2_model
except ImportError:
    from detectron2.export import Caffe2Model, Caffe2Tracer
    def export_caffe2_model(cfg, model, inputs):
        return Caffe2Tracer(cfg, model, inputs).export_caffe2()

# Set cfg
cfg = get_cfg()
cfg.DATALOADER.NUM_WORKERS = 0
cfg = add_export_config(cfg)
# Add PointRend-specific config
point_rend.add_pointrend_config(cfg)
# Load a config from file
cfg.merge_from_file("/home/michal/dev/tetavi_main/TV_Python/occupancy_networks/im2mask/PointRendSegmentation/configs/InstanceSegmentation/pointrend_rcnn_R_50_FPN_3x_coco.yaml")
cfg.MODEL.WEIGHTS = "/home/michal/dev/tetavi_main/TV_Python/occupancy_networks/im2mask/PointRendSegmentation/trainedModels/model_final_3c3198.pkl"  # @thiagocrepaldi check your checkpoint file
cfg.MODEL.DEVICE = 'cpu'  # @thiagocrepaldi changed to CPU
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
cfg.freeze()

# create a torch model
torch_model = build_model(cfg)
DetectionCheckpointer(torch_model).resume_or_load(cfg.MODEL.WEIGHTS)

# get a sample data
data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0])
first_batch = next(iter(data_loader))

# convert and save caffe2 model
caffe2_model = export_caffe2_model(cfg, torch_model, first_batch)
caffe2_model.save_protobuf('onnx')

Error

Traceback (most recent call last):
  File "/home/michal/dev/local_git_repo/detectron2/onnx_export_sample_pointrend.py", line 45, in <module>
    caffe2_model = export_caffe2_model(cfg, torch_model, first_batch)
  File "/home/michal/dev/local_git_repo/detectron2/detectron2/export/api.py", line 265, in export_caffe2_model
    return Caffe2Tracer(cfg, model, inputs).export_caffe2()
  File "/home/michal/dev/local_git_repo/detectron2/detectron2/export/api.py", line 104, in export_caffe2
    self.traceable_model, self.traceable_inputs
  File "/home/michal/dev/local_git_repo/detectron2/detectron2/export/caffe2_export.py", line 146, in export_caffe2_detection_model
    onnx_model = export_onnx_model(model, (tensor_inputs,))
  File "/home/michal/dev/local_git_repo/detectron2/detectron2/export/caffe2_export.py", line 60, in export_onnx_model
    operator_export_type=OperatorExportTypes.ONNX_ATEN_FALLBACK,
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/__init__.py", line 280, in export
    custom_opsets, enable_onnx_checker, use_external_data_format)
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/utils.py", line 94, in export
    use_external_data_format=use_external_data_format)
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/utils.py", line 695, in _export
    dynamic_axes=dynamic_axes)
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/utils.py", line 467, in _model_to_graph
    module=module)
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/utils.py", line 200, in _optimize_graph
    graph = torch._C._jit_pass_onnx(graph, operator_export_type)
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/__init__.py", line 313, in _run_symbolic_function
    return utils._run_symbolic_function(*args, **kwargs)
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/utils.py", line 994, in _run_symbolic_function
    return symbolic_fn(g, *inputs, **attrs)
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/symbolic_helper.py", line 167, in wrapper
    for arg, arg_desc, arg_name in zip(args, arg_descriptors, arg_names)]
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/symbolic_helper.py", line 167, in <listcomp>
    for arg, arg_desc, arg_name in zip(args, arg_descriptors, arg_names)]
  File "/home/michal/anaconda3/envs/algo_clone/lib/python3.6/site-packages/torch/onnx/symbolic_helper.py", line 84, in _parse_arg
    "', since it's not constant, please try to make "
RuntimeError: Failed to export an ONNX attribute 'onnx::Gather', since it's not constant, please try to make things (e.g., kernel size) static if possible

System Info

PyTorch version: 1.9.0
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.3 LTS (x86_64)
GCC version: (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.17

Python version: 3.6.13 |Anaconda, Inc.| (default, Jun  4 2021, 14:25:59)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.11.0-41-generic-x86_64-with-debian-bullseye-sid
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3080
Nvidia driver version: 470.86
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.19.2
[pip3] pytorch3d==0.3.0
[pip3] torch==1.9.0
[pip3] torchaudio==0.9.0a0+33b2469
[pip3] torchvision==0.10.0
[conda] blas                      1.0                         mkl    conda-forge
[conda] cudatoolkit               11.1.74              h6bb024c_0    nvidia
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2020.2                      256  
[conda] mkl-service               2.3.0            py36h8c4c3a4_2    conda-forge
[conda] mkl_fft                   1.3.0            py36h54f3939_0  
[conda] mkl_random                1.1.1            py36h0573a6f_0  
[conda] mypy-extensions           0.4.3                    pypi_0    pypi
[conda] numpy                     1.19.2           py36h54aff64_0  
[conda] numpy-base                1.19.2           py36hfa32c7d_0  
[conda] pytorch                   1.9.0           py3.6_cuda11.1_cudnn8.0.5_0    pytorch
[conda] pytorch3d                 0.3.0                    pypi_0    pypi
[conda] torchaudio                0.9.0                      py36    pytorch
[conda] torchvision               0.10.0               py36_cu111    pytorch

Metadata

Metadata

Labels

module: onnxRelated to torch.onnxonnx-triagedtriaged by ONNX teamtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions