RMBG 2.0

Bria's background remover, which returns the subject on transparency and the mask on its own. It reads the image at a fixed size and gives the mask back at the size you sent, so the result drops straight onto the original. Its weights are gated, so this one needs your own Hugging Face token stored as HF_TOKEN.

Image generationl40sBriaBria RMBG license, non-commercialbriaai/RMBG-2.0

What runs

This is the whole app. The entry names the model and what serves it, and the build turns that into these files: there is nothing else to write.

model.py
"""RMBG 2.0, generated from a declarative template."""

from __future__ import annotations

import base64
import io

from pydantic import BaseModel, ConfigDict, Field

from runware_serverless import serve

MODEL = "briaai/RMBG-2.0"
REVISION = "2fd7b4b0f2393ddb39a267fb62df6c010928a6c6"
CACHE = "/weights"

# What the network reads, which is not what it answers about. The image is
# resized to this to be segmented and the mask is resized back, so the result
# keeps the dimensions that were sent in.
INPUT_SIZE = (1024, 1024)

# ImageNet statistics, which is what the backbone was trained against. They are
# not a knob: other values shift the input distribution and the edges go soft.
MEAN = (0.485, 0.456, 0.406)
STD = (0.229, 0.224, 0.225)


class Request(BaseModel):
    # An input this model does not read is a line the caller believed was doing
    # something, so it is refused rather than dropped. Without this, pydantic
    # ignores it: a width sent to a model of fixed size, or a misspelled seed,
    # both come back as a result that silently answered a different request.
    model_config = ConfigDict(extra="forbid")

    image: str = Field(description="The image to cut the subject out of, base64 encoded.")


class Removed(BaseModel):
    image: str = Field(description="The image with the background removed, PNG with an alpha channel.")
    mask: str = Field(description="The alpha channel on its own, as a grayscale PNG.")
    width: int
    height: int


@serve
class GeneratedBackgroundRemoval:
    def load(self) -> None:
        if hasattr(self, "model"):
            return

        # Imported here rather than at the top of the file. The build imports
        # this file with the platform's own dependencies, so nothing above
        # exists until a worker runs.
        import torch
        from torchvision import transforms
        from transformers import AutoModelForImageSegmentation

        self.torch = torch

        # The weights repository carries the architecture as Python beside the
        # tensors, so loading it is running it. transformers refuses to do that
        # without being told.
        self.model = AutoModelForImageSegmentation.from_pretrained(
            MODEL, revision=REVISION, cache_dir=CACHE, trust_remote_code=True
        )
        self.model.to("cuda")
        self.model.eval()
        self.model.half()

        self.transform = transforms.Compose([
            transforms.Resize(INPUT_SIZE),
            transforms.ToTensor(),
            transforms.Normalize(MEAN, STD),
        ])
        self.to_image = transforms.ToPILImage()

        self._run(self.transform, (8, 8))

    def predict(self, request: Request) -> Removed:
        image = self._decode(request.image)
        mask = self._run(self.transform, image.size, image)

        cut = image.copy()
        cut.putalpha(mask)

        return Removed(
            image=self._encode(cut),
            mask=self._encode(mask),
            width=image.width,
            height=image.height,
        )

    def _run(self, transform, size, image=None):
        """The mask for one image, at the size it was sent in.

        Called once with a placeholder at load so the first real request does not
        pay for the lazy allocations the first forward pass makes.
        """
        from PIL import Image

        subject = image if image is not None else Image.new("RGB", size, "black")
        batch = transform(subject).unsqueeze(0).to("cuda").half()

        with self.torch.no_grad():
            # The model answers with a list of maps, coarse to fine, and the last
            # one is the prediction. The others are what training supervised.
            prediction = self.model(batch)[-1].sigmoid().cpu()

        return self.to_image(prediction[0].squeeze()).resize(size)

    @staticmethod
    def _decode(encoded: str):
        from PIL import Image

        return Image.open(io.BytesIO(base64.b64decode(encoded))).convert("RGB")

    @staticmethod
    def _encode(image) -> str:
        buffer = io.BytesIO()
        image.save(buffer, format="PNG")

        return base64.b64encode(buffer.getvalue()).decode("ascii")
runware.yaml
configVersion: 1
id: rmbg-2-0
name: RMBG 2.0
source:
  type: code
  entry: model.py
compute:
  gpuType: l40s
build:
  requirements:
    - torch==2.14.1
    - torchvision==0.29.1
    - transformers==5.21.0
    - huggingface-hub==2.4.0
    - pillow==13.0.0
    - timm==1.0.31
    - kornia==0.9.0
    - einops==0.8.2
volumes:
  - /weights
secrets:
  - name: HF_TOKEN

Deploying

Deploy it as it is, or take the files above and deploy your own copy. Nothing you change reaches back to this page.

From a URL
runware serverless deploy --from https://runware.ai/docs/serverless/examples/rmbg-2-0.zip
From a clone
runware serverless deploy model.py \
  --id rmbg-2-0 \
  --gpu-type l40s \
  --requirement torch==2.14.1 \
  --requirement torchvision==0.29.1 \
  --requirement transformers==5.21.0 \
  --requirement huggingface-hub==2.4.0 \
  --requirement pillow==13.0.0 \
  --requirement timm==1.0.31 \
  --requirement kornia==0.9.0 \
  --requirement einops==0.8.2 \
  --volume /weights \
  --secret HF_TOKEN

Calling it

One request that works, against the app you just deployed. The fields are the ones the entry declares above, and anything it does not declare is refused rather than ignored.

import { createClient } from '@runware/sdk'

const client = await createClient({ apiKey: process.env.RUNWARE_API_KEY })

const task = await client.invoke({
  appId: 'rmbg-2-0',
  endpointPath: 'predict',
  payload: {
    request: {
      image: '<base64 of the image to cut out>'
    }
  }
})
import asyncio
import os

from runware import Runware


async def main():
    client = Runware(api_key=os.environ["RUNWARE_API_KEY"])
    try:
        task = await client.invoke(
            "rmbg-2-0",
            "predict",
            {
                "request": {
                    "image": "<base64 of the image to cut out>"
                }
            },
        )
    finally:
        await client.close()


asyncio.run(main())
curl https://api.serverless.runware.ai/v1/apps/rmbg-2-0/invoke-async/predict \
  -H "Authorization: Bearer $RUNWARE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "taskId": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
    "payload": {
      "request": {
        "image": "<base64 of the image to cut out>"
      }
    }
  }'
cat <<'EOF' | runware serverless apps invoke rmbg-2-0 predict -f -
{
  "request": {
    "image": "<base64 of the image to cut out>"
  }
}
EOF
{
  "taskId": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
  "payload": {
    "request": {
      "image": "<base64 of the image to cut out>"
    }
  }
}

What it costs

This entry is pinned to l40s, which is the card it was tested on rather than the only one it runs on: --gpu-typedeploys it somewhere else. You pay for that card by the second while a worker holds it, and for nothing else. Pricing covers what meters, and the rates are published per GPU-second.