本文主要是介绍Detectron2安装测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Detectron2安装测试
Detectron2是FAIR开源的基于Pytorch1.3.1的目标检测算法实现.
Detectron2-github
1.Detectron2安装
1.1 安装配置基本环境
可参考项目中的Installtion
- conda create -n detectron2 python=3.7
- conda activate detectron2
- PyTorch 1.3
- torchvision版本需要和pytorch的版本相适应,可参考 pytorch.org 安装。
conda install pytorch torchvision cudatoolkit=10.1 -c pytorch
- OpenCV, Demo和显示时需要使用,
pip install opencv-python
- fvcore:
pip install 'git+https://github.com/facebookresearch/fvcore'
- pycocotools:
pip install cython; pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
- GCC >= 4.9
注意:conda安装添加清华源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
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 --set show_channel_urls yes
1.2 检测coda是否正确安装
运行如下代码,检查cuda安装是否正确
python -c 'import torch; from torch.utils.cpp_extension import CUDA_HOME; print(torch.cuda.is_available(), CUDA_HOME)'
输出:true /usr/local/cuda
,我用的是 cuda10.1
如不是上述输出结果,查看.bashrc环境变量设置是否正确,如下设置:
# vim ~/.bashrc
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:/usr/local/cuda/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/lib
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/cuda/lib64
source .bashrc
使设置的环境变量生效
1.3 detectron2 安装:
git clone https://github.com/facebookresearch/detectron2.git
cd detectron2
python setup.py build develop
2.Detectron2测试
2.1 目标检测
模型提前下载好,存在目录下
python demo/demo.py \--config-file /home/**/project/detectron2/configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml \--input ceshi/test.jpg --output outputs/ \--opts MODEL.WEIGHTS '/home/**/project/detectron2/pre_train_model/COCO-Detection/faster_rcnn_R_50_FPN_1x/137257794/model_final_b275ba.pkl'
在CPU环境下配置好环境也可以进行测试,可参考Detectron2在CPU上执行出现“ Torch not compiled with CUDA enabled”的错误
2.2 实例分割
eg1:
python demo/demo.py \--config-file /home/**/project/detectron2/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \--input ceshi/test.jpg --output outputs/ \--opts MODEL.WEIGHTS '/home/**/project/detectron2/pre_train_model/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl'
eg2:
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_imshowimport 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()
2.3 关键点检测
eg1:
python demo/demo.py \--config-file /home/**/project/detectron2/configs/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml \--input ceshi/test.jpg ceshi/test1.jpg ceshi/test2.jpg --output outputs/ \--opts MODEL.WEIGHTS '/home/**/project/detectron2/pre_train_model/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x/137849621/model_final_a6e10b.pkl'
eg2:
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_imshowimport 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 MetadataCatalogim = cv.imread("/home/**/project/detectron2/ceshi/input.jpg")cfg = get_cfg()
cfg.merge_from_file("/home/**/project/detectron2/configs/COCO-Keypoints/keypoint_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/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x/137849621/model_final_a6e10b.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()[:, :, ::-1])
plt.imshow(v.get_image())
plt.show()
2.4 全景分割
eg1:
python demo/demo.py \--config-file /home/**/project/detectron2/configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml \--input ceshi/test.jpg ceshi/test1.jpg ceshi/test2.jpg --output outputs/ \--opts MODEL.WEIGHTS '/home/**/project/detectron2/pre_train_model/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x/139514519/model_final_cafdb1.pkl'
eg2:
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_imshowimport 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 MetadataCatalogim = cv.imread("/home/**/project/detectron2/ceshi/input.jpg")cfg = get_cfg()
cfg.merge_from_file("/home/**/project/detectron2/configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml")
cfg.MODEL.WEIGHTS = "/home/**/project/detectron2/pre_train_model/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x/139514519/model_final_cafdb1.pkl"
predictor = DefaultPredictor(cfg)
panoptic_seg, segments_info = predictor(im)["panoptic_seg"]
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_panoptic_seg_predictions(panoptic_seg.to("cpu"), segments_info)
plt.imshow(v.get_image())
plt.show()
3.感谢
-
https://github.com/facebookresearch/detectron2/blob/master/INSTALL.md
-
https://www.aiuai.cn/aifarm1288.html#1.detectron2%E5%AE%89%E8%A3%85
如有错误,请指教。
这篇关于Detectron2安装测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!