M-LSD-tiny β€” LiteRT (on-device line segment detection, fully-GPU)

M-LSD (NAVER, AAAI 2022) light-weight real-time line segment detection, converted to LiteRT and running fully on the CompiledModel GPU (ML Drift) on Android. Detects straight line segments β€” building edges, document borders, wireframes, room layout. The tiny variant (MobileNetV2 backbone, 0.62M params) is 1.4 MB in fp16.

M-LSD β€” input | detected line segments (on-device LiteRT GPU)

On-device (Pixel 8a, Tensor G3 β€” verified)

nodes on GPU 99 / 99 LITERT_CL (full residency)
inference ~2 ms (512Γ—512)
size 1.4 MB (fp16)
accuracy device-vs-PyTorch corr 0.997 (127 vs 128 lines decoded)
image[1,4,512,512] (RGB + ones channel, scaled to [-1,1]) β†’[GPU: MobileNetV2 U-Net]β†’ tpMap[1,9,256,256]

The output is a "TP map": channel 0 = line-center heatmap, channels 1–4 = start/end displacement. The decode (sigmoid + 3Γ—3 NMS over centers, displacement β†’ endpoints, Γ—2) runs on the host.

How it converts (litert-torch)

Pure CNN encoder-decoder. A single re-authoring: the decoder's F.interpolate(bilinear, align_corners=True) β†’ align_corners=False (the Mali delegate bans align_corners=True + half-pixel). MobileNetV2 has no max-pool (strided convs β†’ no PADV2), and the upsample is RESIZE_BILINEAR, not a transposed conv β†’ fully GPU-clean. Result: banned ops NONE, all tensors ≀4D, tflite-vs-torch corr 1.0, device-vs-torch corr 0.997.

Preprocessing & decode

Resize to 512Γ—512, append a 4th channel of ones, scale (x/127.5) - 1, NCHW. Decode: sigmoid the center map, 3Γ—3 max NMS, threshold (0.10), displacement β†’ endpoints, filter by length, Γ—2 to 512-space.

Minimal usage

Android (Kotlin, CompiledModel GPU)

val model = CompiledModel.create(context.assets, "mlsd_fp16.tflite",
    CompiledModel.Options(Accelerator.GPU), null)
val inputs = model.createInputBuffers(); val outputs = model.createOutputBuffers()
inputs[0].writeFloat(x)             // [1,4,512,512] NCHW: RGB + ones channel, x/127.5 - 1
model.run(inputs, outputs)
val tpMap = outputs[0].readFloat()  // [1,9,256,256]: ch0 center, ch1-4 displacement
// sigmoid + 3x3 NMS + displacement -> segments: port of the Python decode below.

Python (desktop verification)

import numpy as np
from PIL import Image
from scipy.ndimage import maximum_filter
from ai_edge_litert.interpreter import Interpreter

im = Image.open("photo.jpg").convert("RGB").resize((512, 512))
a = np.asarray(im, np.float32)
a = np.concatenate([a, np.ones((512, 512, 1), np.float32)], -1)   # 4th channel of ones
x = ((a.transpose(2, 0, 1)[None] / 127.5) - 1.0).copy()           # [1,4,512,512]

it = Interpreter(model_path="mlsd_fp16.tflite"); it.allocate_tensors()
it.set_tensor(it.get_input_details()[0]["index"], x); it.invoke()
tp = it.get_tensor(it.get_output_details()[0]["index"])[0]        # [9,256,256]

center = 1 / (1 + np.exp(-tp[0])); disp = tp[1:5]
peak = (center == maximum_filter(center, 3)) & (center > 0.10)    # 3x3 NMS + threshold
ys, xs = np.where(peak)
order = center[ys, xs].argsort()[::-1][:200]                      # top-200 centers
lines = []
for y, x0 in zip(ys[order], xs[order]):
    dxs, dys, dxe, dye = disp[:, y, x0]
    if np.hypot(dxs - dxe, dys - dye) > 20:                       # min segment length (px)
        lines.append([(x0 + dxs) * 2, (y + dys) * 2, (x0 + dxe) * 2, (y + dye) * 2])
print(f"{len(lines)} line segments (x0,y0,x1,y1 in 512-space)")

Performance

Measured on a Pixel 8a (Tensor G3, Android 16) with the standard TFLite benchmark_model tool β€” 10 warm-up runs then 50 timed runs, reported as the tool's mean.

Runtime Backend Graph on GPU Latency
LiteRT CompiledModel (LITERT_CL) GPU 99 / 99 ~2 ms
TFLite benchmark_model (TfLiteGpuDelegateV2) GPU (OpenCL) 99 / 99 26.3 ms
TFLite benchmark_model CPU (XNNPACK, 4 threads) β€” XNNPACK declined the graph

The two GPU rows are different runtimes, not a contradiction. The LITERT_CL figure is the one recorded when this model shipped, taken through LiteRT's own CompiledModel accelerator β€” the path the Kotlin sample app and the LiteRT API use. The TfLiteGpuDelegateV2 figure is the classic TFLite OpenCL delegate, measured with a tool anyone can download and re-run. They agree on how much of the graph the GPU takes; they disagree on speed, and the classic delegate is the slower of the two here. Read the TfLiteGpuDelegateV2 row as a reproducible floor, not as this model's speed on LiteRT.

XNNPACK declines these fp16 graphs β€” it reports failed to delegate DEPTHWISE_CONV_2D and then fails to allocate tensors β€” so there is no usable CPU number. Disabling XNNPACK falls back to reference kernels, which measured about 20Γ— slower than the GPU on models of this size and would not represent CPU inference anyone would ship.

Snapdragon NPU (Hexagon)

The NPU is 2.80x faster than the GPU (2.21 ms against 6.17 ms) and loads 9.00x faster (101 ms against 913 ms).

backend compiled inference (median / min) load
NPU (Hexagon v81) on-device JIT 2.21 ms / 2.13 ms 101 ms
GPU (Adreno) β€” 6.17 ms / 5.10 ms 913 ms

Measured on a Samsung Galaxy S26 (Snapdragon 8 Elite Gen 5 / SM8850, Hexagon v81, Android 16) with LiteRT CompiledModel 2.2.0, one accelerator per process, 5 warm-up runs then N=50 timed runs, median reported. Every run held thermal status NONE throughout. Headroom 0.69, where 1.0 is the throttling threshold.

The NPU rows ran the published file unchanged. LiteRT compiled it for the Hexagon on the device at first load. That first compile took 1.5 s here. The load column above is the cached load every later run pays. Recipe and the runtime libraries it needs: NPU guide.

GPU wiring: GPU guide.

Raspberry Pi 5 (CPU)

Measured on a Raspberry Pi 5 Model B Rev 1.1 (8 GB, Raspberry Pi OS 64-bit) with the LiteRT benchmark_model tool from litert-cli-nightly 0.2.0.dev20260805: CPU inference (XNNPACK, 4 threads), 3 invocations per file of 10 warm-up plus 50 timed runs (the tool caps a phase at 150 s, so very slow graphs run fewer β€” the Runs column is the actual timed total). The latency is the median across invocations; the spread is the min–max over all timed runs. No thermal throttling occurred during these runs (vcgencmd get_throttled stayed 0x0).

File Inference (median) Spread (min–max) Runs Peak memory
mlsd_fp16.tflite 106.6 ms 106.0–108.1 ms 150 75 MB

License

Apache-2.0. Upstream: navervision/mlsd; PyTorch port lhwcv/mlsd_pytorch.

Downloads last month
35
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Collection including litert-community/M-LSD-tiny-LiteRT