使用Docker安装detectron2

Detectron2 是一个用于目标检测、分割和其他视觉识别任务的平台。

Detectron2 官网安装教程是基于 linux 安装的,在 windows 上直接安装有很多问题,下面采用 docker 方式在 windows 上安装。

拉取cuda116镜像

docker pull nvcr.io/nvidia/cuda:11.6.1-cudnn8-devel-ubuntu20.04

创建容器

docker run --gpus=all  -it --name ernerf -v D:\Projects\ER-NeRF:/ernerf nvcr.io/nvidia/cuda:11.6.1-cudnn8-devel-ubuntu20.04

安装依赖环境

apt-get update -yq --fix-missing \
 && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
    pkg-config \
    wget \
    cmake \
    curl \
    git \
    vim

安装Miniconda3

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh -b -u -p ~/miniconda3
~/miniconda3/bin/conda init
source ~/.bashrc

创建环境

conda create -n detectron python=3.10
conda activate detectron

安装依赖库

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113
pip install cython opencv-python opencv-python-headless

安装detectron2

git clone https://github.com/facebookresearch/detectron2.git

pip install -e detectron2

调用示例

from detectron2.config import get_cfg
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
import cv2

# 加载配置文件
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # 设置阈值
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")

# 创建预测器
predictor = DefaultPredictor(cfg)

# 加载图像
im = cv2.imread("./image.jpg")

# 进行预测
outputs = predictor(im)

# 可视化预测结果
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))

cv2.imwrite('./output.jpg', v.get_image()[:, :, ::-1])

相关推荐

  1. nacos v2.2.3 docker简单安装使用

    2024-03-25 03:16:01       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-25 03:16:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-25 03:16:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-25 03:16:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-25 03:16:01       20 阅读

热门阅读

  1. Vue修饰符总结

    2024-03-25 03:16:01       19 阅读
  2. AcWing 3417.砝码称重

    2024-03-25 03:16:01       20 阅读
  3. qinakun实现公共依赖的加载

    2024-03-25 03:16:01       26 阅读
  4. Git tag总结

    2024-03-25 03:16:01       16 阅读
  5. vscode集成git管理项目

    2024-03-25 03:16:01       20 阅读
  6. PiflowX-Faker组件

    2024-03-25 03:16:01       23 阅读
  7. bind更改this指向问题

    2024-03-25 03:16:01       17 阅读
  8. 三维重建-单目相机标定

    2024-03-25 03:16:01       19 阅读
  9. c语言如何颠倒字符串顺序

    2024-03-25 03:16:01       19 阅读