コンテンツにスキップ

labels= — クラス数と ground truth

labels= は ModelSpec の唯一の必須フィールドです。 LabelSchema(n_class=..., ground_truth=...) の形で、クラス数と ground truth の構造を定義します。

from ml_debugger.spec import LabelSchema, GroundTruthSchema, ModelSpec
from ml_debugger.spec import schema as S

spec = ModelSpec(
    labels=LabelSchema(
        n_class=3,               # 前景クラス数(背景チャネルは数えない)
        ground_truth=gt,         # GT の構造(運用フェーズのみで使う場合は省略可)
    ),
    output=...,
)

LabelSchema のフィールド

フィールド 型 内容
n_class int / NClassFrom 前景クラス数(必須)。NClassFrom("属性パス") でモデル属性からも取得できます
ground_truth GroundTruthSchema / 構造ノード ground truth コンテナの構造。省略可

GroundTruthSchema のフィールド

フィールド 型 内容
structure 構造ノード ground truth コンテナの構造(必須)。モデル出力と同じ leaf(S.Coords / S.Coords3D / S.LabelId)で記述します

ground_truth= に構造ノードを直接渡した場合は GroundTruthSchema(structure=...) として扱われるため、 通常は GroundTruthSchema を明示的に書く必要はありません。

# この 2 つは同じ意味
labels=LabelSchema(n_class=3, ground_truth=gt)
labels=LabelSchema(n_class=3, ground_truth=GroundTruthSchema(structure=gt))

ground_truth= を省略できるケース

GT が流れない運用フェーズ(Logger)だけで使う場合は ground_truth= を省略できます。 Tracer で学習データを計測する場合は記述してください。


ground truth の構造

GT の構造は、モデル出力と同じ書き方(S.Coords / S.LabelId の leaf)で記述します。 torchvision 系の「1 画像ごとに {"boxes", "labels"} を持つ dict の list」であれば次のとおりです。

gt = S.BatchAxis[                                    # 画像(サンプル)の並び
    S.Dict(
        boxes=S.DetAxis[S.Coords("xyxy", "pixel")],  # 検出(物体)の並び
        labels=S.DetAxis[S.LabelId(0)],
    )
]

spec = ModelSpec(
    labels=LabelSchema(n_class=3, ground_truth=gt),
    output=S.BatchAxis[
        S.Dict(
            boxes=S.DetAxis[S.Coords("xyxy", "pixel")],
            scores=S.DetAxis[S.Score("raw")],
            labels=S.DetAxis[S.LabelId(0)],
        )
    ],
)

GT の座標形式が出力と異なる場合

GT がピクセル xyxy 以外の形式(正規化 cxcywh など)の場合も、ground_truth= の leaf に その形式を書くだけです。GT の座標変換はこの記述を通してのみ行われます。

spec = ModelSpec(
    labels=LabelSchema(
        n_class=3,
        ground_truth=S.BatchAxis[S.Dict(
            boxes=S.DetAxis[S.Coords("cxcywh", "normalized")],   # GT は正規化 cxcywh
            labels=S.DetAxis[S.LabelId(0)],
        )],
    ),
    output=S.BatchAxis[S.Dict(
        boxes=S.DetAxis[S.Coords("xyxy", "pixel")],              # 出力はピクセル xyxy
        scores=S.DetAxis[S.Score("raw")],
        labels=S.DetAxis[S.LabelId(0)],
    )],
)

3D 検出の GT

3D は S.Coords3D(layout=...) で box の列構成を指定します(→ output=)。

labels=LabelSchema(
    n_class=10,
    ground_truth=S.BatchAxis[                        # フレームごとの GT
        S.Dict(
            gt_boxes=S.DetAxis[S.Coords3D(layout="standard_7")],
            gt_labels=S.DetAxis[S.LabelId(0)],
        )
    ],
)

分類の GT

分類の GT はクラス ID の配列をそのまま渡すため、通常は ground_truth= の構造の記述は不要です (規定値は S.BatchAxis[S.LabelId(0)])。

spec = ModelSpec(
    labels=LabelSchema(n_class=5),
    output=S.ClassConfs(activation="raw"),
)

クラス数(n_class)

n_class に書くのは前景クラス数です。背景チャネルは数えません。

モデル属性から取りたいときは NClassFrom に属性パスを渡します。

from ml_debugger.spec import NClassFrom

labels=LabelSchema(n_class=NClassFrom("fc.out_features"), ground_truth=gt)

ラベル起点と背景チャネル

ModelSpec で最も間違えやすいのが、クラス ID の起点と背景チャネルの位置の組み合わせです。

記述 何を表すか
S.LabelId(base) クラス ID が 0 始まりなら 0、1 始まりなら 1
S.ClassConfs(background=...) 背景チャネルの位置。"none" / "index0" / "last"

組み合わせは 6 通りあります(ラベルの値そのものは変換されません)。

LabelId(base=) ClassConfs(background=) 代表例
1 index0 Faster R-CNN / SSD(背景が先頭列・1 始まりラベル)
0 none 背景チャネル無し・0 始まり
0 last 背景が末尾列・0 始まり
0 index0 背景が先頭列・0 始まり
1 last 背景が末尾列・1 始まり
1 none 背景チャネル無し・1 始まり

1 始まりラベル + 背景チャネル無し

LabelId(1) + background="none" の組み合わせでは、構築時に RuntimeWarning が出ます。 仕様どおりの動作なので、意図した記述であればそのまま進めて構いません。

記述ミスは検査で見つかります

起点の取り違え・背景位置の誤りは、検査 の class-confs グループが 実データで検出します。警告が出た場合は、まずこの 2 つを見直してください。


関連ページ