MMDetection学习教程(一)
官方文檔:MMDetection–》點擊進入
**
第一步 安裝相關環境
**
1、MMDetection提供了GPU與CPU兩個版本,但是cpu版本可用的算法會少很多。
2、參考我的前一篇博文安裝好cuda等等環境
3、MMDetection所支持的最低依賴
4、開始安裝MMDetection
MIM 能夠自動地安裝 OpenMMLab 的項目以及對應的依賴包。
或者,可以手動安裝 MMDetection:
安裝 mmcv-full,我們建議使用預構建包來安裝:
需要把命令行中的 {cu_version} 和 {torch_version} 替換成對應的版本。例如:在 CUDA 11 和 PyTorch 1.7.0 的環境下,可以使用下面命令安裝最新版本的 MMCV:
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html請參考 MMCV 獲取不同版本的 MMCV 所兼容的的不同的 PyTorch 和 CUDA 版本。同時,也可以通過以下命令行從源碼編譯 MMCV:
git clone https://github.com/open-mmlab/mmcv.git cd mmcv MMCV_WITH_OPS=1 pip install -e . # 安裝好 mmcv-full cd ..或者,可以直接使用命令行安裝:
pip install mmcv-full4.安裝 MMDetection:
你可以直接通過如下命令從 pip 安裝使用 mmdetection:
pip install mmdet或者從 git 倉庫編譯源碼
git clone https://github.com/open-mmlab/mmdetection.git cd mmdetection pip install -r requirements/build.txt pip install -v -e . # or "python setup.py develop"5.安裝額外的依賴以使用 Instaboost, 全景分割, 或者 LVIS 數據集
# 安裝 instaboost 依賴 pip install instaboostfast # 安裝全景分割依賴 pip install git+https://github.com/cocodataset/panopticapi.git # 安裝 LVIS 數據集依賴 pip install git+https://github.com/lvis-dataset/lvis-api.git # 安裝 albumentations 依賴 pip install albumentations>=0.3.2 --no-binary imgaug,albumentations驗證
為了驗證是否正確安裝了 MMDetection 和所需的環境,我們可以運行示例的 Python 代碼來初始化檢測器并推理一個演示圖像:
from mmdet.apis import init_detector, inference_detectorconfig_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py' # 從 model zoo 下載 checkpoint 并放在 `checkpoints/` 文件下 # 網址為: 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_r50_fpn_1x_coco_20200130-047c8118.pth' device = 'cuda:0' # 初始化檢測器 model = init_detector(config_file, checkpoint_file, device=device) # 推理演示圖像 inference_detector(model, 'demo/demo.jpg')如果成功安裝 MMDetection,則上面的代碼可以完整地運行。
具體的內容可以點擊傳送門MODEL_ZOO庫
第二步 正式進入學習
1、在已有的數據集上進行推理
MMDetection 在 Model Zoo 中提供了數以百計的檢測模型,并支持多種標準數據集,包括 Pascal VOC,COCO,Cityscapes,LVIS 等。這份文檔將會講述如何使用這些模型和標準數據集來運行一些常見的任務,包括:
- 使用現有模型在給定圖片上進行推理
- 在標準數據集上測試現有模型
- 在標準數據集上訓練預定義的模
2、使用現有模型進行推理
推理是指使用訓練好的模型來檢測圖像上的目標。在 MMDetection 中,一個模型被定義為一個配置文件和對應的存儲在 checkpoint 文件內的模型參數的集合。
首先,我們建議從 Faster RCNN 開始,其 配置 文件和 checkpoint 文件在此。 我們建議將 checkpoint 文件下載到 checkpoints 文件夾內。
3、推理的高層編程接口
MMDetection 為在圖片上推理提供了 Python 的高層編程接口。下面是建立模型和在圖像或視頻上進行推理的例子。
from mmdet.apis import init_detector, inference_detector import mmcv# 指定模型的配置文件和 checkpoint 文件路徑 config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py' checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'# 根據配置文件和 checkpoint 文件構建模型 model = init_detector(config_file, checkpoint_file, device='cuda:0')# 測試單張圖片并展示結果 img = 'test.jpg' # 或者 img = mmcv.imread(img),這樣圖片僅會被讀一次 result = inference_detector(model, img) # 在一個新的窗口中將結果可視化 model.show_result(img, result) # 或者將可視化結果保存為圖片 model.show_result(img, result, out_file='result.jpg')# 測試視頻并展示結果 video = mmcv.VideoReader('video.mp4') for frame in video:result = inference_detector(model, frame)model.show_result(frame, result, wait_time=1)異步接口-支持 Python 3.7+
對于 Python 3.7+,MMDetection 也有異步接口。利用 CUDA 流,綁定 GPU 的推理代碼不會阻塞 CPU,從而使得 CPU/GPU 在單線程應用中能達到更高的利用率。在推理流程中,不同數據樣本的推理和不同模型的推理都能并發地運行。
您可以參考 tests/async_benchmark.py 來對比同步接口和異步接口的運行速度。
import asyncio import torch from mmdet.apis import init_detector, async_inference_detector from mmdet.utils.contextmanagers import concurrentasync def main():config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'device = 'cuda:0'model = init_detector(config_file, checkpoint=checkpoint_file, device=device)# 此隊列用于并行推理多張圖像streamqueue = asyncio.Queue()# 隊列大小定義了并行的數量streamqueue_size = 3for _ in range(streamqueue_size):streamqueue.put_nowait(torch.cuda.Stream(device=device))# 測試單張圖片并展示結果img = 'test.jpg' # or 或者 img = mmcv.imread(img),這樣圖片僅會被讀一次async with concurrent(streamqueue):result = await async_inference_detector(model, img)# 在一個新的窗口中將結果可視化model.show_result(img, result)# 或者將可視化結果保存為圖片model.show_result(img, result, out_file='result.jpg')asyncio.run(main())演示樣例
我們還提供了三個演示腳本,它們是使用高層編程接口實現的。
圖片樣例
這是在單張圖片上進行推理的腳本,可以開啟 --async-test 來進行異步推理。
運行樣例:
python demo/image_demo.py demo/demo.jpg \configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \--device cpu攝像頭樣例
這是使用攝像頭實時圖片的推理腳本。
python demo/webcam_demo.py \${CONFIG_FILE} \${CHECKPOINT_FILE} \[--device ${GPU_ID}] \[--camera-id ${CAMERA-ID}] \[--score-thr ${SCORE_THR}]運行樣例:
python demo/webcam_demo.py \configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth視頻樣例
這是在視頻樣例上進行推理的腳本。
python demo/video_demo.py \${VIDEO_FILE} \${CONFIG_FILE} \${CHECKPOINT_FILE} \[--device ${GPU_ID}] \[--score-thr ${SCORE_THR}] \[--out ${OUT_FILE}] \[--show] \[--wait-time ${WAIT_TIME}]運行樣例:
python demo/video_demo.py demo/demo.mp4 \configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \--out result.mp4在標準數據集上測試現有模型
為了測試一個模型的精度,我們通常會在標準數據集上對其進行測試。MMDetection 支持多個公共數據集,包括 COCO , Pascal VOC ,Cityscapes 等等。 這一部分將會介紹如何在支持的數據集上測試現有模型。
數據集準備
一些公共數據集,比如 Pascal VOC 及其鏡像數據集,或者 COCO 等數據集都可以從官方網站或者鏡像網站獲取。 注意:在檢測任務中,Pascal VOC 2012 是 Pascal VOC 2007 的無交集擴展,我們通常將兩者一起使用。 我們建議將數據集下載,然后解壓到項目外部的某個文件夾內,然后通過符號鏈接的方式,將數據集根目錄鏈接到 $MMDETECTION/data 文件夾下,格式如下所示。 如果你的文件夾結構和下方不同的話,你需要在配置文件中改變對應的路徑。
mmdetection ├── mmdet ├── tools ├── configs ├── data │ ├── coco │ │ ├── annotations │ │ ├── train2017 │ │ ├── val2017 │ │ ├── test2017 │ ├── cityscapes │ │ ├── annotations │ │ ├── leftImg8bit │ │ │ ├── train │ │ │ ├── val │ │ ├── gtFine │ │ │ ├── train │ │ │ ├── val │ ├── VOCdevkit │ │ ├── VOC2007 │ │ ├── VOC2012有些模型需要額外的 COCO-stuff 數據集,比如 HTC,DetectoRS 和 SCNet,你可以下載并解壓它們到 coco 文件夾下。文件夾會是如下結構:
mmdetection ├── data │ ├── coco │ │ ├── annotations │ │ ├── train2017 │ │ ├── val2017 │ │ ├── test2017 │ │ ├── stuffthingmapsPanopticFPN 等全景分割模型需要額外的 COCO Panoptic 數據集,你可以下載并解壓它們到 coco/annotations 文件夾下。文件夾會是如下結構:
mmdetection
├── data │ ├── coco │ │ ├── annotations │ │ │ ├── panoptic_train2017.json │ │ │ ├── panoptic_train2017 │ │ │ ├── panoptic_val2017.json │ │ │ ├── panoptic_val2017 │ │ ├── train2017 │ │ ├── val2017 │ │ ├── test2017Cityscape 數據集的標注格式需要轉換,以與 COCO 數據集標注格式保持一致,使用 tools/dataset_converters/cityscapes.py 來完成轉換:
pip install cityscapesscriptspython tools/dataset_converters/cityscapes.py \./data/cityscapes \--nproc 8 \--out-dir ./data/cityscapes/annotations測試現有模型
我們提供了測試腳本,能夠測試一個現有模型在所有數據集(COCO,Pascal VOC,Cityscapes 等)上的性能。我們支持在如下環境下測試:
-
單 GPU 測試
-
單節點多 GPU 測試
-
多節點測試
根據以上測試環境,選擇合適的腳本來執行測試過程。
# 單 GPU 測試 python tools/test.py \${CONFIG_FILE} \${CHECKPOINT_FILE} \[--out ${RESULT_FILE}] \[--eval ${EVAL_METRICS}] \[--show]# 單節點多 GPU 測試 bash tools/dist_test.sh \${CONFIG_FILE} \${CHECKPOINT_FILE} \${GPU_NUM} \[--out ${RESULT_FILE}] \[--eval ${EVAL_METRICS}]可選參數:
RESULT_FILE: 結果文件名稱,需以 .pkl 形式存儲。如果沒有聲明,則不將結果存儲到文件。
EVAL_METRICS: 需要測試的度量指標。可選值是取決于數據集的,比如 proposal_fast,proposal,bbox,segm 是 COCO 數據集的可選值,mAP,recall 是 Pascal VOC 數據集的可選值。Cityscapes 數據集可以測試 cityscapes 和所有 COCO 數據集支持的度量指標。
show: 如果開啟,檢測結果將被繪制在圖像上,以一個新窗口的形式展示。它只適用于單 GPU 的測試,是用于調試和可視化的。請確保使用此功能時,你的 GUI 可以在環境中打開。否則,你可能會遇到這么一個錯誤 cannot connect to X server。
--show-dir: 如果指明,檢測結果將會被繪制在圖像上并保存到指定目錄。它只適用于單 GPU 的測試,是用于調試和可視化的。即使你的環境中沒有 GUI,這個選項也可使用。
--show-score-thr: 如果指明,得分低于此閾值的檢測結果將會被移除。
--cfg-options: 如果指明,這里的鍵值對將會被合并到配置文件中。
--eval-options: 如果指明,這里的鍵值對將會作為字典參數被傳入 dataset.evaluation() 函數中,僅在測試階段使用。
總結
以上是生活随笔為你收集整理的MMDetection学习教程(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PTA】【Python】【拼题A 20
- 下一篇: maven 实战 (许晓斌)