--- base_model: schirrmacher/ormbg base_model_relation: quantized license: apache-2.0 pipeline_tag: image-segmentation library_name: tensorrt tags: - tensorrt - moonbox - background-removal - isnet - image-matting - ormbg - image-segmentation - portrait --- # ORMBG TensorRT TensorRT engines of [ORMBG](https://huggingface.co/schirrmacher/ormbg), 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`](https://huggingface.co/moonbox-dev/ormbg-TensorRT/blob/main/fp16/config.json) | 0.0 kB | `5b1a91adebb1` | vs fp32: IoU 0.9969, MAE 0.0005; DIS5K MAE 0.1461, S 0.720; 197 ms a picture (ONNX fp16 176 ms), 461 MiB | | fp16 | [`fp16/cutout_trt.py`](https://huggingface.co/moonbox-dev/ormbg-TensorRT/blob/main/fp16/cutout_trt.py) | 1.7 kB | `5c0a5d2a60e5` | vs fp32: IoU 0.9969, MAE 0.0005; DIS5K MAE 0.1461, S 0.720; 197 ms a picture (ONNX fp16 176 ms), 461 MiB | | fp16 | [`fp16/model.engine`](https://huggingface.co/moonbox-dev/ormbg-TensorRT/blob/main/fp16/model.engine) | 142.2 MB | `a6e9c7b72e89` | vs fp32: IoU 0.9969, MAE 0.0005; DIS5K MAE 0.1461, S 0.720; 197 ms a picture (ONNX fp16 176 ms), 461 MiB | | fp16 | [`fp16/trt_runner.py`](https://huggingface.co/moonbox-dev/ormbg-TensorRT/blob/main/fp16/trt_runner.py) | 2.2 kB | `efa8ba97631b` | vs fp32: IoU 0.9969, MAE 0.0005; DIS5K MAE 0.1461, S 0.720; 197 ms a picture (ONNX fp16 176 ms), 461 MiB | ## How to run ORMBG (trained for pictures of people) as a TensorRT engine built from Moonbox's fp16 ONNX graph of the official model. ```python 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 with no normalization (RGB in 0-1), as [1, 3, 1024, 1024]. It returns the matte as output 0 (the others are side outputs), rescaled to 0-1 as the original inference does. `cutout_trt.py` resizes the matte back to the picture's size. **Precisions.** fp16 only: the fp16 ONNX graph's weights and math (fp32 inputs and outputs). No int8: the ONNX int8 build is weight-only, while TensorRT's int8 needs calibrated activations. **Benchmark.** - **Data:** the [DIS5K](https://github.com/xuebinqin/DIS) test sets, 250 pictures (DIS-TE1..4 and DIS-VD, 50 each), on an RTX 3080 Laptop GPU. - **Fidelity:** agreement with the fp32 ONNX graph's mattes: IoU of the masks at 0.5, mean absolute difference. - **Quality:** MAE and S-measure against the ground truth (PySODMetrics, on masks shrunk to at most 2048 px). - **Pass mark:** MAE <= 0.005 and IoU >= 0.98 against fp32, as for this model's other builds. | | vs fp32: IoU / MAE | DIS5K MAE / S-measure | ms a picture | peak GPU memory | |---|---|---|---|---| | ONNX fp32 (reference) | - | 0.1462 / 0.720 | 197 | 1717 MiB | | ONNX fp16 | 0.9986 / 0.0003 | 0.1461 / 0.720 | 176 | 836 MiB | | TensorRT `fp16/` | 0.9969 / 0.0005 | 0.1461 / 0.720 | 197 | 461 MiB | The times are medians per picture, including the resizing to and from 1024 x 1024. The ONNX rows used ONNX Runtime's CUDA provider. ## How these were made From [ORMBG](https://huggingface.co/schirrmacher/ormbg) at revision `6253b31824`: 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 Apache-2.0. 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 # Open Remove Background Model (ormbg) [>>> DEMO <<<](https://huggingface.co/spaces/schirrmacher/ormbg) Join our [Research Discord Group](https://discord.gg/YYZ3D66t)! ![](examples/image/image01_no_background.png) This model is a **fully open-source background remover** optimized for images with humans. It is based on [Highly Accurate Dichotomous Image Segmentation research](https://github.com/xuebinqin/DIS). ## Inference ``` python ormbg/inference.py ``` ## Training Install dependencies: ``` conda env create -f environment.yaml conda activate ormbg ``` Replace dummy dataset with [training dataset](https://huggingface.co/datasets/schirrmacher/humans). ``` python3 ormbg/train_model.py ``` # Research I started training the model with synthetic images of the [Human Segmentation Dataset](https://huggingface.co/datasets/schirrmacher/humans) crafted with [LayerDiffuse](https://github.com/layerdiffusion/LayerDiffuse). However, I noticed that the model struggles to perform well on real images. Synthetic datasets have limitations for achieving great segmentation results. This is because artificial lighting, occlusion, scale or backgrounds create a gap between synthetic and real images. A "model trained solely on synthetic data generated with naïve domain randomization struggles to generalize on the real domain", see [PEOPLESANSPEOPLE: A Synthetic Data Generator for Human-Centric Computer Vision (2022)](https://arxiv.org/pdf/2112.09290). ### Next steps: - Expand dataset with synthetic and real images - Research on state of the art loss functions ### Latest changes (26/07/2024): - Created synthetic dataset with 10k images, crafted with [BlenderProc](https://github.com/DLR-RM/BlenderProc) - Removed training data created with [LayerDiffuse](https://github.com/layerdiffusion/LayerDiffuse), since it lacks the accuracy needed - Improved model performance (after 100k iterations): - F1: 0.9888 -> 0.9932 - MAE: 0.0113 -> 0.008 - Scores based on [this validation dataset](https://drive.google.com/drive/folders/1Yy9clZ58xCiai1zYESQkEKZCkslSC8eg) ### 05/07/2024 - Added [P3M-10K](https://paperswithcode.com/dataset/p3m-10k) dataset for training and validation - Added [AIM-500](https://paperswithcode.com/dataset/aim-500) dataset for training and validation - Added [PPM-100](https://github.com/ZHKKKe/PPM) dataset for training and validation - Applied [Grid Dropout](https://albumentations.ai/docs/api_reference/augmentations/dropout/grid_dropout/) to make the model smarter