Image Segmentation
BEN2
ONNX
Safetensors
PyTorch
BEN2
background-remove
mask-generation
Dichotomous image segmentation
background remove
foreground
background
remove background
model_hub_mixin
pytorch_model_hub_mixin
background removal
background-removal
Instructions to use PramaLLC/BEN2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- BEN2
How to use PramaLLC/BEN2 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("PramaLLC/BEN2") model.to("cuda").eval() foreground = model.inference(image) - Notebooks
- Google Colab
- Kaggle
Download onnx_run.py from PramaLLC/BEN2: direct link, hf CLI and curl.
- Browser
- Download file 1.68 kB
-
https://huggingface.co/PramaLLC/BEN2/resolve/main/onnx_run.py
- Command line
-
hf download hf://PramaLLC/BEN2/onnx_run.py
-
curl -L -o onnx_run.py https://huggingface.co/PramaLLC/BEN2/resolve/main/onnx_run.py
1.68 kB
| import onnxruntime | |
| import numpy as np | |
| from PIL import Image | |
| import torchvision.transforms as transforms | |
| import torch | |
| import torch.nn.functional as F | |
| session = onnxruntime.InferenceSession("./BEN2_Base.onnx", providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) | |
| def postprocess_image(result_np: np.ndarray, im_size: list) -> np.ndarray: | |
| result = torch.from_numpy(result_np) | |
| if len(result.shape) == 3: | |
| result = result.unsqueeze(0) | |
| result = torch.squeeze(F.interpolate(result, size=im_size, mode='bilinear'), 0) | |
| ma = torch.max(result) | |
| mi = torch.min(result) | |
| result = (result - mi) / (ma - mi) | |
| im_array = (result * 255).permute(1, 2, 0).cpu().data.numpy().astype(np.uint8) | |
| im_array = np.squeeze(im_array) | |
| return im_array | |
| def preprocess_image(image): | |
| original_size = image.size | |
| transform = transforms.Compose([ | |
| transforms.Resize((1024, 1024)), | |
| transforms.ToTensor(), | |
| ]) | |
| img_tensor = transform(image) | |
| img_tensor = img_tensor.unsqueeze(0) | |
| return img_tensor.numpy(), image, original_size | |
| def run_inference(image): | |
| input_data, original_image, (w, h) = preprocess_image(image) | |
| input_name = session.get_inputs()[0].name | |
| outputs = session.run(None, {input_name: input_data}) | |
| alpha = postprocess_image(outputs[0], im_size=[w, h]) | |
| mask = Image.fromarray(alpha) | |
| mask = mask.resize((w, h)) | |
| original_image.putalpha(mask) | |
| return original_image | |
| # Example usage | |
| image_path = "image.png" | |
| output_path = "output.png" | |
| image = Image.open(image_path) | |
| result_image = run_inference(image) | |
| result_image.save(output_path) | |