MAC安装detectron2介绍
目标检测算法可以说是构成图像理解和计算机视觉的重要前提,在解决分割、场景理解、目标追踪、图像描述、事件检测和活动识别等更复杂更高层次的视觉任务中起到了基石的作用。目标检测的应用非常广泛,覆盖了了人工智能和信息技术中,包括机器人视觉、消费电子产品、安保、自动驾驶、人机交互、基于内容的图像检索、智能视频监控和增强现实等。目前人工智能目标检测学习领域,岗位需求较大。Detectron2是Facebook AI Research推出的最强目标检测平台,它源自maskrcnn-benchmark,实现了最新的目标检测算法。Detectron2 基于PyTorch完全重构,是GitHub榜首最强目标检测平台之一。
Detectron2构建在Caffe2和Python之上,实现了10多篇计算机视觉最新的成果。支持包括何恺明组提出的Mask R-CNN(曾获ICCV 2017最佳论文)、Fast RCNN、Faster RCNN、RFCN、FPN、RetinaNet、ResNet,ResNeXt在内的多种模型。
环境要求(Requirements):
python >= 3.6
PyTorch 1.3,torchvision 能跟torch匹配 (其他版本没有试过,1.3和1.1版本有一些函数变化,没有实际用过)
Opencv
pycocotools
brew install build-essential python3-dev
brew install libpng-dev libjpeg-dev python3-opencv
brew install ca-certificates pkg-config
brew install git curl wget automake libtool
pip install opencv-python
pip install cloudpickle
pip install matplotlib
pip install tabulate
pip install tensorboard
pip install torch torchvision
pip install ‘git+https://github.com/facebookresearch/fvcore’
pip install cython
pip install ‘git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI’#
pip install pycocotools
torch 1.4.0 pypi_0 pypi
torchvision 0.5.0 pypi_0 pypi
detectron2 0.1.1 pypi_0 pypi
cython 0.29.15 py37h0a44026_0 defaults
opencv-python 4.2.0.34 pypi_0 pypi
tabulate 0.8.7 pypi_0 pypi
fvcore 0.1 pypi_0 pypi
Building wheels for collected packages: detectron2
Building wheel for detectron2 (setup.py) … done
Created wheel for detectron2: filename=detectron2-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl size=597594 sha256=ded0db7e38fd7bbb8e8e909519a7e58fa1b6b5397d65e7a830fb6989e2e68fbd
Stored in directory: /private/var/folders/d5/vws4r8550qd9dy5msk1jzkh40000gn/T/pip-ephem-wheel-cache-ccizcfn6/wheels/07/dc/32/0322cb484dbefab8b9366bfedbaff5060ac7d149d69c27ca5d
Successfully built detectron2
Installing collected packages: pydot, detectron2
Successfully installed detectron2-0.1.1 pydot-1.4.1
二、验证安装
import numpy as np
import cv2
from matplotlib import pyplot
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
import matplotlib.pyplot as plt
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
im = cv2.imread("./input.jpg")
plt.figure()
plt.imshow(im[:, :, ::-1])
plt.show()
cfg = get_cfg()
cfg.merge_from_file("./keypoint_rcnn_R_50_FPN_3x.yml")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7
cfg.MODEL.WEIGHTS = “detectron2://COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x/137849621/model_final_a6e10b.pkl”
predictor = DefaultPredictor(cfg)
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”))
plt.imshow(v.get_image()[:, :, ::-1])
python3 demo/demo.py --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input input1.jpg input2.jpg --output outputs/ --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl
增加下面二行:
import os
os.environ[‘KMP_DUPLICATE_LIB_OK’]=‘True’
运行结果如下: