Skip to content
yakhyoPublic

About

Portrait matting with PP-Matting in PyTorch and ONNX Runtime, from PaddleSeg's released weights

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

PP-Matting: Portrait Matting in PyTorch and ONNX

Downloads License

Tip

The models in this repository are integrated into UniFace, an all-in-one face analysis toolkit. PyPI Version GitHub Stars

PyTorch and ONNX Runtime inference for the PaddleSeg portrait matting models (PP-Matting paper). Works on images, videos and webcam feeds.

PP-Matting wiping from the input photo to the matted portrait on a new background

Input, alpha matte and composite for a portrait with curly hair Input, alpha matte and composite for a portrait with flyaway hair

Mattes predicted by ppmatting_v2.onnx. Photos from UniFace's demo set.

Models

PyTorch checkpoints and the ONNX exports built from them. Download via bash download.sh or from Releases.

Model Backbone Params SAD PyTorch ONNX
PP-MattingV2 STDC1 8.95M 40.59 pt onnx
PP-Matting HRNet-W18 24.5M 31.56 pt onnx

The weights are PaddleSeg's released human_512 weights converted to PyTorch, and the ONNX files reproduce PaddleSeg's published scores. PP-MattingV2 is the fast one, PP-Matting the accurate one (SAD on PPM-AIM-195, lower is better). Both take an RGB image in [-1, 1] with sides that are multiples of 32 and return an alpha matte in [0, 1]; the scripts handle the resizing.

Installation

pip install -r requirements.txt
bash download.sh  # Download model weights

Usage

CLI

# PyTorch
python main.py --source assets/inputs/src_img1.jpg --output results/
python main.py --source video.mp4 --model ppmatting_v1 --background bg.jpg --output out.mp4
python main.py --source 0

# ONNX Runtime
python onnx_inference.py --source assets/inputs/src_img1.jpg --weights weights/ppmatting_v2.onnx --output results/
python onnx_inference.py --source video.mp4 --weights weights/ppmatting_v1.onnx --background bg.jpg --output out.mp4
python onnx_inference.py --source 0 --weights weights/ppmatting_v2.onnx

An image gives the matte, a green-screen composite and an RGBA cutout. A video or webcam feed is composited over --background, or over green.

Python API

import cv2
from model import PPMattingONNX

model = PPMattingONNX('weights/ppmatting_v2.onnx')
alpha = model.predict(cv2.imread('assets/inputs/src_img1.jpg'))  # (H, W) float32 in [0, 1]

Exporting Your Own Weights

onnx_export.py converts a .pt checkpoint to ONNX with dynamic batch, height and width.

python onnx_export.py --model ppmatting_v2 --simplify
python onnx_export.py --model ppmatting_v1 --simplify

Reference

License

Apache License 2.0

About

Portrait matting with PP-Matting in PyTorch and ONNX Runtime, from PaddleSeg's released weights

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages