MMDetection 是一個(gè)基于 PyTorch 的目標(biāo)檢測(cè)開(kāi)源工具箱,它是 OpenMMLab 項(xiàng)目的一部分。包含以下主要特性:
- 支持三個(gè)任務(wù)
- 目標(biāo)檢測(cè)(Object Detection)是指分類并定位圖片中物體的任務(wù)
- 實(shí)例分割(Instance Segmentation)是指分類,分割圖片物體的任務(wù)
- 全景分割(Panoptic Segmentation)是統(tǒng)一了語(yǔ)義分割(對(duì)圖像的每個(gè)像素進(jìn)行分類)和實(shí)例分割(檢測(cè)出對(duì)象實(shí)例并進(jìn)行分割)的檢測(cè)任務(wù)
- 模塊化設(shè)計(jì)以靈活支持 6 個(gè)數(shù)據(jù)集,57 種不同算法和豐富的數(shù)據(jù)增強(qiáng),提供 450+ 個(gè)預(yù)訓(xùn)練模型
- 支持?jǐn)?shù)十個(gè)算法模型的部署
安裝(目標(biāo)檢測(cè))
使用下面的命令快速生成虛擬環(huán)境:
$ python -m venv venv
# Windows 下進(jìn)入虛擬環(huán)境
$ venv/Scripts/activate
提前看下 MMDetection 和 MMCV 版本兼容性 里 PyTorch + MMDetection + MMCV 的匹配版本號(hào),比如我當(dāng)下看到的版本要求是:
- PyTorch: 1.3+
- MMDetection: 2.28.1
- MMCV: >=1.3.17, <1.8.0
MMDetection 是基于 PyTorch 的檢測(cè)框架,首先安裝 torch
庫(kù):
$ pip install torch
$ pip install opencv-python
$ pip install torchvision
MMDetection 包括 MMDetection 和 MMCV,兩者是一體的,需要安裝 MMCV,按 安裝 MMCV - 使用 mim 安裝 的說(shuō)明使用 mim(OpenMMLab項(xiàng)目的包管理工具)安裝:
$ pip install openmim
$ mim install mmcv-full==1.7.1
然后再安裝 MMDetection,(重要的事說(shuō)兩遍)提前看下 MMDetection 和 MMCV 版本兼容性 里 PyTorch + MMDetection + MMCV 的匹配版本號(hào),選擇合適的版本安裝,建議直接用 mim
快速安裝:
$ mim install mmdet==2.28.1
在 Windows 系統(tǒng)下,如果安裝過(guò)程中遇到下面的異常:
$ error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
$ [end of output]
$
$ note: This error originates from a subprocess, and is likely not a problem with pip.
$ ERROR: Failed building wheel for pycocotools
$ Failed to build pycocotools
$ ERROR: Could not build wheels for pycocotools, which is required to install pyproject.toml-based projects
可以按照 《Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools"的解決辦法》 提供的方法解決。
安裝完成后,執(zhí)行下面的 Python 代碼(demo.py)驗(yàn)證是否正確安裝了 MMDetection 和所需的環(huán)境:
import mmcv
from mmdet.apis import init_detector, inference_detector
# 一、指定模型的配置文件和 checkpoint 文件路徑
# 下載 https://github.com/open-mmlab/mmdetection 項(xiàng)目并解壓
# 把 mmdetection-master/configs/_base_ 文件夾復(fù)制到當(dāng)前項(xiàng)目 configs/ 目錄下
# 把 mmdetection-master/configs/faster_rcnn 文件夾復(fù)制到當(dāng)前項(xiàng)目 configs/ 目錄下
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
# 從 model zoo 下載 checkpoint 并放在 `checkpoints/faster_rcnn/` 文件下
# 網(wǎng)址為: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
checkpoint_file = 'checkpoints/faster_rcnn/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
# 二、根據(jù)配置文件和 checkpoint 文件構(gòu)建模型
# 有 GPU 時(shí)使用 device = 'cuda:0'
device = 'cpu'
# 初始化檢測(cè)器
model = init_detector(config_file, checkpoint_file, device=device)
# 三、測(cè)試單張圖片并展示結(jié)果
# 圖像地址: https://github.com/open-mmlab/mmdetection/blob/master/demo/demo.jpg
img = mmcv.imread('demo/demo.jpg')
# 推理演示圖像, 結(jié)果是一個(gè) `numpy.ndarray` 列表
result = inference_detector(model, img)
# 將結(jié)果可視化
model.show_result(img, result)
# 將可視化結(jié)果保存為圖片
model.show_result(img, result, out_file='demo/demo_result.jpg')
這個(gè)時(shí)候的項(xiàng)目目錄結(jié)構(gòu)如下:
如果成功安裝 MMDetection,則上面的代碼可以完整地運(yùn)行,并順利生成下面的 demo/demo_result.jpg
文件:
實(shí)例分割
實(shí)例分割(Instance Segmentation)是指分類,分割圖片物體的任務(wù)。打開(kāi) MMDetection - 模型庫(kù)/MMDetection 并選擇 Instance Segmentation
任務(wù)類型:
挑選一個(gè) AP(平均精確度)值 較高,而且年份比較新的算法,比如當(dāng)下看到的算法名是 SCNet(算法 RF-Next 需要 GPU 才能跑起來(lái)),就在前面下載好的 mmdetection-master/configs/
目錄下找到對(duì)應(yīng)的 scnet
文件夾及其依賴的 htc
文件夾,并將它們復(fù)制到當(dāng)前項(xiàng)目 configs/
目錄下。點(diǎn)擊 算法名 進(jìn)入算法詳情頁(yè)面:
同樣的,挑選一個(gè) AP(平均精確度)值 較高的模型,這里先復(fù)制 模型名稱 的文本,然后在 configs/scnet/metafile.yml
文件中搜索這個(gè)文本:
搜索完成可以獲得兩個(gè)配置及參數(shù):
- Config: 模型的配置文件路徑(
config_file = 'xxx.py'
) - Weights: 模型的下載網(wǎng)址,通過(guò)這個(gè)地址下載模型文件(
checkpoint_file = 'xxx.pth'
)
完成上面的準(zhǔn)備工作后,執(zhí)行下面的 Python 代碼(demo.py)驗(yàn)證是否可以對(duì)圖像進(jìn)行分類、分割物體目標(biāo)的任務(wù):
import mmcv
from mmdet.apis import init_detector, inference_detector
print('指定模型的配置文件和checkpoint文件路徑')
config_file = 'configs/scnet/scnet_r50_fpn_1x_coco.py'
checkpoint_file = 'checkpoints/scnet/scnet_r50_fpn_1x_coco-c3f09857.pth'
print('根據(jù)配置文件和checkpoint文件構(gòu)建模型')
device = 'cpu'
model = init_detector(config_file, checkpoint_file, device=device)
print('測(cè)試單張圖片并展示結(jié)果')
img = mmcv.imread('demo/demo.jpg')
result = inference_detector(model, img)
model.show_result(img, result)
model.show_result(img, result, out_file='demo/demo_result.jpg')
這個(gè)時(shí)候的項(xiàng)目目錄結(jié)構(gòu)如下:
如果上面代碼中 MMDetection 的實(shí)例分割任務(wù)可以完整地運(yùn)行,就會(huì)順利生成下面的 demo/demo_result.jpg
文件:
全景分割
全景分割(Panoptic Segmentation)是統(tǒng)一了語(yǔ)義分割(對(duì)圖像的每個(gè)像素進(jìn)行分類)和實(shí)例分割(檢測(cè)出對(duì)象實(shí)例并進(jìn)行分割)的檢測(cè)任務(wù)。同前面一樣,打開(kāi) MMDetection - 模型庫(kù)/MMDetection 并選擇 Panoptic Segmentation
任務(wù)類型:
這里的算法目前就只有一個(gè),只能選擇 PanopticFPN 算法,還是在前面下載好的 mmdetection-master/configs/
目錄下找到對(duì)應(yīng)的 panoptic_fpn
文件夾,并將其復(fù)制到當(dāng)前項(xiàng)目 configs/
目錄下。點(diǎn)擊 算法名 進(jìn)入算法詳情頁(yè)面:
再?gòu)闹羞x一個(gè)模型,復(fù)制 模型名稱 的文本,到 configs/panoptic_fpn/metafile.yml
文件中搜索:
搜索完成可以獲得 Config(config_file = 'xxx.py'
)和 Weights 下載后文件路徑(checkpoint_file = 'xxx.pth'
)配置參數(shù)。然后執(zhí)行下面的 Python 代碼(demo.py)驗(yàn)證是否可以對(duì)圖像進(jìn)行對(duì)每個(gè)像素進(jìn)行分類同時(shí)檢測(cè)出對(duì)象實(shí)例并進(jìn)行分割的任務(wù):
import mmcv
from mmdet.apis import init_detector, inference_detector
# 上面代碼沒(méi)有變,就下面兩個(gè)變量的值改一下
config_file = 'configs/panoptic_fpn/panoptic_fpn_r50_fpn_1x_coco.py'
checkpoint_file = 'checkpoints/panoptic_fpn/panoptic_fpn_r50_fpn_1x_coco_20210821_101153-9668fd13.pth'
# 下面代碼也沒(méi)有變
device = 'cpu'
model = init_detector(config_file, checkpoint_file, device=device)
img = mmcv.imread('demo/demo.jpg')
result = inference_detector(model, img)
model.show_result(img, result)
model.show_result(img, result, out_file='demo/demo_result.jpg')
這時(shí)項(xiàng)目的目錄結(jié)構(gòu)如下:
如果上面代碼里的全景分割任務(wù)可以順利運(yùn)行,就會(huì)生成下面的 demo/demo_result.jpg
文件:
實(shí)踐完成到這里,就會(huì)發(fā)現(xiàn),其實(shí)調(diào)用代碼是一樣的,就是改一下配置文件和模型文件,就可以實(shí)現(xiàn)不同的功能!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-465021.html
接下來(lái)還可以看 《Python調(diào)用MMDetection實(shí)現(xiàn)AI摳圖去背景》 進(jìn)一步了解實(shí)際應(yīng)用。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-465021.html
到了這里,關(guān)于使用MMDetection進(jìn)行目標(biāo)檢測(cè)、實(shí)例和全景分割的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!