GluelyAI TikTok app - Go viral!Try It Now

How to Remove Backgrounds With the BiRefNet API

10 min read
How to Remove Backgrounds With the BiRefNet API

Background removal used to be the least interesting problem in image tooling. You ran an image through a segmentation model, you got a mask back, and if the subject had hair or fur or a chain-link fence behind it, you accepted the grey halo and moved on. That tolerance disappeared the moment product photos, thumbnails, and ad creative started getting generated in the hundreds per day. At that volume, a mask that is 94 percent right is not a small quality issue, it is a queue of manual touch-ups nobody has time for.

BiRefNet is the model that changed the arithmetic. It came out of the high-resolution dichotomous segmentation research line, and the thing it does better than its predecessors is edge fidelity: individual hair strands, glass, mesh, and thin fabric survive the cut instead of being averaged into a soft grey rim. Practitioners comparing it against older pipelines report that it keeps fine strands that older background removal workflows lose outright, which is the difference between a batch you can ship and a batch you have to review.

This guide covers which variant to pick, what a working request looks like, how to wire it into a batch pipeline, and where the model still gets things wrong. It assumes the output has to be good enough to composite without a human checking every frame.

What BiRefNet does differently

BiRefNet uses a bilateral reference design. One path looks at the image at reduced resolution to work out what the subject is; a second keeps high-resolution source detail available so the boundary can be reconstructed sharply. Older models like U2Net compress hard and then upsample the mask, which is exactly why thin structures dissolve. If you have previously fought with halos while trying to make image backgrounds transparent, that upsampling step is usually the culprit.

So BiRefNet earns its extra compute on three categories: anything with hair or fur, anything transparent such as glass or plastic packaging, and anything with repeating thin structure such as netting, wicker, or lace. On a hard silhouette against a plain sweep backdrop, older models are close enough and cheaper. Knowing which bucket an image falls into is most of the optimization.

Single feather resting on a black lightbox

Picking a variant before you pick an endpoint

BiRefNet ships as a family, not a single checkpoint, and choosing wrong is the most common reason people conclude the model is overrated, in the same way that picking the wrong tool from a roundup of AI image editors leaves you blaming the category rather than the choice. The general use light model is the default and handles most catalog work. The heavy variant trades latency for accuracy on cluttered scenes. There is a portrait checkpoint tuned for faces and hair, a matting checkpoint trained for soft alpha rather than hard binary masks, and a dynamic-resolution variant covering roughly 256 by 256 up to 2304 by 2304 for inputs whose size you do not control.

The distinction that matters most is matting versus segmentation. Segmentation gives you a decision per pixel, subject or not subject. Matting gives you a continuous alpha value, which is what you need for hair, smoke, motion blur, and semi-transparent material. If you are compositing onto varied backgrounds, matting output holds up; binary masks show their seams. Teams building product photography pipelines usually end up running matting for apparel and hard segmentation for boxed goods.

Resolution is the other axis. The high-resolution checkpoints were trained around 2048 by 2048 and the 2K lite variant accepts inputs near 2560 by 1440, which means you can feed a real product photo without downscaling first. Downscaling before segmentation and upscaling the mask afterward is the single most common self-inflicted quality loss in these pipelines.

Making your first call

The fastest hosted path is fal, which exposes BiRefNet as a straightforward image-to-image endpoint, no local GPU and no hosted node-graph deployment required. Install the client, set your key in the environment, and submit an image URL:

import { fal } from "@fal-ai/client";

const result = await fal.subscribe("fal-ai/birefnet/v2", {
  input: {
    image_url: "https://example.com/product-01.jpg",
    model: "General Use (Heavy)",
    output_mask: true
  },
  logs: true
});

console.log(result.data.image.url);

Three things in that snippet are worth calling out. The model field is where you select the variant discussed above, so it is not a detail you set once and forget. The output_mask flag returns the alpha mask alongside the cut image, which you want if any downstream step needs to feather, erode, or recomposite. And subscribe blocks until the job finishes, which is fine for a script and wrong for a web request; production code should submit to the queue and take a webhook instead. The same pattern applies to any hosted generation endpoint, including the ones covered in our walkthrough of running batch image generation via API.

You can also self-host. The weights are published openly and the model runs on a single consumer GPU at modest resolution. The tradeoff is the usual one: you own the cold starts, the batching, and the queue.

Wiring it into a pipeline

A single call is easy. The interesting work is everything around it: fetching source images, routing each one to the right variant, retrying failures, storing both the cut image and its mask, and handing the result to whatever composites next. That glue is the part that rots, which is why a lot of teams run this stage on a visual pipeline tool instead. Node-based builders such as wireflow.ai let you drop a BiRefNet node between an input source and a compositing step and expose the whole chain as a single API call, which keeps the routing logic in one visible place rather than scattered across a worker.

Whichever way you build it, a few things belong in the design from day one. Store the mask, not just the composited PNG, because you will want to redo the composite later without paying for inference again. Key your cache on a hash of the source bytes plus the variant name, since the same image run through two checkpoints is two different results. Set a per-day spend ceiling before you turn on the batch loop; the failure mode people report is not an expensive image, it is a retry loop discovering an unbounded budget, which is the whole argument for generation APIs with spend limits.

Scissors on a cutting mat beside paper scraps

Order of operations matters more than people expect. Remove the background before you upscale, not after: upscaling first bakes the old background's color fringe into the edge pixels, and the model then has to cut through an artifact it did not create. The sequence in our guide to upscaling images via API applies here too, segment then enlarge then sharpen.

Where BiRefNet still gets it wrong

It is not solved. Reflective surfaces confuse it: a chrome kettle that mirrors the room often keeps a slice of the reflected background inside the mask, because the model cannot know the reflection is not part of the object. Contact shadows under a product are the second failure, where the model has to guess whether the shadow is subject or scene. That is a judgment call, not a segmentation error, so no checkpoint gets it right for every use case.

The third category is multi-object scenes with ambiguous intent. Given a person holding a bag, the model returns both, and if you wanted only the bag you need a prompt-driven segmentation model instead. The mitigation is a confidence gate: compute the mask's edge entropy or simply the count of partially transparent pixels, and route anything above your threshold to human review. Roughly five percent of a mixed catalog will trip that gate, which is a manageable queue and far better than reviewing everything. Teams that also need object removal rather than background removal should treat that as a separate model entirely, not a flag on this one.

Cost, latency, and caching

Per-image inference on a hosted endpoint is cheap enough that it will not be the line item you notice, but latency will shape your architecture. Light variants return in around a second at moderate resolution; heavy and high-resolution variants take several. Pick your variant per surface rather than per project: light for anything a user is waiting on, heavy for anything queued.

Cache aggressively and cache the mask. Background removal is deterministic enough that re-running the same source image is pure waste, and masks are small. If you later change background color, crop, or output format, you recomposite from the stored mask in milliseconds. The same reasoning shows up in most production image stacks, including the SaaS generation pipelines where inference cost per tenant is the number that decides margin.

FAQ

Is BiRefNet free to use? The weights are released openly, so self-hosting has no per-image fee beyond compute; hosted endpoints charge per call. Check the licence before commercial deployment, since research-origin models sometimes carry restrictions a wrapper API does not surface. Our survey of AI APIs for developers covers how providers differ here.

Which variant should I default to? General use light. Move to heavy only when your review queue tells you the light model is missing edges on your specific content, and move to the matting checkpoint when you are compositing hair or transparent material onto varied backgrounds.

Can it output a transparent PNG directly? Yes, hosted endpoints return a cut image with an alpha channel. Request the separate mask as well if any later step needs to adjust the edge, because recovering a clean mask from a composited PNG is lossy. If your end goal is a new backdrop rather than transparency, swapping photo backgrounds starts from the same mask.

How does it compare to rembg? rembg is a wrapper around several older checkpoints, U2Net most commonly, and it is faster and lighter. On hard silhouettes the difference is small. On hair, fur, and mesh, BiRefNet keeps detail that U2Net-class models average away, which is the entire reason to pay the extra compute.

Does it handle batches? Not natively in a single call on most hosted endpoints. You submit each image to the queue and collect results by webhook. Concurrency is yours to manage, and a queue with bounded workers beats firing hundreds of parallel requests, the same shape described in our notes on setting up batch image generation.

What resolution should I send? Send the native resolution if it is within the variant's trained range. The high-resolution checkpoints handle roughly 2048 by 2048 and the dynamic variant covers a wide band, so pre-downscaling usually costs you edge quality for no benefit.

Can I use it on video frames? Frame by frame, yes, and the quality per frame is good. Temporal stability is the problem: independently segmented frames flicker at the edges. You need a smoothing pass across frames or a video-native matting model if the result has to hold up in motion, which is worth budgeting for alongside whatever video generation endpoint produced the footage.

Wrapping up

BiRefNet is the sensible default for programmatic background removal right now, for one concrete reason: it preserves edges that older segmentation models destroy, at resolutions you actually shoot at. Adopting it is less about the model call than the surrounding decisions, which variant per surface, matting versus binary, cache on the mask, segment before upscale, and a confidence gate so the hard five percent reaches a human.

If you want to see the call shape and its parameters before writing any code, the endpoint documentation at wireflow.ai/birefnet-api lays out the inputs and outputs, and the same pipeline thinking carries over to adjacent tasks like generating custom backgrounds for product photos once you have the subject cleanly isolated.