Image Segmentation
LiteRT
LiteRT
LiteRT
on-device
android
gpu
line-segment-detection
mlsd
wireframe
mobilenetv2
Instructions to use litert-community/M-LSD-tiny-LiteRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use litert-community/M-LSD-tiny-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
Add minimal usage snippets (Kotlin + Python)
Browse files
README.md
CHANGED
|
@@ -43,6 +43,49 @@ GPU-clean. Result: banned ops NONE, all tensors ≤4D, tflite-vs-torch corr **1.
|
|
| 43 |
Resize to 512×512, append a 4th channel of ones, scale `(x/127.5) - 1`, NCHW. Decode: sigmoid the center map,
|
| 44 |
3×3 max NMS, threshold (0.10), displacement → endpoints, filter by length, ×2 to 512-space.
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
## License
|
| 47 |
|
| 48 |
[Apache-2.0](https://github.com/navervision/mlsd/blob/main/LICENSE). Upstream:
|
|
|
|
| 43 |
Resize to 512×512, append a 4th channel of ones, scale `(x/127.5) - 1`, NCHW. Decode: sigmoid the center map,
|
| 44 |
3×3 max NMS, threshold (0.10), displacement → endpoints, filter by length, ×2 to 512-space.
|
| 45 |
|
| 46 |
+
## Minimal usage
|
| 47 |
+
|
| 48 |
+
**Android (Kotlin, CompiledModel GPU)**
|
| 49 |
+
|
| 50 |
+
```kotlin
|
| 51 |
+
val model = CompiledModel.create(context.assets, "mlsd_fp16.tflite",
|
| 52 |
+
CompiledModel.Options(Accelerator.GPU), null)
|
| 53 |
+
val inputs = model.createInputBuffers(); val outputs = model.createOutputBuffers()
|
| 54 |
+
inputs[0].writeFloat(x) // [1,4,512,512] NCHW: RGB + ones channel, x/127.5 - 1
|
| 55 |
+
model.run(inputs, outputs)
|
| 56 |
+
val tpMap = outputs[0].readFloat() // [1,9,256,256]: ch0 center, ch1-4 displacement
|
| 57 |
+
// sigmoid + 3x3 NMS + displacement -> segments: port of the Python decode below.
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
**Python (desktop verification)**
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
import numpy as np
|
| 64 |
+
from PIL import Image
|
| 65 |
+
from scipy.ndimage import maximum_filter
|
| 66 |
+
from ai_edge_litert.interpreter import Interpreter
|
| 67 |
+
|
| 68 |
+
im = Image.open("photo.jpg").convert("RGB").resize((512, 512))
|
| 69 |
+
a = np.asarray(im, np.float32)
|
| 70 |
+
a = np.concatenate([a, np.ones((512, 512, 1), np.float32)], -1) # 4th channel of ones
|
| 71 |
+
x = ((a.transpose(2, 0, 1)[None] / 127.5) - 1.0).copy() # [1,4,512,512]
|
| 72 |
+
|
| 73 |
+
it = Interpreter(model_path="mlsd_fp16.tflite"); it.allocate_tensors()
|
| 74 |
+
it.set_tensor(it.get_input_details()[0]["index"], x); it.invoke()
|
| 75 |
+
tp = it.get_tensor(it.get_output_details()[0]["index"])[0] # [9,256,256]
|
| 76 |
+
|
| 77 |
+
center = 1 / (1 + np.exp(-tp[0])); disp = tp[1:5]
|
| 78 |
+
peak = (center == maximum_filter(center, 3)) & (center > 0.10) # 3x3 NMS + threshold
|
| 79 |
+
ys, xs = np.where(peak)
|
| 80 |
+
order = center[ys, xs].argsort()[::-1][:200] # top-200 centers
|
| 81 |
+
lines = []
|
| 82 |
+
for y, x0 in zip(ys[order], xs[order]):
|
| 83 |
+
dxs, dys, dxe, dye = disp[:, y, x0]
|
| 84 |
+
if np.hypot(dxs - dxe, dys - dye) > 20: # min segment length (px)
|
| 85 |
+
lines.append([(x0 + dxs) * 2, (y + dys) * 2, (x0 + dxe) * 2, (y + dye) * 2])
|
| 86 |
+
print(f"{len(lines)} line segments (x0,y0,x1,y1 in 512-space)")
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
## License
|
| 90 |
|
| 91 |
[Apache-2.0](https://github.com/navervision/mlsd/blob/main/LICENSE). Upstream:
|