项目github地址:https://github.com/facebookresearch/detectron2
要求:
Python ≥ 3.6
PyTorch ≥ 1.3
OpenCV(用于可视化)
pycocotools
Python安装:网上教程很多
PyTorch 安装:https://pytorch.org/get-started/locally/ (自己选择需要版本即可,需选择安装cuda)
如(win+pip版本):
pip install -U torch==1.4+cu100 torchvision==0.5+cu100 -f https://download.pytorch.org/whl/torch_stable.html
OpenCV安装
pip install opencv-python
pycocotools安装:
pip install cython pyyaml==5.1
pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
(第二句需要安装git,教程地址:git安装)
Detectron2 安装:
pip install detectron2
(经提醒,删除-f https://dl.fbaipublicfiles.com/detectron2/wheels/cu100/index.html ,但需要安装pytorch时连带cuda一起安装了)
demo测试
随意下载一张图片,命名为input1.jpg
找到detectron2安装文件夹,将图片复制到detectron2根目录(我的是在C:\Users\用户名\detectron2)
打开cmd,切换到此目录
运行以下代码即可
python demo/demo.py --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input input1.jpg --output ./out.jpg --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl
没梯子的话,中间下载模型速度很慢
参数解释
demo/demo.py :demo文件
–config-file :选择配置文件
–output :输出位置(如果想直接查看,删除这个参数即可)
MODEL.WEIGHTS:预先训练好的模型
在detectron2根目录下找到out.jpg