โ† All models

image-segmentation ยท vision ยท WebGPU / WASM (fp16) ยท ~219 MB

BEN2 โ€” matte the subject out of any photo

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.

At a glance

Modelonnx-community/BEN2-ONNX (ONNX build of PramaLLC/BEN2)
Task / pipelineimage-segmentation โ€” one foreground alpha matte (confidence-guided matting)
ArchitectureBEN2 matting network (transformers.js custom ben architecture), input 1024ร—1024, output a 0โ€“1 confidence map
Precisionfp16 (the only exported precision) on both WebGPU and the WASM fallback
BackendWebGPU 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
LicenseMIT
Web APIsWebGPU (optional, for speed), Web Workers, OffscreenCanvas, Canvas 2D, FileReader

Run it

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.

Drop a photo, or click to choose
Two cats on a couch A sunlit bedroom scene A swan on a river at golden hour

Input

Selected photo

Cutout (on transparency)

See inside

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.

Remove a background to see the matte.

Why it matters

"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:

Use cases

Basics

One photo in, a clean cutout on transparency out.

Practical

Product-shot maker โ€” swap in any colour or backdrop, export a PNG.

Wild

Hair & fur stress test โ€” zoom the soft-edge matte on the hardest boundaries.

Multi-model

BEN2 matte โ†’ classify the cutout subject with an on-device image model.

How the API works

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.

References