【目次】
MMDetection は, OpenMMLab の構成物で,物体検出, インスタンス・セグメンテーション (instance segmentation), パノプティック・セグメンテーション (panoptic segmentation) の機能を提供する.
【文献】
Chen, Kai and Wang, Jiaqi and Pang, Jiangmiao and Cao, Yuhang and Xiong, Yu and Li, Xiaoxiao and Sun, Shuyang and Feng, Wansen and Liu, Ziwei and Xu, Jiarui and Zhang, Zheng and Cheng, Dazhi and Zhu, Chenchen and Cheng, Tianheng and Zhao, Qijie and Li, Buyu and Lu, Xin and Zhu, Rui and Wu, Yue and Dai, Jifeng and Wang, Jingdong and Shi, Jianping and Ouyang, Wanli and Loy, Chen Change and Lin, Dahua, MMDetection: Open MMLab Detection Toolbox and Benchmark, arXiv:1906.07155, 2019.
【関連する外部ページ】
【関連項目】 DETR, Deformable DETR, Mask R-CNN, MMCV, MMFewShot, MMPose, MMSegmentation, MMSegmentation3D, MMSelfSup, MMTracking, OpenMMLab, RetinaNet, Seesaw Loss, SSD, YOLOv3, YOLOv4, YOLOX, インスタンス・セグメンテーション (instance segmentation), パノプティック・セグメンテーション (panoptic segmentation) 物体検出
Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.
【サイト内の関連ページ】
Windows での Git のインストール: 別ページ »で説明している.
【関連する外部ページ】
Git の公式ページ: https://git-scm.com/
【サイト内の関連ページ】
【関連する外部ページ】
Python の公式ページ: https://www.python.org/
【サイト内の関連ページ】
NVIDIA グラフィックスボードを搭載しているパソコンの場合には, NVIDIA ドライバ, NVIDIA CUDA ツールキット, NVIDIA cuDNN のインストールを行う.
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
PyTorch のページ: https://pytorch.org/index.html
次のコマンドを実行することにより, PyTorch 2.3 (NVIDIA CUDA 11.8 用)がインストールされる. 但し,Anaconda3を使いたい場合には別手順になる.
事前に NVIDIA CUDA のバージョンを確認しておくこと(ここでは,NVIDIA CUDA ツールキット 11.8 が前もってインストール済みであるとする).
PyTorch で,GPU が動作している場合には,「torch.cuda.is_available()」により,True が表示される.
python -m pip install -U --ignore-installed pip python -m pip uninstall -y torch torchvision torchaudio python -m pip install -U torch torchvision torchaudio numpy --index-url https://download.pytorch.org/whl/cu118 python -c "import torch; print(torch.__version__, torch.cuda.is_available())"
Anaconda3を使いたい場合には, Anaconda プロンプト (Anaconda Prompt) を管理者として実行し, 次のコマンドを実行する. (PyTorch と NVIDIA CUDA との連携がうまくいかない可能性があるため,Anaconda3を使わないことも検討して欲しい).
conda install -y pytorch torchvision torchaudio pytorch-cuda=11.8 cudnn -c pytorch -c nvidia py -c "import torch; print(torch.__version__, torch.cuda.is_available())"
【サイト内の関連ページ】
【関連する外部ページ】
インストールの方法は複数ある. ここでは, NVIDIA CUDA ツールキットを使うことも考え, インストールしやすい方法として,ソースコードからビルドしてインストールする方法を案内している.
コマンドプロンプトを管理者として実行: 別ページ »で説明
インストール手順は, https://mmdetection.readthedocs.io/en/latest/get_started.html による.
「mim install -e .」でエラーメッセージが出たときは, 「mim install -e .」の代わりに「pip install -e .」を実行.
python -m pip install -U --ignore-installed pip python -m pip uninstall -y openmim mmcv mmcv-full opencv-python opencv-python-headless python -m pip install -U openmim opencv-python cd %HOMEPATH% rmdir /s /q mmdetection git clone https://github.com/open-mmlab/mmdetection.git cd mmdetection mim uninstall -y mmdet pip install -r requirements.txt mim install -e . mim list
パノプティック・セグメンテーションと LVIS データセットのため
python -m pip install instaboostfast python -m pip install git+https://github.com/cocodataset/panopticapi.git python -m pip install git+https://github.com/lvis-dataset/lvis-api.git
コマンドプロンプトを管理者として実行: 別ページ »で説明
COCO データセットを用いての事前学習済みモデルを使用している.
MMDetection の model zoo のページ: https://github.com/open-mmlab/mmdetection/blob/master/docs/en/model_zoo.md
ここではCOCO, Faster-RCNN, R-50-FPN を選ぶことにする. COCO データセットで学習済みのモデルである.
次のコマンドを実行する.
cd %HOMEPATH%\mmdetection mkdir checkpoints cd checkpoints curl -O https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
Python プログラムの実行
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
Python のまとめ: 別ページ »にまとめ
物体検出を行う
Python プログラムは,公式ページhttps://mmdetection.readthedocs.io/en/latest/get_started.html のものを書き換えて使用.
import os import torch import mmcv from mmdet.apis import init_detector, inference_detector from mmdet.utils import register_all_modules register_all_modules() os.chdir((os.getenv('HOMEPATH') + '\\' + 'mmdetection')) 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 = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = init_detector(config_file, checkpoint_file, device=device, cfg_options = {'show': True}) img = 'demo/demo.jpg' # or img = mmcv.imread(img), which will only load it once # https://mmdetection.readthedocs.io/en/v3.0.0/user_guides/inference.html from mmdet.registry import VISUALIZERS visualizer = VISUALIZERS.build(model.cfg.visualizer) visualizer.dataset_meta = model.dataset_meta result = inference_detector(model, img) image = mmcv.imconvert(mmcv.imread(img), 'bgr', 'rgb') visualizer.add_datasample('result', image, data_sample=result, show=False, draw_gt=False) mmcv.imshow(visualizer.get_image())
エラーメッセージが出ないことを確認.
Python プログラムの実行
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
Python のまとめ: 別ページ »にまとめ
動画ファイルのセグメンテーションを行う.
import os import torch import mmcv from mmdet.apis import init_detector, inference_detector from mmdet.utils import register_all_modules register_all_modules() os.chdir((os.getenv('HOMEPATH') + '\\' + 'mmdetection')) 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 = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = init_detector(config_file, checkpoint_file, device=device) video = mmcv.VideoReader('demo/demo.mp4') # https://mmdetection.readthedocs.io/en/v3.0.0/user_guides/inference.html from mmdet.registry import VISUALIZERS visualizer = VISUALIZERS.build(model.cfg.visualizer) visualizer.dataset_meta = model.dataset_meta for frame in video: result = inference_detector(model, frame) image = mmcv.imconvert(frame, 'bgr', 'rgb') visualizer.add_datasample('result', image, data_sample=result, show=False, draw_gt=False) mmcv.imshow(visualizer.get_image(), wait_time=100)