mean Intersection over Union(mIoU)의 이해 및 구현

6825 단어 Pythonsegmentation
Segmentation의 평가 기법 중 하나인 mIoU가 계산하고 있는지 잘 몰라서 그려봤어요.

표현식 1


$$
mIoU =\frac{1}{k}\sum_{i=1}^k\frac{N_{ii}}{\sum_{i=1}^k N_{ij} +\sum_{j=1}^k N_{ji} - N_{ii}}
$$
  • $N_{ij}달러: 모델은 정확한 클래스가 $i$i인 픽셀을 클래스 j의 수량
  • 으로 분류합니다
  • $k$: 클래스 수
  • 총결산



    Acuracy의 이점

  • 학급 편차가 있어도 불합리한 평가치가 되기 어렵다
  • 예) Acuracy에서 거의 모든 픽셀이 1인 샘플은 모든 픽셀에서 1을 출력한 모델에서도 평가가 높아진다.
  • mIoU에서는 대략적인 카테고리를 평가해 평균적으로 적용하기 때문에 2의 라벨을 전혀 예측하지 못하면 대부분의 픽셀이 1이라도 평가치에 큰 영향을 미칠 수 있다.
  • 이루어지다

    import numpy as np
    from sklearn.metrics import confusion_matrix
    
    def mIoU(true: np.ndarray, pred: np.ndarray) -> float:
        """Calculate mIoU between 2 class label arrays
    
        Parameters
        ----------
        true : np.ndarray
            A 2d class label array
        pred : np.ndarray
            A 2d class label array of the same shape of true
    
        Return
        ------
        float
            mIoU
        """
        c_matrix = confusion_matrix(true.ravel(), pred.ravel())
        intersection = np.diag(c_matrix)
        union = np.sum(c_matrix, axis=0) + np.sum(c_matrix, axis=1) - intersection
        iou = intersection / union
        miou = np.mean(iou)
        return miou
    
    # Test
    true = np.zeros((5, 5))
    true[1:3, 1:3] = 1
    pred = np.zeros((5, 5))
    pred[2:4, 2:4] = 1
    assert mIoU(true, pred) == np.mean([1 / 7, (5 * 5 - 7) / (5 * 5 - 1)])
    print("true")
    print(true)
    print("pred")
    print(pred)
    print("mIoU = %.2f" % mIoU(true, pred))
    
    이해와 실시에 오류가 있다면 지적해 주십시오.

    참고 자료

  • Intersection over Union (IoU) for object detection - PyImageSearch

  • 5. 실천 편: MRI 이미지 분할 - 의료 AI 전공 과정 온라인 강의 자료 문서
  • 5. 실천 편: MRI 이미지 분할 - 의료 AI 전공 과정 온라인 강의 자료 문서 인용

    좋은 웹페이지 즐겨찾기