コンテンツにスキップ

チートシート

ModelSpec の記述に使うノードとフィールドの一覧です。

from ml_debugger.spec import ModelSpec, LabelSchema, InputSchema, RawOutputSchema, OutputSchema
from ml_debugger.spec import NMS, InModelNMS, TopK, Decode, PostProcess, ImageShapes
from ml_debugger.spec import Invoke, LayerHook, HeadHook, NClassFrom
from ml_debugger.spec import FromCall, NMSScoreThresh, NMSIouThresh, NMSMaxDet
from ml_debugger.spec import inspect_model, infer_spec, validate_spec
from ml_debugger.spec import schema as S      # leaf / 構造ノードは S.* でまとめて使う

leaf ノード(S.* — 中身の意味を持つ末端)

leaf 記述例 意味
S.Coords S.Coords("xyxy", "pixel") 2D 枠。→ box 形式
S.Coords3D S.Coords3D(layout="standard_7") 3D 枠。→ 列構成
S.Score S.Score("raw") 検出スコア(1 列)。"raw" / "sigmoid" / "softmax"
S.LabelId S.LabelId(0) クラス ID(1 列)。0 / 1 始まり
S.ClassConfs S.ClassConfs(3, "raw", "none") クラス別確信度ベクトル。第 1 引数がクラス数、第 2 が活性化、第 3 が背景チャネル位置("none" / "index0" / "last")
S.Objectness S.Objectness("sigmoid") 物体らしさ(1 列)。既定の in_score=True は選択スコアに掛け合わされることを表します
S.ClassConfMap S.ClassConfMap() 空間的なクラス確信度マップ。検出ごとに box 中心で読み出します
S.KeepIndex S.KeepIndex() モデルが自ら報告する採択検出のインデックス
S.Velocity S.Velocity() 速度ブロック (N, 2)
S.BackgroundConf S.BackgroundConf() 単独の背景確信度の列
S.ValidCount S.ValidCount() パディング付き固定長出力の有効行数
S.Array S.Array(("C", "H", "W"), input_tensor=True) 汎用配列。input_tensor=True はモデルが読み込んだ入力の在処
S.Ignore S.Ignore() 読み飛ばし(記録されません)

S.Coords — 2D box の形式

S.Coords(box_format="xyxy", scale="pixel", size_from="input")
引数 取り得る値
box_format "xyxy" / "cxcywh" / "xywh" / "yxyx"
scale "pixel" / "normalized"
size_from "input"(既定)/ (H, W) タプル / コンテナ内へのドット区切りパス / fn(container, i) -> (H, W)

S.Coords3D — 3D box の列構成

S.Coords3D(layout="velocity_9", pc_range=None, frame=None, to_ground=None)
layout 列数 列構成 pc_range
"standard_7"(既定) 7 [x, y, z, w, l, h, yaw](メートル) 不要
"velocity_9" 9 [x, y, z, w, l, h, yaw, vx, vy](mmdet3d 系) 不要
"velocity_9_yaw_last" 9 [x, y, z, w, l, h, vx, vy, yaw](det3d 系) 不要
"normalized_8" 8 正規化座標 必須
"normalized_10" 10 正規化座標(BEVFormer 系) 必須
"mmdet3d_logdim" 10 メートル中心 + log 寸法 不要
"camera_7_lhw" 7 [x, y, z, l, h, w, rot_y](単眼カメラヘッド系) 不要
引数 取り得る値
pc_range [x_min, y_min, z_min, x_max, y_max, z_max]。正規化系の layout で必須
frame "camera" / "lidar" / "ego" / "ground"。
to_ground frame="camera" のときの取り付け位置。4x4 / 3x3 行列、または {"height":..., "pitch":..., "roll":...}

構造・軸ノード(S.* — 入れ子を組み立てる)

ノード 記述例 意味
S.BatchAxis S.BatchAxis[S.Dict(...)] サンプル(画像・フレーム)の並び
S.DetAxis S.DetAxis[S.Coords("xyxy", "pixel")] 検出(候補)の並び
S.Axis S.Axis("layer", take=-1)[S.DetAxis[...]] 名前付き軸。take=int で 1 枚選ぶ / take="flatten" で後続の DetAxis に融合
S.Dict S.Dict(boxes=..., scores=...) 辞書キー / オブジェクト属性
S.Obj S.Obj(MyDetections, boxes=..., scores=...) 特定クラスのインスタンス(属性読み)
S.Seq S.Seq(boxes_node, scores_node, labels_node) タプル / list を位置で分解
S.Channels S.Channels(S.Coords("xyxy","pixel"), S.Score("raw")) 1 tensor の末尾次元を leaf ごとに分割
S.Getter S.Getter(lambda c: c.dets)[S.DetAxis[...]] 上記で表せない位置を関数で取り出す逃げ道

補助的な引数:

引数 記述例 意味
S.Dict(missing_ok=[...]) S.Dict(boxes=..., missing_ok=["boxes"]) 検出 0 件のとき枝ごと欠損しうるキー
S.Dict(key_aliases={...}) S.Dict(boxes=..., key_aliases={"boxes": ["bboxes"]}) 指定キーが無いときにだけ読む代替キー
S.DetAxis(valid_by=...) S.DetAxis(valid_by="score", valid_min=0.0)[...] パディング行を score で判定して除く

ModelSpec のフィールド

フィールド 型 主なフィールド ページ
labels LabelSchema n_class / ground_truth labels=
inputs InputSchema structure / resolve inputs=
invoke Invoke method / unpack_input / kwargs / container invoke=
raw_output RawOutputSchema structure / hook raw_output=
postprocess NMS / TopK / callable → postprocess= postprocess=
output OutputSchema structure / slice_with output=

構造ノードは直接渡せます

構造ノードを raw_output= / output= / inputs= に直接渡すと、 対応する *Schema で自動的に包まれます。

後処理のフィールド

クラス 主なフィールド
NMS score_thresh_attr / iou_thresh_attr / max_det_attr / apply_func / custom / decode / min_box_edge_px / prenms_topk_per_class / clip_boxes_to_image / nms_mode
TopK max_num / score_thresh / topk_mode / decode
InModelNMS method / returns / kwargs / decoder / decode / activation
PostProcess fn / kwargs
Decode fn / returns / kwargs

呼び出し時のマーカー

マーカー 用途
FromCall() tracer(...) の同名 kwarg をモデルへ転送
NMSScoreThresh() / NMSIouThresh() / NMSMaxDet() 推論引数で受け取る NMS 閾値のプレースホルダ
ImageShapes() 後処理関数に画像サイズを渡す(2D 検出のみ)

関数・エントリポイント

名前 用途
inspect_model(model, sample_batch=..., ground_truth=...) ModelSpec の推定(質問付き)→ AutoMode
infer_spec(model, known={...}) ModelSpec を 1 行で取得。不足時は SpecInferenceError
validate_spec(spec, model, sample_batch) 実モデルに対する検査 → 検査
spec.verify(model, sample_batch) validate_spec と同じ検査(メソッド版)

定型パターン早見

モデルの形 記述
1 画像ごとの dict の list を返す 2D 検出 output=S.BatchAxis[S.Dict(boxes=..., scores=..., labels=...)]
1 画像分の dict をそのまま返す output=S.Dict(...)(BatchAxis を付けない)
tuple で返す output=S.Seq(...)
分類(logit をそのまま返す) output=S.ClassConfs(activation="raw")
分類(.logits 属性) output=S.Dict(logits=S.ClassConfs(activation="raw"))
query head(上位 K 件) raw_output=S.Dict(...) + postprocess=TopK(max_num=...)
NMS の閾値がモデル属性 postprocess=NMS(score_thresh_attr=..., iou_thresh_attr=..., max_det_attr=...)
NMS がモデル内部のメソッド postprocess=NMS(custom=InModelNMS(method=..., returns=...))
後処理が自前の関数 raw_output=... + postprocess=<fn> + output=<戻り値の構造>
別メソッドで推論を呼ぶ invoke=Invoke(method="forward_test")
入力がファイルパス output 内に S.Array(..., input_tensor=True) または inputs=InputSchema(resolve=fn)