MODNet: Trimap-Free Portrait Matting in Real Time

April 11, 2026 ยท View on GitHub

Downloads GitHub Repo stars License

Ported weights and clean inference codebase for MODNet: Real-Time Trimap-Free Portrait Matting via Objective Decomposition (AAAI 2022). Provides both PyTorch and ONNX Runtime inference for images, videos, and webcam feeds.

Tip

The models and functionality in this repository are integrated into UniFace โ€” an all-in-one face analysis toolkit.
PyPI Version GitHub Stars License

InputMatteGreen ScreenRGBA

Installation

pip install -r requirements.txt

Usage

Image Inference

python main.py --source image.jpg --model weights/modnet_photographic.pt --output results/

Image with Custom Background

python main.py --source image.jpg --model weights/modnet_photographic.pt --background bg.jpg --output results/

Video Inference

python main.py --source video.mp4 --model weights/modnet_photographic.pt --background bg.jpg --output out.mp4

Webcam Inference

python main.py --source 0 --model weights/modnet_webcam.pt

ONNX Inference

No PyTorch dependency -- only onnxruntime and opencv-python.

python onnx_inference.py --source image.jpg --model weights/modnet_photographic.onnx --output results/
python onnx_inference.py --source video.mp4 --model weights/modnet_photographic.onnx --background bg.jpg --output out.mp4
python onnx_inference.py --source 0 --model weights/modnet_webcam.onnx

ONNX Export

python -m utils.export_onnx --weights weights/modnet_photographic.pt --output weights/modnet_photographic.onnx
python -m utils.export_onnx --weights weights/modnet_webcam.pt --output weights/modnet_webcam.onnx --simplify

Model Weights

Weights ported from the official repository. Model weights are licensed under Apache 2.0 by the original authors.

ModelSizeDownload
MODNet Photographic25 MBPyTorch | ONNX
MODNet Webcam25 MBPyTorch | ONNX

Reference

Based on MODNet by Zhanghan Ke

License

Apache License 2.0