ailia-models icon indicating copy to clipboard operation
ailia-models copied to clipboard

ADD CenterNet2

Open kyakuno opened this issue 3 years ago • 5 comments

Two-stage CenterNet https://github.com/xingyizhou/CenterNet2 Apache

kyakuno avatar May 21 '22 11:05 kyakuno

■detectron2モデルのonnxエクスポート(公式) https://github.com/randombenj/detectron2onnx-inference/blob/master/detectron2-onnx.ipynb ①環境設定  ・環境:google colaboratory(CPU)  ・pytorchのバージョン変更(torch==1.8.0、torchvision==0.9.0)

import os
p = os.getenv('PATH')
ld = os.getenv('LD_LIBRARY_PATH')
os.environ['PATH'] = f"/usr/local/cuda-10.1/bin:{p}"
os.environ['LD_LIBRARY_PATH'] = f"/usr/local/cuda-10.1/lib64:{ld}"
!nvcc --version
!pip install torch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0

 ・onnxのインストール(バージョンを1.8.1に指定) !pip install onnx==1.8.1  ・detectron2のインストール !python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.8/index.html  公式が指定している「Caffe2Tracer」を用いたエクスポートには、pytorchのバージョンを1.8に設定し、detectron2のバージョンを合わせる必要がある。

②ONNXエクスポート  ・cfg:CenterNet2_R50_1x.yaml

  self.torch_model = build_model(cfg)
        DetectionCheckpointer(self.torch_model).resume_or_load(cfg.MODEL.WEIGHTS)
        self.torch_model.eval()

        # get a sample data
        data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0])
        first_batch = next(iter(data_loader))
        tracer = Caffe2Tracer(
            cfg,
            self.torch_model,
            first_batch
        )
        onnx_model = tracer.export_onnx()
        onnx.save(onnx_model, "seved_model.onnx")

「tracer.export_onnx()」の処理でエラーが発生。 (TypeError: forward() missing 1 required positional argument: 'gt_instances')

hattan1990 avatar Jun 29 '22 13:06 hattan1990

■torch.onnx.exportを使用 ①環境設定(複数のパターンで検証) pytorchのバージョンにより、ONNXエクスポートのエラーが変化するため複数のパターンで検証。  ・環境:google colaboratory(GPU)   →cuda-10.1, cuda-11.0  ・pytorchのバージョン変更   →1.8.0, 1.9.0+cu111, 1.9.1+cu111, 1.10.1+cu111  ・detectron2:   →https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.8/index.html   →https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.9/index.html   →https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.10/index.html

②ONNXエクスポート

        torch._C._jit_set_bailout_depth(1)
        self.torch_model = build_model(cfg)
        DetectionCheckpointer(self.torch_model).resume_or_load(cfg.MODEL.WEIGHTS)
        self.torch_model.eval()

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

        image = first_batch[0]["image"]
        inputs = [{"image": image}]
        inference = None

        traceable_model = TracingAdapter(self.torch_model, inputs, inference)
        torch.onnx.export(traceable_model, (image,), 'saved_model.onnx', opset_version=11)

③エクスポート時のエラー  ・pytorch:1.8.0  ・detectron2: https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.8/index.html RuntimeError: Exporting the operator repeat_interleave to ONNX opset version 11 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub.

 ・pytorch: 1.9.0+cu111, 1.9.1+cu111, 1.10.1+cu111  ・detectron2: https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.9/index.html, https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.10/index.html TypeError: 'torch._C.Value' object is not iterable

hattan1990 avatar Jul 04 '22 15:07 hattan1990

■ONNXエクスポート時のエラー対応 TypeError: 'torch._C.Value' object is not iterable ①pytorch以外の実装がされているか調査(C, CUDA)  detectron2の場合、pytorch以外で実装されている可能性が高いためネットワークを調査 ・preprocess_image:該当なし ・backbone:該当なし ・proposal_generator:該当なし ・roi_heads:custom_roi_heads.py / CustomCascadeROIHeadsでエラーが発生 ・postprocess

②roi_headsの修正  detectron2の「poolers.py」で実装される”convert_boxes_to_pooler_format”に問題があるため修正

③opset=12でエクスポート&動作確認  エクスポートは問題なく行えたが、onnxruntimeで動作確認を行った際に下記のエラーが発生 onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Non-zero status code returned while running Concat node. Name:'Concat_1939' Status Message: concat.cc:159 onnxruntime::ConcatBase::PrepareForCompute Non concat axis dimensions must match: Axis 0 has mismatched dimensions of 130 and 256

hattan1990 avatar Jul 14 '22 15:07 hattan1990

onnx_netron

hattan1990 avatar Jul 19 '22 14:07 hattan1990

エクスポートする際にモデルのネットワークを修正したが、concat部分は特に修正を行っていない。 また、修正したモデルで正しく推論されていることも確認済み。

hattan1990 avatar Jul 21 '22 14:07 hattan1990