anudit commited on
Commit
fddd2c2
·
verified ·
1 Parent(s): b8d15a7

Dynamic H×W export (multiples of 64); remove 1024 variant

Browse files
Files changed (3) hide show
  1. README.md +16 -15
  2. onnx/model_fp16.onnx +2 -2
  3. onnx/model_fp16_1024.onnx +0 -3
README.md CHANGED
@@ -11,7 +11,7 @@ tags:
11
  - birefnet
12
  ---
13
 
14
- # BiRefNet_lite-2K — ONNX fp16 (static 2560×1440, low-memory graph)
15
 
16
  ONNX export of [ZhengPeng7/BiRefNet_lite-2K](https://huggingface.co/ZhengPeng7/BiRefNet_lite-2K)
17
  (Swin-T backbone, 44M params, trained for 2560×1440), made for in-browser inference with
@@ -19,30 +19,31 @@ ONNX export of [ZhengPeng7/BiRefNet_lite-2K](https://huggingface.co/ZhengPeng7/B
19
 
20
  | file | precision | input | output | size |
21
  |---|---|---|---|---|
22
- | `onnx/model_fp16.onnx` | fp16 weights, fp32 I/O | `input_image` `[1,3,1440,2560]` (ImageNet-normalised RGB) | `output_image` `[1,1,1440,2560]` logits (apply sigmoid) | ~99 MB |
23
 
24
- `onnx/model_fp16_1024.onnx` is the same weights exported at a fixed `[1,3,1024,1024]` input (~93 MB, ~4× faster, ~¼ the memory);
25
- `birefnet-webgpu` uses it automatically for small source images.
26
-
27
- The input size is **fixed**: resize (stretch, no aspect preservation — this matches training) your image
28
- to 2560×1440, run, then resize the mask back.
29
 
30
  ## What is different from a plain `torch.onnx.export`
31
 
32
- A naive export needs ~11 GB of activations at this resolution, which is impossible in a browser tab.
33
- This graph computes the same function with ~0.45 GB peak live activations (~1.1 GB allocated on GPU in
34
- the WebGPU engine):
35
 
36
  * Deformable convolutions use the standard ONNX **`DeformConv`** op (opset 19+) instead of an im2col gather chain.
37
  * The 5-branch ASPP concat (1280 ch) followed by a 1×1 conv is rewritten as a sum of five 1×1 convs.
38
  * The full-resolution decoder tail (bilinear upsample → concat → 1×1 conv, and the `ipt_blk1` branch) is
39
- algebraically collapsed into a 1×1 conv at low resolution plus one 5×5 conv on the input image.
40
- This is exact except for a ~1 px outer ring where zero-padding interacts with the folded convolutions
41
- (≤ 3e-5 sigmoid difference on a test image).
 
42
  * GELU is exported as the `Gelu` op (opset 20).
43
 
44
- Validated against the original PyTorch model on a 2560×1440 image: fp32 graph IoU 0.999996, fp16 graph IoU 0.9995 (ORT CPU).
45
- Requires a runtime that implements `DeformConv` and `Gelu` (verified with onnxruntime on CPU; the `birefnet-webgpu` engine on WebGPU).
 
46
 
47
  ## License / credits
48
 
 
11
  - birefnet
12
  ---
13
 
14
+ # BiRefNet_lite-2K — ONNX fp16 (dynamic input size, low-memory graph)
15
 
16
  ONNX export of [ZhengPeng7/BiRefNet_lite-2K](https://huggingface.co/ZhengPeng7/BiRefNet_lite-2K)
17
  (Swin-T backbone, 44M params, trained for 2560×1440), made for in-browser inference with
 
19
 
20
  | file | precision | input | output | size |
21
  |---|---|---|---|---|
22
+ | `onnx/model_fp16.onnx` | fp16 weights, fp32 I/O | `input_image` `[1,3,H,W]` (ImageNet-normalised RGB) | `output_image` `[1,1,H,W]` logits (apply sigmoid) | ~91 MB |
23
 
24
+ **H and W are dynamic but must both be multiples of 64** (Swin patch merging, including the half-resolution
25
+ multi-scale path). Resize your image to such a size (the 2K model was trained at 2560×1440; `birefnet-webgpu`
26
+ buckets the long side to 512 / 1024 / 1536 / 2048 / 2560 and keeps the aspect ratio), run, then resize the mask back.
27
+ Verified against PyTorch at 512², 640×960, 1024², 1536×1024 and 1472×2560.
 
28
 
29
  ## What is different from a plain `torch.onnx.export`
30
 
31
+ A naive export needs ~11 GB of activations at 2560×1440, which is impossible in a browser tab.
32
+ This graph computes the same function with ~0.45 GB peak live activations at 2560×1472 (~1.1 GB allocated on GPU in
33
+ the WebGPU engine; far less at smaller sizes):
34
 
35
  * Deformable convolutions use the standard ONNX **`DeformConv`** op (opset 19+) instead of an im2col gather chain.
36
  * The 5-branch ASPP concat (1280 ch) followed by a 1×1 conv is rewritten as a sum of five 1×1 convs.
37
  * The full-resolution decoder tail (bilinear upsample → concat → 1×1 conv, and the `ipt_blk1` branch) is
38
+ algebraically collapsed into a 1×1 conv at low resolution plus one 5×5 conv on the input image, with the
39
+ branch's bias response built exactly from a single-channel conv over a ones image. This is exact except for a
40
+ ~1 px outer ring where zero-padding interacts with the folded convolutions (max logit difference ≈ 0.64 there,
41
+ ≤ 7e-3 anywhere else).
42
  * GELU is exported as the `Gelu` op (opset 20).
43
 
44
+ fp32 graph IoU vs the original PyTorch model: ≥ 0.999997 on the test sizes; fp16 graph IoU 0.9995 (2560×1440, ORT CPU).
45
+ Requires a runtime that implements `DeformConv` and `Gelu` (verified with onnxruntime on CPU; the `birefnet-webgpu`
46
+ engine on WebGPU).
47
 
48
  ## License / credits
49
 
onnx/model_fp16.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:5d03dc5de5f3295dfcf4b84829fbbcd52bc3adfac890965c7316b99d40739a02
3
- size 98728016
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:30359ab09268c342c71308f836db45485c5f8ac4f4b55c268dce99cf174bc25b
3
+ size 91435003
onnx/model_fp16_1024.onnx DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:ab11fddbecb0bbb8b07aa698d07f8fe15771b2714c992404a90588d7df2dc028
3
- size 93445455