← All models

image-segmentation Β· matting Β· vision Β· WebGPU (fp32) / WASM (q8) Β· ~7 MB

MODNet β€” matting that keeps the hair

Removing a background is easy until you reach the hair. MODNet is a matting network: instead of a hard in/out decision it predicts a soft alpha value for every pixel, so flyaway strands, fur and soft edges survive the cut. Give it a portrait and it returns a matte you can drop onto transparency, a colour, or a new scene β€” entirely on your device.

At a glance

ModelXenova/modnet
Task / pipelineimage-segmentation β€” a single soft foreground alpha matte
ArchitectureMODNet β€” trimap-free portrait matting (one network, three branches: semantics, detail, fusion)
InputDynamic resolution (any aspect ratio) β€” no fixed 1024Β² crop
Precisionfp32 on WebGPU; q8 (8-bit) on the WASM fallback
BackendWebGPU (fp32) when available; falls back to WebAssembly (q8) everywhere else
Download~7 MB (q8) / ~26 MB (fp32) β€” tiny; cached after first load
LicenseApache-2.0 (MODNet / ZHKKKe); the ONNX export is by Xenova
Web APIsWeb Workers, Canvas 2D, FileReader β€” all Baseline; WebGPU is used opportunistically and the page falls back honestly without it

Run it

Pick or drop a photo (portraits work best β€” it's trained on people), then pull the matte. Drag the slider to wipe between the original and the cut-out on a transparency checkerboard.

Drop a photo, or click to choose
A smiling woman with long flowing hair A woman with dark hair and wispy bangs against a green background Two cats with fur on a pink blanket

Before / after β€” drag to compare

Original photo
55%

Cut-out (on transparency)

See inside

MODNet never sees "a person" β€” it outputs an alpha matte: a number from 0 (background) to 255 (subject) for every pixel. What makes a matting model different from a plain background remover is the middle of that range: the grey, half-transparent band along hair and fur where it hedges. Those in-between values are exactly what a hard segmentation throws away β€” and what makes a composite look real. The histogram below should show mass at both ends and a spread in between; the soft-edge view isolates that band.

Pull a matte to see it.

Why it matters

There are two jobs hiding inside "remove the background". RMBG-1.4 answers where is the subject with a confident, general-purpose mask. MODNet answers the harder question β€” exactly how opaque is each pixel β€” which is what you need whenever the edge is soft:

Use cases

Basics

Matte vs. mask β€” see MODNet's soft edges next to RMBG's hard cut on the same photo.

Practical

Headshot studio β€” drop your portrait onto a brand colour or backdrop, hair intact.

Wild

Edge stress test β€” flyaway hair, fur and glass, with a matte-quality score.

Multi-model

Matte β†’ caption: cut the subject out, then a VLM describes just what's left.

How the API works

MODNet uses a custom architecture, so the model card's path loads the model and processor directly and reads the matte tensor:

import { AutoModel, AutoProcessor, RawImage } from "@huggingface/transformers";

const model = await AutoModel.from_pretrained("Xenova/modnet", {
  device: "webgpu", dtype: "fp32", config: { model_type: "custom" },
});
const processor = await AutoProcessor.from_pretrained("Xenova/modnet");

const image = await RawImage.read(imageURL);
const { pixel_values } = await processor(image);
const { output } = await model({ input: pixel_values });   // the ONNX input is named "input"

// output is a [1,1,H,W] soft alpha map in 0–1. Scale to 0–255 and resize to the original photo:
const matte = (await RawImage.fromTensor(output[0].mul(255).to("uint8")))
  .resize(image.width, image.height);
// then copy `matte` into the image's alpha channel β†’ the cut-out

This page runs that in a Web Worker and stamps the matte into the photo's alpha on a canvas, so the interface stays responsive and the composite (transparency, colour, or a new scene) is all client-side. The input tensor's name (input) is read off the live ONNX session β€” no invented signature.

References