Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while exporting Detectron2 model to Onnx model #5210

Open
AakashGoyal25 opened this issue Feb 8, 2024 · 11 comments
Open

Error while exporting Detectron2 model to Onnx model #5210

AakashGoyal25 opened this issue Feb 8, 2024 · 11 comments

Comments

@AakashGoyal25
Copy link

AakashGoyal25 commented Feb 8, 2024

I am trying to export my detectron2 model to onnx model but I am getting a KeyError: 'Non-existent config key: DATASETS.VAL'

Command I run:

python detectron2/tools/deploy/export_model.py --config-file output.yaml --output model.onnx --format onnx --sample-image /home/aakash/Aakash Workings/HB_Codes/detectron2_working/XYZ/val/images/0e3561a5-am_c1_20231004-094138.jpeg --export-method caffe2_tracing MODEL.DEVICE cuda MODEL.WEIGHTS /home/aakash/Aakash Workings/HB_Codes/detectron2_working/output/model_final.pth`

Error Traceback:

(detectron_env) aakash@Goyal:~/Aakash Workings/HB_Codes/detectron2_working$ python detectron2/tools/deploy/export_model.py --config-file output.yaml --output model 
--format onnx --sample-image /home/aakash/Aakash Workings/HB_Codes/detectron2_working/d2_seal_control_ob_l/val/images/0e3561a5-am_c1_20231004-094138.jpeg --export-m
ethod caffe2_tracing MODEL.DEVICE cuda MODEL.WEIGHTS /home/aakash/Aakash Workings/HB_Codes/detectron2_working/output/model_final.pth
[02/08 22:51:33 detectron2]: Command line arguments: Namespace(format='onnx', export_method='tracing', config_file='output.yaml', sample_image='/home/aakash/Aakash', run_eval=False, output='model', opts=['Workings/HB_Codes/detectron2_working/d2_seal_control_ob_l/val/images/0e3561a5-am_c1_20231004-094138.jpeg', '--export-method', 'caffe2_tracing', 'MODEL.DEVICE', 'cuda', 'MODEL.WEIGHTS', '/home/aakash/Aakash', 'Workings/HB_Codes/detectron2_working/output/model_final.pth'])
[W init.cpp:855] Warning: Use _jit_set_fusion_strategy, bailout depth is deprecated. Setting to (STATIC, 1) (function operator())
Traceback (most recent call last):
  File "/home/aakash/Aakash Workings/HB_Codes/detectron2_working/detectron2/tools/deploy/export_model.py", line 210, in <module>
    cfg = setup_cfg(args)
  File "/home/aakash/Aakash Workings/HB_Codes/detectron2_working/detectron2/tools/deploy/export_model.py", line 34, in setup_cfg
    cfg.merge_from_file(args.config_file)
  File "/home/aakash/Aakash Workings/HB_Codes/detectron2_working/detectron2/detectron2/config/config.py", line 69, in merge_from_file
    self.merge_from_other_cfg(loaded_cfg)
  File "/home/aakash/Aakash Workings/HB_Codes/detectron2_working/detectron_env/lib/python3.10/site-packages/fvcore/common/config.py", line 132, in merge_from_other_cfg
    return super().merge_from_other_cfg(cfg_other)
  File "/home/aakash/Aakash Workings/HB_Codes/detectron2_working/detectron_env/lib/python3.10/site-packages/yacs/config.py", line 217, in merge_from_other_cfg
    _merge_a_into_b(cfg_other, self, self, [])
  File "/home/aakash/Aakash Workings/HB_Codes/detectron2_working/detectron_env/lib/python3.10/site-packages/yacs/config.py", line 478, in _merge_a_into_b
    _merge_a_into_b(v, b[k], root, key_list + [k])
  File "/home/aakash/Aakash Workings/HB_Codes/detectron2_working/detectron_env/lib/python3.10/site-packages/yacs/config.py", line 491, in _merge_a_into_b
    raise KeyError("Non-existent config key: {}".format(full_key))
KeyError: 'Non-existent config key: DATASETS.VAL'

config.yaml

CUDNN_BENCHMARK: false
DATALOADER:
  ASPECT_RATIO_GROUPING: true
  FILTER_EMPTY_ANNOTATIONS: true
  NUM_WORKERS: 2
  REPEAT_THRESHOLD: 0.0
  SAMPLER_TRAIN: TrainingSampler
DATASETS:
  PRECOMPUTED_PROPOSAL_TOPK_TEST: 1000
  PRECOMPUTED_PROPOSAL_TOPK_TRAIN: 2000
  PROPOSAL_FILES_TEST: []
  PROPOSAL_FILES_TRAIN: []
  TEST: []
  TRAIN:
  - train
  VAL:
  - val
GLOBAL:
  HACK: 1.0
INPUT:
  CROP:
    ENABLED: false
    SIZE:
    - 0.9
    - 0.9
    TYPE: relative_range
  FORMAT: BGR
  MASK_FORMAT: polygon
  MAX_SIZE_TEST: 1333
  MAX_SIZE_TRAIN: 1333
  MIN_SIZE_TEST: 800
  MIN_SIZE_TRAIN:
  - 640
  - 672
  - 704
  - 736
  - 768
  - 800
  MIN_SIZE_TRAIN_SAMPLING: choice
  RANDOM_FLIP: horizontal
MODEL:
  ANCHOR_GENERATOR:
    ANGLES:
    - - -90
      - 0
      - 90
    ASPECT_RATIOS:
    - - 0.5
      - 1.0
      - 2.0
    NAME: DefaultAnchorGenerator
    OFFSET: 0.0
    SIZES:
    - - 32
    - - 64
    - - 128
    - - 256
    - - 512
  BACKBONE:
    FREEZE_AT: 2
    NAME: build_resnet_fpn_backbone
  DEVICE: cuda
  FPN:
    FUSE_TYPE: sum
    IN_FEATURES:
    - res2
    - res3
    - res4
    - res5
    NORM: ''
    OUT_CHANNELS: 256
  KEYPOINT_ON: false
  LOAD_PROPOSALS: false
  MASK_ON: false
  META_ARCHITECTURE: GeneralizedRCNN
  PANOPTIC_FPN:
    COMBINE:
      ENABLED: true
      INSTANCES_CONFIDENCE_THRESH: 0.5
      OVERLAP_THRESH: 0.5
      STUFF_AREA_LIMIT: 4096
    INSTANCE_LOSS_WEIGHT: 1.0
  PIXEL_MEAN:
  - 103.53
  - 116.28
  - 123.675
  PIXEL_STD:
  - 1.0
  - 1.0
  - 1.0
  PROPOSAL_GENERATOR:
    MIN_SIZE: 0
    NAME: RPN
  RESNETS:
    DEFORM_MODULATED: false
    DEFORM_NUM_GROUPS: 1
    DEFORM_ON_PER_STAGE:
    - false
    - false
    - false
    - false
    DEPTH: 101
    NORM: FrozenBN
    NUM_GROUPS: 1
    OUT_FEATURES:
    - res2
    - res3
    - res4
    - res5
    RES2_OUT_CHANNELS: 256
    RES5_DILATION: 1
    STEM_OUT_CHANNELS: 64
    STRIDE_IN_1X1: true
    WIDTH_PER_GROUP: 64
  RETINANET:
    BBOX_REG_LOSS_TYPE: smooth_l1
    BBOX_REG_WEIGHTS: &id002
    - 1.0
    - 1.0
    - 1.0
    - 1.0
    FOCAL_LOSS_ALPHA: 0.25
    FOCAL_LOSS_GAMMA: 2.0
    IN_FEATURES:
    - p3
    - p4
    - p5
    - p6
    - p7
    IOU_LABELS:
    - 0
    - -1
    - 1
    IOU_THRESHOLDS:
    - 0.4
    - 0.5
    NMS_THRESH_TEST: 0.5
    NORM: ''
    NUM_CLASSES: 80
    NUM_CONVS: 4
    PRIOR_PROB: 0.01
    SCORE_THRESH_TEST: 0.05
    SMOOTH_L1_LOSS_BETA: 0.1
    TOPK_CANDIDATES_TEST: 1000
  ROI_BOX_CASCADE_HEAD:
    BBOX_REG_WEIGHTS:
    - &id001
      - 10.0
      - 10.0
      - 5.0
      - 5.0
    - - 20.0
      - 20.0
      - 10.0
      - 10.0
    - - 30.0
      - 30.0
      - 15.0
      - 15.0
    IOUS:
    - 0.5
    - 0.6
    - 0.7
  ROI_BOX_HEAD:
    BBOX_REG_LOSS_TYPE: smooth_l1
    BBOX_REG_LOSS_WEIGHT: 1.0
    BBOX_REG_WEIGHTS: *id001
    CLS_AGNOSTIC_BBOX_REG: false
    CONV_DIM: 256
    FC_DIM: 1024
    FED_LOSS_FREQ_WEIGHT_POWER: 0.5
    FED_LOSS_NUM_CLASSES: 50
    NAME: FastRCNNConvFCHead
    NORM: ''
    NUM_CONV: 0
    NUM_FC: 2
    POOLER_RESOLUTION: 7
    POOLER_SAMPLING_RATIO: 0
    POOLER_TYPE: ROIAlignV2
    SMOOTH_L1_BETA: 0.0
    TRAIN_ON_PRED_BOXES: false
    USE_FED_LOSS: false
    USE_SIGMOID_CE: false
  ROI_HEADS:
    BATCH_SIZE_PER_IMAGE: 128
    IN_FEATURES:
    - p2
    - p3
    - p4
    - p5
    IOU_LABELS:
    - 0
    - 1
    IOU_THRESHOLDS:
    - 0.5
    NAME: StandardROIHeads
    NMS_THRESH_TEST: 0.5
    NUM_CLASSES: 7
    POSITIVE_FRACTION: 0.25
    PROPOSAL_APPEND_GT: true
    SCORE_THRESH_TEST: 0.05
  ROI_KEYPOINT_HEAD:
    CONV_DIMS:
    - 512
    - 512
    - 512
    - 512
    - 512
    - 512
    - 512
    - 512
    LOSS_WEIGHT: 1.0
    MIN_KEYPOINTS_PER_IMAGE: 1
    NAME: KRCNNConvDeconvUpsampleHead
    NORMALIZE_LOSS_BY_VISIBLE_KEYPOINTS: true
    NUM_KEYPOINTS: 17
    POOLER_RESOLUTION: 14
    POOLER_SAMPLING_RATIO: 0
    POOLER_TYPE: ROIAlignV2
  ROI_MASK_HEAD:
    CLS_AGNOSTIC_MASK: false
    CONV_DIM: 256
    NAME: MaskRCNNConvUpsampleHead
    NORM: ''
    NUM_CONV: 4
    POOLER_RESOLUTION: 14
    POOLER_SAMPLING_RATIO: 0
    POOLER_TYPE: ROIAlignV2
  RPN:
    BATCH_SIZE_PER_IMAGE: 256
    BBOX_REG_LOSS_TYPE: smooth_l1
    BBOX_REG_LOSS_WEIGHT: 1.0
    BBOX_REG_WEIGHTS: *id002
    BOUNDARY_THRESH: -1
    CONV_DIMS:
    - -1
    HEAD_NAME: StandardRPNHead
    IN_FEATURES:
    - p2
    - p3
    - p4
    - p5
    - p6
    IOU_LABELS:
    - 0
    - -1
    - 1
    IOU_THRESHOLDS:
    - 0.3
    - 0.7
    LOSS_WEIGHT: 1.0
    NMS_THRESH: 0.7
    POSITIVE_FRACTION: 0.5
    POST_NMS_TOPK_TEST: 1000
    POST_NMS_TOPK_TRAIN: 1000
    PRE_NMS_TOPK_TEST: 1000
    PRE_NMS_TOPK_TRAIN: 2000
    SMOOTH_L1_BETA: 0.0
  SEM_SEG_HEAD:
    COMMON_STRIDE: 4
    CONVS_DIM: 128
    IGNORE_VALUE: 255
    IN_FEATURES:
    - p2
    - p3
    - p4
    - p5
    LOSS_WEIGHT: 1.0
    NAME: SemSegFPNHead
    NORM: GN
    NUM_CLASSES: 54
  WEIGHTS: https://dl.fbaipublicfiles.com/detectron2/COCO-Detection/faster_rcnn_R_101_FPN_3x/137851257/model_final_f6e8b1.pkl
OUTPUT_DIR: ./output
SEED: -1
SOLVER:
  AMP:
    ENABLED: false
  BASE_LR: 0.00025
  BASE_LR_END: 0.0
  BIAS_LR_FACTOR: 1.0
  CHECKPOINT_PERIOD: 50
  CLIP_GRADIENTS:
    CLIP_TYPE: value
    CLIP_VALUE: 1.0
    ENABLED: false
    NORM_TYPE: 2.0
  GAMMA: 0.1
  IMS_PER_BATCH: 4
  LR_SCHEDULER_NAME: WarmupMultiStepLR
  MAX_ITER: 100
  MOMENTUM: 0.9
  NESTEROV: false
  NUM_DECAYS: 3
  REFERENCE_WORLD_SIZE: 0
  RESCALE_INTERVAL: false
  STEPS: []
  WARMUP_FACTOR: 0.001
  WARMUP_ITERS: 1000
  WARMUP_METHOD: linear
  WEIGHT_DECAY: 0.0001
  WEIGHT_DECAY_BIAS: null
  WEIGHT_DECAY_NORM: 0.0
TEST:
  AUG:
    ENABLED: false
    FLIP: true
    MAX_SIZE: 4000
    MIN_SIZES:
    - 400
    - 500
    - 600
    - 700
    - 800
    - 900
    - 1000
    - 1100
    - 1200
  DETECTIONS_PER_IMAGE: 100
  EVAL_PERIOD: 0
  EXPECTED_RESULTS: []
  KEYPOINT_OKS_SIGMAS: []
  PRECISE_BN:
    ENABLED: false
    NUM_ITER: 200
VERSION: 2
VIS_PERIOD: 0

In the config.yaml file, I can see that "val" is there still I am getting this issue. What am I doing wrong?

"Instructions To Reproduce the Issue and Full Logs":
config.yaml file is provided and It can be run using the above command.

Environment:
It is environemnt is setup using https://detectron2.readthedocs.io/en/latest/tutorials/install.html

@github-actions github-actions bot added the needs-more-info More info is needed to complete the issue label Feb 8, 2024
Copy link

github-actions bot commented Feb 8, 2024

You've chosen to report an unexpected problem or bug. Unless you already know the root cause of it, please include details about it by filling the issue template.
The following information is missing: "Instructions To Reproduce the Issue and Full Logs"; "Your Environment";

@github-actions github-actions bot removed the needs-more-info More info is needed to complete the issue label Feb 8, 2024
@RajUpadhyay
Copy link

RajUpadhyay commented Feb 13, 2024

@AakashGoyal25 trying changing your export-method from caffe2_tracing to tracing.

@AakashGoyal25
Copy link
Author

@RajUpadhyay Even after changing export method to "tracing", I am getting same error mentioned above. Any other suggestion?

@RajUpadhyay
Copy link

RajUpadhyay commented Feb 14, 2024

@AakashGoyal25
Your error indicates that problem is related to output.yaml.

Just for the peace of mind, can you replace output.yaml with normal mask_rcnn_R_50_FPN_3x.yaml or its faster_rcnn version.
Lets narrow down where the cause of the issue.

Also this is probably not the issue but Is your image path correct?
--sample-image /home/aakash/Aakash Workings/HB_Codes/detectron2_working/XYZ/val/images/0e3561a5-am_c1_20231004-094138.jpeg

There is a space /Aakash Workings/ , just copy your image to your current directory and try running it again.

@AakashGoyal25
Copy link
Author

python3 detectron2/tools/deploy/export_model.py --config-file /home/aakash/Aakash_Workings/HB_Codes/detectron2_working/detectron2/configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml --output model.onnx --format onnx --sample-image d2_seal_control_ob_l/train/images/902f32eb-am_c1_20231005-104409.jpeg --export-method caffe2_tracing MODEL.DEVICE cuda MODEL.WEIGHTS output/model_final.pth

As you mentioned I have replaced with COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml and took relative path to the sample image.

Now I am getting:

[02/20 09:54:22 detectron2]: Command line arguments: Namespace(format='onnx', export_method='caffe2_tracing', config_file='/home/aakash/Aakash_Workings/HB_Codes/detectron2_working/detectron2/configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml', sample_image='d2_seal_control_ob_l/train/images/902f32eb-am_c1_20231005-104409.jpeg', run_eval=False, output='model.onnx', opts=['MODEL.DEVICE', 'cuda', 'MODEL.WEIGHTS', 'output/model_final.pth'])
[W init.cpp:855] Warning: Use _jit_set_fusion_strategy, bailout depth is deprecated. Setting to (STATIC, 1) (function operator())
[02/20 09:54:24 d2.checkpoint.detection_checkpoint]: [DetectionCheckpointer] Loading from output/model_final.pth ...
Skip loading parameter 'roi_heads.box_predictor.cls_score.weight' to the model due to incompatible shapes: (8, 1024) in the checkpoint but (81, 1024) in the model! You might want to double check if this is expected.
Skip loading parameter 'roi_heads.box_predictor.cls_score.bias' to the model due to incompatible shapes: (8,) in the checkpoint but (81,) in the model! You might want to double check if this is expected.
Skip loading parameter 'roi_heads.box_predictor.bbox_pred.weight' to the model due to incompatible shapes: (28, 1024) in the checkpoint but (320, 1024) in the model! You might want to double check if this is expected.
Skip loading parameter 'roi_heads.box_predictor.bbox_pred.bias' to the model due to incompatible shapes: (28,) in the checkpoint but (320,) in the model! You might want to double check if this is expected.
Some model parameters or buffers are not found in the checkpoint:
roi_heads.box_predictor.bbox_pred.{bias, weight}
roi_heads.box_predictor.cls_score.{bias, weight}

Traceback (most recent call last):
File "/home/aakash/Aakash_Workings/HB_Codes/detectron2_working/detectron2/tools/deploy/export_model.py", line 220, in
exported_model = export_caffe2_tracing(cfg, torch_model, sample_inputs)
File "/home/aakash/Aakash_Workings/HB_Codes/detectron2_working/detectron2/tools/deploy/export_model.py", line 41, in export_caffe2_tracing
from detectron2.export import Caffe2Tracer
ImportError: cannot import name 'Caffe2Tracer' from 'detectron2.export' (/home/aakash/Aakash_Workings/HB_Codes/detectron2_working/detectron2/detectron2/export/init.py)

@RajUpadhyay
Copy link

@AakashGoyal25
I am sorry for requesting this again but can you use tracing instead of caffe_tracing for the same example i.e., default model and relative path.

@AakashGoyal25
Copy link
Author

AakashGoyal25 commented Feb 20, 2024

Yes, it successfully export the onnx file. Thanks ! I can see some light 👍

But I am still not able to export it with my config.yaml.. Still facing the same issue mentioned in the first comment.

I saved the config.yaml using the below code:

Save the configuration

cfg_yaml = cfg.dump()
with open(os.path.join(cfg.OUTPUT_DIR, "config.yaml"), 'w') as f:
f.write(cfg_yaml)

@RajUpadhyay
Copy link

Maybe you could search a bit more on the internet regarding this.
Perhaps some blog or some repo related to detectron2.
I do not think this repo is being maintained anymore so, I wish you good luck.

Can you try this way though
https://medium.com/innovation-res/detectron2-config-optimizer-lr-scheduler-part-1-4555842e1ea#:~:text=weights%20and%20metrics.-,import%20yaml,-%23%20Dump%20the%20config

@Huxwell
Copy link

Huxwell commented Apr 23, 2024

@AakashGoyal25 have you been able to successfully export your Faster RCNN Model after all?
Did you by chance also try to export it to TensorRT?

@AakashGoyal25
Copy link
Author

@Huxwell..I was able to export it successfully to Onnx. But I didn't try to export to TensorRT.

@Huxwell
Copy link

Huxwell commented Apr 23, 2024

Thanks. I have realized, I am also able to export to onnx, only failing during onnx graphsurgeon for trt, so sorry for bothering you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants