Image Segmentation
LiteRT
LiteRT
android
on-device
gpu
face-parsing
face-segmentation
bisenet
celebamask-hq
real-time
Instructions to use litert-community/BiSeNet-Face-Parsing-LiteRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use litert-community/BiSeNet-Face-Parsing-LiteRT 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
- Notebooks
- Google Colab
- Kaggle
Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
library_name: litert
|
| 4 |
+
pipeline_tag: image-segmentation
|
| 5 |
+
tags:
|
| 6 |
+
- litert
|
| 7 |
+
- tflite
|
| 8 |
+
- android
|
| 9 |
+
- on-device
|
| 10 |
+
- gpu
|
| 11 |
+
- face-parsing
|
| 12 |
+
- face-segmentation
|
| 13 |
+
- bisenet
|
| 14 |
+
- celebamask-hq
|
| 15 |
+
- real-time
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# BiSeNet Face Parsing — LiteRT (GPU)
|
| 19 |
+
|
| 20 |
+
On-device **real-time face parsing** running **fully on the LiteRT `CompiledModel`
|
| 21 |
+
GPU** delegate (no CPU fallback). [BiSeNet](https://arxiv.org/abs/1808.00897)
|
| 22 |
+
(zllrunning/face-parsing.PyTorch) segments a face into the **19 CelebAMask-HQ
|
| 23 |
+
classes** (skin, brows, eyes, nose, lips, ears, hair, hat, glasses, neck, cloth, …)
|
| 24 |
+
— for AR / beauty / makeup. ~22 ms/frame on a Pixel 8a.
|
| 25 |
+
|
| 26 |
+
- **Architecture:** BiSeNet (ResNet18 backbone + context path + feature-fusion) — pure CNN.
|
| 27 |
+
- **Weights:** [zllrunning/face-parsing.PyTorch](https://github.com/zllrunning/face-parsing.PyTorch) · MIT · ~13.3 M params.
|
| 28 |
+
- **Size:** 53 MB.
|
| 29 |
+
|
| 30 |
+

|
| 31 |
+
|
| 32 |
+
## I/O
|
| 33 |
+
|
| 34 |
+
- **Input:** `[1, 3, 512, 512]` NCHW, RGB, ImageNet-normalized
|
| 35 |
+
(mean `[0.485,0.456,0.406]`, std `[0.229,0.224,0.225]`).
|
| 36 |
+
- **Output:** `[1, 19, 512, 512]` class logits — argmax over the 19 classes per pixel.
|
| 37 |
+
|
| 38 |
+
Classes: `background, skin, l_brow, r_brow, l_eye, r_eye, eyeglass, l_ear, r_ear,
|
| 39 |
+
earring, nose, mouth, u_lip, l_lip, neck, necklace, cloth, hair, hat`.
|
| 40 |
+
|
| 41 |
+
## GPU conversion
|
| 42 |
+
|
| 43 |
+
BiSeNet is a pure CNN; three re-authoring patches make it a **fully GPU-compatible
|
| 44 |
+
graph — 74/74 nodes on the delegate, 1 partition** (device corr 0.99999, argmax
|
| 45 |
+
99.96% vs PyTorch):
|
| 46 |
+
|
| 47 |
+
1. **`align_corners=True` → `False`** — the output upsamples use `align_corners=True`,
|
| 48 |
+
which the GPU delegate rejects (1.6% argmax change vs original).
|
| 49 |
+
2. **global `avg_pool2d(x, x.size()[2:])` → `mean([2,3])`** — the context/attention
|
| 50 |
+
modules pool with a full-spatial kernel, which the Mali delegate rejects as an
|
| 51 |
+
`AVERAGE_POOL_2D`; a MEAN reduce is supported.
|
| 52 |
+
3. **zero-pad maxpool** — the ResNet stem `MaxPool2d(padding=1)` lowers to a PADV2
|
| 53 |
+
with `-inf` padding (`PADV2: src has wrong size` on Mali); an explicit 0-pad +
|
| 54 |
+
unpadded maxpool is exact (input is post-ReLU ≥ 0).
|
| 55 |
+
|
| 56 |
+
CPU-exact vs PyTorch (corr 0.99999999999).
|
| 57 |
+
|
| 58 |
+
## Minimal usage
|
| 59 |
+
|
| 60 |
+
### Kotlin (Android, LiteRT CompiledModel GPU)
|
| 61 |
+
|
| 62 |
+
```kotlin
|
| 63 |
+
val options = CompiledModel.Options(Accelerator.GPU)
|
| 64 |
+
val model = CompiledModel.create(context.assets, "faceparsing.tflite", options, null)
|
| 65 |
+
val inBufs = model.createInputBuffers()
|
| 66 |
+
val outBufs = model.createOutputBuffers()
|
| 67 |
+
|
| 68 |
+
inBufs[0].writeFloat(inputNCHW) // [1,3,512,512], RGB, ImageNet-norm
|
| 69 |
+
model.run(inBufs, outBufs)
|
| 70 |
+
val logits = outBufs[0].readFloat() // [19,512,512] (NCHW, batch dropped)
|
| 71 |
+
|
| 72 |
+
val hw = 512 * 512
|
| 73 |
+
val label = IntArray(hw) { i ->
|
| 74 |
+
var best = 0; var bv = logits[i]
|
| 75 |
+
for (c in 1 until 19) { val v = logits[c * hw + i]; if (v > bv) { bv = v; best = c } }
|
| 76 |
+
best
|
| 77 |
+
}
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### Python (LiteRT / ai-edge-litert)
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
from ai_edge_litert.interpreter import Interpreter
|
| 84 |
+
import numpy as np
|
| 85 |
+
|
| 86 |
+
it = Interpreter(model_path="faceparsing.tflite"); it.allocate_tensors()
|
| 87 |
+
inp, out = it.get_input_details(), it.get_output_details()
|
| 88 |
+
it.set_tensor(inp[0]["index"], x) # [1,3,512,512] float32, ImageNet-norm
|
| 89 |
+
it.invoke()
|
| 90 |
+
logits = it.get_tensor(out[0]["index"])[0] # [19,512,512]
|
| 91 |
+
label = logits.argmax(0) # [512,512] class ids
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
## Conversion
|
| 95 |
+
|
| 96 |
+
Converted with **litert-torch** (`build_faceparsing.py`): loads the trained BiSeNet
|
| 97 |
+
weights, applies the three patches, and exports.
|
| 98 |
+
|
| 99 |
+
## License
|
| 100 |
+
|
| 101 |
+
MIT (BiSeNet / zllrunning/face-parsing.PyTorch). CelebAMask-HQ label taxonomy.
|