MODNet Portrait Matting

MODNET_PORTRAIT_MATTING

Cuts people out of photos with a soft alpha matte that keeps hair and fine edges, and returns the cutout or the matte as a PNG.

Utilitiesmattingportraitbackground-removalalpha-matteapache-2.0Fine-tunable
MODNet Portrait Matting: Utilities model on Kainguru

About MODNet Portrait Matting

Capabilities

  • Soft edges — an alpha matte with partial transparency at hair and other fine edges
  • Cutout or matte — the people on a transparent background (RGBA PNG), or the matte alone (grayscale PNG)
  • Full-size results — the cutout or matte has the photo's own width and height
  • Fine-tunable — train it on your own photos and their mattes

Good to know

  • Made for photos of people: portraits, selfies and half-body shots. For objects and products, a general background remover works better.
  • Photos up to 30 MB and 40 megapixels, as PNG, JPEG or WebP. The network sees the photo with its shorter side at 512 pixels (a photo close to that size is used as it is) and at most 2048 pixels on its longer side; the matte is scaled back to the photo's size.
  • Fine-tuning takes one ZIP (at most 4 GB, 5000 entries, no password) with 4 to 1000 photos (each up to 30 MB and 40 megapixels), a matte for each and a manifest.jsonl with one line per pair: {"file": "photos/a.jpg", "matte": "mattes/a.png"}. Paths are relative to the manifest, and manifest.jsonl is at most 16 MB.
  • A matte has its photo's size as displayed (after its EXIF rotation): white for the person, black for the background and gray where both mix, as at hair; a matte with no person in it is refused. A cutout with a transparent background also works as a matte.
  • Training letterboxes every pair to 512 x 512 pixels and mirrors half of them; it stops after 10,000 steps or 9 hours, whichever comes first, and keeps what it learned.

Attribution

MODNet by Zhanghan Ke, Jiayu Sun, Kaican Li, Qiong Yan and Rynson W.H. Lau, released under the Apache License 2.0.

MODNet Portrait Matting inputs and output

NameTypeRequiredDefaultDetails
imagefileYes—The photo of one or more people: PNG, JPEG or WebP, at most 30 MB and 40 megapixels.
1–2048 characters · image/png, jpeg, webp
outputtextNocutoutcutout: the photo with the people kept and the background transparent (RGBA PNG); matte: the alpha matte, white = person (grayscale PNG).
Allowed: cutout, matte

Output. Returns PNG image: PNG at the photo's size: the cutout (RGBA, the background transparent) or the alpha matte (grayscale, white = person).

Run MODNet Portrait Matting with the API

Send your API key in the X-API-Key header. The run is asynchronous: submit it, then poll it until it finishes. Create an API key.

cURL
curl -X POST "https://api.kainguru.com/api/v1/executions?mlFlowId=MODNET_PORTRAIT_MATTING" \
  -H "X-API-Key: kg_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
  "input": {
    "image": "https://example.com/input.png",
    "output": "cutout"
  },
  "outputFormat": "json"
}'

# Poll until the execution reaches a terminal state
curl "https://api.kainguru.com/api/v1/executions/{executionId}" \
  -H "X-API-Key: kg_your_api_key"
Python
from kainguru import KainguruClient

with KainguruClient(api_key="kg_your_api_key", base_url="https://api.kainguru.com/api") as client:
    submitted = client.executions.execute(
        "MODNET_PORTRAIT_MATTING",
        {
          "image": "https://example.com/input.png",
          "output": "cutout"
        },
        output_format="json",
    )
    result = client.executions.await_completion(submitted.id)
    print(result.execution.output)

Fine-tune MODNet Portrait Matting

You can fine-tune MODNet Portrait Matting on your own data and run the tuned model through the same API. Dataset: One ZIP (at most 4 GB, no password) with 4 to 1000 photos, an alpha matte for each and a manifest.jsonl with one {"file", "matte"} line per pair. More rules: Good to know, in the README.

The steps are in the docs.

MODNet Portrait Matting pricing

Price per image: 0.0874 credits ($0.000874). The price is shown again before every run; all prices are on the pricing page.

Licence and source

Rembg U2-Net: Utilities model on Kainguru
Utilities

Rembg U2-Net

Fast background removal that returns the subject of a photo on a transparent background, or as a black-and-white mask.

Fine-tunable
SAM 2.1 Hiera Large: Utilities model on Kainguru
Utilities

SAM 2.1 Hiera Large

Click or draw a box on an object and get back a mask outlining it.

Fine-tunable
Grounded SAM 2: Utilities model on Kainguru
Utilities

Grounded SAM 2

Outlines the things you name in a picture, such as "red car, person with a helmet", with a mask and a box for each one.

Fine-tunable
BiRefNet General: Utilities model on Kainguru
Utilities

BiRefNet General

Removes the background from a photo and returns the subject on a transparent background, or as a black-and-white mask.

Fine-tunable
Depth Anything v2 Small: Utilities model on Kainguru
Utilities

Depth Anything v2 Small

Turns any photo into a depth map that shows which parts are near and which are far.

Fine-tunable
DWPose Wholebody: Utilities model on Kainguru
Utilities

DWPose Wholebody

Finds every person in a photo and returns 133 keypoints for each: body, feet, face and both hands, as JSON in the…

Fine-tunable