MODNet — LiteRT (trimap-free portrait matting, GPU)

On-device real-time portrait matting running fully on the LiteRT CompiledModel GPU delegate (no CPU fallback). MODNet (AAAI 2022) predicts a soft alpha matte for a person — no trimap, no green screen — for background blur/replace (video calls, virtual backgrounds). ~79 ms/frame on a Pixel 8a.

  • Architecture: MODNet — MobileNetV2 low-res branch + high-res + fusion branches (pure CNN).
  • Weights: ZHKKKe/MODNet · Apache-2.0 · ~6.5 M params.
  • Size: 26 MB.

MODNet portrait matting

I/O

  • Input: [1, 3, 512, 512] NCHW, RGB, normalized to [-1, 1] ((x/255 - 0.5) / 0.5).
  • Output: [1, 1, 512, 512] soft alpha matte in [0, 1] (composite: fg·α + bg·(1-α)).

GPU conversion

MODNet is a pure CNN with align_corners=False interpolation. Two re-authoring patches make it a fully GPU-compatible graph — 0 tensors of rank > 4, 0 banned ops:

  1. SE block Linear1×1 conv — the stock squeeze-excite pool → Linear → view(b,c,1,1) → x*w confuses the NCHW↔NHWC layout (mul broadcast mismatch); 1×1 convs on the pooled tensor are identical and NCHW-clean.
  2. fp16-safe hierarchical-mean InstanceNorm — MODNet's IBNorm runs InstanceNorm2d over up to 512×512 spatial; on the Mali GPU (fp16) the variance sum(dd²) overflows (≫ 65504) and the matte degrades (halos, blotchy interior, corr 0.94). Computing the spatial mean via a cascade of /2 average-pools (magnitude-bounded, exact for power-of-2) + dd·rsqrt(mean(dd²)+eps) restores it to GPU corr 0.99994 with clean edges.

CPU-exact vs PyTorch (corr 0.99999999999); device Mali GPU corr 0.99994.

Minimal usage

Kotlin (Android, LiteRT CompiledModel GPU)

val options = CompiledModel.Options(Accelerator.GPU)
val model = CompiledModel.create(context.assets, "modnet.tflite", options, null)
val inBufs = model.createInputBuffers()
val outBufs = model.createOutputBuffers()

inBufs[0].writeFloat(inputNCHW)          // [1,3,512,512], RGB, (x/255-0.5)/0.5
model.run(inBufs, outBufs)
val alpha = outBufs[0].readFloat()       // [512*512] soft matte in [0,1]
// composite:  out = fg*alpha + bg*(1-alpha)

Python (LiteRT / ai-edge-litert)

from ai_edge_litert.interpreter import Interpreter
import numpy as np

it = Interpreter(model_path="modnet.tflite"); it.allocate_tensors()
inp, out = it.get_input_details(), it.get_output_details()
x = ((img[None].transpose(0,3,1,2) / 255.0 - 0.5) / 0.5).astype(np.float32)  # [1,3,512,512]
it.set_tensor(inp[0]["index"], x); it.invoke()
alpha = it.get_tensor(out[0]["index"])[0, 0]   # [512,512] in [0,1]

Conversion

Converted with litert-torch (build_modnet.py): loads the trained MODNet weights, applies the two patches (SE 1×1-conv, SafeInstanceNorm), and exports.

Performance

Measured on a Pixel 8a (Tensor G3, Android 16) with the standard TFLite benchmark_model tool — 10 warm-up runs then 50 timed runs, reported as the tool's mean.

Runtime Backend Graph on GPU Latency
LiteRT CompiledModel (LITERT_CL) GPU ~79 ms
TFLite benchmark_model (TfLiteGpuDelegateV2) GPU (OpenCL) 551 / 551 59.6 ms
TFLite benchmark_model CPU (XNNPACK, 4 threads) 418.9 ms

The two GPU rows are different runtimes, not a contradiction. The LITERT_CL figure is the one recorded when this model shipped, taken through LiteRT's own CompiledModel accelerator — the path the Kotlin sample app and the LiteRT API use. The TfLiteGpuDelegateV2 figure is the classic TFLite OpenCL delegate, measured with a tool anyone can download and re-run. They agree on how much of the graph the GPU takes; they disagree on speed, and the classic delegate is the slower of the two here. Read the TfLiteGpuDelegateV2 row as a reproducible floor, not as this model's speed on LiteRT.

Snapdragon NPU (Hexagon)

The NPU is 1.70x faster than the GPU (10.48 ms against 17.83 ms) and loads 8.07x faster (126 ms against 1016 ms).

backend inference (median / min) load
NPU (Hexagon v81) 10.48 ms / 10.41 ms 126 ms
GPU (Adreno) 17.83 ms / 16.99 ms 1016 ms

Measured on a Samsung Galaxy S26 (Snapdragon 8 Elite Gen 5 / SM8850, Hexagon v81, Android 16), LiteRT CompiledModel 2.2.0, one accelerator per process, 5 warm-up runs then N=50 timed runs, median reported. Every run held thermal status NONE throughout. Headroom 0.67, where 1.0 is the throttling threshold.

The NPU rows here ran artifacts compiled ahead of time for SM8850 with QAIRT 2.47.0; the GPU rows ran the published files as they are. LiteRT can also compile for the NPU on the device at first load, which is what lets you ship the published file unchanged — that path and the ten runtime libraries it needs are in the NPU recipe, and we did not measure it here. GPU wiring is in the GPU recipe.

License

Apache-2.0 (MODNet / ZHKKKe/MODNet).

Downloads last month
138
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Collection including litert-community/MODNet-LiteRT

Paper for litert-community/MODNet-LiteRT