File size: 2,959 Bytes
f4f82a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: apache-2.0
pipeline_tag: image-segmentation
tags:
- webgpu
- kaminos
- browser
- background-removal
- u2net
- rembg
---

# U²-Net WebGPU Weights

Browser-loadable U²-Net (`u2net`) weights for background removal on WebGPU, as used by [Kaminos](https://github.com/lyonsno/kaminos) to cut a subject out of an ordinary photo before image-to-3D generation.

The browser route reproduces [rembg](https://github.com/danielgatis/rembg) 2.0.76 `remove(image, session=u2net)`: the Lanczos resize to 320×320, ImageNet normalization, U²-Net inference, min–max mask, Lanczos mask back to image size and the composited cutout. Every step around the network matches rembg and Pillow 12.3 byte for byte; the WebGPU network output differs from onnxruntime's CPU result by at most about 4e-5, and finished cutouts on test photos are within 1/255 of rembg's.

## Package

`f32/graph.json` lists the 259 operations of the network (convolutions with their ReLU fused, max-pools, bilinear resizes, concatenations, additions and sigmoids) with static shapes at the fixed 1×3×320×320 input, plus offsets into `f32/weights.f32`: 43,981,069 little-endian float32 values, 175,924,276 bytes. The graph records the weights' SHA-256 and the browser loader refuses a mismatch.

Use an immutable Hub commit in the package URL:

```javascript
import {loadU2NetPackage, createU2NetRunner} from './models/u2net/u2net-webgpu.js';
import {removeForegroundU2Net} from './models/u2net/rembg-u2net.js';

const pkg = await loadU2NetPackage('https://huggingface.co/BasinShapers/u2net-webgpu/resolve/<commit>/f32/');
const runner = createU2NetRunner({device, ...pkg});
const {rgba, width, height} = await removeForegroundU2Net({pixels, width: w, height: h, channels: 3, runner});
```

The loader, WebGPU kernels and rembg-equivalent pre- and post-processing are in [Kaminos models/u2net](https://github.com/lyonsno/kaminos/tree/main/models/u2net). This repository contains weights, not executable model code.

## Sources And Terms

Weights: rembg's distributed `u2net.onnx` ([release asset](https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx)), MD5 `60024c5c889badc19c04ad937298a77b` as pinned by rembg, SHA-256 `8d10d2f3bb75ae3b6d527c77944fc5e7dcd94b29809d47a739a7a728a912b491`.

U²-Net is by Xuebin Qin, Zichen Zhang, Chenyang Huang, Masood Dehghan, Osmar R. Zaiane and Martin Jagersand ([xuebinqin/U-2-Net](https://github.com/xuebinqin/U-2-Net); "U²-Net: Going Deeper with Nested U-Structure for Salient Object Detection", Pattern Recognition 2020). Its Apache License 2.0 is included as `LICENSE-U2NET`. rembg's MIT notice is included as `LICENSE-REMBG`.

Modification notice for `f32/`: Kaminos read the ONNX graph, folded its shape-computation operations into static shapes, fused each ReLU into its convolution and wrote the convolution weights and biases unchanged as float32 in one file. No values were quantized, fine-tuned or otherwise changed.