物体検出の実行(MMDetection,PyTorch,Python を使用)(Windows 上)
![](../../tools/man/470.png)
【目次】
MMDetection
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.
【関連する外部ページ】
- MMDetection の GitHub のページ: https://github.com/open-mmlab/mmdetection
- MMDetection の公式ドキュメント: https://mmdetection.readthedocs.io
- MMDetection の訓練,検証,推論の公式チュートリアル: https://github.com/open-mmlab/mmdetection/blob/master/demo/MMDet_Tutorial.ipynb
- MMDetection の公式の学習済みモデル: https://github.com/open-mmlab/mmdetection/blob/master/docs/en/model_zoo.md
【関連項目】 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) 物体検出
前準備
Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022)または Visual Studio 2022 のインストール(Windows 上)
【インストールの判断】 Build Tools for Visual Studio は,開発ツールセットである. Visual Studio は統合開発環境であり,いくつかの種類があり,Build Tools for Visual Studioの機能を含むか連携して使用するものである.インストールは以下の基準で判断してください:
- Build Tools for Visual Studio の機能のみが必要な場合
- Visual Studio の機能が必要である,あるいは,よく分からない場合
Visual Studio 2022 をインストールする際に,「C++ によるデスクトップ開発」を選択することで, Build Tools for Visual Studio 2022 の機能も一緒にインストールされる.
不明な点がある場合は,Visual Studio 全体をインストール を行う方が良い.
Build Tools for Visual Studio 2022 のインストール(Windows 上)
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行
次のコマンドは,Build Tools for Visual Studio 2022と VC2015 再配布可能パッケージをインストールするものである.
- Build Tools for Visual Studio 2022 での C++ によるデスクトップ開発,CLI,ATL,MFC のインストール(Windows 上)
- Visual Studio Installer の起動
起動方法: スタートメニューの「Visual Studio Installer」を選ぶ.
- Visual Studio Build Tools 2022 で「変更」を選ぶ.
- 「C++ によるデスクトップ開発」をクリック.そして,画面右側の「インストール」の詳細で「v143 ビルドツール用 C++/CLI サポート(最新)」,「ATL」,「MFC」をチェックする.その後,「変更」をクリック.
- Visual Studio Installer の起動
Visual Studio のインストール(Windows 上)
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行
次のコマンドは,Visual Studio Community 2022と VC2015 再配布可能パッケージをインストールするものである.
- Visual Studio での C++ によるデスクトップ開発,CLI のインストール(Windows 上)
- Visual Studio Installer の起動
起動方法: スタートメニューの「Visual Studio Installer」を選ぶ.
- Visual Studio Community 2022 で「変更」を選ぶ.
- 「C++ によるデスクトップ開発」をチェック.そして,画面右側の「インストール」の詳細で「v143 ビルドツール用 C++/CLI サポート(最新)」をチェックする.その後,「インストール」をクリック.
- Visual Studio Installer の起動
Python 3.10,Git のインストール(Windows 上)
Pythonは,プログラミング言語の1つ. Gitは,分散型のバージョン管理システム.
【手順】
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行
次のコマンドは,Python ランチャーとPython 3.10とGitをインストールし,Gitにパスを通すものである.
次のコマンドでインストールされるGitは 「git for Windows」と呼ばれるものであり, Git,MinGW などから構成されている.
winget install --scope machine Python.Launcher winget install --scope machine Python.Python.3.10 winget install --scope machine Git.Git powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\Git\cmd\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
【関連する外部ページ】
- Python の公式ページ: https://www.python.org/
- Git の公式ページ: https://git-scm.com/
【サイト内の関連ページ】
【関連項目】 Python, Git バージョン管理システム, Git の利用
Build Tools for Visual Studio 2022,NVIDIA ドライバ,NVIDIA CUDA ツールキット 11.8,NVIDIA cuDNN 8.9.7 のインストール(Windows 上)
【サイト内の関連ページ】 NVIDIA グラフィックスボードを搭載しているパソコンの場合には, NVIDIA ドライバ, NVIDIA CUDA ツールキット, NVIDIA cuDNN のインストールを行う.
- Windows での Build Tools for Visual Studio 2022 のインストール: 別ページ »で説明
- Windows での NVIDIA ドライバ,NVIDIA CUDA ツールキット 11.8,NVIDIA cuDNN v8.9.7 のインストール手順: 別ページ »で説明
【関連する外部ページ】
- Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022)の公式ダウンロードページ: https://visualstudio.microsoft.com/ja/visual-cpp-build-tools/
- NVIDIA ドライバのダウンロードの公式ページ: https://www.nvidia.co.jp/Download/index.aspx?lang=jp
- NVIDIA CUDA ツールキットのアーカイブの公式ページ: https://developer.nvidia.com/cuda-toolkit-archive
- NVIDIA cuDNN のダウンロードの公式ページ: https://developer.nvidia.com/cudnn
PyTorch のインストール(Windows 上)
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
- PyTorch のページを確認
- 次のようなコマンドを実行(実行するコマンドは,PyTorch のページの表示されるコマンドを使う).
次のコマンドを実行することにより, 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 torchtext xformers 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())"
【サイト内の関連ページ】
【関連する外部ページ】
MMDetection のインストール(Windows 上)
インストールの方法は複数ある. ここでは, NVIDIA CUDA ツールキットを使うことも考え, インストールしやすい方法として,ソースコードからビルドしてインストールする方法を案内している.
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
- MIM,MMDetection のインストール
インストール手順は, https://mmdetection.readthedocs.io/en/latest/get_started.html による.
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 /d c:%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
MMDetection を用いた物体検出の実行(Windows 上)
物体検出(COCO, Faster-RCNN, R-50-FPN を使用)
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
- 事前学習済みモデルのダウンロード
COCO データセットを用いての事前学習済みモデルを使用している.
MMDetection の model zoo のページ: https://github.com/open-mmlab/mmdetection/blob/master/docs/en/model_zoo.md
ここではCOCO, Faster-RCNN, R-50-FPN を選ぶことにする. COCO データセットで学習済みのモデルである.
- configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py
- https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
次のコマンドを実行する.
cd /d c:%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 プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
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())
- 結果が表示される
エラーメッセージが出ないことを確認.
動画ファイルのセグメンテーション
- 動画ファイル video.mp4 を %HOMEPATH%\mmsegmentation に置く
- Python プログラムの実行
Python プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
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)
- 結果を確認.