Detectron2
1. 简介
Detectron2是Facebook AI Research(FAIR)推出的基于Pytorch的视觉算法开源框架,主要聚焦于目标检测和分割任务等视觉算法,此外还支持全景分割,关键点检测,旋转框检测等任务。Detectron2继承自Detectron 和mask-rcnn。
Detectron2具有较强的灵活性和可扩展性,支持快速的单GPU训练,多GPU并行训练和多节点分布式训练。
2. 环境依赖
- Linux or macOS with Python ≥ 3.7
- PyTorch ≥ 1.8 and torchvision
- OpenCV 可选
3.推荐步骤
python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'
# (add --user if you don't have permission)
# Or, to install it from a local clone:
git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2
# On macOS, you may need to prepend the above commands with a few environment variables:
CC=clang CXX=clang++ ARCHFLAGS="-arch x86_64" python -m pip install ...
4. 环境搭建
前置硬件环境
- Ubuntu 20.04.1 LTS ($lsb_release -a)
- Linux kernel 5.15.0-46-generic ($uname -a)
- CUDA Version 11.7 Driver Version 515.65.01 ($nvidia-smi)
- CUDNN Version 8.4.1
- A40 GPU
- python环境
conda create --name det python==3.10.4
- torch &torchvison
pip3 install torchvision+cu117 -f https://download.pytorch.org/whl/torch_stable.html
pip3 install torchvision==0.14.0+cu117 -f https://download.pytorch.org/whl/torch_stable.html
- openCV
pip install opencv-python==4.5.5.64
- detectron2
git clone https://github.com/facebookresearch/detectron2.git
cd detectron2
python setup.py install
输出 Finished processing dependencies for detectron2==0.6 则为安装成功
5.可选项: 在.bashrc
设置CUDA 环境变量
vim ~/.bashrc
export PATH=/usr/local/cuda/bin${
PATH:+:${
PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_HOME=/usr/local/cuda
source ~/.bashrc
5. demo 教程
环境搭建完成后就可以开始学习demo代码了,demo脚本在detectron2目录下,GETTING_STARTED.md
中有详细步骤
- 获取测试图像
wget http://images.cocodataset.org/val2017/000000439715.jpg -q -O input.jpg
- 测试输出
python demo.py --input input.jpg --output out.png
将加载默认的模型配置文件: ../configs/quick_schedules/mask_rcnn_R_50_FPN_inference_acc_test.yaml
和checkpoint
detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl
6. demo colab源代码
参考代码教程
打不开上面链接的可以直接看以下全文代码:
# -*- coding: utf-8 -*-
"""Detectron2 Tutorial.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/16jcaJoc6bCFAQ96jDe2HwtXj7BMD_-m5
# Detectron2 Beginner's Tutorial
<img src="https://dl.fbaipublicfiles.com/detectron2/Detectron2-Logo-Horz.png" width="500">
Welcome to detectron2! This is the official colab tutorial of detectron2. Here, we will go through some basics usage of detectron2, including the following:
* Run inference on images or videos, with an existing detectron2 model
* Train a detectron2 model on a new dataset
You can make a copy of this tutorial by "File -> Open in playground mode" and make changes there. __DO NOT__ request access to this tutorial.
# Install detectron2
"""
!python -m pip install pyyaml==5.1
import sys, os, distutils.core
# Note: This is a faster way to install detectron2 in Colab, but it does not include all functionalities.
# See https://detectron2.readthedocs.io/tutorials/install.html for full installation instructions
!git clone 'https://github.com/facebookresearch/detectron2'
dist = distutils.core.run_setup("./detectron2/setup.py")
!python -m pip install {
' '.join([f"'{
x}'" for x in dist.install_requires])}
sys.path.insert(0, os.path.abspath('./detectron2'))
# Properly install detectron2. (Please do not install twice in both ways)
# !python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'
import torch, detectron2
!nvcc --version
TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
CUDA_VERSION = torch.__version__.split("+")[-1]
print("torch: ", TORCH_VERSION, "; cuda: ", CUDA_VERSION)
print("detectron2:", detectron2.__version__)
# Some basic setup:
# Setup detectron2 logger
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger(