物体認識モデルの評価

PyTorch で学ぶ画像向け Deep Learning

Michal Oleszak

Machine Learning Engineer

分類と位置推定

物体の位置推定

  • 出力1: 分類(例: cat)
PyTorch で学ぶ画像向け Deep Learning

分類と位置推定

物体の位置推定

  • 出力1: 分類(例: cat)
  • 出力2: バウンディングボックス回帰 [x1, y1, x2, y2]
PyTorch で学ぶ画像向け Deep Learning

Intersection over Union(IoU)

  • 注目物体: 検出したい画像内の物体(例: dog)
  • 正解ボックス: 注目物体を正確に囲むバウンディングボックス
  • Intersection over Union: 2つのボックスの重なりを測る指標

ジャッカード

  • IoU = 交差領域 / 和集合領域
    • IoU = 0 重なりなし、IoU = 1 完全一致
    • IoU >0.5 は良い予測
PyTorch で学ぶ画像向け Deep Learning

PyTorch での IoU

bbox1 = [50, 50, 150, 150]
bbox2 = [100, 100, 200, 200]


bbox1 = torch.tensor(bbox1).unsqueeze(0) bbox2 = torch.tensor(bbox2).unsqueeze(0)
from torchvision.ops import box_iou

iou = box_iou(bbox1, bbox2)
print(iou)
tensor([[0.1429]])
  • 2組のボックス(x1, y1, x2, y2)

2つのボックス集合

  • ベクトルを2次元テンソルへ変換
  • IoU を計算
PyTorch で学ぶ画像向け Deep Learning

バウンディングボックスの予測

model.eval()
with torch.no_grad():

output = model(input_image)
print(output)
[{'boxes': tensor([[ 42.8553, 271.9481, 180.6003, 346.7082],
                  [191.6016,  80.4759, 247.8009, 387.5475], ....),
'scores': tensor([1.0000, 1.0000, 0.9998, ... ]),
'labels': tensor([18,  1, 20, 18, 18, 18 ...])
}]
boxes = output[0]["boxes"]

scores = output[0]["scores"]
PyTorch で学ぶ画像向け Deep Learning

非最大抑制(NMS)

複数のボックス

PyTorch で学ぶ画像向け Deep Learning

非最大抑制(NMS)

複数のボックス

非最大抑制: 最も適切なバウンディングボックスを選ぶ一般的手法

  • 非最大: 物体を含む確信度が低いボックスを除外

  • 抑制: IoU が低いボックスを除外

PyTorch で学ぶ画像向け Deep Learning

PyTorch での非最大抑制

from torchvision.ops import nms


box_indices = nms( boxes=boxes, scores=scores, iou_threshold=0.5, ) print(box_indices)
tensor([ 0,   1,   2,   8])
filtered_boxes = boxes[box_indices]
  • boxes: 形状 [N, 4] のバウンディングボックス座標のテンソル

  • scores: 各ボックスの確信度スコア。形状 [N]

  • iou_threshold: 0.0〜1.0 のしきい値

  • 出力: フィルタ済みボックスのインデックス

PyTorch で学ぶ画像向け Deep Learning

Let's practice!

PyTorch で学ぶ画像向け Deep Learning

Preparing Video For Download...