LoginSignup

エンジニアとしての市場価値を測りませんか?PR

企業からあなたに合ったオリジナルのスカウトを受け取って、市場価値を測りましょう

2
1

IoUとは

Intersection over Union; IoU

2つの領域がどれくらい重なっているかをあらわす指標。
物体検出やセグメンテーションのタスクで、正解の領域と予測の領域がどれくらい重なっているかをあらわす精度指標として使える。

A:正解領域
B:予測領域

IoU=ABAB

2となる

バウンディングボックス

image.png

セグメンテーション

image.png

物体検出のバウンディングボックス

物体検出のバウンディングについてみてみる

算出

各矩形領域について

  • xmin: 領域左上のx座標
  • ymin: 領域左上のy座標
  • xmax: 領域右下のx座標
  • ymax: 領域右下のy座標
  • w: 領域の幅
  • h: 領域の高さ

image.png

dx=min(xAmax,xBmax)max(xAmin,xBmin)
dy=min(yAmax,yBmax)max(yAmin,yBmin)

IoU=ABAB=dxdywAhA+wBhBdxdy

ただしdx0またはdy0の場合、ABは0となる。

image.png

IoUの値はどんな感じになるか

  • 完全一致すると1.0。
  • 全く重ならないと0.0。
  • 大きめに予測しても、小さめに予測しても、分母となるABが大きくなるので、IoUの値は小さくなる。
  • 少しずれても大きくIoUの値が下がる。

完全に一致

image.png

AB10,000
AB10,000
IoU1.0

ほぼ一致

image.png

AB9,604
AB10,397
IoU0.924

一部一致

image.png

この例だとAB:AB=9:23となっている

AB5,625
AB14,375
IoU0.391

image.png

この例だとAB:AB=1:7となっている

AB2,500
AB17,500
IoU0.143

全く重なっていない

image.png

AB0
AB20,000
IoU0.0

すごく大きく予測

image.png

AB10,000
AB90,000
IoU0.111

すごく小さく予測

image.png

AB10,000
AB90,000
IoU0.111

Pythonサンプル

def calc_iou(a, b):
    """
    IoUを算出する。

    Args:
        a (list[int, int, int, int]) : 矩形Aの情報 [左上のX座標, 左上のY座標, 幅, 高さ] 
        b (list[int, int, int, int]) : 矩形Bの情報 [左上のX座標, 左上のY座標, 幅, 高さ] 

    Returns:
        float : 算出したIoU
    """

    x_a_min, y_a_min = a[:2]
    x_a_max = x_a_min + a[2]
    y_a_max = y_a_min + a[3]

    x_b_min, y_b_min = b[:2]
    x_b_max = x_b_min + b[2]
    y_b_max = y_b_min + b[3]

    dx = min(x_a_max, x_b_max) - max(x_a_min, x_b_min)
    dy = min(y_a_max, y_b_max) - max(y_a_min, y_b_min)

    if dx < 0 or dy < 0:
        intersection = 0
    else:
        intersection = dx * dy

    union = (a[2] * a[3]) + (b[2] * b[3]) - intersection

    #print(f"dx: {dx}, dy: {dy}, intersection: {intersection}, union: {union}")

    return intersection / union
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
CM_Koga

@CM_Koga(N KOGA)

株式会社コンピュータマインド R&D推進室の者です
computermind
山梨・東京に拠点を置くソフトウェアベンダーです。 製造業様、メディカルベンダー様向けに深層学習ベースの画像認識、SLAMを用いたソリューションを提供しています。

Comments

No comments

Let's comment your feelings that are more than good

Being held Article posting campaign

paiza×Qiita記事投稿キャンペーン「プログラミング問題をやってみて書いたコードを投稿しよう!」

~
View details
2
1

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address