金子邦彦研究室人工知能Windows でのインストールと動作確認(人工知能関係)MMDetection のインストールと動作確認(物体検出,インスタンス・セグメンテーション)(PyTorch,Python を使用)(Windows 上)

MMDetection のインストールと動作確認(物体検出,インスタンス・セグメンテーション)(PyTorch,Python を使用)(Windows 上)

MMDetection は, OpenMMLab の構成物で,物体検出インスタンス・セグメンテーション (instance segmentation), パノプティック・セグメンテーション (panoptic segmentation) の機能を提供する.

【関連情報】

前準備

Git のインストール(Windows 上)

Windows での Git のインストール: 別ページ »で説明

関連する外部ページ

Git の公式ページ: https://git-scm.com/

Python のインストール(Windows 上)

Windows での Python 3.10,関連パッケージ,Python 開発環境のインストール: 別ページ »で説明

サイト内の関連ページ

Python のまとめ: 別ページ »にまとめ

関連する外部ページ

Python の公式ページ: https://www.python.org/

Build Tools for Visual Studio 2022,NVIDIA ドライバ,NVIDIA CUDA ツールキット,NVIDIA cuDNN のインストール(Windows 上)

Windows での Build Tools for Visual Studio 2022NVIDIA ドライバNVIDIA CUDA ツールキット 11.8,NVIDIA cuDNN v8.6 のインストールと動作確認: 別ページ »で説明

関連する外部ページ

PyTorch のインストール(Windows 上)

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. PyTorch のページを確認

    PyTorch のページ: https://pytorch.org/index.html

  3. 次のようなコマンドを実行(実行するコマンドは,PyTorch のページの表示されるコマンドを使う).

    次のコマンドは, PyTorch 2.0 (NVIDIA CUDA 11.8 用) をインストールする. 事前に NVIDIA CUDA のバージョンを確認しておくこと(ここでは,NVIDIA CUDA ツールキット 11.8 が前もってインストール済みであるとする).

    python -m pip install -U pip
    python -m pip install -U torch torchvision torchaudio numpy numba --index-url https://download.pytorch.org/whl/cu118
    python -c "import torch; print(torch.__version__, torch.cuda.is_available())" 
    

    (途中省略)
    [image]

MMDetection のインストール(Windows 上)

インストールの方法は複数ある. ここでは, NVIDIA CUDA ツールキットを使うことも考え, インストールしやすい方法として,ソースコードからビルドしてインストールする方法を案内している.

MMCV のインストール(Windows 上)

MMCV のインストールを行う.

インストールの方法は複数ある. ここでは, NVIDIA CUDA ツールキットを使うことも考え, インストールしやすい方法として,ソースコードからビルドしてインストールする方法を案内している.

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. PyTorch がインストールできていることを確認するために,PyTorch のバージョンを表示
    python -c "import torch; TORCH_VERSION = '.'.join(torch.__version__.split('.')[:2]); print(TORCH_VERSION)"
    

    [image]
  3. PyTorch が NVIDIA CUDA ツールキットを認識できていることを確認するために, PyTorch が認識しているNVIDIA CUDA ツールキット のバージョンを表示

    このとき,実際には 11.8 をインストールしているのに,「cu117」のように古いバージョンが表示されることがある.このような場合は,気にせずに続行する.

    python -c "import torch; CUDA_VERSION = torch.__version__.split('+')[-1]; print(CUDA_VERSION)"
    

    [image]
  4. MMCV 1.6.2 のインストール

    MMTracking が MMCV 1.6.2 に依存している (2022/12). 将来の MMTracking の利用を想定して MMCV 1.6.2 をインストール

    https://mmcv.readthedocs.io/en/latest/get_started/installation.html に記載の手順による

    python -m pip install -U pip
    python -m pip install -U opencv-python
    python -m pip install mmcv-full==1.6.2
    

    [image]
  5. MMCV をインストールできたことの確認

    python -c "from mmcv.ops import get_compiling_cuda_version, get_compiler_version; print(get_compiling_cuda_version()); print(get_compiler_version())"
    

    [image]

MIM,MMDetection のインストール(Windows 上)

MIM, MMDetection のインストールを行う.

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. MIM, MMDetection のインストール

    https://mmdetection3d.readthedocs.io/en/latest/getting_started.html#installation による.

    python -m pip install -U git+https://github.com/open-mmlab/mim.git
    python -m pip install -U git+https://github.com/open-mmlab/mmdetection.git
    python -c "import mmdet; print(mmdet.__version__)"
    
  3. MMDetection のファイルをダウンロードし,使える状態にする

    cd %HOMEPATH%
    rmdir /s /q mmdetection
    git clone https://github.com/open-mmlab/mmdetection.git
    cd mmdetection
    python setup.py develop
    
  4. 関連ソフトウェアのインストール

    パノプティック・セグメンテーションと 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 を使用)

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. 事前学習済みモデルのダウンロード

    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 %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
    

    [image]
  3. 物体検出を行う Python プログラム

    次の Python プログラムを実行する.mmcv を用いた表示を行うので,Jupyter QtConsoleJupyter ノートブック (Jupyter Notebook) の利用が便利である.

    Python プログラムは,公式ページhttps://mmdetection.readthedocs.io/en/latest/get_started.html のものを書き換えて使用.

    下図では,Python プログラムの実行のため,jupyter qtconsole を使用している.

    import os
    import torch
    from mmdet.apis import init_detector, inference_detector
    import mmcv
    
    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)
    img = 'demo/demo.jpg'  # or img = mmcv.imread(img), which will only load it once
    result = inference_detector(model, img)
    model.show_result(img, result, show=True)
    model.show_result(img, result, out_file='result.jpg')
    

    [image]
  4. 結果が表示される

    エラーメッセージが出ないことを確認.

    [image]

動画ファイルのセグメンテーション

  1. 動画ファイル video.mp4 を %HOMEPATH%\mmsegmentation に置く
  2. 動画ファイルのセグメンテーションを行う Python プログラム

    次の Python プログラムを実行する.mmcv を用いた表示を行うので,Jupyter QtConsoleJupyter ノートブック (Jupyter Notebook) の利用が便利である.

    import os
    import torch
    from mmdet.apis import init_detector, inference_detector
    import mmcv
    
    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')
    for frame in video:
       result = inference_detector(model, frame)
       model.show_result(frame, result, show=True, wait_time=1)
    

    [image]
  3. 結果を確認.

    [image]