Image Segmentation
ONNX
Safetensors
MLX
background-removal
image-matting
knowledge-distillation
lightweight
Instructions to use JvasquezT/eraser with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use JvasquezT/eraser with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download JvasquezT/eraser --local-dir eraser
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
|
Download README.md from JvasquezT/eraser: direct link, hf CLI and curl.
- Browser
- Download file 12.2 kB
-
https://huggingface.co/JvasquezT/eraser/resolve/main/README.md
- Command line
-
hf download hf://JvasquezT/eraser/README.md
-
curl -L -o README.md https://huggingface.co/JvasquezT/eraser/resolve/main/README.md
12.2 kB
| license: cc-by-nc-4.0 | |
| library_name: onnx | |
| pipeline_tag: image-segmentation | |
| tags: | |
| - background-removal | |
| - image-matting | |
| - onnx | |
| - mlx | |
| - knowledge-distillation | |
| - lightweight | |
| # Eraser | |
| A **small background-removal model** (3.9 M parameters, 15.7 MB as fp32 ONNX, 4 MB as int8) distilled from | |
| [BiRefNet](https://huggingface.co/ZhengPeng7/BiRefNet). Give it an RGB photo, get back an alpha matte. | |
| It runs on CPU in roughly 50-100 ms per 512Γ512 image, so it fits browsers, mobile and batch pipelines | |
| where a 200+ MB model is too heavy. | |
| **Intended use:** cutting out the **main subject** of product photos (catalog / e-commerce) and everyday photos, including | |
| portraits, with a clear subject. It is **not** designed for complex scenes with thin structures or ambiguous subjects | |
| (e.g. the DIS5K benchmark), nor for picking one specific object out of a scene with several (see the Finegrain results below). | |
| > **Status:** a compact student. On portraits and natural photos it keeps about 93-98 % of BiRefNet's agreement with human masks | |
| > at 1/56 of the size (see [Benchmarks](#benchmarks-real-ground-truth-masks)); it is clearly weaker on busy scenes, ambiguous subjects and | |
| > thin structures. See [Limitations](#limitations). | |
| ## Files | |
| | File | Size | What it is | | |
| |---|---|---| | |
| | `onnx/model.onnx` | 15.7 MB | fp32, reference | | |
| | `onnx/model_fp16.onnx` | 7.9 MB | fp16 weights, fp32 inputs/outputs. Same output as fp32 (see below) | | |
| | `onnx/model_int8.onnx` | 4.1 MB | int8 static quantization (QDQ). ~2Γ faster on CPU, small quality drift | | |
| | `pytorch/model.safetensors` | 15.7 MB | fp32 weights for PyTorch | | |
| | `mlx/model.safetensors` | 15.7 MB | fp32 weights for Apple MLX | | |
| | `mlx/model_fp16.safetensors` | 7.9 MB | fp16 weights for Apple MLX | | |
| | `model.py` | | PyTorch model definition | | |
| | `model_mlx.py` | | MLX model definition | | |
| | `examples/inference.py` | | ONNX Runtime example: image β RGBA cut-out | | |
| | `examples/inference_mlx.py` | | MLX example: image β RGBA cut-out | | |
| | `benchmark/` | | benchmark script, instructions and raw results (see [Benchmarks](#benchmarks-real-ground-truth-masks)) | | |
| | `NOTICE.md` | | data and teacher attribution | | |
| ## Input / output | |
| * **Input** `image`: `float32 [batch, 3, 512, 512]`, RGB, values in **[0, 1]**. Do **not** apply mean/std; ImageNet | |
| normalization is inside the model. Resize the whole image to 512Γ512 (plain bilinear, no letterboxing). | |
| * **Output** `alpha`: `float32 [batch, 1, 512, 512]` in [0, 1]. Resize it back to the original size and use it as the alpha channel. | |
| * Batch size is dynamic; the spatial size is fixed at 512Γ512. | |
| ## Quick start (ONNX Runtime) | |
| ```python | |
| import numpy as np, onnxruntime as ort | |
| from PIL import Image | |
| sess = ort.InferenceSession("onnx/model.onnx", providers=["CPUExecutionProvider"]) | |
| img = Image.open("photo.jpg").convert("RGB") | |
| w, h = img.size | |
| x = np.asarray(img.resize((512, 512), Image.BILINEAR), dtype=np.float32).transpose(2, 0, 1)[None] / 255 | |
| alpha = sess.run(None, {"image": x})[0][0, 0] # (512, 512) in [0, 1] | |
| alpha = Image.fromarray((alpha * 255).astype("uint8")).resize((w, h), Image.BILINEAR) | |
| img.putalpha(alpha) | |
| img.save("cutout.png") | |
| ``` | |
| Or run `python examples/inference.py photo.jpg --model onnx/model_int8.onnx`. | |
| ### PyTorch | |
| ```python | |
| import torch | |
| from model import load_model | |
| model = load_model("pytorch/model.safetensors") # eval mode | |
| with torch.no_grad(): | |
| alpha = model(torch.rand(1, 3, 512, 512)) # (1, 1, 512, 512) in [0, 1] | |
| ``` | |
| ### Apple MLX | |
| On an Apple silicon Mac, run from the repository root: | |
| ```sh | |
| python -m pip install mlx numpy pillow | |
| python examples/inference_mlx.py photo.jpg # fp32, writes photo_cutout.png | |
| python examples/inference_mlx.py photo.jpg --model mlx/model_fp16.safetensors --mask photo_mask.png | |
| ``` | |
| Both MLX variants use the input/output contract above, including float32 RGB inputs and float32 alpha outputs. The FP16 variant uses half-precision weights and activations internally. If the weight files are Git LFS pointers, run `git lfs pull --include='mlx/*.safetensors'`. | |
| For custom inference, `from model_mlx import load_model` loads the FP32 variant by default. To regenerate both MLX checkpoints from the existing PyTorch weights, install `safetensors` and run `python convert_to_mlx.py --fp16`. To check output parity with the FP32 ONNX model, install `onnxruntime` and run `python -m benchmark.verify_mlx` (you can pass photo paths after the command). The checker disables MLX's TF32 GPU mode where available to compare full-precision FP32 outputs; you can also use `MLX_ENABLE_TF32=0` with the inference example. | |
| ## ONNX precision variants | |
| Measured against the fp32 model on 100 photos that were not used for calibration or training | |
| (Open Images validation split, resized to 512Γ512). CPU time is batch 1 on one desktop CPU | |
| (your numbers will differ). | |
| | Variant | Size | CPU time | mean abs. diff vs fp32 | IoU vs fp32 (Ξ±>0.5) | 95th-percentile image (mean diff) | | |
| |---|---|---|---|---|---| | |
| | fp32 `model.onnx` | 15.7 MB | 93 ms | (reference) | (reference) | (reference) | | |
| | fp16 `model_fp16.onnx` | 7.9 MB | 95 ms | 0.0001 | 0.9996 | 0.0004 | | |
| | int8 `model_int8.onnx` | 4.1 MB | 50 ms | 0.0110 | 0.967 | 0.034 | | |
| * **fp16** is effectively lossless. On a plain CPU it is *not* faster; its benefit is size, and speed on GPUs / NPUs with fp16 support. | |
| * **int8** halves CPU time and shrinks the file 4Γ, at the price of a small drift from fp32 (mostly on soft edges). | |
| The edge-refinement layers were kept in fp32. It was calibrated on 100 photos; if your images are very different | |
| (e.g. line art, X-rays), re-calibrate on your own data. | |
| ## Benchmarks (real ground-truth masks) | |
| Measured with the script in [`benchmark/`](benchmark/) (included in this repo), on public datasets with human-made masks. The teacher (BiRefNet, 1024 px) | |
| was run on the **same images** with the same code, so the last column is a measured retention (student Γ· teacher). Student = fp32 model. | |
| S = S-measure, IoU at threshold 0.5 (higher is better), MAE lower is better; all at a long side of at most 1024 px. | |
| | Dataset | What it is | Images | S teacher β student | IoU teacher β student | MAE teacher β student | Retention S / IoU | | |
| |---|---|---|---|---|---|---| | |
| | [P3M-500](https://huggingface.co/datasets/nobg/P3M-10K) (NP) | portraits, soft alpha | 500 | 0.982 β 0.959 | 0.993 β 0.964 | 0.003 β 0.017 | **97.7 % / 97.1 %** | | |
| | [AIM-500](https://huggingface.co/datasets/nobg/AIM-500) | natural photos, soft alpha | 500 | 0.967 β 0.921 | 0.966 β 0.899 | 0.006 β 0.035 | **95.3 % / 93.1 %** | | |
| | [DUT-OMRON](https://huggingface.co/datasets/nobg/DUT-OMRON) | everyday photos, main object | 1,000 of 5,168 (random, fixed) | 0.869 β 0.789 | 0.774 β 0.637 | 0.045 β 0.103 | 90.8 % / 82.3 % | | |
| | [DUTS-TE](https://huggingface.co/datasets/nobg/DUTS) | everyday photos, main object | 1,000 of 5,019 (random, fixed) | 0.929 β 0.812 | 0.870 β 0.666 | 0.021 β 0.085 | 87.4 % / 76.6 % | | |
| | [Finegrain Products](https://huggingface.co/datasets/finegrain/finegrain-product-masks-lite) | e-commerce, **only the product** | 120 | 0.786 β 0.684 | 0.639 β 0.482 | 0.090 β 0.161 | 87.1 % / 75.4 % | | |
| | [DIS5K](https://huggingface.co/datasets/not-lain/DIS5K) (DIS-VD) | thin structures, hard scenes | 470 | 0.912 β 0.718 | 0.842 β 0.518 | 0.031 β 0.144 | 78.7 % / 61.6 % | | |
| The student is about **56Γ smaller** than the teacher (3.9 M vs ~220 M parameters; 15.7 MB vs 880 MB in fp32). | |
| Full tables (maxF, wF, E, all three precisions) are in `benchmark/results/<dataset>/report.md`. | |
| How to read it: | |
| * **Close to the teacher on portraits and natural photos** (P3M-500, AIM-500: 93-98 % retention). This is the target use. | |
| * **Noticeably behind on everyday saliency sets** (DUTS-TE, DUT-OMRON: 77-82 % IoU retention), where the "main object" is often ambiguous. | |
| * **Finegrain Products asks for *only the product*** (the bottle in a hand, the dress without the model). Even the teacher scores only 0.64 IoU there, | |
| because it also cuts out the hand or the person. The student keeps 75 % of that. Plain packshots looked fine in spot checks (not measured separately). | |
| * **DIS5K is a stress test** (thin, complex structures; the teacher was trained on that distribution, the student was not). 62 % IoU retention. | |
| * **Sanity check of the benchmark:** the teacher gets S = 0.912 on DIS-VD here, matching the 0.911 published for BiRefNet. | |
| * **Precision variants:** fp16 is identical to fp32 and int8 is within 0.005 on every metric on every dataset. | |
| * The test sets were **not** used for training. Check each dataset's original license before reusing it. | |
| * Method notes: the student numbers come from a CPU run, the teacher numbers from a GPU run on Colab (L4, fp16 autocast). The two runs use the same | |
| code and the same fixed random subsets (image counts match). Teacher figures were taken from the notebook output at 3-decimal precision. | |
| ## Agreement with the teacher (validation during training) | |
| Measured against the **teacher's** masks (BiRefNet at 1024 px), not human ground truth, on images never used for training | |
| (an 80/20 split by product for ABO; held-out photos for Open Images). Lower MAE and higher IoU are better. | |
| | Validation set | Images | MAE | IoU (Ξ±>0.5) | | |
| |---|---|---|---| | |
| | Product photos (ABO catalog) | 11,435 | 0.036 | 0.926 | | |
| | Products composited on unseen backgrounds | 11,435 | 0.032 | 0.923 | | |
| | General photos (Open Images: people, animals, objects in scenes) | 5,157 | 0.102 | 0.746 | | |
| The gap between product and general photos is real: general scenes are harder and the teacher's own "main subject" | |
| choice is ambiguous in many of them. A larger variant of this student (with an extra ResNet `layer4`, 13.7 M parameters) | |
| improved general-photo MAE to 0.088 / IoU 0.771 but is 3.5Γ larger and ~1.9Γ slower, so it is not part of this release. | |
| ## Architecture and training | |
| * **Student:** ResNet18 encoder (up to `layer3`, ImageNet-initialized) β pyramid pooling (bins 1/2/4/8) β U-Net style | |
| decoder with deep supervision β full-resolution edge refiner. 3.92 M parameters, input 512Γ512. | |
| * **Teacher:** BiRefNet, 1024Γ1024, fp16. Its soft alpha output is the training target ("pseudo-labels"); unreliable | |
| teacher masks (very uncertain, or nearly empty / nearly full) were filtered out. | |
| * **Data:** 60k product photos (Amazon Berkeley Objects), plus 30k general photos (Open Images) in a second stage; | |
| products were randomly pasted over real backgrounds (COCO, DTD) to avoid learning "white background = background". | |
| * **Loss:** BCE + IoU + 2Β·Sobel gradient loss (sharp edges), with auxiliary heads. | |
| * **Stage 1:** 30 epochs on products. **Stage 2:** 15 epochs fine-tuning on a 50 / 50 mix of products and general photos. | |
| ## Limitations | |
| * **Hard or ambiguous scenes.** Cluttered backgrounds, large close-up portraits, dark painted backgrounds and scenes where | |
| "the main object" is unclear can produce soft grey halos or blobby silhouettes. Typical failure: faint haze over large background areas. | |
| * **It imitates the teacher,** including its mistakes and its notion of what the "main subject" is. Do not expect it to beat BiRefNet. | |
| * **Fine structures** (hair, fur, thin wires, transparent objects) are less precise than in the teacher. The output is 512Γ512, then upscaled. | |
| * **Fixed 512Γ512 input.** Aspect ratio is not preserved when resizing; very wide or tall images lose detail. | |
| * **Human-labeled evaluation is limited to the public benchmarks above;** the training-time numbers are agreement with the teacher. | |
| * **Licensing is non-commercial** (see below). | |
| ## License and data | |
| Released under **CC BY-NC 4.0** (non-commercial), because training used Amazon Berkeley Objects, which is CC BY-NC 4.0. | |
| Other sources: Open Images (images CC BY 2.0), COCO and DTD (backgrounds), BiRefNet (teacher), torchvision ResNet18 (initialization). | |
| Details and links are in [NOTICE.md](NOTICE.md). Check every source's terms before any commercial use. | |
| ## Acknowledgements | |
| * BiRefNet (Zheng et al.), the teacher. | |
| * Amazon Berkeley Objects, Open Images, COCO and Describable Textures Dataset. | |