Instructions to use moonbox-dev/BEN2-TensorRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- TensorRT
How to use moonbox-dev/BEN2-TensorRT with TensorRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- BEN2
How to use moonbox-dev/BEN2-TensorRT with BEN2:
import requests from PIL import Image from ben2 import AutoModel url = "https://huggingface.co/datasets/mishig/sample_images/resolve/main/teapot.jpg" image = Image.open(requests.get(url, stream=True).raw) model = AutoModel.from_pretrained("moonbox-dev/BEN2-TensorRT") model.to("cuda").eval() foreground = model.inference(image) - Notebooks
- Google Colab
- Kaggle
# No code snippets available yet for this library.
# To use this model, check the repository files and the library's documentation.
# Want to help? PRs adding snippets are welcome at:
# https://github.com/huggingface/huggingface.jsBEN2 Base TensorRT
TensorRT engines of BEN2 Base, made by Moonbox for NVIDIA RTX GPUs. Each precision is one folder holding the
engine, trt_runner.py (a small numpy runner: TensorRT plus Polygraphy, no PyTorch) and a script that does the rest
of the pipeline. What the engine takes and returns is under How to run.
Compatibility. The engines are built hardware compatible (Ampere and newer: RTX 30, 40 and 50 series) and
version compatible (any TensorRT 10.x from 10.13 on, through its lean runtime). That costs a little speed against an
engine built for one GPU, but no rebuild is needed. Install the runtime with
pip install "tensorrt-cu12>=10.13,<11" "tensorrt-lean-cu12>=10.13,<11" polygraphy numpy.
Files
The last column is the benchmark (details under How to run).
| Precision | File | Size | sha256 | Benchmark |
|---|---|---|---|---|
| fp16 | fp16/config.json |
0.0 kB | 5b1a91adebb1 |
weakly typed: vs the fp16 ONNX IoU 0.9983, MAE 0.0003; DIS5K MAE 0.0244, S 0.937; 370 ms a picture (ONNX fp16 445 ms) |
| fp16 | fp16/cutout_trt.py |
1.7 kB | 5c0a5d2a60e5 |
weakly typed: vs the fp16 ONNX IoU 0.9983, MAE 0.0003; DIS5K MAE 0.0244, S 0.937; 370 ms a picture (ONNX fp16 445 ms) |
| fp16 | fp16/model.engine |
490.2 MB | 2d4ac960f6f3 |
weakly typed: vs the fp16 ONNX IoU 0.9983, MAE 0.0003; DIS5K MAE 0.0244, S 0.937; 370 ms a picture (ONNX fp16 445 ms) |
| fp16 | fp16/trt_runner.py |
2.2 kB | efa8ba97631b |
weakly typed: vs the fp16 ONNX IoU 0.9983, MAE 0.0003; DIS5K MAE 0.0244, S 0.937; 370 ms a picture (ONNX fp16 445 ms) |
How to run
BEN2 (BEN2_Base, background erase network) as a TensorRT engine built from the official fp16 ONNX graph.
import sys
from PIL import Image
sys.path.insert(0, "fp16")
import json, cutout_trt
from trt_runner import Engine
e = Engine("fp16/model.engine")
cfg = json.load(open("fp16/config.json"))
img = Image.open("photo.jpg").convert("RGB")
img.putalpha(cutout_trt.matte(e, img, cfg)); img.save("cutout.png")
Or python fp16/cutout_trt.py photo.jpg cutout.png. The engine takes the picture resized to 1024 x 1024, RGB in 0-1
without normalization, as [1, 3, 1024, 1024]. It returns the matte [1, 1, 1024, 1024], rescaled to 0-1 as the
original's onnx_run.py does. cutout_trt.py resizes the matte back to the picture's size.
Precisions. fp16/ is built weakly typed: TensorRT chooses each layer's precision itself, fp16 where it can,
fp32 where it must. A strongly typed build that keeps every fp16 tensor as the ONNX graph has it overflows inside
TensorRT's fused kernels: every output was NaN. No int8: TensorRT's int8 needs calibrated activations.
Benchmark.
- Data: the DIS5K test sets, 250 pictures (DIS-TE1..4 and DIS-VD, 50 each), on an RTX 3080 Laptop GPU.
- Reference: the official fp16 ONNX graph, which is this model's own release (there is no fp32 one).
- Fidelity: agreement with that reference's mattes: IoU of the masks at 0.5, mean absolute difference.
- Quality: MAE and S-measure against the ground truth (PySODMetrics, masks shrunk to at most 2048 px).
- Pass mark: MAE <= 0.005 and IoU >= 0.98 against the reference, as for this model's other builds.
| vs the fp16 ONNX: IoU / MAE | DIS5K MAE / S-measure | ms a picture | peak GPU memory | |
|---|---|---|---|---|
| ONNX fp16 (reference) | - | 0.0244 / 0.937 | 445 | 4515 MiB |
TensorRT fp16/ |
0.9983 / 0.0003 | 0.0244 / 0.937 | 370 | 2472 MiB |
The times are medians per picture, including the resizing to and from 1024 x 1024.
How these were made
From BEN2 Base at revision e48a20765f: each engine was built with TensorRT's builder from Moonbox's published
ONNX graph of the same model and precision. The network is strongly typed: every tensor keeps the precision the
ONNX graph gives it, so an fp16 engine computes what the fp16 ONNX build computes. Built on an RTX 3080 Laptop GPU,
it was benchmarked against the original before upload, and the sha256 column above is what the files must hash to.
License
MIT. These files are a derivative of the original model and carry its license: it is copied in this repository, and it governs your use of these files. The original model card follows unchanged.
Original model card
BEN2: Background Erase Network
Overview
BEN2 (Background Erase Network) introduces a novel approach to foreground segmentation through its innovative Confidence Guided Matting (CGM) pipeline. The architecture employs a refiner network that targets and processes pixels where the base model exhibits lower confidence levels, resulting in more precise and reliable matting results. This model is built on BEN:
BEN2 access
BEN2 was trained on the DIS5k and our 22K proprietary segmentation dataset. Our enhanced model delivers superior performance in hair matting, 4K processing, object segmentation, and edge refinement. Our Base model is open source. To try the full model through our free web demo or integrate BEN2 into your project with our API:
- π backgrounderase.com
Contact us
- For access to our commercial model email us at sales@backgrounderase.com
- Our website: https://backgrounderase.com/
- Follow us on X: https://x.com/PramaResearch/
Installation
pip install -e "git+https://github.com/PramaLLC/BEN2.git#egg=ben2"
Quick start code
from ben2 import BEN_Base
from PIL import Image
import torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
file = "./image.png" # input image
model = BEN_Base.from_pretrained("PramaLLC/BEN2")
model.to(device).eval()
image = Image.open(file)
foreground = model.inference(image, refine_foreground=False,) #Refine foreground is an extract postprocessing step that increases inference time but can improve matting edges. The default value is False.
foreground.save("./foreground.png")
Batch image processing
from ben2 import BEN_Base
from PIL import Image
import torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = BEN_Base.from_pretrained("PramaLLC/BEN2")
model.to(device).eval()
file1 = "./image1.png" # input image1
file2 = "./image2.png" # input image2
image1 = Image.open(file1)
image2 = Image.open(file2)
foregrounds = model.inference([image1, image2]) # We recommend that the batch size not exceed 3 for consumer GPUs as there are minimal inference gains due to our custom batch processing for the MVANet decoding steps.
foregrounds[0].save("./foreground1.png")
foregrounds[1].save("./foreground2.png")
BEN2 video segmentation
Video Segmentation
sudo apt update
sudo apt install ffmpeg
from ben2 import BEN_Base
from PIL import Image
import torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
video_path = "/path_to_your_video.mp4"# input video
model = BEN_Base.from_pretrained("PramaLLC/BEN2")
model.to(device).eval()
model.segment_video(
video_path= video_path,
output_path="./", # Outputs will be saved as foreground.webm or foreground.mp4. The default value is "./"
fps=0, # If this is set to 0 CV2 will detect the fps in the original video. The default value is 0.
refine_foreground=False, #refine foreground is an extract postprocessing step that increases inference time but can improve matting edges. The default value is False.
batch=1, # We recommended that batch size not exceed 3 for consumer GPUs as there are minimal inference gains. The default value is 1.
print_frames_processed=True, #Informs you what frame is being processed. The default value is True.
webm = False, # This will output an alpha layer video but this defaults to mp4 when webm is false. The default value is False.
rgb_value= (0, 255, 0) # If you do not use webm this will be the RGB value of the resulting background only when webm is False. The default value is a green background (0,255,0).
)
RMBG 2.0 did not preserve the DIS 5k validation dataset
- Downloads last month
- -
Model tree for moonbox-dev/BEN2-TensorRT
Base model
PramaLLC/BEN2





