【目次】
MMSegmentation は, OpenMMLab の構成物で,セグメンテーションの機能を提供する.
【文献】
MMSegmentation Contributors, MMSegmentation: OpenMMLab Semantic Segmentation Toolbox and Benchmark, https://github.com/open-mmlab/mmsegmentation, 2020.
【関連する外部ページ】
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.0 (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 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
https://mmdetection.readthedocs.io/en/latest/get_started.html による.
cd %HOMEPATH% rmdir /s /q mmsegmentation git clone https://github.com/open-mmlab/mmsegmentation.git cd mmsegmentation mim uninstall -y mmseg pip install -r requirements.txt mim install -e . python -c "import mmseg; print(mmseg.__version__)"
コマンドプロンプトを管理者として実行: 別ページ »で説明
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 プログラムの実行
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
Python のまとめ: 別ページ »にまとめ
セマンティック・セグメンテーションを行う.
プログラムは,公式ページ https://mmsegmentation.readthedocs.io/en/latest/get_started.html のものを書き換えて使用.
下図では,Python プログラムの実行のため,jupyter qtconsole を使用している.
import os import torch from mmseg.apis import inference_model, init_model import mmcv os.chdir((os.getenv('HOMEPATH') + '\\' + 'mmsegmentation')) config_file = 'configs/deeplabv3/deeplabv3_r50-d8_4xb2-80k_cityscapes-512x1024.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_model(config_file, checkpoint_file, device=device) img = 'demo/demo.png' # or img = mmcv.imread(img), which will only load it once from mmseg.registry import VISUALIZERS visualizer = VISUALIZERS.build(model.cfg.visualizer) visualizer.dataset_meta = model.dataset_meta result = inference_model(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 プログラムを実行する.mmcv を用いた表示を行うので,Jupyter QtConsole や Jupyter ノートブック (Jupyter Notebook) の利用が便利である.
import os import torch from mmseg.apis import inference_model, init_model, show_result_pyplot import mmcv os.chdir((os.getenv('HOMEPATH') + '\\' + 'mmsegmentation')) config_file = 'configs/deeplabv3/deeplabv3_r50-d8_4xb2-80k_cityscapes-512x1024.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_model(config_file, checkpoint_file, device=device) video = mmcv.VideoReader(os.getenv('HOMEPATH') + '\\' + 'mmdetection' + '\\' + 'demo' + '\\' + 'demo.mp4') from mmseg.registry import VISUALIZERS visualizer = VISUALIZERS.build(model.cfg.visualizer) visualizer.dataset_meta = model.dataset_meta for frame in video: result = inference_model(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)
エラーメッセージが出ないことを確認.
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) の利用が便利である.
import os import torch import cv2 from mmseg.apis import inference_model, init_model, show_result_pyplot import mmcv os.chdir((os.getenv('HOMEPATH') + '\\' + 'mmsegmentation')) config_file = 'configs/deeplabv3/deeplabv3_r50-d8_4xb2-80k_cityscapes-512x1024.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_model(config_file, checkpoint_file, device=device) video = mmcv.VideoReader(os.getenv('HOMEPATH') + '\\' + 'mmdetection' + '\\' + 'demo' + '\\' + 'demo.mp4') from mmseg.registry import VISUALIZERS visualizer = VISUALIZERS.build(model.cfg.visualizer) visualizer.dataset_meta = model.dataset_meta v = cv2.VideoCapture(0) while(v.isOpened()): r, f = v.read() if ( r == False ): break result = inference_model(model, f) image = mmcv.imconvert(f, 'bgr', 'rgb') visualizer.add_datasample('result', image, data_sample=result, show=False, draw_gt=False) mmcv.imshow(visualizer.get_image(), wait_time=100)
エラーメッセージが出ないことを確認.
Pascal VOC データセットを用いての事前学習済みモデルを使用している.
MMSegmentation の model zoo のページ: https://github.com/open-mmlab/mmsegmentation/blob/master/docs/en/model_zoo.md
ここではPascal VOC, DeepLabV3, R-50-D8 を選ぶことにする. Cityscapes データセットで学習済みのモデルである.
次のコマンドを実行する.
cd %HOMEPATH%\mmsegmentation mkdir checkpoints cd checkpoints curl -O https://download.openmmlab.com/mmsegmentation/v0.5/deeplabv3plus/deeplabv3plus_r50-d8_512x512_80k_ade20k/deeplabv3plus_r50-d8_512x512_80k_ade20k_20200614_185028-bf1400d8.pth dir /w
次の Python プログラムを実行する.mmcv を用いた表示を行うので,Jupyter QtConsole や Jupyter ノートブック (Jupyter Notebook) の利用が便利である.
import os import torch import cv2 from mmseg.apis import inference_model, init_model, show_result_pyplot import mmcv os.chdir((os.getenv('HOMEPATH') + '\\' + 'mmsegmentation')) config_file = 'configs/deeplabv3plus/deeplabv3plus_r50-d8_4xb4-80k_ade20k-512x512.py' checkpoint_file = 'checkpoints/deeplabv3plus_r50-d8_512x512_80k_ade20k_20200614_185028-bf1400d8.pth' device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = init_model(config_file, checkpoint_file, device=device) video = mmcv.VideoReader(os.getenv('HOMEPATH') + '\\' + 'mmdetection' + '\\' + 'demo' + '\\' + 'demo.mp4') from mmseg.registry import VISUALIZERS visualizer = VISUALIZERS.build(model.cfg.visualizer) visualizer.dataset_meta = model.dataset_meta v = cv2.VideoCapture(0) while(v.isOpened()): r, f = v.read() if ( r == False ): break result = inference_model(model, f) image = mmcv.imconvert(f, 'bgr', 'rgb') visualizer.add_datasample('result', image, data_sample=result, show=False, draw_gt=False) mmcv.imshow(visualizer.get_image(), wait_time=100)
エラーメッセージが出ないことを確認.