AI开箱 C++调用Detectron2的Mask R-CNN
欢迎订阅我的频道
- bilibili频道
- youtube频道
- 为保障项目复现,本视频在虚拟机下录制,系统: ubuntu-18.04.5-desktop-amd64.iso
- 虚拟机磁盘空间,建议分配40G
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-16nnXWoo-1606283199472)(https://github.com/dyh/unbox_use_detectron2_model_in_cpp_of_caffe2/blob/main/cover.png?raw=true)]
系统需求
- ubuntu 18.04
- python 3.6
- cuda 10.1
- cudnn 8.0.5
- 下载代码
安装 git
sudo apt install git
下载代码
git clone https://github.com/dyh/unbox_use_detectron2_model_in_cpp_of_caffe2.git unbox_cpp_caffe2
- 进入目录
cd unbox_cpp_caffe2/python_project/
- 创建 python 虚拟环境
sudo apt install python3-venv python3 -m venv venv
- 激活虚拟环境
source venv/bin/activate
- 升级pip
python -m pip install --upgrade pip
- 安装软件包
- 安装gcc
sudo apt install gcc
- 安装CUDA和CUDNN
下载 http://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_418.87.00_linux.run sudo bash cuda_10.1.243_418.87.00_linux.run下载 https://developer.nvidia.com/compute/machine-learning/cudnn/secure/8.0.5/10.1_20201106/cudnn-10.1-linux-x64-v8.0.5.39.tgz 解压,将include目录和lib64目录下的文件拷贝至 /usr/local/cuda 对应目录
- 安装 torch==1.6.0+cu101
pip install torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html
- 安装 detectron2==0.3+cu101
sudo apt install python3.6-dev python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.6/index.html
- 安装其他包
sudo apt-get install graphviz pip install opencv-python==4.4.0.46 pip install onnx==1.8.0 pip install protobuf==3.14.0
- 安装gcc
- 下载训练好的 Mask R-CNN weights 文件
model_0124999.pth,334.8 MB,这是我在上一期视频中,使用Mask R-CNN检测隧道裂缝的权重文件,包含2个分类:裂缝fissure和渗水water
- 下载链接1: https://pan.baidu.com/s/1BqUTgciTeDxng21dYcMlag 提取码: puaq
- 下载链接2: https://drive.google.com/file/d/1SLs-dCHibUMJY0dgcbkAb82h-Yxm6gAs/view?usp=sharing
- 形成 ./python_project/weights/model_0124999.pth 的目录结构
- 关于训练图片和标注文件
python_project/images/train 目录中已经包含训练图片和标注文件,关于如何制作这个JSON标注文件,请参考这个视频:
- bilibili
- youtube
python caffe2_converter.py
程序运行成功后,将在 python_project/output 目录生成 model.pb 和 model_init.pb 文件
第二部分:使用 C++ 调用模型 C++ 程序环境配置
- 进入目录
cd ../cpp_project/
- 安装依赖项
sudo apt install libgflags-dev libgoogle-glog-dev libopencv-dev pip install mkl-include
- 安装 protobuf
下载 https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protobuf-cpp-3.11.4.tar.gz tar xf protobuf-cpp-3.11.4.tar.gz cd protobuf-3.11.4 export CXXFLAGS=-D_GLIBCXX_USE_CXX11_ABI=$(python3 -c 'import torch; print(int(torch.compiled_with_cxx11_abi()))') ./configure --prefix=$HOME/.local && make && make install
- 配置 CMakeLists.txt
回到 cpp_project 目录,修改 CMakeLists.txt 文件中的内容
- 配置 pytorch 路径
修改路径 set(CMAKE_PREFIX_PATH $ENV{HOME}/workspace/unbox/unbox_cpp_caffe2/python_project/venv/lib64/python3.6/site-packages/torch) 指向 python 虚拟环境中的 pytorch 安装目录
- 配置 protobuf 路径
# point to the include folder of protobuf include_directories($ENV{HOME}/.local/include) # point to the lib folder of protobuf link_directories($ENV{HOME}/.local/lib)
- 配置 pytorch 路径
- 编译
回到 cpp_project 目录
安装cmake
sudo apt install cmake mkdir build && cd build cmake .. && make
- 运行
./caffe2_mask_rcnn
- https://github.com/facebookresearch/detectron2/tree/master/tools/deploy
- https://detectron2.readthedocs.io/tutorials/deployment.html
- youtube channel
- bilibili channel
- this video is recorded under the virtual machine (ubuntu-18.04.5-desktop-amd64.iso) to ensure the recurrence of the project
- it is recommended to allocate 40G disk space of virtual machine
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-548FEoU5-1606283199474)(https://github.com/dyh/unbox_use_detectron2_model_in_cpp_of_caffe2/blob/main/cover.png?raw=true)]
System Requirements
- ubuntu 18.04
- python 3.6
- cuda 10.1
- cudnn 8.0.5
- download source code
install git
sudo apt install git
clone source code to unbox_cpp_caffe2 folder
git clone https://github.com/dyh/unbox_use_detectron2_model_in_cpp_of_caffe2.git unbox_cpp_caffe2
- enter the python project folder
cd unbox_cpp_caffe2/python_project/
- create the virtual environment
sudo apt install python3-venv python3 -m venv venv
- activate virtual environment
source venv/bin/activate
- upgrade pip
python -m pip install --upgrade pip
- install software packages
- install gcc
sudo apt install gcc
- install CUDA and CUDNN
download file: http://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_418.87.00_linux.run sudo bash cuda_10.1.243_418.87.00_linux.rundownload file: https://developer.nvidia.com/compute/machine-learning/cudnn/secure/8.0.5/10.1_20201106/cudnn-10.1-linux-x64-v8.0.5.39.tgz unzip files, copy the files of include and lib64 to /usr/local/cuda/include and /usr/local/cuda/lib64 folder
- install torch==1.6.0+cu101
pip install torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html
- install detectron2==0.3+cu101
sudo apt install python3.6-dev python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.6/index.html
- install other software packages
sudo apt-get install graphviz pip install opencv-python==4.4.0.46 pip install onnx==1.8.0 pip install protobuf==3.14.0
- install gcc
- download pre-trained weights file of Mask R-CNN
model_0124999.pth, 334.8MB, in my last video, this is a weights file that uses Mask R-CNN to detect tunnel fissure, including 2 categories: fissure and water
- download link 1: https://drive.google.com/file/d/1SLs-dCHibUMJY0dgcbkAb82h-Yxm6gAs/view?usp=sharing
- download link 2: https://pan.baidu.com/s/1BqUTgciTeDxng21dYcMlag password: puaq
- make the directory structure as ./python_project/weights/model_0124999.pth
- About sample images and annotation files
The python_project/images/train directory already contains sample images and annotation files. You can refer to this video on how to make this annotation JSON file:
- youtube
- bilibili
python caffe2_converter.py
when the program runs successfully, model.pb and model_init.pb files are generated in the python_project/output directory
Chapter 2: Use C++ to load weights file C++ Environment Configuration
- enter the C++ project folder
cd ../cpp_project/
- install dependency packages
sudo apt install libgflags-dev libgoogle-glog-dev libopencv-dev pip install mkl-include
- install protobuf
download file: https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protobuf-cpp-3.11.4.tar.gz tar xf protobuf-cpp-3.11.4.tar.gz cd protobuf-3.11.4 export CXXFLAGS=-D_GLIBCXX_USE_CXX11_ABI=$(python3 -c 'import torch; print(int(torch.compiled_with_cxx11_abi()))') ./configure --prefix=$HOME/.local && make && make install
- configure the CMakeLists.txt
return to the cpp_project directory and modify the contents of the CMakeLists.txt file
- configure pytorch path
change path: set(CMAKE_PREFIX_PATH $ENV{HOME}/workspace/unbox/unbox_cpp_caffe2/python_project/venv/lib64/python3.6/site-packages/torch) Point to the pytorch installation directory in the python virtual environment
- configure protobuf path
# point to the include folder of protobuf include_directories($ENV{HOME}/.local/include) # point to the lib folder of protobuf link_directories($ENV{HOME}/.local/lib)
- configure pytorch path
- compile
return to the cpp_project directory
install cmake
sudo apt install cmake mkdir build && cd build cmake .. && make
- 【Unbox|C++ 调用 Mask R-CNN Detectron2】run
./caffe2_mask_rcnn
- https://github.com/facebookresearch/detectron2/tree/master/tools/deploy
- https://detectron2.readthedocs.io/tutorials/deployment.html
推荐阅读
- C++从入门到精通|C++( 指针微探(指针与数组))
- 使用30分钟快速入门机器学习时间
- 10个基本的机器学习面试问题分享
- Facebook使用机器学习的5种令人振奋的方式
- python|从零开始用 Python 构建一个简单的神经网络
- python机器学习|从零到一实现神经网络(python)(一)
- 如何使用Jupyter Notebook(-终极指南)
- 人工智能|浙大吴飞「舌战」阿里贾扬清(AI内卷与年薪百万,哪个才是真实())
- Flink|独家专访阿里集团副总裁贾扬清(我为什么选择加入阿里巴巴())