DEV Community

shaojie gong
shaojie gong

Posted on

Background removal in the browser: my laptop's GPU allowed 16 storage buffers and the model wanted 17

I build MakeQuickWork, a set of free browser tools, on my own. This is about its Remove Background tool, and the first proper test run, which didn't go the way I expected.

The tool cuts the subject out of a photo without uploading it anywhere. Both models run in the tab with onnxruntime-web, and there are two of them because no single model fits every device:

  • Best is BiRefNet lite (MIT), an f16 build that runs on the graphics card through WebGPU. It's meant for any subject, people, products, pets, plants, and keeps fine hair and edges. About 140 MB the first time.
  • Fast is MODNet (Apache-2.0), a model made for people and portraits. It runs in any modern browser, phones included. About 26 MB the first time.

The browser caches whichever one you downloaded, so the second run doesn't fetch it again. The page picks Best when the browser has WebGPU and Fast otherwise.

The test file

For the demo I used a drawn product photo: a mug on a wooden table, a plant and a picture frame behind it. 1600×1200, 69 KB. A product shot with clutter in the back is exactly what Best is for.

My laptop runs Windows with Intel integrated graphics, and I tested in Chrome. WebGPU is available there, so the page chose Best. Then the model failed with this:

Too many storage buffers in shader. Current: 17, Max is 16
Enter fullscreen mode Exit fullscreen mode

What that error means

A WebGPU adapter reports limits, and one of them is how many storage buffers a single shader stage may bind. This Intel chip allows 16. One shader in the Best model needs 17. There's no flag to flip on the page side; it's what the hardware reports. You can see your own number in the console:

const adapter = await navigator.gpu.requestAdapter();
console.log(adapter.limits.maxStorageBuffersPerShaderStage);
Enter fullscreen mode Exit fullscreen mode

So "the browser has WebGPU" and "this model can run on this GPU" turned out to be two different questions. When Best can't run, the page switches to Fast by itself and tells you:

Best quality could not run on this device's graphics, so Fast was used. Fast works best on people.

I'd rather show that sentence than a spinner that never ends or a generic error.

What Fast did with a mug

The status line said "Done in 2 s". Take that with salt: I ran the site on localhost, so the one-time 26 MB model download was close to instant. On a real first visit it takes longer, depending on your connection.

The output was mug-on-table-no-bg.png, 1600 × 1200 px (same size as the input), 237.8 KB, with 81% of the pixels fully transparent. The mug came out cleanly, and the wall, the frame and the table were removed. The green plant on the left was kept as a second subject.

That plant is the known weak spot. Fast was trained for people, so on other subjects it can miss parts or keep extra objects. I haven't measured Best on this file, because Best never ran on this machine, so I'm not going to guess what it would have done.

Saving the same cut-out as a JPG on a white background gave a 46 KB file. The other background options are black, any colour, or your own image (scaled to fill, centred), and you can save as PNG (keeps transparency), WebP (transparency, smaller) or JPG (a transparent background becomes white).

Limits, plainly

  • Best needs WebGPU: recent Chrome or Edge on a computer, or Safari 26. Even then, some integrated GPUs don't have the resources for it, as above.
  • Fast is a portrait model. Products, pets and plants can come out with leftovers.
  • First run downloads about 140 MB (Best) or 26 MB (Fast).
  • It doesn't remove text from images; that's a different tool.

Checking that nothing is uploaded

The site is static (Astro on Cloudflare Pages) and the model runs locally, so there's no server receiving the photo. An automated test processes a file in each tool and fails on any POST or any request to a third party other than Google Analytics, and fails if an analytics event contains a file name. GA4 counts page views and a "tool used" event. You still need a connection to open the page and download the model.

If you've shipped a WebGPU model to the public: do you check adapter limits before loading, or try it and fall back like this? I'm not sure which ages better.

https://makequickwork.com/image/remove-background/

Top comments (3)

Collapse
 
alexshev profile image
Alex Shev •

This is a useful example of why browser ML needs capability discovery rather than assuming desktop-class GPU limits. A fallback plan that selects a smaller graph, tiles work, or switches execution paths should be based on the detected adapter limits and measured output quality. Reporting those constraints back to users also turns a mysterious failure into a concrete support signal.

Collapse
 
mp3sss31 profile image
mp3sss •

Running the model entirely in the tab is the right privacy call for an image tool, and the two-model fallback (heavy f16 path on WebGPU, lighter ONNX path anywhere else) mirrors the progressive-enhancement approach used for local-first desktop features. The buffer limit you hit is a classic browser GPU ceiling: WebGPU exposes a per-device resource budget that integrated GPUs enforce more strictly than discrete cards, so when a model's intermediate buffers exceed it the failure shows up as a resource allocation error rather than a clean model error. Worth checking whether splitting the heaviest activation into smaller tiles, or dropping that one layer to f32 with a smaller working set, keeps the fast path usable on constrained hardware while the MODNet fallback stays intact.

Collapse
 
skepagent profile image
SkepAgent •

我倾向于先查 limits 再加载,adapter.limits.maxStorageBuffersPerShaderStage 这种值在 requestAdapter 之后就能拿到,提前分流比等 shader 编译失败再兜底更干净。文中 16 对 17 这个例子很典型,集成显卡经常卡在这种不起眼的上限上。你把失败文案直接写成一句人话的做法也很好,至少用户知道是设备跑不动,而不是工具坏了。