MMSegmentation - 01

이승민·2024년 4월 4일

MMCV

목록 보기
1/3

안녕하세요. Segmentation 연구를 진행하며 MMSegmentation 라이브러리에 대해 알게 되었습니다. 사용법을 익혀가며 블로그에 글을 남기겠습니다.

MMCV

  • OpenMMLab이라는 오픈소스 프로젝트 팀이 만든 딥러닝 라이브러리입니다.
  • Pytorch 기반으로 동작합니다.
  • MMSegmentation 라이브러리는 MMCV의 하위 라이브러리입니다.
  • Classification, Detection, Segmentation 등 여러 Task를 지원합니다.
  • MMSegmentation 공식 페이지

Get Started

Prerequisites

공식 페이지에서 권유하는 순서는 다음과 같습니다.

(이 부분을 참고할 수 있는 글이 있습니다.)
그래픽카드 호환되는 Pytorch & CUDA 설치하기

Step 0. MiniConda 설치

Step 1. Conda 가상환경 생성

conda create --name openmmlab python=3.8 -y
conda activate openmmlab

Step 2. PyTorch 설치

GPU 환경

conda install pytorch torchvision -c pytorch

CPU 환경

conda install pytorch torchvision cpuonly -c pytorch

Installation

Step 0. MIM을 사용하여 MMCV 설치

MMCV를 설치하기 위해 먼저 openmim을 설치합니다.

pip install -U openmim
mim install mmengine
mim install "mmcv>=2.0.0"

이 부분에서 albumentations 라이브러리와 mediapipe 라이브러리를 설치하기 위해 다른 추가적인 라이브러리 설치가 필요하다고 합니다. 이는 따로 다 설치하시면 진행됩니다.

albumentations 1.3.0 requires opencv-python-headless>=4.1.1, which is not installed.  
albumentations 1.3.0 requires scikit-image>=0.16.1, which is not installed.
albumentations 1.3.0 requires scipy, which is not installed.
mediapipe 0.10.1 requires attrs>=19.1.0, which is not installed.
mediapipe 0.10.1 requires flatbuffers>=2.0, which is not installed.
mediapipe 0.10.1 requires protobuf<4,>=3.11, which is not installed.
pip install opencv-python-headless
pip install scikit-image
pip install scipy

pip install attrs
pip install flatbuffers
pip install protobuf==3.19.0

Step 1. MMSegmentation 설치

저는 Case a를 따라 설치하겠습니다.

Case a: MMSegmentation을 직접 개발하고 실행하기 위해, GitHub에서 소스 코드를 가져오고 설치합니다.

git clone -b main https://github.com/open-mmlab/mmsegmentation.git # GitHub에서 소스 코드를 클론합니다.
cd mmsegmentation # 설치한 mmsegmentation 디렉토리로 이동합니다.
pip install -v -e . # mmsegmentation을 설치합니다.
# '-v' means verbose, or more output
# '-e' means installing a project in editable mode,
# thus any local modifications made to the code will take effect without reinstallation.

Case b: 이 경우 MMSegmentation이 이미 PyPI (Python Package Index)에 등록되어 있으므로, pip를 사용하여 설치합니다.

pip install "mmsegmentation>=1.0.0"

Verify the installation

demo 파일을 실행하여 설치를 확인해보겠습니다.

Step 0. config 파일, checkpoint 파일 다운로드

아래의 코드로 다운로드 하시면 pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py 파일과 pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth이 생깁니다. 이것은 각각 config 파일, checkpoint 파일이라 부릅니다.

지금 config 파일을 기억해주세요. MMSeg는 config 파일이 중요합니다.

mim download mmsegmentation --config pspnet_r50-d8_4xb2-40k_cityscapes-512x1024 --dest .

Step 1. demo 파일 Inference

MMSegmentation을 어떻게 설치했는지에 따라 두 가지 옵션이 있습니다.

Option (a) : source를 통해 mmsegmentation을 설치한 경우

코드를 보면
1. demo/image_demo.py 파일을 실행
2. img 파일 : demo/demo.png
3. config 파일 : configs/pspnet/pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py
4. chechpoint 파일 : pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth
5. device : cuda:0 (default=cuda:0)
6. out-file : result.jpg (default=None)

그런데 저희 config 파일은 configs/pspnet 경로에 있지 않기 때문에 경로를 수정해야 합니다. 공식 페이지에서 이런 코드를 제공한 이유는 configs/pspnet 경로에도 비슷한 config 파일이 있기 때문입니다.

아래의 수정한 코드를 따라하시면 demo inference 결과를 mmsegmentation 폴더에 저장하실 수 있습니다. ('--out-file result.jpg' 삭제하시면 결과를 바로 확인할 수 있습니다.)

공식 페이지에서 제공한 코드
python demo/image_demo.py demo/demo.png configs/pspnet/pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth --device cuda:0 --out-file result.jpg
수정한 코드
python demo/image_demo.py demo/demo.png pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth --out-file result.jpg

Option (b) : pip를 이용해 mmsegmentation을 설치한 경우

이 경우 아래의 코드를 복사하여 파일을 만드시면 실행하실 수 있습니다.

from mmseg.apis import inference_model, init_model, show_result_pyplot
import mmcv

config_file = 'pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py'
checkpoint_file = '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 and show the results
img = 'demo/demo.png'  # or img = mmcv.imread(img), which will only load it once
result = inference_model(model, img)
# visualize the results in a new window
show_result_pyplot(model, img, result, show=True)
# or save the visualization results to image files
# you can change the opacity of the painted segmentation map in (0, 1].
show_result_pyplot(model, img, result, show=True, out_file='result.jpg', opacity=0.5)
# test a video and show the results
video = mmcv.VideoReader('video.mp4')
for frame in video:
   result = inference_model(model, frame)
   show_result_pyplot(model, frame, result, wait_time=1)

다음 글은 이에 이어서 MMSegmentation - 02 작성하겠습니다.

0개의 댓글