Instructions to use litert-community/BiRefNet-Lite with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use litert-community/BiRefNet-Lite with LiteRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- BiRefNet
How to use litert-community/BiRefNet-Lite with BiRefNet:
# Option 1: use with transformers from transformers import AutoModelForImageSegmentation birefnet = AutoModelForImageSegmentation.from_pretrained("litert-community/BiRefNet-Lite", trust_remote_code=True)# Option 2: use with BiRefNet # Install from https://github.com/ZhengPeng7/BiRefNet from models.birefnet import BiRefNet model = BiRefNet.from_pretrained("litert-community/BiRefNet-Lite") - Notebooks
- Google Colab
- Kaggle
BiRefNet-Lite for LiteRT
FP32 LiteRT/TFLite export of BiRefNet Lite for foreground segmentation and background removal. The model returns a soft foreground mask with the final sigmoid included.
Source checkpoint: ZhengPeng7/BiRefNet_lite, revision aa62cd87eafb9cc43056d08ef3615a14628b831d.
Model file
BiRefNet-Lite-fp32.tflite · FP32 · 184.00 MB. The file hash is in SHA256SUMS.
The TFLite signature is main. All input and output tensors are float32; shapes are fixed. Image/video decoding, preprocessing, and application postprocessing run on the host.
| Direction | Name | Shape | Meaning |
|---|---|---|---|
| Input | pixel_values |
[1, 3, 1024, 1024] |
Normalized RGB, NCHW |
| Output | foreground_probability |
[1, 1, 1024, 1024] |
Foreground probability in [0, 1], NCHW |
Preprocessing and outputs
Convert the image to RGB, resize to 1024 × 1024 with PIL bilinear interpolation, divide by 255, and normalize with ImageNet mean [0.485, 0.456, 0.406] and standard deviation [0.229, 0.224, 0.225]. Transpose to NCHW. Resize the output probability map to the original image size for an alpha mask.
Run on CPU
pip install ai-edge-litert huggingface_hub numpy pillow
import numpy as np
from huggingface_hub import hf_hub_download
from ai_edge_litert.interpreter import Interpreter
from PIL import Image
image = Image.open("image.jpg").convert("RGB")
rgb = np.asarray(image.resize((1024, 1024), Image.Resampling.BILINEAR),
dtype=np.float32) / 255.0
mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
pixel_values = np.ascontiguousarray(((rgb - mean) / std).transpose(2, 0, 1)[None])
model_path = hf_hub_download("litert-community/BiRefNet-Lite", "BiRefNet-Lite-fp32.tflite")
interpreter = Interpreter(model_path=model_path, num_threads=4)
interpreter.allocate_tensors()
run = interpreter.get_signature_runner("main")
probability = run(pixel_values=pixel_values)["foreground_probability"][0, 0]
# Restore the soft foreground mask to the original image dimensions.
mask = Image.fromarray(probability).resize(image.size, Image.Resampling.BILINEAR)
alpha = Image.fromarray(np.rint(np.clip(np.asarray(mask), 0, 1) * 255).astype(np.uint8))
rgba = image.convert("RGBA")
rgba.putalpha(alpha)
rgba.save("foreground.png")
Source and license
BiRefNet is by Peng Zheng and collaborators. See the project and paper.
This FP32 conversion uses the original pretrained weights with a fixed-shape TFLite interface. License: MIT.
Citation
@article{zheng2024birefnet,
title={Bilateral Reference for High-Resolution Dichotomous Image Segmentation},
author={Zheng, Peng and Gao, Dehong and Fan, Deng-Ping and Liu, Li and Laaksonen, Jorma and Ouyang, Wanli and Sebe, Nicu},
journal={CAAI Artificial Intelligence Research},
volume={3},
pages={9150038},
year={2024}
}
- Downloads last month
- -
Model tree for litert-community/BiRefNet-Lite
Base model
ZhengPeng7/BiRefNet_lite