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
| license: apache-2.0 | |
| library_name: litert | |
| pipeline_tag: image-to-image | |
| tags: | |
| - litert | |
| - tflite | |
| - android | |
| - on-device | |
| - gpu | |
| - portrait | |
| - sketch | |
| - line-drawing | |
| - u2net | |
| - creative | |
| # U²-Net Portrait — Photo → pencil line drawing (LiteRT GPU) | |
| On-device **portrait sketch generation** running **fully on the LiteRT `CompiledModel` GPU** | |
| delegate (no CPU fallback). The [U²-Net](https://github.com/xuebinqin/U-2-Net) portrait model | |
| turns a face photo into a **hand-drawn pencil line portrait** — a fun creative / AR filter. | |
| ~12 ms/frame on a Pixel 8a. | |
| - **Architecture:** U²-Net (RSU / nested residual U-blocks) — pure CNN. | |
| - **Weights:** [xuebinqin/U-2-Net](https://github.com/xuebinqin/U-2-Net) (`u2net_portrait`) · Apache-2.0. | |
| - **Size:** 176 MB. | |
|  | |
| *Input (left) → generated pencil portrait (right). Photo: Unsplash (free license).* | |
| ## I/O | |
| - **Input:** `[1, 3, 512, 512]` NCHW, RGB, `x/max` then ImageNet-normalized | |
| (mean `[0.485,0.456,0.406]`, std `[0.229,0.224,0.225]`). A centered face works best. | |
| - **Output:** `[1, 1, 512, 512]` in `[0,1]`. Min-max normalize, then **invert** (`1 − x`) | |
| for dark strokes on white paper. | |
| ## GPU conversion | |
| U²-Net is a pure CNN → fully GPU-compatible (**893/893 nodes on the delegate, 1 | |
| partition**; device corr 0.998683, ~12 ms) with **one defensive patch**: `align_corners=True` | |
| → `False` on the bilinear upsamples. CPU-exact vs PyTorch (corr 1.0). | |
| ## Minimal usage | |
| ### Kotlin (Android, LiteRT CompiledModel GPU) | |
| ```kotlin | |
| val options = CompiledModel.Options(Accelerator.GPU) | |
| val model = CompiledModel.create(context.assets, "portrait.tflite", options, null) | |
| val inBufs = model.createInputBuffers() | |
| val outBufs = model.createOutputBuffers() | |
| inBufs[0].writeFloat(inputNCHW) // [1,3,512,512] RGB, /max then ImageNet-norm | |
| model.run(inBufs, outBufs) | |
| val d = outBufs[0].readFloat() // [512*512] 0..1; min-max normalize then 1-x -> pencil sketch | |
| ``` | |
| ### Python (LiteRT / ai-edge-litert) | |
| ```python | |
| import numpy as np | |
| from ai_edge_litert.interpreter import Interpreter | |
| it = Interpreter(model_path="portrait.tflite"); it.allocate_tensors() | |
| inp, out = it.get_input_details(), it.get_output_details() | |
| it.set_tensor(inp[0]["index"], x) # [1,3,512,512] float32, RGB, /max, ImageNet-norm | |
| it.invoke() | |
| d = it.get_tensor(out[0]["index"])[0, 0] | |
| d = (d - d.min()) / (d.max() - d.min()); sketch = 1.0 - d # dark strokes on white | |
| ``` | |
| ## Conversion | |
| Converted with **litert-torch** (`build_portrait.py`): loads the Apache-2.0 `u2net_portrait` | |
| weights and exports the sketch map. | |
| ## License | |
| Apache-2.0 (U²-Net / xuebinqin). | |