Semantic Segmentationに挑戦



mmsegmentationを使ってみようと思いましたが。open-mmlabの記述は、Pytorchのバージョンとか、cudaのバージョンとか、ちょっと古いので、四苦八苦しました。ということで、きれいにインストールできる(た)結果を記録しました。

## Cudaのインストール
できれば、pytorchのバージョンに合わせたい (少なくとも、メジャーバージョンは合わせたい)

我が環境
```
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Wed_Aug_14_10:10:22_PDT_2024
Cuda compilation tools, release 12.6, V12.6.68
Build cuda_12.6.r12.6/compiler.34714021_0
```


## Torchのインストール
https://pytorch.org/get-started/locally/
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/213097/491c16d5-6d05-de9d-dda2-15c58e84ac7a.png)

```
$ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
$ python -c 'import torch; print(torch.__version__)'
2.4.1+cu124
```


## mmcvのインストール
```
$ git clone https://github.com/open-mmlab/mmcv.git
$ cd mmcv
$ pip install -r requirements/optional.txt
$ pip install -e . -v
$ python3 .dev_scripts/check_installation.py
/home/ubuntu/venv/mmseg/lib/python3.10/site-packages/mmengine/optim/optimizer/zero_optimizer.py:11: DeprecationWarning: `TorchScript` support for functional optimizers is deprecated and will be removed in a future PyTorch release. Consider using the `torch.compile` optimizer instead.
  from torch.distributed.optim import \
Start checking the installation of mmcv ...
CPU ops were compiled successfully.
CUDA ops were compiled successfully.
mmcv has been installed successfully.

Environment information:
------------------------------------------------------------
sys.platform: linux
Python: 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0]
CUDA available: True
MUSA available: False
numpy_random_seed: 2147483648
GPU 0: NVIDIA GeForce RTX 3050
CUDA_HOME: /usr/local/cuda
NVCC: Cuda compilation tools, release 12.6, V12.6.68
GCC: x86_64-linux-gnu-gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
PyTorch: 2.4.1+cu124
PyTorch compiling details: PyTorch built with:
  - GCC 9.3
  - C++ Version: 201703
  - Intel(R) oneAPI Math Kernel Library Version 2022.2-Product Build 20220804 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v3.4.2 (Git Hash 1137e04ec0b5251ca2b4400a4fd3c667ce843d67)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - LAPACK is enabled (usually provided by MKL)
  - NNPACK is enabled
  - CPU capability usage: AVX2
  - CUDA Runtime 12.4
  - NVCC architecture flags: -gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86;-gencode;arch=compute_90,code=sm_90
  - CuDNN 90.1
  - Magma 2.6.1
  - Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=12.4, CUDNN_VERSION=9.1.0, CXX_COMPILER=/opt/rh/devtoolset-9/root/usr/bin/c++, CXX_FLAGS= -D_GLIBCXX_USE_CXX11_ABI=0 -fabi-version=11 -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -DNDEBUG -DUSE_KINETO -DLIBKINETO_NOROCTRACER -DUSE_FBGEMM -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -O2 -fPIC -Wall -Wextra -Werror=return-type -Werror=non-virtual-dtor -Werror=bool-operation -Wnarrowing -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-unused-parameter -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-stringop-overflow -Wsuggest-override -Wno-psabi -Wno-error=pedantic -Wno-error=old-style-cast -Wno-missing-braces -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_VERSION=2.4.1, USE_CUDA=ON, USE_CUDNN=ON, USE_CUSPARSELT=1, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_GLOO=ON, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=1, USE_NNPACK=ON, USE_OPENMP=ON, USE_ROCM=OFF, USE_ROCM_KERNEL_ASSERT=OFF, 

TorchVision: 0.19.1+cu124
OpenCV: 4.10.0
MMEngine: 0.10.5
MMCV: 2.2.0
MMCV Compiler: GCC 11.4
MMCV CUDA Compiler: 12.6
------------------------------------------------------------
```


## mmsegmentationのインストールと動作確認

### インストール
```
$ git clone https://github.com/open-mmlab/mmsegmentation.git
$ cd mmsegmentation
$ pip3 install -e . -v
$ pip list | grep mm
mmcv                     2.2.0        /home/ubuntu/segmentation/mmcv
mmengine                 0.10.5
mmsegmentation           1.2.2        /home/ubuntu/segmentation/mmsegmentation
```


### 動作確認

demo/inference_demo.ipynbでトライ。
pythonコードでは、こんな感じ
```
#!/usr/bin/env python
# coding: utf-8

get_ipython().system('mkdir ../checkpoints')
get_ipython().system('wget https://download.openmmlab.com/mmsegmentation/v0.5/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth -P ../checkpoints')


import torch
import matplotlib.pyplot as plt
from mmengine.model.utils import revert_sync_batchnorm
from mmseg.apis import init_model, inference_model, show_result_pyplot


config_file = '../configs/pspnet/pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py'
checkpoint_file = '../checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth'


# build the model from a config file and a checkpoint file
model = init_model(config_file, checkpoint_file, device='cuda:0')


# test a single image
img = 'demo.png'
if not torch.cuda.is_available():
    model = revert_sync_batchnorm(model)
result = inference_model(model, img)


# show the results
vis_result = show_result_pyplot(model, img, result, show=True)
plt.imshow(vis_result)
```

jupyter nbconvertで吐き出したので、ipythonで実行しないとエラーが出ます。

```
$ ipython ./inference_demo.py
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/213097/c90fe3d2-6f6e-01df-9517-08247a569783.png)

とりあえず、動いたです。よかった。

#### 怒られたエラー

- mmcvのバージョン

mmsegmentation/mmseg/__init__.py
```
diff --git a/mmseg/__init__.py b/mmseg/__init__.py
index 5fcb84e8..77326df7 100644
--- a/mmseg/__init__.py
+++ b/mmseg/__init__.py
@@ -8,7 +8,7 @@ from packaging.version import parse
 from .version import __version__, version_info
 
 MMCV_MIN = '2.0.0rc4'
-MMCV_MAX = '2.2.0'
+MMCV_MAX = '2.3.0'
 MMENGINE_MIN = '0.5.0'
 MMENGINE_MAX = '1.0.0'
```


- No module named XYZ

  - ftfy
  - regex

```
$ pip3 install ftfy
Collecting ftfy
  Using cached ftfy-6.2.3-py3-none-any.whl (43 kB)
Requirement already satisfied: wcwidth<0.3.0,>=0.2.12 in /home/masa/venv/mmseg/lib/python3.10/site-packages (from ftfy) (0.2.13)
Installing collected packages: ftfy
Successfully installed ftfy-6.2.3
$ pip3 install regex
Collecting regex
  Downloading regex-2024.9.11-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (782 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 782.7/782.7 KB 8.9 MB/s eta 0:00:00
Installing collected packages: regex
Successfully installed regex-2024.9.11
```


学習させる課題がまだ未決デス