이미지에서 특정 부분을 제거하기위해 panoptic segmentation을 시도했다.
instance, segmentic? 두 개가 합쳐진 방법이라고 하는데 공부는 조금 있다가 해보겠다..
앞 부분에서는 local 설치 방법을 올렸으나 지금부터는 구글 코렙에서 실행하는 방법이다.
따로 학습을 진행하지 않고 기존의 있는 모델로 inference만 진행했다.
# Detectron 설치 방법
!pip install pyyaml==5.1
import torch
TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
CUDA_VERSION = torch.__version__.split("+")[-1]
print("torch: ", TORCH_VERSION, "; cuda: ", CUDA_VERSION)
# Install detectron2 that matches the above pytorch version
# See https://detectron2.readthedocs.io/tutorials/install.html for instructions
!pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/$CUDA_VERSION/torch$TORCH_VERSION/index.html
# 필요한 라이브러리
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
# import some common libraries
import numpy as np
import os, json, cv2, random
from google.colab.patches import cv2_imshow
# import some common detectron2 utilities
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
개인 데이터를 준비해주면 되겠습니다.
cfg.MODEL.WEIGHTS 부분에서 튜토리얼과 다르게 작성했다.
# 튜토리얼
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
# 수정해서 작성
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml"))
cfg.MODEL.WEIGHTS = "detectron2://COCO-PanopticSegmentation/panoptic_fpn_R_101_3x/139514519/model_final_cafdb1.pkl"
최종적으로 확인할 수 있다.
predictor = DefaultPredictor(cfg)
panoptic_seg, segments_info = predictor(image)["panoptic_seg"]
v = Visualizer(image[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
out = v.draw_panoptic_seg_predictions(panoptic_seg.to("cpu"), segments_info)
cv2_imshow(out.get_image()[:, :, ::-1])