Upload 3 files
Browse files- 2xNomosUni_compact_otf_medium.fp16.onnx +3 -0
- 2x_Adore_renarchi_fp16.onnx +3 -0
- convert_fp16_onnx.py +157 -0
2xNomosUni_compact_otf_medium.fp16.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:31d3c36ba1a7055698d7bd3d35449fe00148f73d75dad93311fc6c53d02d228a
|
| 3 |
+
size 1213455
|
2x_Adore_renarchi_fp16.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bc0756f06ae8c1a959f01484595391d66410e07b23e5f5687f0f361f9865e79a
|
| 3 |
+
size 2873769
|
convert_fp16_onnx.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Convert an ONNX model (e.g. 2xNomosUni_compact_otf_medium.onnx) to float16,
|
| 4 |
+
optimized for browser deployment via onnxruntime-web with the WebGPU
|
| 5 |
+
execution provider.
|
| 6 |
+
|
| 7 |
+
Why fp16 and not int8 for WebGPU:
|
| 8 |
+
Most modern GPUs support FP16 natively -> ~2x size/memory reduction with
|
| 9 |
+
near-equivalent throughput. INT8/INT4 support in WebGPU compute shaders
|
| 10 |
+
is inconsistent across GPU/driver/ORT-web versions, with common gaps in
|
| 11 |
+
fused kernels and integer ops. FP16 is the safe, fast, broadly-supported
|
| 12 |
+
path for WebGPU specifically (as opposed to WASM, where INT8 wins).
|
| 13 |
+
|
| 14 |
+
What this script does:
|
| 15 |
+
1. Loads and validates the source ONNX model.
|
| 16 |
+
2. Converts weights + compute graph to float16 (keeping a few numerically
|
| 17 |
+
sensitive ops like Resize/Softmax in fp32 via keep_io_types /
|
| 18 |
+
op_block_list, which is standard practice to avoid artifacts).
|
| 19 |
+
3. Verifies the converted model still passes onnx.checker.
|
| 20 |
+
4. Runs a quick numerical sanity check: same input through fp32 and fp16
|
| 21 |
+
models, reports PSNR between the two outputs so you know how close
|
| 22 |
+
the fp16 version is before you ship it.
|
| 23 |
+
5. Saves the result and prints the size comparison.
|
| 24 |
+
|
| 25 |
+
Usage:
|
| 26 |
+
python convert_fp16_onnx.py \
|
| 27 |
+
--model /Users/emay/Downloads/ONNXmodels/models/2xNomosUni_compact_otf_medium.onnx \
|
| 28 |
+
--out /Users/emay/Downloads/ONNXmodels/models/2xNomosUni_compact_otf_medium.fp16.onnx
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
import argparse
|
| 32 |
+
import os
|
| 33 |
+
import sys
|
| 34 |
+
|
| 35 |
+
import numpy as np
|
| 36 |
+
import onnx
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
import onnxruntime as ort
|
| 40 |
+
except ImportError:
|
| 41 |
+
print("onnxruntime is required: pip install onnxruntime", file=sys.stderr)
|
| 42 |
+
raise
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
from onnxconverter_common import float16
|
| 46 |
+
except ImportError:
|
| 47 |
+
print(
|
| 48 |
+
"onnxconverter-common is required: pip install onnxconverter-common",
|
| 49 |
+
file=sys.stderr,
|
| 50 |
+
)
|
| 51 |
+
raise
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def psnr(a: np.ndarray, b: np.ndarray, max_val: float = 1.0) -> float:
|
| 55 |
+
mse = np.mean((a.astype(np.float64) - b.astype(np.float64)) ** 2)
|
| 56 |
+
if mse == 0:
|
| 57 |
+
return float("inf")
|
| 58 |
+
return 10 * np.log10((max_val ** 2) / mse)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def inspect(model_path: str) -> onnx.ModelProto:
|
| 62 |
+
print(f"\n{'='*70}\nSTEP 1: Loading & inspecting source model\n{'='*70}")
|
| 63 |
+
model = onnx.load(model_path)
|
| 64 |
+
onnx.checker.check_model(model)
|
| 65 |
+
|
| 66 |
+
size_mb = os.path.getsize(model_path) / 1e6
|
| 67 |
+
print(f"File: {model_path}")
|
| 68 |
+
print(f"Size: {size_mb:.2f} MB")
|
| 69 |
+
print(f"Input: {model.graph.input[0].name}")
|
| 70 |
+
print(f"Output: {model.graph.output[0].name}")
|
| 71 |
+
|
| 72 |
+
op_counts = {}
|
| 73 |
+
for node in model.graph.node:
|
| 74 |
+
op_counts[node.op_type] = op_counts.get(node.op_type, 0) + 1
|
| 75 |
+
print("Ops:", ", ".join(f"{k}x{v}" for k, v in sorted(op_counts.items())))
|
| 76 |
+
print(f"{'='*70}\n")
|
| 77 |
+
return model
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def convert_to_fp16(model: onnx.ModelProto) -> onnx.ModelProto:
|
| 81 |
+
print(f"{'='*70}\nSTEP 2: Converting to float16\n{'='*70}")
|
| 82 |
+
# keep_io_types=True keeps the graph's external input/output tensors as
|
| 83 |
+
# float32 so callers don't need to change how they feed/read data -- ORT
|
| 84 |
+
# inserts Cast nodes at the boundary, cost is negligible vs the conv body.
|
| 85 |
+
# Resize (used for the pixel-shuffle/upsample path in SRVGGNetCompact-like
|
| 86 |
+
# nets) is block-listed since bilinear/nearest resize in fp16 can behave
|
| 87 |
+
# inconsistently across backends; keeping it in fp32 is cheap and safe.
|
| 88 |
+
fp16_model = float16.convert_float_to_float16(
|
| 89 |
+
model,
|
| 90 |
+
keep_io_types=True,
|
| 91 |
+
disable_shape_infer=False,
|
| 92 |
+
op_block_list=["Resize"],
|
| 93 |
+
)
|
| 94 |
+
onnx.checker.check_model(fp16_model)
|
| 95 |
+
print("Conversion complete, model passes onnx.checker.")
|
| 96 |
+
print(f"{'='*70}\n")
|
| 97 |
+
return fp16_model
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def sanity_check(orig_model: onnx.ModelProto, fp16_model: onnx.ModelProto, tile: int = 128):
|
| 101 |
+
print(f"{'='*70}\nSTEP 3: Numerical sanity check (fp32 vs fp16 output)\n{'='*70}")
|
| 102 |
+
|
| 103 |
+
input_name = orig_model.graph.input[0].name
|
| 104 |
+
x = np.random.rand(1, 3, tile, tile).astype(np.float32)
|
| 105 |
+
|
| 106 |
+
sess_orig = ort.InferenceSession(orig_model.SerializeToString(), providers=["CPUExecutionProvider"])
|
| 107 |
+
sess_fp16 = ort.InferenceSession(fp16_model.SerializeToString(), providers=["CPUExecutionProvider"])
|
| 108 |
+
|
| 109 |
+
out_orig = sess_orig.run(None, {input_name: x})[0]
|
| 110 |
+
out_fp16 = sess_fp16.run(None, {input_name: x})[0]
|
| 111 |
+
|
| 112 |
+
p = psnr(out_orig, out_fp16)
|
| 113 |
+
print(f"PSNR (fp32 vs fp16 output, random calibration tile): {p:.1f} dB")
|
| 114 |
+
if p < 40:
|
| 115 |
+
print("NOTE: PSNR below 40dB -- inspect the fp16 output on a real image "
|
| 116 |
+
"before shipping. This is a synthetic random tile, so also test "
|
| 117 |
+
"with an actual photo for a trustworthy read.")
|
| 118 |
+
else:
|
| 119 |
+
print("Looks good -- fp16 output is numerically very close to fp32.")
|
| 120 |
+
print(f"{'='*70}\n")
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def main():
|
| 124 |
+
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
| 125 |
+
ap.add_argument("--model", required=True, help="Path to input .onnx model (fp32)")
|
| 126 |
+
ap.add_argument("--out", required=True, help="Path to write the fp16 .onnx model")
|
| 127 |
+
ap.add_argument("--tile", type=int, default=128, help="Tile size for the sanity-check input")
|
| 128 |
+
ap.add_argument("--skip-sanity-check", action="store_true")
|
| 129 |
+
args = ap.parse_args()
|
| 130 |
+
|
| 131 |
+
if not os.path.isfile(args.model):
|
| 132 |
+
print(f"Model not found: {args.model}", file=sys.stderr)
|
| 133 |
+
sys.exit(1)
|
| 134 |
+
|
| 135 |
+
orig_model = inspect(args.model)
|
| 136 |
+
fp16_model = convert_to_fp16(orig_model)
|
| 137 |
+
|
| 138 |
+
if not args.skip_sanity_check:
|
| 139 |
+
sanity_check(orig_model, fp16_model, tile=args.tile)
|
| 140 |
+
|
| 141 |
+
onnx.save(fp16_model, args.out)
|
| 142 |
+
|
| 143 |
+
orig_size = os.path.getsize(args.model) / 1e6
|
| 144 |
+
new_size = os.path.getsize(args.out) / 1e6
|
| 145 |
+
print(f"Saved: {args.out}")
|
| 146 |
+
print(f"Size: {orig_size:.2f} MB -> {new_size:.2f} MB "
|
| 147 |
+
f"({(1 - new_size/orig_size)*100:.0f}% smaller)")
|
| 148 |
+
print("\nFor WebGPU in onnxruntime-web, load this model with:\n"
|
| 149 |
+
" const session = await ort.InferenceSession.create(url, {\n"
|
| 150 |
+
" executionProviders: ['webgpu']\n"
|
| 151 |
+
" });\n"
|
| 152 |
+
"Inputs/outputs stay float32 at the JS boundary (keep_io_types=True),\n"
|
| 153 |
+
"so your existing pre/post-processing code doesn't need to change.")
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
main()
|