Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:PeterL1n BackgroundMattingV2 MattingBase

From Leeroopedia
Revision as of 16:18, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/PeterL1n_BackgroundMattingV2_MattingBase.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Image_Matting, Computer_Vision, Deep_Learning
Last Updated 2026-02-09 00:00 GMT

Overview

Concrete tool for coarse alpha matte prediction from source-background image pairs provided by model/model.py.

Description

MattingBase is the first-stage matting model that extends the Base encoder-decoder with 6 input channels (concatenated source + background) and 37 output channels (1 alpha + 3 foreground + 1 error + 32 hidden). It supports ResNet50, ResNet101, and MobileNetV2 encoder backbones. The foreground is predicted as a residual added to the source image.

Usage

Use for coarse-resolution matting when refinement is not needed, or as the first stage when training the full pipeline. For inference, only the first two outputs (alpha, foreground) are typically used.

Code Reference

Source Location

Signature

class MattingBase(Base):
    """
    Coarse global matting at reduced resolution.

    Args:
        backbone: str - one of ['resnet50', 'resnet101', 'mobilenetv2']
    """
    def __init__(self, backbone: str):
        super().__init__(backbone, in_channels=6, out_channels=(1 + 3 + 1 + 32))

    def forward(
        self,
        src: Tensor,  # (B, 3, H, W) source image, RGB, 0-1
        bgr: Tensor   # (B, 3, H, W) background image, RGB, 0-1
    ) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
        """
        Returns:
            pha: (B, 1, H, W) alpha matte, 0-1
            fgr: (B, 3, H, W) foreground RGB, 0-1
            err: (B, 1, H, W) error map, 0-1
            hid: (B, 32, H, W) hidden features for refiner
        """

Import

from model import MattingBase

I/O Contract

Inputs

Name Type Required Description
backbone str Yes Encoder backbone: 'resnet50', 'resnet101', or 'mobilenetv2'
src Tensor[B,3,H,W] Yes Source image (RGB, 0-1)
bgr Tensor[B,3,H,W] Yes Background image (RGB, 0-1)

Outputs

Name Type Description
pha Tensor[B,1,H,W] Alpha matte prediction, clamped 0-1
fgr Tensor[B,3,H,W] Foreground prediction (src + residual), clamped 0-1
err Tensor[B,1,H,W] Error map prediction, clamped 0-1
hid Tensor[B,32,H,W] Hidden encoding for refinement stage (ReLU activated)

Usage Examples

Training

from model import MattingBase

model = MattingBase(backbone='resnet50')
pha, fgr, err, hid = model(src, bgr)  # Use all 4 outputs for training

Inference

model = MattingBase(backbone='resnet50')
model.load_state_dict(torch.load('checkpoint.pth'), strict=False)
model.eval()

with torch.no_grad():
    pha, fgr = model(src, bgr)[:2]  # Only need alpha and foreground

Related Pages

Implements Principle

Requires Environment

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment