--- license: mit library_name: litert pipeline_tag: image-segmentation base_model: ZhengPeng7/BiRefNet_lite tags: - litert - tflite - fp32 - background-removal - foreground-segmentation - birefnet --- # 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](https://huggingface.co/ZhengPeng7/BiRefNet_lite), revision [`aa62cd87eafb9cc43056d08ef3615a14628b831d`](https://huggingface.co/ZhengPeng7/BiRefNet_lite/tree/aa62cd87eafb9cc43056d08ef3615a14628b831d). ## Model file [BiRefNet-Lite-fp32.tflite](BiRefNet-Lite-fp32.tflite) · FP32 · 184.00 MB. The file hash is in [SHA256SUMS](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 ```bash pip install ai-edge-litert huggingface_hub numpy pillow ``` ```python 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](https://github.com/ZhengPeng7/BiRefNet) and [paper](https://arxiv.org/abs/2401.03407). This FP32 conversion uses the original pretrained weights with a fixed-shape TFLite interface. License: [MIT](LICENSE). ## Citation ```bibtex @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} } ```