[Segmentation]

 

고전 Segmentation

- SLIC (Siple Linear Iterative Clustering)
- Normalized Cut
- GrabCut

 

 

🧐 Segmentation with Deep Learning

thing: 셀 수 있는 물체 (자동차, 사람)   //   stuff: 셀 수 없는 물체 (땅, 하늘)
- semantic segmentation: 같은 class의 thing의 경우, 구분하지 않고 같은 class로 할당
- instance segmentation: thing에 대해서만 분할, 같은 class라도 고유번호를 할당해 구분한다.
- panoptic segmentation: 모든 pixel에 대해 thing과 stuff를 할당,분할을 진행한다.

 

 

 

🧐 Metric

classification은 image에 대한 하나의 class 확률벡터를 출력한다.
하지만 segmentation은 pixel단위로 class를 지정한다.
그렇기에 segmentation을 dense classification이라고도 부른다.

segmentation에 사용되는 정확률을 PA: Pixel Accuracy(화소 정확률)이라 하며 아래와 같이 측정한다.

또한 class별로 PA를 계산하고 결과를 평균한 식인 MPA를 metric으로 사용할 수도 있다.

또 다른 metric는 Object Detection에서 사용되는 IoU(Intersection over Union)를 segmentation에 맞게 고쳐 사용하는 방법이다.
i. class별 AP(Average Precision)을 계산
ii. AP를 모든 class에 대해 평균해 mAP를 계산
이때, 아래 식에서 A와 B가 객체가 아닌 영역이라는 점만 다르다.


마지막으로 Dice coefficient를 metric으로 이용할 수도 있다.

 cf. instance segmentation with Object Detection

instance segmentation은 object detection과 연관성이 많고

semantic segmentation은 object classification과 연관성이 많다.

- semantic segmentation은 개별 객체의 경계를 구분하는 것보다는 image내의 pixel수준에서 class에 대한 classification에 중점을 두기 때문이다. 즉, 개별 객체의 경계식별보다는 이미지 전체에 대한 class분류에 중점을 둔다.

 

따라서 본 글에서는 semantic segmentation에 집중하며 이후 Object Detection 이후 instance segmentation을 소개할 것이다.

본 글에서는 간단하게 instance segmentation을 사용만 해볼 것이다.

 

 

 

 

 

[Semantic Segmentation]

Semantic Segmentation => 이미지 내 피사체에서 의미를 갖고 분할을 진행
- 상위버전=> Instance Segmentation: 같은 클래스 피사체여도 다른 물체를 구분. (정보 多)


[방법]
pixel by pixel 
- 클래스 개수만큼 pixel당 channel을 생성

polygon(다면체)
- 물체의 특정부분의 점들을 잇는 방법
- 좌표를 이용해 라벨에 대해 적은 비용이 들어감
- 점의 개수가 많아질수록 정확도는 올라감 ,, key point detection 기반


cf. [밝기 -> 좌표]
- 기존: Gaussian Distribution(밝기) -> Least Squared Approximation
- 현재: softargmax사용


Segmentation은 단지 pixel에 대한 classification을 하는 것.
→ pixel의 개수만큼 classification을 진행하는 것.




case 1. 클래스간의 연관성이 없을 때: CCE, softmax output
- ex) Human, 개, 고양이

case 2. 클래스간의 연관성이 있을 때: BCE, sigmoid output
- ex) Asian, Human -> 둘 다 1로 one hot encoding (element-wise)



🧐 Semantic Segmentation을 위한 Models
- FCN (Fully Convolutional Network)

- DeConvNet
- U-Net
- DeepLabv3+

 

 

 

 

 

 

 

 

 

 😶 실습 _ by tensorflow

import os
import math
import wandb
import random
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt

from tensorflow import keras
from tensorflow.keras.preprocessing.image import load_img

from tensorflow.keras.models import *
from tensorflow.keras.layers import *
from tensorflow.keras.losses import *
from tensorflow.keras.metrics import *
from tensorflow.keras.optimizers import *
from tensorflow.keras.activations import *

from tensorflow.keras.regularizers import *

from tensorflow.keras.callbacks import *
from tensorflow.keras.preprocessing import *
import tensorflow_datasets as tfds

dataset, info = tfds.load('oxford_iiit_pet:3.*.*', with_info=True)
def normalize(input_image, input_mask):
    # 배경 활성화
    input_image = tf.cast(input_image, tf.float32) / 255.0
    input_mask -= 1
    
    # 피사체라면 True로 반전.
    input_mask = 1 - (tf.cast(input_mask>=1, tf.float32) - tf.cast(input_mask>1, tf.float32))
    return input_image, input_mask
    
    
def load_image(datapoint):
    input_image = tf.image.resize(datapoint['image'], (128, 128))
    input_mask = tf.image.resize(datapoint['segmentation_mask'], (128,128))
    input_image, input_mask = normalize(input_image, input_mask)
    return input_image, input_mask
TRAIN_LENGTH = info.splits['train'].num_examples
BATCH_SIZE = 16
BUFFER_SIZE = 320
STEPS_PER_EPOCH = TRAIN_LENGTH // BATCH_SIZE


train_image = dataset['train'].map(load_image, num_parallel_calls=tf.data.AUTOTUNE)
test_image = dataset['test'].map(load_image, num_parallel_calls=tf.data.AUTOTUNE)
train_batches = (
    train_image
    .cache()
    .shuffle(BUFFER_SIZE)
    .batch(BATCH_SIZE)
    .prefetch(buffer_size=tf.data.AUTOTUNE)
)

test_batches = test_image.batch(BATCH_SIZE)

 

 

 

+ Recent posts