detectron2云服务器部署 安装 测试

    技术2025-12-26  18

    Detectron2,基于 Pytorch 。 记录一下Detectron2 环境搭建过程,弄了一下午,踩了好多坑。 我们先来看看官方的配置需求 Requirements Linux or macOS with Python ≥ 3.6 PyTorch ≥ 1.4 torchvision that matches the PyTorch installation. You can install them together at pytorch.org to make sure of this. pycocotools. Install it by pip install pycocotools>=2.0.1. OpenCV, optional, needed by demo and visualization

    环境配置

    本人系统centos7(无GPU)、anaconda3

    anaconda虚拟环境

    conda create -n py37 python=3.7 conda env list source activate py37

    注意:后面所有的安装指令都在py37虚拟环境中运行

    pytorch环境

    进入官网选择自己对应的版本 注意:去掉后面的 -c pytorch 根据该命令的提示很容易就能安装好 Pytorch。

    conda install pytorch torchvision cpuonly

    如果安装失败,很可能是因为网络的延迟过高而超时。 所以我们可以通过配置 anacoda 国内镜像源来加速安装。 命令执行以下命令

    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --set show_channel_urls yes

    同时添加第三方的下载源

    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

    然后重新安装 Pytorch

    OpenCV

    在Python3.7中用conda下载OpenCV是无效的 我们可以用pip安装

    pip install opencv-python

    如果网络不好很容易失败,我们在后面加上清华的pypi镜像

    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python

    fvcore

    fvcore 是 FAIR 开源的 一个轻量级的核心库,它提供了在各种计算机视觉框架(如 Detectron2)中共享的最常见和最基本的功能。

    pip install fvcore

    gcc

    yum install gcc gcc --version

    pycocotools

    微软发布的 COCO 数据库是一个大型图像数据集, 专为对象检测、分割、人体关键点检测、语义分割和字幕生成而设计。

    pip install -U Cython pip install -U pycocotools

    安装 detectron2

    方法一:可以直接用pip安装,前提需要下载git

    pip install 'git+https://github.com/facebookresearch/detectron2.git'

    git安装方法

    yum install -y git git version

    不过我的服务器不知道怎么回事,老是报错,搞了好久,索性…直接在本地下载好,然后上传到阿里云服务器上

    方法二:本地下载好后上传 在detectron2项目官网上,将文件下载到本地 通过FileZilla工具,将文件上传到服务器上 然后在服务器上下载解压ZIP文件的工具,并解压文件

    yum install -y unzip zip unzip detectron2-master.zip

    预编译包安装(仅支持 Linux)

    在官方文档里选择自己的版本 由于我的服务器没有GPU,所以选择只有CPU的版本

    python -m pip install detectron2 -f \ https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.5/index.html

    测试运行Demo 目标检测

    进入detectron2根目录,创建好inputs outputs文件夹 将要训练的图片放在inputs文件夹里 运行Demo程序

    python demo/demo.py \ --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \ --input inputs/input.jpg --output outputs/ \ --opts MODEL.WEIGHTS detectron2://COCO-Detection/faster_rcnn_R_50_FPN_1x/137257794/model_final_b275ba.pkl MODEL.DEVICE cpu

    注意:MODEL.DEVICE cpu,是专门在CPU上执行的,如果你的服务器上有GPU,则不需要添加 在outputs里查看目标检测结果: emmmm,黑人识别率有些低啊…

    测试运行Demo 实例分割

    python demo/demo.py \ --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \ --input inputs/input.jpg --output outputs/ \ --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl \ MODEL.DEVICE cpu

    效果不错 好啦,老师的课堂任务完成,就不继续往下深究了~

    Processed: 0.028, SQL: 9