image-segmentation ยท vision ยท WebGPU / WASM (fp16) ยท ~219 MB
BEN2 (Background Erase Network 2) is a confidence-guided matting model: it decides, pixel by pixel, how much each pixel belongs to the foreground โ and it's tuned for crisp, detailed edges (hair, fur, thin structures). You get a soft alpha matte and a transparent cutout, entirely on your device. Nothing is uploaded.
| Model | onnx-community/BEN2-ONNX (ONNX build of PramaLLC/BEN2) |
|---|---|
| Task / pipeline | image-segmentation โ one foreground alpha matte (confidence-guided matting) |
| Architecture | BEN2 matting network (transformers.js custom ben architecture), input 1024ร1024, output a 0โ1 confidence map |
| Precision | fp16 (the only exported precision) on both WebGPU and the WASM fallback |
| Backend | WebGPU where available (fast); WebAssembly fallback runs too, but a single 1024ยฒ matte can take ~30โ60 s on CPU |
| Download | ~219 MB, cached after first load |
| License | MIT |
| Web APIs | WebGPU (optional, for speed), Web Workers, OffscreenCanvas, Canvas 2D, FileReader |
Pick or drop a photo, then remove the background. The result sits on a checkerboard so you can see exactly where it made the image transparent.
No WebGPU adapter here โ BEN2 will run on the WASM backend. A single 1024ร1024 matte can take ~30โ60 seconds on CPU. It still runs entirely on your device.
BEN2 never sees "a cat" โ it outputs an alpha matte: a confidence from 0 (background) to 255 (subject) for every pixel. The cutout above is just the photo with that matte stamped into its transparency. The soft, in-between values along the boundary are where the model expresses partial coverage โ wisps of fur, hair, motion blur โ and BEN2's confidence-guided design is built to keep those edges crisp and detailed.
| matte resolution | โ |
|---|---|
| foreground coverage | โ |
| soft-edge pixels | โ |
"Remove the background" used to mean a round-trip to a paid API or twenty minutes in Photoshop. BEN2 does it in a web page, on-device โ and its confidence-guided matting is aimed squarely at the hard part: edges. That changes what you can build:
One photo in, a clean cutout on transparency out.
Product-shot maker โ swap in any colour or backdrop, export a PNG.
Hair & fur stress test โ zoom the soft-edge matte on the hardest boundaries.
BEN2 matte โ classify the cutout subject with an on-device image model.
BEN2 uses a custom architecture (model_type: "ben"), which transformers.js registers as a custom image-segmentation model. Load the model + processor directly and read the alpha tensor:
import { AutoModel, AutoProcessor, RawImage } from "@huggingface/transformers";
const model = await AutoModel.from_pretrained("onnx-community/BEN2-ONNX", {
device: "webgpu", dtype: "fp16", // fp16 is the only exported precision
});
const processor = await AutoProcessor.from_pretrained("onnx-community/BEN2-ONNX");
const image = await RawImage.read(imageURL);
const { pixel_values } = await processor(image); // [1,3,1024,1024]
const { alphas } = await model({ pixel_values }); // [1,1,1024,1024], 0..1
// Scale to 0โ255 and resize the matte back to the original photo:
const matte = (await RawImage.fromTensor(alphas[0].mul(255).to("uint8")))
.resize(image.width, image.height);
// then copy `matte` into the image's alpha channel โ the cutout
This page runs that in a Web Worker and does the per-pixel composite in the worker too
(OffscreenCanvas โ transferToImageBitmap), so the main thread only draws the finished
cutout โ the interface never janks even at full resolution.