#!/usr/bin/env python3 """Reproduce bp_fp32.onnx from a torchvision checkpoint you trained yourself. The published ONNX was exported from a checkpoint saved as {"state_dict": ..., "class_ids": {"객체_가스레인지": 1, "객체_변기": 2, "객체_세면대": 3, "객체_싱크대": 4, "객체_욕조": 5}, "arch": "r50fpn2", "max_side": 2048} Architecture: torchvision fasterrcnn_resnet50_fpn_v2(num_classes=6, min_size=max_side, max_size=max_side, box_detections_per_img=300), initialized for training from the torchvision COCO weights (weights="DEFAULT"). The ONNX graph takes one pre-processed image `image` [3,H,W] float32 0..1 (H, W dynamic) and returns `boxes` [N,4] (input-tensor xyxy), `labels` [N] int64 (1..5), `scores` [N]. Opset 17. Metadata keys: class_ids, arch, max_side, checkpoint_sha256, precision, opset. python export_onnx.py --ckpt my_checkpoint.pt --out bp_fp32.onnx [--device cuda] python export_onnx.py --check bp_fp32.onnx --ckpt my_checkpoint.pt # ONNX vs torch on synthetic drawings """ from __future__ import annotations import argparse import hashlib import json import sys from pathlib import Path OPSET, INPUT_NAME, OUTPUT_NAMES = 17, "image", ("boxes", "labels", "scores") def sha256_file(p) -> str: h = hashlib.sha256() with open(p, "rb") as f: for b in iter(lambda: f.read(1 << 22), b""): h.update(b) return h.hexdigest() def build_model(ncls: int, max_side: int): from torchvision.models.detection import fasterrcnn_resnet50_fpn_v2 return fasterrcnn_resnet50_fpn_v2(weights=None, weights_backbone=None, num_classes=ncls + 1, min_size=max_side, max_size=max_side, box_detections_per_img=300) def load_checkpoint(path): import torch ck = torch.load(str(path), map_location="cpu", weights_only=False) # your own file; contains plain dict/str/int + tensors cid = {str(k): int(v) for k, v in ck["class_ids"].items()} max_side = int(ck.get("max_side", 2048)) m = build_model(len(cid), max_side) m.load_state_dict(ck["state_dict"]) return m.eval(), {"class_ids": cid, "arch": "r50fpn2", "max_side": max_side, "checkpoint_sha256": sha256_file(path)} def _single(model): import torch class W(torch.nn.Module): def __init__(self, m): super().__init__(); self.m = m def forward(self, image): o = self.m([image])[0] return o["boxes"], o["labels"], o["scores"] return W(model).eval() def stamp_metadata(path, meta: dict) -> None: import onnx m = onnx.load(str(path)) old = {p.key: p.value for p in m.metadata_props} old.update({k: v if isinstance(v, str) else json.dumps(v, ensure_ascii=False) for k, v in meta.items()}) del m.metadata_props[:] for k, v in old.items(): e = m.metadata_props.add(); e.key, e.value = k, v onnx.save(m, str(path)) def export(ckpt, out, device="cpu", sample_hw=(1448, 2048)) -> dict: import onnx import torch from blueprintparse import synthetic_drawing, preprocess model, meta = load_checkpoint(ckpt) w = _single(model).to(device) x, _, _ = preprocess(synthetic_drawing(*sample_hw), meta["max_side"]) x = torch.from_numpy(x).to(device) kw = dict(opset_version=OPSET, input_names=[INPUT_NAME], output_names=list(OUTPUT_NAMES), dynamic_axes={INPUT_NAME: {1: "H", 2: "W"}, "boxes": {0: "N"}, "labels": {0: "N"}, "scores": {0: "N"}}, do_constant_folding=True) with torch.no_grad(): try: torch.onnx.export(w, (x,), str(out), dynamo=False, **kw) except TypeError: torch.onnx.export(w, (x,), str(out), **kw) stamp_metadata(out, {**meta, "precision": "fp32", "opset": OPSET}) onnx.checker.check_model(str(out)) return {"out": str(out), "sha256": sha256_file(out), "bytes": Path(out).stat().st_size} def check(onnx_path, ckpt, n=3) -> dict: """Greedy box match (IoU >= 0.5, same label) of ONNX vs torch on n synthetic drawings, scores >= 0.3.""" import numpy as np import torch from blueprintparse import BlueprintParse, preprocess, synthetic_drawing model, meta = load_checkpoint(ckpt) bp = BlueprintParse(onnx_path, verify_sha=False) def iou(a, b): ix = max(0, min(a[2], b[2]) - max(a[0], b[0])); iy = max(0, min(a[3], b[3]) - max(a[1], b[1])) u = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - ix * iy return ix * iy / u if u > 0 else 0.0 tot = {"n_ref": 0, "matched": 0, "max_abs_score": 0.0} for seed in range(n): x, _, _ = preprocess(synthetic_drawing(seed=seed), meta["max_side"]) with torch.no_grad(): o = model([torch.from_numpy(x)])[0] ref = [(b, int(l), float(s)) for b, l, s in zip(o["boxes"].numpy(), o["labels"].numpy(), o["scores"].numpy()) if s >= 0.3] got = [(b, int(l), float(s)) for b, l, s in zip(*bp.raw(x)) if s >= 0.3] used = set(); tot["n_ref"] += len(ref) for b, l, s in ref: j = max((k for k in range(len(got)) if k not in used and got[k][1] == l), key=lambda k: iou(b, got[k][0]), default=None) if j is not None and iou(b, got[j][0]) >= 0.5: used.add(j); tot["matched"] += 1; tot["max_abs_score"] = max(tot["max_abs_score"], abs(s - got[j][2])) return tot if __name__ == "__main__": ap = argparse.ArgumentParser() ap.add_argument("--ckpt", required=True); ap.add_argument("--out", default="bp_fp32.onnx") ap.add_argument("--device", default="cpu"); ap.add_argument("--check") a = ap.parse_args() print(json.dumps(check(a.check, a.ckpt) if a.check else export(a.ckpt, a.out, a.device), indent=1)) sys.exit(0)