Image Segmentation
Transformers
PyTorch
ONNX
Safetensors
Transformers.js
SegformerForSemanticSegmentation
remove background
background
background-removal
Pytorch
vision
legal liability
custom_code
Instructions to use SolonD/RMBG-1.4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use SolonD/RMBG-1.4 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-segmentation", model="SolonD/RMBG-1.4", trust_remote_code=True)# Load model directly from transformers import AutoModelForImageSegmentation model = AutoModelForImageSegmentation.from_pretrained("SolonD/RMBG-1.4", trust_remote_code=True, dtype="auto") - Transformers.js
How to use SolonD/RMBG-1.4 with Transformers.js:
// npm i @huggingface/transformers import { pipeline } from '@huggingface/transformers'; // Allocate pipeline const pipe = await pipeline('image-segmentation', 'SolonD/RMBG-1.4'); - Notebooks
- Google Colab
- Kaggle
| import torch | |
| import torch.nn.functional as F | |
| from torchvision.transforms.functional import normalize | |
| import numpy as np | |
| def preprocess_image(im: np.ndarray, model_input_size: list) -> torch.Tensor: | |
| if len(im.shape) < 3: | |
| im = im[:, :, np.newaxis] | |
| # orig_im_size=im.shape[0:2] | |
| im_tensor = torch.tensor(im, dtype=torch.float32).permute(2,0,1) | |
| im_tensor = F.interpolate(torch.unsqueeze(im_tensor,0), size=model_input_size, mode='bilinear').type(torch.uint8) | |
| image = torch.divide(im_tensor,255.0) | |
| image = normalize(image,[0.5,0.5,0.5],[1.0,1.0,1.0]) | |
| return image | |
| def postprocess_image(result: torch.Tensor, im_size: list)-> np.ndarray: | |
| 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 | |