您好,欢迎来到爱问旅游网。
搜索
您的当前位置:首页detectron2训练目标检测模型、转caffe2、C++调用

detectron2训练目标检测模型、转caffe2、C++调用

来源:爱问旅游网

遇到問題可參考TIPS

安装虚拟环境

conda create -n detectron2 python=3.7
source activate detectron2
conda install pytorch=1.6.0 torchvision cudatoolkit=10.2 -c pytorch
conda install protobuf numpy
pip install onnx

安裝detectron2

參考github detectron2官網INSTALL.md

python -m pip install 'git+https://github.com/research/detectron2.git'

detectron2训练模型

參考train_boatdet.ipynb

模型转换torch–>caffe2

detectron2/tools/deploy目錄下

./caffe2_converter.py --config-file ../../configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml --output ./caffe2_model_gpu  MODEL.WEIGHTS ../../output/model_final.pth  MODEL.DEVICE cuda

執行成功會在當前目錄生成caffe2_model_gpu文件夾,下面生成 model.pb,model.svg,model_init.pb,model.pbtxt

编译推理文件

,https://download.pytorch.org/libtorch/cu102/libtorch-cxx11-abi-shared-with-deps-1.7.0.zip

detectron2/tools/deploy目錄下

cmake -DCMAKE_PREFIX_PATH=path/libtorch 
make

测试

測試單個文件

./caffe2_mask_rcnn  --predict_net=caffe2_model_gpu/model.pb --init_net=caffe2_model_gpu/model_init.pb --input=/home/fzy/project/mmdetction/pytorch/1.jpg 

分辨率越高,=識別效果越好=,識別速度越慢(2070 S)
分辨率    耗時     GPU-Util 顯存
19201088 每張160ms    83% 6513MiB
1280
704   每張73ms     83% 3659MiB

TIPS

原因: protobuf版本可以在libtorch/include/caffe2/proto/caffe2.pb.h 中17,18,19行找到

 pip  install -i https://pypi.tuna.tsinghua.edu.cn/simple protobuf==3.11.0
  1. fatal error: mkl_cblas.h: No such file or directory

參考:

下載安裝mkl參考

export CPATH=/opt/intel/compilers_and_libraries_2019.5.281/linux/mkl/include/:$CPATH
  1. **RuntimeError: CUDA error: invalid device function **

torch版本與cuda版本不對

  1. 4’c10::Error’
    what(): [enforce fail at context_gpu.cu:52u4] error == cudaSuccess. 2 vs 0. Error at: /pytorch/caffe2/core/context_gpu.cu:524: out of memory (Error from operator:

顯存不夠用,換一張分辨率小的圖片

  1. ’c10::Error’
    what(): [enforce fail at elementwise_ops_utils.cc:56] A_dim == B_dim || A_dim == 1 || B_dim == 1. (Error from operator:

忘了怎麼解決了,貌似圖片尺寸不是32的倍數就會出現這種錯誤
記得height 與width不要寫反了

  1. CUDA版本與torch版本要對應

可以下載安裝多個版本的CUDA\CUDNN,在~/.bashrc中修改當前配置的版本

  1. 好像不用编译torch,编译的时候连接libtorch即可

編譯的話參考libtorch官網

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务