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/facebookresearch/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

编译推理文件

pytorch官網下載libtorch,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

  1. protobuf版本不對  error: #error This file was generated by an older version of protoc which is

原因: 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

參考:https://zhuanlan.zhihu.com/p/104395486
https://blog.csdn.net/huang826336127/article/details/88586335
下載安裝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官網https://github.com/pytorch/pytorch

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值