遇到問題可參考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
1280704 每張73ms 83% 3659MiB
TIPS
- 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
- 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
- **RuntimeError: CUDA error: invalid device function **
torch版本與cuda版本不對
- 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:
顯存不夠用,換一張分辨率小的圖片
- ’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不要寫反了
- CUDA版本與torch版本要對應
可以下載安裝多個版本的CUDA\CUDNN,在~/.bashrc中修改當前配置的版本
- 好像不用编译torch,编译的时候连接libtorch即可
編譯的話參考libtorch官網https://github.com/pytorch/pytorch