jennyxue commited on
Commit
1845d69
·
verified ·
1 Parent(s): 1e9a5d0

Model card and the original license

Browse files
Files changed (1) hide show
  1. README.md +209 -0
README.md ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: PramaLLC/BEN2
3
+ base_model_relation: quantized
4
+ license: mit
5
+ pipeline_tag: image-segmentation
6
+ library_name: mlx
7
+ tags:
8
+ - mlx
9
+ - moonbox
10
+ - ben2
11
+ - background-removal
12
+ - image-matting
13
+ ---
14
+
15
+ # BEN2 Base MLX
16
+
17
+ MLX builds of [BEN2 Base](https://huggingface.co/PramaLLC/BEN2), made by Moonbox for Apple Silicon Macs. Each precision is a folder; keep a folder's
18
+ files together. Only the parts listed below are converted: the rest of the pipeline comes from the original
19
+ repository unless the section below says otherwise.
20
+
21
+ ## Files
22
+
23
+ The last column is how far each build is from the original PyTorch model on the same inputs (see the section below
24
+ for what was measured).
25
+
26
+ | Precision | File | Size | sha256 | Checked against the original |
27
+ |---|---|---|---|---|
28
+ | fp16 | [`fp16/config.json`](https://huggingface.co/moonbox-ai/BEN2-MLX/blob/main/fp16/config.json) | 0.2 kB | `cdad08ebd797` | MAE <= 0.00003, mask IoU >= 0.9996 |
29
+ | fp16 | [`fp16/mlx_model.py`](https://huggingface.co/moonbox-ai/BEN2-MLX/blob/main/fp16/mlx_model.py) | 20.5 kB | `eff7a4afb5ad` | MAE <= 0.00003, mask IoU >= 0.9996 |
30
+ | fp16 | [`fp16/model.safetensors`](https://huggingface.co/moonbox-ai/BEN2-MLX/blob/main/fp16/model.safetensors) | 188.3 MB | `5c446d204f3f` | MAE <= 0.00003, mask IoU >= 0.9996 |
31
+
32
+ ## How to run
33
+
34
+ BEN2 Base ported to MLX. Each folder holds `model.safetensors`, `config.json` and `mlx_model.py`, the port (only
35
+ `mlx`, `numpy` and `pillow` needed):
36
+
37
+ ```python
38
+ import numpy as np, mlx_model
39
+ from PIL import Image
40
+ model = mlx_model.load("fp16")
41
+ rgb = np.array(Image.open("in.jpg").convert("RGB"))
42
+ alpha = mlx_model.matte(model, rgb) # HxW uint8, as BEN2.py's inference() (1024x1024 internally)
43
+ Image.fromarray(np.dstack([rgb, alpha])).save("cutout.png")
44
+ ```
45
+
46
+ The port runs `BEN_Base` as released (Swin-B on four 512 px crops plus the whole image, MCLM / MCRM decoder). The
47
+ quantized folders quantize the linear layers (Swin, attention, feed-forward; group size 64); the convolutions stay
48
+ bf16. The original's optional `refine_foreground` colour decontamination isn't ported.
49
+
50
+ | Precision | Size | MAE vs PyTorch fp32 (worst image) | Mask IoU (worst) |
51
+ |---|---|---|---|
52
+ | `fp16/` | 188 MB | 0.00003 | 0.9996 |
53
+ | `bf16/` | 188 MB | 0.0002 | 0.9987 |
54
+ | `8bit/` | 106 MB | 0.0003 | 0.9983 |
55
+ | `6bit/` | 84 MB | 0.0004 | 0.9966 |
56
+ | `4bit/` | 62 MB | 0.0012 | 0.9923 |
57
+
58
+ About 0.7 s per image on an Apple M5 at every precision.
59
+
60
+ ## How these were made
61
+
62
+ From [BEN2 Base](https://huggingface.co/PramaLLC/BEN2) at revision `e48a20765f`: the original weights loaded in MLX and stored as bf16 or fp16, or
63
+ quantized with MLX's affine quantization (`8bit`, `6bit`, `4bit`, group size 64; layers MLX can't quantize, such as
64
+ convolutions and norms, stay bf16). Every build was checked against the original before upload; the `sha256` column
65
+ above is what the files must hash to.
66
+
67
+ ## License
68
+
69
+ MIT. These files are a derivative of the original model and carry its license: it is copied in this
70
+ repository, and it governs your use of these files. The original model card follows unchanged.
71
+
72
+ ---
73
+
74
+ # Original model card
75
+
76
+ # BEN2: Background Erase Network
77
+
78
+ [![arXiv](https://img.shields.io/badge/arXiv-2501.06230-b31b1b.svg)](https://arxiv.org/abs/2501.06230)
79
+ [![GitHub](https://img.shields.io/badge/GitHub-BEN2-black.svg)](https://github.com/PramaLLC/BEN2/)
80
+ [![Website](https://img.shields.io/badge/Website-backgrounderase.net-104233)](https://backgrounderase.net)
81
+
82
+ ## Overview
83
+ 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:
84
+ [![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/ben-using-confidence-guided-matting-for/dichotomous-image-segmentation-on-dis-vd)](https://paperswithcode.com/sota/dichotomous-image-segmentation-on-dis-vd?p=ben-using-confidence-guided-matting-for)
85
+
86
+
87
+
88
+
89
+ ## BEN2 access
90
+ 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:
91
+ - 🌐 [backgrounderase.com](https://backgrounderase.com)
92
+
93
+
94
+ ## Contact us
95
+ - For access to our commercial model email us at sales@backgrounderase.com
96
+ - Our website: https://backgrounderase.com/
97
+ - Follow us on X: https://x.com/PramaResearch/
98
+
99
+
100
+ ## Installation
101
+
102
+ ```
103
+ pip install -e "git+https://github.com/PramaLLC/BEN2.git#egg=ben2"
104
+ ```
105
+
106
+ ## Quick start code
107
+
108
+ ```python
109
+ from ben2 import BEN_Base
110
+ from PIL import Image
111
+ import torch
112
+
113
+
114
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
115
+
116
+ file = "./image.png" # input image
117
+
118
+ model = BEN_Base.from_pretrained("PramaLLC/BEN2")
119
+ model.to(device).eval()
120
+
121
+ image = Image.open(file)
122
+ 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.
123
+
124
+ foreground.save("./foreground.png")
125
+
126
+ ```
127
+
128
+
129
+ ## Batch image processing
130
+
131
+ ```python
132
+ from ben2 import BEN_Base
133
+ from PIL import Image
134
+ import torch
135
+
136
+
137
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
138
+
139
+
140
+
141
+ model = BEN_Base.from_pretrained("PramaLLC/BEN2")
142
+ model.to(device).eval()
143
+
144
+
145
+ file1 = "./image1.png" # input image1
146
+ file2 = "./image2.png" # input image2
147
+ image1 = Image.open(file1)
148
+ image2 = Image.open(file2)
149
+
150
+
151
+
152
+ 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.
153
+ foregrounds[0].save("./foreground1.png")
154
+ foregrounds[1].save("./foreground2.png")
155
+
156
+ ```
157
+
158
+
159
+
160
+ # BEN2 video segmentation
161
+ [![BEN2 Demo](https://img.youtube.com/vi/skEXiIHQcys/0.jpg)](https://www.youtube.com/watch?v=skEXiIHQcys)
162
+
163
+ ## Video Segmentation
164
+
165
+ ```bash
166
+ sudo apt update
167
+ sudo apt install ffmpeg
168
+ ```
169
+
170
+ ```python
171
+ from ben2 import BEN_Base
172
+ from PIL import Image
173
+ import torch
174
+
175
+
176
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
177
+
178
+ video_path = "/path_to_your_video.mp4"# input video
179
+
180
+ model = BEN_Base.from_pretrained("PramaLLC/BEN2")
181
+ model.to(device).eval()
182
+
183
+
184
+ model.segment_video(
185
+ video_path= video_path,
186
+ output_path="./", # Outputs will be saved as foreground.webm or foreground.mp4. The default value is "./"
187
+ fps=0, # If this is set to 0 CV2 will detect the fps in the original video. The default value is 0.
188
+ refine_foreground=False, #refine foreground is an extract postprocessing step that increases inference time but can improve matting edges. The default value is False.
189
+ batch=1, # We recommended that batch size not exceed 3 for consumer GPUs as there are minimal inference gains. The default value is 1.
190
+ print_frames_processed=True, #Informs you what frame is being processed. The default value is True.
191
+ webm = False, # This will output an alpha layer video but this defaults to mp4 when webm is false. The default value is False.
192
+ 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).
193
+ )
194
+
195
+
196
+ ```
197
+
198
+
199
+
200
+ **# BEN2 evaluation**
201
+ ![Model Comparison](BEN2_demo_pictures/model_comparison.png)
202
+
203
+ RMBG 2.0 did not preserve the DIS 5k validation dataset
204
+
205
+ ![Example 1](BEN2_demo_pictures/grid_example1.png)
206
+ ![Example 2](BEN2_demo_pictures/grid_example2.png)
207
+ ![Example 3](BEN2_demo_pictures/grid_example3.png)
208
+ ![Example 6](BEN2_demo_pictures/grid_example6.png)
209
+ ![Example 7](BEN2_demo_pictures/grid_example7.png)