--- license: mit tags: - onnx - background-removal - image-matting - dichotomous-image-segmentation - qtmesheditor library_name: onnx pipeline_tag: image-segmentation base_model: ZhengPeng7/BiRefNet --- # BiRefNet — ONNX (high-resolution background removal) **BiRefNet** (Zheng et al., *"Bilateral Reference for High-Resolution Dichotomous Image Segmentation"*) exported to ONNX. MIT — code **and** weights. All credit to the BiRefNet authors. Mirrored for **[QtMeshEditor](https://github.com/fernandotonon/QtMeshEditor)** (issue #1016, epic #818 Track C2) as the **Best** matting tier for image-to-3D, alongside U2Net as the Fast tier. > The file QtMeshEditor downloads at runtime lives in the shared > [`fernandotonon/QtMeshEditor-models`](https://huggingface.co/fernandotonon/QtMeshEditor-models) > repo under `rembg/`. This repo is the standalone model card + mirror. ## Why it exists Image-to-3D quality is bounded by the cutout. U2Net runs at 320x320 and visibly loses hair and fur edges, which the reconstruction then turns into surface noise. BiRefNet runs at 1024x1024 and is measurably crisper: | model | input | ambiguous edge pixels | |---|---|---| | U2Net | 320x320 | 2.56% | | **BiRefNet** | **1024x1024** | **0.86%** | (Fraction of pixels with alpha strictly between 0.05 and 0.95 on a reference photo — lower is a cleaner cut. BiRefNet is ~3x crisper.) Original image|U2Net|BiRefNet -|-|- ![00_source](https://cdn-uploads.huggingface.co/production/uploads/6670d17fb0c03c4e9d684b81/8k0YIYbywoOnKQhVvjjn3.png)|![02_matte_FAST_u2net](https://cdn-uploads.huggingface.co/production/uploads/6670d17fb0c03c4e9d684b81/inbRwyixH7ToocL6qlwsC.png)|![03_matte_BEST_birefnet](https://cdn-uploads.huggingface.co/production/uploads/6670d17fb0c03c4e9d684b81/akMjZHtnifoJmAOXb-YY-.png) Comparison of final result, generating the model in [QtMeshEditor](https://github.com/fernandotonon/QtMeshEditor) with TRELLIS.2 after U2Net(Viewport1) or BiRefNet(Viewport2) background removal ![Captura de Tela 2026-09-15 às 10.07.29](https://cdn-uploads.huggingface.co/production/uploads/6670d17fb0c03c4e9d684b81/TosRaUHa6pf94qluJ-D8Y.png) ## I/O ``` input pixel_values float32 [1,3,1024,1024] ImageNet-normalised RGB (NCHW) output alpha float32 [1,1,1024,1024] foreground matte in [0,1] ``` Normalisation: `mean = [0.485, 0.456, 0.406]`, `std = [0.229, 0.224, 0.225]`. The sigmoid is folded INTO the graph, and only the final refined map is returned — upstream `forward()` emits a list of progressively refined logits, which would otherwise leave a consumer guessing which output is authoritative. Spatial dims are **pinned** (only batch is dynamic). ## Export notes BiRefNet is not straightforwardly exportable; three issues had to be solved: 1. The published checkpoint carries **fp16** weights, so tracing with an fp32 input fails. Exported as fp32. 2. **`torchvision::deform_conv2d` has no ONNX representation**, and it is not optional — the checkpoint carries 150 deformable tensors, so disabling ASPPDeformable would discard trained weights. The exporter decomposes it into GridSample + MatMul, verified against `torchvision.ops.deform_conv2d` to **~7e-07** on every kernel size BiRefNet uses (1x1, 3x3, 7x7), modulation mask included. 3. `birefnet.py` does `from torchvision.ops import deform_conv2d`, so patching torchvision after import silently has no effect. Export script: [`scripts/export-birefnet-onnx.py`](https://github.com/fernandotonon/QtMeshEditor/blob/master/scripts/export-birefnet-onnx.py) (opset 17). It asserts torch-vs-ORT parity (**1.5e-07**) and refuses to write a graph with non-finite output or a near-constant matte. ## License MIT, inherited from [`ZhengPeng7/BiRefNet`](https://huggingface.co/ZhengPeng7/BiRefNet). Credit: Peng Zheng et al.