Instructions to use litert-community/U2Net-Portrait-Sketch-LiteRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use litert-community/U2Net-Portrait-Sketch-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,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
library_name: litert
|
| 4 |
+
pipeline_tag: image-to-image
|
| 5 |
+
tags:
|
| 6 |
+
- litert
|
| 7 |
+
- tflite
|
| 8 |
+
- android
|
| 9 |
+
- on-device
|
| 10 |
+
- gpu
|
| 11 |
+
- portrait
|
| 12 |
+
- sketch
|
| 13 |
+
- line-drawing
|
| 14 |
+
- u2net
|
| 15 |
+
- creative
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# U²-Net Portrait — Photo → pencil line drawing (LiteRT GPU)
|
| 19 |
+
|
| 20 |
+
On-device **portrait sketch generation** running **fully on the LiteRT `CompiledModel` GPU**
|
| 21 |
+
delegate (no CPU fallback). The [U²-Net](https://github.com/xuebinqin/U-2-Net) portrait model
|
| 22 |
+
turns a face photo into a **hand-drawn pencil line portrait** — a fun creative / AR filter.
|
| 23 |
+
~12 ms/frame on a Pixel 8a.
|
| 24 |
+
|
| 25 |
+
- **Architecture:** U²-Net (RSU / nested residual U-blocks) — pure CNN.
|
| 26 |
+
- **Weights:** [xuebinqin/U-2-Net](https://github.com/xuebinqin/U-2-Net) (`u2net_portrait`) · Apache-2.0.
|
| 27 |
+
- **Size:** 176 MB.
|
| 28 |
+
|
| 29 |
+

|
| 30 |
+
|
| 31 |
+
*Input (left) → generated pencil portrait (right). Photo: Unsplash (free license).*
|
| 32 |
+
|
| 33 |
+
## I/O
|
| 34 |
+
|
| 35 |
+
- **Input:** `[1, 3, 512, 512]` NCHW, RGB, `x/max` then ImageNet-normalized
|
| 36 |
+
(mean `[0.485,0.456,0.406]`, std `[0.229,0.224,0.225]`). A centered face works best.
|
| 37 |
+
- **Output:** `[1, 1, 512, 512]` in `[0,1]`. Min-max normalize, then **invert** (`1 − x`)
|
| 38 |
+
for dark strokes on white paper.
|
| 39 |
+
|
| 40 |
+
## GPU conversion
|
| 41 |
+
|
| 42 |
+
U²-Net is a pure CNN → fully GPU-compatible (**893/893 nodes on the delegate, 1
|
| 43 |
+
partition**; device corr 0.998683, ~12 ms) with **one defensive patch**: `align_corners=True`
|
| 44 |
+
→ `False` on the bilinear upsamples. CPU-exact vs PyTorch (corr 1.0).
|
| 45 |
+
|
| 46 |
+
## Minimal usage
|
| 47 |
+
|
| 48 |
+
### Kotlin (Android, LiteRT CompiledModel GPU)
|
| 49 |
+
|
| 50 |
+
```kotlin
|
| 51 |
+
val options = CompiledModel.Options(Accelerator.GPU)
|
| 52 |
+
val model = CompiledModel.create(context.assets, "portrait.tflite", options, null)
|
| 53 |
+
val inBufs = model.createInputBuffers()
|
| 54 |
+
val outBufs = model.createOutputBuffers()
|
| 55 |
+
|
| 56 |
+
inBufs[0].writeFloat(inputNCHW) // [1,3,512,512] RGB, /max then ImageNet-norm
|
| 57 |
+
model.run(inBufs, outBufs)
|
| 58 |
+
val d = outBufs[0].readFloat() // [512*512] 0..1; min-max normalize then 1-x -> pencil sketch
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
### Python (LiteRT / ai-edge-litert)
|
| 62 |
+
|
| 63 |
+
```python
|
| 64 |
+
import numpy as np
|
| 65 |
+
from ai_edge_litert.interpreter import Interpreter
|
| 66 |
+
|
| 67 |
+
it = Interpreter(model_path="portrait.tflite"); it.allocate_tensors()
|
| 68 |
+
inp, out = it.get_input_details(), it.get_output_details()
|
| 69 |
+
it.set_tensor(inp[0]["index"], x) # [1,3,512,512] float32, RGB, /max, ImageNet-norm
|
| 70 |
+
it.invoke()
|
| 71 |
+
d = it.get_tensor(out[0]["index"])[0, 0]
|
| 72 |
+
d = (d - d.min()) / (d.max() - d.min()); sketch = 1.0 - d # dark strokes on white
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
## Conversion
|
| 76 |
+
|
| 77 |
+
Converted with **litert-torch** (`build_portrait.py`): loads the Apache-2.0 `u2net_portrait`
|
| 78 |
+
weights and exports the sketch map.
|
| 79 |
+
|
| 80 |
+
## License
|
| 81 |
+
|
| 82 |
+
Apache-2.0 (U²-Net / xuebinqin).
|