項目github地址:https://github.com/facebookresearch/detectron2
要求:
Linux or macOS with 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