Developer docs / AI Fabric tutorials

Batch background removal on GPU

  • GPU
  • Batch / map
  • Image DSL
  • Volume

Cutting the background out of a product photo means predicting, for every pixel, is this the subject or not: a segmentation task. On a CPU that runs many seconds per image; on a GPU it's a fraction of a second. And when you have a folder of them, AI Fabric can run the whole batch at once, one container per image.

In this tutorial you'll cut backgrounds from a batch of images with BRIA's RMBG-1.4 model on RTX A4500 GPUs, fanned out with .map(). Each result is a transparent RGBA PNG (the subject on a clear background), and the weights are cached on a Volume so the download happens once. Per-minute billing means an idle fleet costs nothing.

Before you start

You need the AI Fabric CLI installed and authenticated. Run blade setup once. The function asks for a single RTX A4500 per image and builds on fabric/torch, the catalog image with CUDA, PyTorch and Python 3.12 already baked in. RMBG-1.4 ships its modeling code on the Hub, so you'll load it with trust_remote_code.

Set up the app, Volume, and image

Create a file called bg_removal_batch.py. Declare the App, a Volume to cache weights, and the container image. One detail matters: transformers is pinned to 4.46.3 because RMBG-1.4's Hub modeling code predates the tied-weights refactor in newer releases. Leave it unpinned and the model fails to load.

bg_removal_batch.py
import time

import bladekit

app = bladekit.app("bg-removal-batch")

# Cache HF model weights across runs (downloaded once, reused forever).
hf_cache = bladekit.Volume.from_name("hf-cache", create_if_missing=True)

# fabric/torch = CUDA + PyTorch + Python 3.12 (AI Fabric catalog). RMBG-1.4 ships its
# modeling code on the Hub (trust_remote_code); we add transformers + Pillow and
# do the pre/post-processing by hand so we don't need torchvision.
image = (
    bladekit.Image.from_catalog("fabric/torch")
    # transformers pinned <4.50: RMBG-1.4's Hub modeling code predates the
    # tied-weights refactor (newer transformers expects all_tied_weights_keys).
    .pip_install("transformers==4.46.3", "pillow", "numpy", "scikit-image")
    .env({"HF_HOME": "/hf-cache"})
)

Pick the model and sample images

Name the RMBG-1.4 checkpoint, the model's input size, and a few sample image URLs. Like the upscaling tutorial, the samples are fetched inside the container, so there are no local files to provide.

Python
MODEL_ID = "briaai/RMBG-1.4"
SIZE = 1024  # model input size

# A few small sample images from Lorem Picsum (reliable, any size, no hotlink
# limits). Each is fetched inside the container; NO local files to provide.
SAMPLE_URLS = [
    "https://picsum.photos/seed/forge1/512",
    "https://picsum.photos/seed/forge2/512",
    "https://picsum.photos/seed/forge3/512",
]

_model = None

Load the model once per container

Request the GPU, image, and Volume on the function. The _model global keeps RMBG-1.4 resident: the first call loads it onto the GPU and every later call reuses it. RMBG-1.4 is loaded with trust_remote_code=True because its architecture lives in the Hub repo rather than in the transformers library itself.

Python
@app.function(
    gpu="RTX-A4500",
    image=image,
    timeout=900,
    volumes={"/hf-cache": hf_cache},
)
def remove_bg(url: str) -> dict:
    """Download one image, cut its background on the GPU, return a RGBA PNG + timing."""
    global _model
    import io
    import time as _t
    import urllib.request

    import numpy as np
    import torch
    import torch.nn.functional as F
    from PIL import Image as PILImage
    from transformers import AutoModelForImageSegmentation

    device = "cuda" if torch.cuda.is_available() else "cpu"

    if _model is None:
        t_load = _t.perf_counter()
        _model = AutoModelForImageSegmentation.from_pretrained(
            MODEL_ID, trust_remote_code=True
        ).to(device).eval()
        print(f"[remote] model loaded on {device} in {_t.perf_counter() - t_load:.1f}s")

Fetch, preprocess, and segment

Each call downloads its own image, then preprocesses by hand: resize to the model's SIZE, scale to [0,1], and normalize. Doing this manually avoids pulling in torchvision and keeps the image lean. The segmentation runs under torch.no_grad(), and torch.cuda.synchronize() makes the timing measure real GPU work.

Python
    # Fetch the sample image (no local files required).
    req = urllib.request.Request(url, headers={"User-Agent": "fabric-tutorial/1.0"})
    with urllib.request.urlopen(req, timeout=30) as resp:
        raw = resp.read()
    src = PILImage.open(io.BytesIO(raw)).convert("RGB")
    W, H = src.size

    # Manual preprocess: HWC uint8 -> 1,C,SIZE,SIZE float, normalized (mean .5/std 1).
    arr = np.asarray(src).astype("float32")
    t = torch.from_numpy(arr).permute(2, 0, 1).unsqueeze(0)
    t = F.interpolate(t, size=(SIZE, SIZE), mode="bilinear", align_corners=False)
    t = ((t / 255.0) - 0.5) / 1.0
    t = t.to(device)

    # Segment on the GPU and time just the inference.
    t0 = _t.perf_counter()
    with torch.no_grad():
        result = _model(t)
    if device == "cuda":
        torch.cuda.synchronize()
    gpu_seconds = _t.perf_counter() - t0

Build the transparent cutout

The model returns a foreground mask. Resize it back to the original dimensions, normalize it to the 0–255 range, and use it as the alpha channel stacked onto the original RGB, which is what turns the result into a transparent cutout. Return a small dict of facts (size, byte count, foreground fraction, GPU time) so the batch results stay light.

Python
    # Postprocess: take the final mask, resize back to (H,W), normalize to 0-255.
    mask = result[0][0] if isinstance(result, (list, tuple)) else result
    mask = F.interpolate(mask, size=(H, W), mode="bilinear", align_corners=False)
    mask = mask.squeeze().float().cpu()
    mask = (mask - mask.min()) / (mask.max() - mask.min() + 1e-8)
    alpha = (mask * 255.0).round().byte().numpy()

    # Compose RGBA: original RGB + predicted alpha.
    rgba = np.dstack([np.asarray(src), alpha]).astype("uint8")
    out_img = PILImage.fromarray(rgba, mode="RGBA")
    buf = io.BytesIO()
    out_img.save(buf, format="PNG")
    png = buf.getvalue()

    fg_frac = float((alpha > 127).mean())
    return {
        "url": url,
        "size": [W, H],
        "png_bytes": len(png),
        "foreground_fraction": round(fg_frac, 3),
        "gpu_seconds": round(gpu_seconds, 3),
        "device": device,
    }

Fan the batch out with .map()

The local_entrypoint runs on your laptop. remove_bg.map(SAMPLE_URLS, ...) launches one GPU container per URL, all at once, and collects the results in input order. return_exceptions=True keeps a single bad URL from cancelling the rest of the batch.

Python
@app.local_entrypoint()
def main():
    print(f"Removing backgrounds from {len(SAMPLE_URLS)} images, "
          f"fanning out across the fleet...\n")

    t0 = time.perf_counter()
    results = list(remove_bg.map(SAMPLE_URLS, return_exceptions=True))
    wall = time.perf_counter() - t0

Run it

Run the program with the AI Fabric CLI:

Terminal
blade run bg_removal_batch.py

Output
Cut backgrounds from 3/3 images in ~Ns wall (avg ~0.1s GPU/image, all in parallel).

The first run downloads the RMBG-1.4 weights into the Volume; later runs skip it. Because the containers ran at once and billing is per started minute, a batch of product photos costs a few cents and an idle fleet costs nothing.

Take it for a spin

Everything above in one file, ready to run.

bg_removal_batch.py
"""Batch background removal on the GPU fleet.

Fetch a handful of small public sample images, cut the background out of each one
on an RTX-A4500 with BRIA's RMBG-1.4 segmentation model, and fan the batch out
across the fleet with ``.map()``: one GPU container per image, all at once. Each
result is a transparent RGBA PNG (the subject on a clear background).

    blade run bg_removal_batch.py

RMBG-1.4 weights (~170 MB) download once from the Hugging Face Hub and are cached
on a Volume, so later runs skip the download. The model fits easily in 20 GB.
"""

import time

import bladekit

app = bladekit.app("bg-removal-batch")

# Cache HF model weights across runs (downloaded once, reused forever).
hf_cache = bladekit.Volume.from_name("hf-cache", create_if_missing=True)

# fabric/torch = CUDA + PyTorch + Python 3.12 (AI Fabric catalog). RMBG-1.4 ships its
# modeling code on the Hub (trust_remote_code); we add transformers + Pillow and
# do the pre/post-processing by hand so we don't need torchvision.
image = (
    bladekit.Image.from_catalog("fabric/torch")
    # transformers pinned <4.50: RMBG-1.4's Hub modeling code predates the
    # tied-weights refactor (newer transformers expects all_tied_weights_keys).
    .pip_install("transformers==4.46.3", "pillow", "numpy", "scikit-image")
    .env({"HF_HOME": "/hf-cache"})
)

MODEL_ID = "briaai/RMBG-1.4"
SIZE = 1024  # model input size

# A few small sample images from Lorem Picsum (reliable, any size, no hotlink
# limits). Each is fetched inside the container, so there are NO local files to provide.
SAMPLE_URLS = [
    "https://picsum.photos/seed/forge1/512",
    "https://picsum.photos/seed/forge2/512",
    "https://picsum.photos/seed/forge3/512",
]

_model = None


@app.function(
    gpu="RTX-A4500",
    image=image,
    timeout=900,
    volumes={"/hf-cache": hf_cache},
)
def remove_bg(url: str) -> dict:
    """Download one image, cut its background on the GPU, return a RGBA PNG + timing."""
    global _model
    import io
    import time as _t
    import urllib.request

    import numpy as np
    import torch
    import torch.nn.functional as F
    from PIL import Image as PILImage
    from transformers import AutoModelForImageSegmentation

    device = "cuda" if torch.cuda.is_available() else "cpu"

    if _model is None:
        t_load = _t.perf_counter()
        _model = AutoModelForImageSegmentation.from_pretrained(
            MODEL_ID, trust_remote_code=True
        ).to(device).eval()
        print(f"[remote] model loaded on {device} in {_t.perf_counter() - t_load:.1f}s")

    # Fetch the sample image (no local files required).
    req = urllib.request.Request(url, headers={"User-Agent": "fabric-tutorial/1.0"})
    with urllib.request.urlopen(req, timeout=30) as resp:
        raw = resp.read()
    src = PILImage.open(io.BytesIO(raw)).convert("RGB")
    W, H = src.size

    # Manual preprocess: HWC uint8 -> 1,C,SIZE,SIZE float, normalized (mean .5/std 1).
    arr = np.asarray(src).astype("float32")
    t = torch.from_numpy(arr).permute(2, 0, 1).unsqueeze(0)
    t = F.interpolate(t, size=(SIZE, SIZE), mode="bilinear", align_corners=False)
    t = ((t / 255.0) - 0.5) / 1.0
    t = t.to(device)

    # Segment on the GPU and time just the inference.
    t0 = _t.perf_counter()
    with torch.no_grad():
        result = _model(t)
    if device == "cuda":
        torch.cuda.synchronize()
    gpu_seconds = _t.perf_counter() - t0

    # Postprocess: take the final mask, resize back to (H,W), normalize to 0-255.
    mask = result[0][0] if isinstance(result, (list, tuple)) else result
    mask = F.interpolate(mask, size=(H, W), mode="bilinear", align_corners=False)
    mask = mask.squeeze().float().cpu()
    mask = (mask - mask.min()) / (mask.max() - mask.min() + 1e-8)
    alpha = (mask * 255.0).round().byte().numpy()

    # Compose RGBA: original RGB + predicted alpha.
    rgba = np.dstack([np.asarray(src), alpha]).astype("uint8")
    out_img = PILImage.fromarray(rgba, mode="RGBA")
    buf = io.BytesIO()
    out_img.save(buf, format="PNG")
    png = buf.getvalue()

    fg_frac = float((alpha > 127).mean())
    return {
        "url": url,
        "size": [W, H],
        "png_bytes": len(png),
        "foreground_fraction": round(fg_frac, 3),
        "gpu_seconds": round(gpu_seconds, 3),
        "device": device,
    }


@app.local_entrypoint()
def main():
    print(f"Removing backgrounds from {len(SAMPLE_URLS)} images, "
          f"fanning out across the fleet...\n")

    t0 = time.perf_counter()
    results = list(remove_bg.map(SAMPLE_URLS, return_exceptions=True))
    wall = time.perf_counter() - t0

    ok = []
    for url, res in zip(SAMPLE_URLS, results):
        name = url.rsplit("/", 1)[-1]
        if isinstance(res, Exception):
            print(f"  FAILED {name}: {res}")
            continue
        ok.append(res)
        print(
            f"  seed-{name}: {tuple(res['size'])} cutout, "
            f"{res['foreground_fraction'] * 100:.0f}% foreground, "
            f"{res['png_bytes'] / 1e6:.1f} MB RGBA PNG, "
            f"{res['gpu_seconds']}s on {res['device']}"
        )

    if ok:
        avg_gpu = sum(r["gpu_seconds"] for r in ok) / len(ok)
        print(
            f"\nCut backgrounds from {len(ok)}/{len(SAMPLE_URLS)} images in "
            f"{wall:.1f}s wall (avg {avg_gpu:.2f}s GPU/image, all in parallel)."
        )
        print(
            "On a CPU this segmentation runs many seconds PER image; the GPU does "
            "it in a fraction of a second, and the whole batch ran at once."
        )
        print(
            "Per-minute billing: a batch of product photos is a few cents, and an "
            "idle fleet costs nothing."
        )

Concepts used: GPU, Run, Volume

Contact the team

Loading the form…