MMSgmentationをGoogle Collaboratoryで使ってみた


はじめに

先日チャレンジしようとコンペティションでセグメンテーションの課題があって、Deeplab v3を使おうと思ったのですが、うまく動作させることができなかったです。

物体検知では、普段MMDetectionを使っているのですが、同様にMMSegmentationといういうものがあったので、使ったみようと思ったのですが日本語の解説記事が見当たらなかったので使いながら解説をしてみようかと思います。

概要

以下になります。

https://github.com/open-mmlab/mmsegmentation

2021年の「Vision Transfer」や「Swin Transfer」も利用できるようですし、かなり更新されているようです。(2021/7/16現在)

まずは、以下のチュートリアルに従って、デモの実行をしてみたいと思います。

https://github.com/open-mmlab/mmsegmentation/tree/master/demo

チュートリアルの実行

とりあえずチュートリアルに従って、実行していきます。

# Check nvcc version
!nvcc -V
# Check GCC version
!gcc --version

チュートリアルに記載のmmcvのバージョンでは動作せず、mmcv 1.3.7以上が必要と言われたので以下のように変更しています。

# Install PyTorch

!pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 -f https://download.pytorch.org/whl/torch_stable.html

# Install MMCV
!pip install mmcv-full==1.3.7

mmsegmentationのダウンロード及びインストール
(継続して使うのであれば、googleドライブ上に移動してから実行してください。)

!rm -rf mmsegmentation
!git clone https://github.com/open-mmlab/mmsegmentation.git 
%cd mmsegmentation
!pip install -e .

インストールチェック

# Check Pytorch installation
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())

# Check MMSegmentation installation
import mmseg
print(mmseg.__version__)

チェックポイントのダウンロード

!mkdir checkpoints
!wget https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmsegmentation/models/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth -P checkpoints
from mmseg.apis import inference_segmentor, init_segmentor, show_result_pyplot
from mmseg.core.evaluation import get_palette

デモファイルのセグメンテーション予測の実行

%cd /content/mmsegmentation
device = "cuda" if torch.cuda.is_available() else "cpu"

# build the model from a config file and a checkpoint file
model = init_segmentor(config_file, checkpoint_file, device=device)

# test a single image
img = 'demo/demo.png'
result = inference_segmentor(model, img)

# show the results
show_result_pyplot(model, img, result, get_palette('cityscapes'))

とりあえず、今回はチュートリアルデモの実行までとします。

次は、実際にデータを使った学習方法について、記載したいと思います。

次回の記事