Image Segmentation
LiteRT
LiteRT
on-device
android
background-removal
salient-object-detection
image-matting
u2net
Instructions to use litert-community/U-2-Net with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use litert-community/U-2-Net 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
Add minimal usage snippets (Kotlin + Python)
Browse files
README.md
CHANGED
|
@@ -41,18 +41,40 @@ rewrites** (pure CNN).
|
|
| 41 |
- **Output**: `[1, 1, 320, 320]` saliency mask in `[0, 1]` (sigmoid). Upscale to the input
|
| 42 |
size and use as the foreground alpha.
|
| 43 |
|
| 44 |
-
##
|
|
|
|
|
|
|
| 45 |
|
| 46 |
```kotlin
|
| 47 |
-
val model = CompiledModel.create(
|
| 48 |
-
|
| 49 |
-
CompiledModel.Options(Accelerator.GPU), null
|
| 50 |
-
)
|
| 51 |
val inputs = model.createInputBuffers()
|
| 52 |
val outputs = model.createOutputBuffers()
|
| 53 |
-
inputs[0].writeFloat(
|
| 54 |
model.run(inputs, outputs)
|
| 55 |
-
val mask = outputs[0].readFloat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
```
|
| 57 |
|
| 58 |
A complete Android sample (live camera + gallery background removal) is available in
|
|
|
|
| 41 |
- **Output**: `[1, 1, 320, 320]` saliency mask in `[0, 1]` (sigmoid). Upscale to the input
|
| 42 |
size and use as the foreground alpha.
|
| 43 |
|
| 44 |
+
## Minimal usage
|
| 45 |
+
|
| 46 |
+
**Android (Kotlin, CompiledModel GPU)**
|
| 47 |
|
| 48 |
```kotlin
|
| 49 |
+
val model = CompiledModel.create(context.assets, "u2net_fp16.tflite",
|
| 50 |
+
CompiledModel.Options(Accelerator.GPU), null)
|
|
|
|
|
|
|
| 51 |
val inputs = model.createInputBuffers()
|
| 52 |
val outputs = model.createOutputBuffers()
|
| 53 |
+
inputs[0].writeFloat(chw) // [1,3,320,320] /max then ImageNet-norm, NCHW
|
| 54 |
model.run(inputs, outputs)
|
| 55 |
+
val mask = outputs[0].readFloat() // [1,1,320,320] saliency in [0,1]
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
**Python (desktop verification)**
|
| 59 |
+
|
| 60 |
+
```python
|
| 61 |
+
MEAN = np.array([0.485, 0.456, 0.406], np.float32)
|
| 62 |
+
STD = np.array([0.229, 0.224, 0.225], np.float32)
|
| 63 |
+
import numpy as np
|
| 64 |
+
from PIL import Image
|
| 65 |
+
from ai_edge_litert.interpreter import Interpreter
|
| 66 |
+
|
| 67 |
+
orig = Image.open("photo.jpg").convert("RGB")
|
| 68 |
+
a = np.asarray(orig.resize((320, 320)), np.float32)
|
| 69 |
+
a = a / a.max() # per-image max, then ImageNet
|
| 70 |
+
x = ((a - MEAN) / STD).transpose(2, 0, 1)[None] # [1,3,320,320]
|
| 71 |
+
|
| 72 |
+
it = Interpreter(model_path="u2net_fp16.tflite"); it.allocate_tensors()
|
| 73 |
+
it.set_tensor(it.get_input_details()[0]["index"], x); it.invoke()
|
| 74 |
+
m = it.get_tensor(it.get_output_details()[0]["index"])[0, 0] # [320,320], [0,1]
|
| 75 |
+
alpha = Image.fromarray((m * 255).astype(np.uint8)).resize(orig.size)
|
| 76 |
+
cutout = orig.copy(); cutout.putalpha(alpha) # foreground on transparency
|
| 77 |
+
cutout.save("cutout.png")
|
| 78 |
```
|
| 79 |
|
| 80 |
A complete Android sample (live camera + gallery background removal) is available in
|