birefnet-onnx

An ONNX export of BiRefNet (background removal) with the deformable convolutions rewritten as plain operations, so it runs under ONNX Runtime on DirectML or CPU. Used by Realmspinner for cutting a subject out of a picture.

This is ZhengPeng7/BiRefNet at revision bb4456abab70b5e6604f21964672ab3ac9e7fb44, converted to ONNX. The model, its training and its quality are the work of Zheng Peng and the BiRefNet authors; only the file format changed. Please read the original model card and cite the BiRefNet paper listed there if you use this.

What changed from the original

Weights The original fp32 weights, unchanged (no quantization)
Format ONNX, opset 17, one self-contained file (birefnet.onnx, 932,329,255 bytes)
Deformable convolution The original uses torchvision's deform_conv2d, which exports to a graph that ONNX Runtime cannot run faithfully. It is replaced by an equivalent made of grid_sample and matrix multiplication (bilinear sampling with zero padding, the same semantics as torchvision). The exported graph has no DeformConv operator.
Export PyTorch 2.8.0, legacy tracer (dynamo=False), fixed 1024ร—1024 input

The replacement matches torchvision's deformable convolution to about 1e-9 (max absolute difference on a random input). Against the original PyTorch model the exported graph's output differed by at most 8.6e-5 on the test image used while developing it. That is a measurement on one image, not a benchmark of segmentation quality.

Inputs and outputs

name type shape
input input_image float32 [1, 3, 1024, 1024]
output logits float32 [1, 1, 1024, 1024]

The output is logits, not probabilities. Realmspinner prepares the input by resizing the picture to 1024ร—1024 (bilinear), scaling to 0..1 and normalising with the ImageNet mean and standard deviation, then takes a sigmoid of the output and scales it to 0..255; a threshold on that gives the mask.

Speed

On an RTX 5090 with ONNX Runtime 1.24.4 and the DirectML provider, a warm image took 0.17 to 0.28 seconds including pre- and post-processing; the first call is slower while DirectML compiles the graph. The file is fp32 and about 0.9 GB, so it needs that much memory plus working space.

Reproducing the file

Load the original weights at the revision above, replace deform_conv2d with the grid_sample version, and call torch.onnx.export on a zero tensor of shape [1, 3, 1024, 1024] with input_names=["input_image"], output_names=["logits"], opset_version=17, dynamo=False. With torch 2.8.0, torchvision 0.23.0, timm 1.0.28, einops 0.8.2, kornia 0.8.3, safetensors 0.8.0 and transformers 5.14.1 on CPU, this reproduces the file byte for byte: the SHA-256 is df912cb21f25599791f428d2a7118337531713594d9ff7ac8c94e584f072ed69.

License and attribution

BiRefNet is released under the MIT License, Copyright (c) 2024 ZhengPeng; the notice is in LICENSE and travels with this file as the license requires. This derivative is released under the same license. The upstream card at this revision describes the training data; this repository makes no claim about it beyond what the upstream card says.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Model tree for jmbell88/birefnet-onnx

Quantized
(11)
this model