Skip to content

Add BiRefNet module wrapper code#10

Closed
Warwlock wants to merge 2 commits intoedenaion:mainfrom
Warwlock:EZ-GUI
Closed

Add BiRefNet module wrapper code#10
Warwlock wants to merge 2 commits intoedenaion:mainfrom
Warwlock:EZ-GUI

Conversation

@Warwlock
Copy link
Copy Markdown

@Warwlock Warwlock commented Mar 10, 2026

What changes?

  • Created new file which is the wrapper script that inferences with BiRefNet model: ./BiRefNetModule/wrapper.py
  • Added run_birefnet and get_birefnet_usage_options functions to the clip_manager.py script to connect backend to GUI.
  • Added kornia package to requirements.txt and toml files.
  • .gitignore modified to ignore BiRefNetModule checkpoint folders.
  • Model downloads automatically handled within the wrapper script. Can be found at lines 67-82 in wrapper.py.

Code Documnetation

def run_birefnet(
    clips,
    usage="General",
    dilate_radius=0
)

run_birefnet: Starts inference with loaded clips.
usage: Model selection string which is selected from usage_to_weights_file (placed in wrapper script) dictionary. Dropdown selection can be directly passed to this.
dilate_radius: Mask expansion and contraction amount. I think range between -50 to 50 is ideal. Implemented as post-processor at line 177 inside wrapper.py file

def get_birefnet_usage_options(): # This converts `usage_to_weights_file` dictionary to keys list. Instead of including wrapper script to the frontend directly, using this makes it a little bit cleaner.
    return list(usage_to_weights_file.keys())

This is the model dictionary

Key values can be used to create dopdown values for GUI.

usage_to_weights_file = {
    "General": "BiRefNet",
    "General-dynamic": "BiRefNet_dynamic",
    "General-HR": "BiRefNet_HR",
    "General-Lite": "BiRefNet_lite",
    "General-Lite-2K": "BiRefNet_lite-2K",
    "General-reso_512": "BiRefNet_512x512",
    "Matting": "BiRefNet-matting",
    "Matting-dynamic": "BiRefNet_dynamic-matting",
    "Matting-HR": "BiRefNet_HR-Matting",
    "Matting-Lite": "BiRefNet_lite-matting",
    "Portrait": "BiRefNet-portrait",
    "DIS": "BiRefNet-DIS5K",
    "HRSOD": "BiRefNet-HRSOD",
    "COD": "BiRefNet-COD",
    "DIS-TR_TEs": "BiRefNet-DIS5K-TR_TEs",
    "General-legacy": "BiRefNet-legacy",
}

BiRefNet Model Zoo & Resource Guide:

Model ID Usage Resolution VRAM Estimate Recommended Use
1 General 1024 x 1024 Medium Balanced performance/quality
2 General-dynamic Dynamic* Variable Aspect-ratio preserving, variable size inputs
3 General-HR 2048 x 2048 High High-res VFX shots
4 General-Lite 1024 x 1024 Low Faster inference, lower detail
5 General-Lite-2K 2560 x 1440 High 1440p Ultra-wide / High-res sequences
6 General-reso_512 512 x 512 Very Low Very fast, low-detail drafts
7 Matting 1024 x 1024 Medium Hair/Fine edge detail, trimap-free matting
8 Matting-dynamic Dynamic* Variable Variable size fine edge matting
9 Matting-HR 2048 x 2048 High Production-grade fine matting
10 Matting-Lite 1024 x 1024 Low Fast inference for trimap-free hair/fine edges
11 Portrait 1024 x 1024 Medium Human subjects & close-ups
12 DIS 1024 x 1024 Medium Highly detailed structures (DIS5K dataset)
13 HRSOD 1024 x 1024 Medium Salient object detection
14 COD 1024 x 1024 Medium Camouflaged object detection
15 DIS-TR_TEs 1024 x 1024 Medium Specialized research tasks/benchmark
16 General-legacy 1024 x 1024 Medium Backward compatibility
  • Default model is General
  • Selecting a model will automatically adjust resolution for BiRefNet inference, it will not affect CorridorKey resolution!
  • Dynamic models doesn't change the resolution. It will process on the original resolution of the image.
  • "Note: Models listed as 'High' resolution require ~10GB+ VRAM. If you encounter OOM errors, consider using medium or small models."

@Warwlock Warwlock changed the title Add BiRefNet module wrapper and code Add BiRefNet module wrapper code Mar 10, 2026
- Add support for 3 new models: General-dynamic, Matting-dynamic, and Matting-Lite.
- Update resolution logic to support dynamic aspect ratios.
- Implement mask contraction (erosion) support with negative dilate_radius.
- Add `on_frame_complete` callback to track frame processing progress
@Warwlock
Copy link
Copy Markdown
Author

I have updated only the wrapper code to accomodate the changes:

  • Add support for 3 new models: General-dynamic, Matting-dynamic, and Matting-Lite.
  • Update resolution logic to support dynamic aspect ratios.
  • Implement mask contraction (erosion) support with negative dilate_radius.

I have also added callback for progress bar update in case you need:

  • Add on_frame_complete callback to track frame processing progress

I also modified the model zoo and updated my first comment.

edenaion added a commit that referenced this pull request Mar 15, 2026
- BiRefNet automatic alpha generation (16 model variants, download-on-demand)
  Adapted from Warwlock's PR #10
- Fix processed output color accuracy (remove match_luminance on CUDA + MLX)
- Fix processed viewer washout (remove global Reinhard tone map)
- Fix viewer/strip behavior on clip deletion (clear, left-neighbor, sync)
- Fix torch.compile hang after BiRefNet model switch
- Debug console stays on top
@edenaion
Copy link
Copy Markdown
Owner

Merged, thank you again!

@edenaion edenaion closed this Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants