File size: 2,671 Bytes
451ada1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182b1eb
 
 
 
451ada1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
---
library_name: onnx
license: mit
pipeline_tag: image-segmentation
tags:
  - onnx
  - birefnet
  - background-removal
  - image-segmentation
base_model:
  - ZhengPeng7/BiRefNet
---

# imageback models

ONNX builds of [BiRefNet](https://github.com/ZhengPeng7/BiRefNet) used by imageback, a C++
command-line tool that replaces the background of a photo with an image, a colour or transparency.
They run on ONNX Runtime (CPU, CUDA or WebGPU) with no Python involved.

Nothing here was trained or fine-tuned by us. Each folder is derived from the ONNX export at
[onnx-community/BiRefNet-ONNX](https://huggingface.co/onnx-community/BiRefNet-ONNX) and stays under
the original MIT licence.

| Folder | Size | Weights |
| --- | --- | --- |
| `birefnet-fp16/` | 490 MB | Half precision. Best choice on a GPU (recommended download) |
| `birefnet-int8/` | 309 MB | 8-bit. Closest to full precision of the small ones |
| `birefnet-int4/` | 209 MB | 4-bit backbone, 8-bit elsewhere. Smallest |
| `birefnet-fp32/` | 973 MB | Full precision; the source for quantizing |

Each folder holds `model.onnx` and `config.json` (input size and normalisation).

```sh
hf download baker76/imageback-models --include "birefnet-fp16/*" --local-dir models
imageback replace photo.jpg -o cutout.png --model models/birefnet-fp16
```

## Using the models elsewhere

Input `input_image`: float32 `[1, 3, 1024, 1024]`, RGB scaled to 0-1 and normalised with the ImageNet
mean and standard deviation; the picture is stretched to 1024 x 1024. Output `output_image`: float32
`[1, 1, 1024, 1024]` logits; apply a sigmoid and resize to the picture's size to get the alpha matte.

## Changes from the upstream export

- All folders: `Split` nodes with 16 or 32 outputs are replaced by two levels of smaller ones.
  WebGPU allows a shader 16 buffers, which the original nodes exceed. Results are unchanged.
- `birefnet-int8`, `birefnet-int4`: made from the fp32 graph. The Swin backbone's `MatMul` weights
  (84% of the model) are stored as 8- or 4-bit blocks of 32 (ONNX Runtime `MatMulNBits`). Convolution
  kernels and relative position biases are stored as 8-bit integers behind `DequantizeLinear`.
  Activations stay float32, so these are smaller than fp16 but not faster.

## Accuracy

Difference between each model's alpha matte and the fp32 model's, on five test photos (people, a dog,
a cat, a bicycle), with the CUDA provider:

| Model | Mean absolute difference | Pixels differing by more than 0.1 |
| --- | --- | --- |
| fp16 | 0.00017 | 0.001% |
| int8 | 0.00011 | 0.000% |
| int4 | 0.00081 | 0.076% |

The scripts that produced these files are in the imageback repository under `tools/export/`.