计算机视觉知识点-Detectron2Beginner‘sTutorial

tech2022-07-08  185

这篇博客介绍Detectron2的使用,参考率官方文档.Detectron2是PyTorch官方用于检测和分割的一个库,这个库的优点是有很多训练好的模型.

安装

# install dependencies: !pip install pyyaml==5.1 pycocotools>=2.0.1 import torch, torchvision print(torch.__version__, torch.cuda.is_available()) !gcc --version # opencv is pre-installed on colab # install detectron2: (Colab has CUDA 10.1 + torch 1.6) # See https://detectron2.readthedocs.io/tutorials/install.html for instructions assert torch.__version__.startswith("1.6") !pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.6/index.html

下载一张图片

!wget http://images.cocodataset.org/val2017/000000439715.jpg -q -O input.jpg im = cv2.imread("./input.jpg") cv2_imshow(im)

设置config和pridictor

cfg = get_cfg() # add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")) cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model # Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml") predictor = DefaultPredictor(cfg) outputs = predictor(im)

打印结果

# look at the outputs. See https://detectron2.readthedocs.io/tutorials/models.html#model-output-format for specification print(outputs["instances"].pred_classes) print(outputs["instances"].pred_boxes)

显示结果

最后的话:

这篇文章发布在/蓝色的杯子, 没事多留言,让我们一起爱智求真吧.我的邮箱wisdomfriend@126.com.

最新回复(0)