MMSegmentation は, OpenMMLab の構成物で,セグメンテーションの機能を提供する.
MMSegmentation Contributors, MMSegmentation: OpenMMLab Semantic Segmentation Toolbox and Benchmark, https://github.com/open-mmlab/mmsegmentation, 2020.
Windows での Git のインストール: 別ページ »で説明
【関連する外部ページ】
Git の公式ページ: https://git-scm.com/
Windows での Python 3.10,関連パッケージ,Python 開発環境のインストール: 別ページ »で説明
【サイト内の関連ページ】
Python のまとめ: 別ページ »にまとめ
【関連する外部ページ】
Python の公式ページ: https://www.python.org/
Windows での Build Tools for Visual Studio 2022,NVIDIA ドライバ,NVIDIA CUDA ツールキット 11.8,NVIDIA cuDNN v8.6 のインストールと動作確認: 別ページ »で説明
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
PyTorch のページ: https://pytorch.org/index.html
次のコマンドは, 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())"
インストールの方法は複数ある. ここでは, NVIDIA CUDA ツールキットを使うことも考え, インストールしやすい方法として,ソースコードからビルドしてインストールする方法を案内している.
MMCV のインストールを行う.
インストールの方法は複数ある. ここでは, NVIDIA CUDA ツールキットを使うことも考え, インストールしやすい方法として,ソースコードからビルドしてインストールする方法を案内している.
コマンドプロンプトを管理者として実行: 別ページ »で説明
python -c "import torch; TORCH_VERSION = '.'.join(torch.__version__.split('.')[:2]); print(TORCH_VERSION)"
このとき,実際には 11.8 をインストールしているのに,「cu117」のように古いバージョンが表示されることがある.このような場合は,気にせずに続行する.
python -c "import torch; CUDA_VERSION = torch.__version__.split('+')[-1]; print(CUDA_VERSION)"
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
python -c "from mmcv.ops import get_compiling_cuda_version, get_compiler_version; print(get_compiling_cuda_version()); print(get_compiler_version())"
MMSegmentation, 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 -m pip install -U git+https://github.com/open-mmlab/mmsegmentation.git python -c "import mmdet; print(mmdet.__version__)" python -c "import mmseg; print(mmseg.__version__)"
cd %HOMEPATH% rmdir /s /q mmsegmentation git clone https://github.com/open-mmlab/mmsegmentation.git cd mmsegmentation python setup.py develop
コマンドプロンプトを管理者として実行: 別ページ »で説明
Cityscapes データセットを用いての事前学習済みモデルを使用している.
MMSegmentation の model zoo のページ: https://github.com/open-mmlab/mmsegmentation/blob/master/docs/en/model_zoo.md
ここではCityScapes, DeepLabV3, R-50-D8 を選ぶことにする. Cityscapes データセットで学習済みのモデルである.
次のコマンドを実行する.
cd %HOMEPATH%\mmsegmentation mkdir checkpoints cd checkpoints curl -O https://download.openmmlab.com/mmsegmentation/v0.5/deeplabv3/deeplabv3_r50-d8_512x1024_80k_cityscapes/deeplabv3_r50-d8_512x1024_80k_cityscapes_20200606_113404-b92cfdd4.pth dir /w
次の Python プログラムを実行する.mmcv を用いた表示を行うので,Jupyter QtConsole や Jupyter ノートブック (Jupyter Notebook) の利用が便利である.
プログラムは,公式ページhttps://mmsegmentation.readthedocs.io/en/latest/get_started.html# のものを書き換えて使用.
下図では,Python プログラムの実行のため,jupyter qtconsole を使用している.
import os import torch from mmseg.apis import inference_segmentor, init_segmentor import mmcv os.chdir((os.getenv('HOMEPATH') + '\\' + 'mmsegmentation')) config_file = 'configs/deeplabv3/deeplabv3_r50-d8_512x1024_80k_cityscapes.py' checkpoint_file = 'checkpoints/deeplabv3_r50-d8_512x1024_80k_cityscapes_20200606_113404-b92cfdd4.pth' device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = init_segmentor(config_file, checkpoint_file, device=device) img = 'demo/demo.png' # or img = mmcv.imread(img), which will only load it once result = inference_segmentor(model, img) model.show_result(img, result, show=True) model.show_result(img, result, out_file='result.jpg', opacity=0.5)
エラーメッセージが出ないことを確認.
次の Python プログラムを実行する.mmcv を用いた表示を行うので,Jupyter QtConsole や Jupyter ノートブック (Jupyter Notebook) の利用が便利である.
import os import torch from mmseg.apis import inference_segmentor, init_segmentor import mmcv os.chdir((os.getenv('HOMEPATH') + '\\' + 'mmsegmentation')) config_file = 'configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py' checkpoint_file = 'checkpoints/deeplabv3_r50-d8_512x1024_40k_cityscapes_20200605_022449-acadc2f8.pth' device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = init_segmentor(config_file, checkpoint_file, device=device) video = mmcv.VideoReader('video.mp4') for frame in video: result = inference_segmentor(model, frame) model.show_result(frame, result, show=True, wait_time=1)