【Detectron2】代码库学习-2. 环境搭建和demo

本文档介绍了Detectron2,Facebook AI Research的PyTorch目标检测和分割框架,强调其灵活性和可扩展性。内容包括环境依赖(如Python 3.7+,PyTorch 1.8+,CUDA 11.7和CUDNN 8.4.1),环境搭建步骤,以及在Ubuntu 20.04.1 LTS系统上的GPU配置。完成环境搭建后,提供了一个通过加载默认模型配置文件和checkpoint进行demo教程的概述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Detectron2

1. 简介

Detectron2是Facebook AI Research(FAIR)推出的基于Pytorch的视觉算法开源框架,主要聚焦于目标检测和分割任务等视觉算法,此外还支持全景分割,关键点检测,旋转框检测等任务。Detectron2继承自Detectronmask-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
  1. python环境
conda create --name det python==3.10.4
  1. 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
  1. openCV
pip install opencv-python==4.5.5.64
  1. 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中有详细步骤

  1. 获取测试图像
wget http://images.cocodataset.org/val2017/000000439715.jpg -q -O input.jpg
  1. 测试输出
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

input.png
output.ping

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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vcbe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值