Ubuntu18.04--Detectron2环境配置与安装

目录

  • 环境配置
  • detectron2安装
  • 数据集准备
  • Detectron2测试

环境配置

创建并激活detectron2环境

conda create --name detectron2 python=3.6
conda activate detectron2

安装pytorch、torchvision、cudatoolkit,添加国内源解决网络问题,依次执行以下命令:

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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes

执行完毕后,安装

conda install pytorch==1.6.0 torchvision==0.7.0 cudatoolkit=10.1 

安装Opencv

conda install --channel https://conda.anaconda.org/menpo opencv3

安装cython

pip install cython

安装pycocotools

pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'

detectron2安装

git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2

安装成功
Ubuntu18.04--Detectron2环境配置与安装_第1张图片

数据集准备

http://images.cocodataset.org/zips/train2017.zip
http://images.cocodataset.org/zips/val2017.zip
http://images.cocodataset.org/zips/test2017.zip
http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip
http://images.cocodataset.org/annotations/annotations_trainval2017.zip
http://images.cocodataset.org/annotations/image_info_test2017.zip

解压选中数据集,并按照截图所示放置文件位置。
Ubuntu18.04--Detectron2环境配置与安装_第2张图片

Ubuntu18.04--Detectron2环境配置与安装_第3张图片

Detectron2测试

conda activate detectron2
# 进去demo目录·
cd ./detectron2/demo
# 执行测试demo.py
python demo.py --config-file ../configs/COCO-Detection/retinanet_R_50_FPN_1x.yaml --input 000000000016.jpg [--otheroptions] --opts MODEL.WEIGHTS detectron2://COCO-Detection/retinanet_R_50_FPN_1x/190397773/model_final_bfca0b.pkl
import numpy as np
import cv2 as cv
from PIL import Image
#from matplotlib import pyplot
import matplotlib.pyplot as plt
import random
#from google.colab.patches import cv2_imshow

import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()

from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
#下载图片
#wget http://images.cocodataset.org/val2017/000000439715.jpg -O input.jpg
im = cv.imread("/home/**/project/detectron2/ceshi/input.jpg")

cfg = get_cfg()
cfg.merge_from_file("/home/**/project/detectron2/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  #模型阈值
#cfg.MODEL.WEIGHTS = "./COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl"
cfg.MODEL.WEIGHTS = "/home/**/project/detectron2/pre_train_model/model_final_f10217.pkl"
predictor = DefaultPredictor(cfg)
outputs = predictor(im)

pred_classes = outputs["instances"].pred_classes
pred_boxes = outputs["instances"].pred_boxes

#在原图上画出检测结果
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
plt.figure(2)
plt.imshow(v.get_image())
plt.show()

Ubuntu18.04--Detectron2环境配置与安装_第4张图片
参考1
参考2

你可能感兴趣的:(14周AI人工智能)