M-LSD line segments
M-LSD (NAVER, AAAI 2022): finds the straight line segments of a photo (rooms, buildings) with a small MobileNetV2 network and draws them as a 1-pixel line map for the MLSD ControlNet; returns the segments too. Apache-2.0 code and weights.
experimental Fork of huggingface/controlnet_aux@b1aac31 · backends: portable, kernels
- Paper Towards Light-weight and Real-time Line Segment Detection AAAI 2022 · first released 1 Jun 2021
Checkpoint mlsd
Paper 2021 Added in 0.2.0 Evaluated with 0.2.0
Use it
pip install fastforks Lean install with only what this model needs: pip install fastforks-core
from fastforks import edges
model = edges.load("mlsd") # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.edges # also: lines
result.backend # what ran: mode, device, dtype, kernels fastforks bench mlsd Times the model on this machine. load(…, mode="portable") or mode="kernels" picks a backend; usage guide.
Speed and memory
Every backend on one GPU (RTX 5060 Ti 16 GB) and the same input, the authors’ code included. Kernels runs fp16. Portable runs fp16 on CUDA.
| Backend | dtype | Time per call (ms) | vs original fp16 | Peak memory allocated / reserved | Load (s) | First call (s) |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | not measured | – | – | – | – |
| Original fp16torch.autocast(float16) around the call; huggingface/controlnet_aux@b1aac318b15c mlsd, host OpenCV resize, float post-processing | fp16 | 35.6 | – | 185 MiB/212 MiB | 0.1 | 0.30 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 9.33 | 3.8× | 127 MiB/170 MiB | 0.2 | 0.01 |
| Kernels | not measured | – | – | – | – | |
| Portable + torch.compile | not measured here | – | – | – | – | |
| TensorRT | not measured here | – | – | – | – |
Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-14. First call, 3 warm-up, peak reset, median of 3 rounds x median of 5. Ratios divide two
times of this table (same GPU, same input). Original fp16 is the authors’ own precision option, else torch.autocast. torch.compile and TensorRT: build time instead of load time. TensorRT memory (engine, not comparable) is the GPU memory the engine took, read from the driver because TensorRT allocates outside PyTorch, so it can't be compared with the PyTorch peaks of the other rows.
Other devices
| Device | Backend | dtype | ms / call | Peak memory | Output vs reference | Input |
|---|---|---|---|---|---|---|
| Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14 | Original | fp32 | 55.3 | 1,500 MiB process | – | 1024x1024 |
| Portable | fp16 | 44.6 | 1,373 MiB process | matches | 1024x1024 | |
| Portable | fp32 | 50.0 | 1,371 MiB process | matches | 1024x1024 | |
| Kernels | fp32 | not implemented | – | 1024x1024 | ||
CUDA memory is torch’s peak allocated / reserved; Apple has no such counter, so the Mac rows show the process footprint (weights, Python and torch included). “Output vs reference” compares the device’s output with the stored golden output under the card’s tolerance; – = not checked on that device. Kernels “not implemented” = no kernels for this model on that backend yet: it runs portable there.
Licence and weights
Commercial use allowed
- Code
- Apache-2.0 LICENSE
- Weights
- Apache-2.0 source
- Training data
- Not stated for this checkpoint; the M-LSD paper trains on the Wireframe dataset (Huang et al. 2018, no licence stated).
Our reading of the licences, not legal advice.
Weights
mlsd_large_512_fp32.pth 6.3 MB· hosted on raw.githubusercontent.comfastforks downloads and checks these files on first use (fastforks download mlsd).
Accuracy
The test set through the authors’ code and through ours: same checkpoint, same protocol. The distance between the original’s fp32 and fp16 runs is what half precision alone costs; our runs should stay within it.
YorkUrban test (102 images)
line segment sAP@5 / @10 / @15 %
| Run | dtype | sap5 ↑ | sap10 ↑ | sap15 ↑ | Δ sap5 vs fp32 original | images | ms / image |
|---|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 6.69 | 13.44 | 17.01 | reference | 102 | 18.3 |
| Original fp16 | not measured | ||||||
| Portable fp32 | fp32 | 6.69 | 13.44 | 17.02 | +0.00 | 102 | 95.9 |
| Portable | not measured | ||||||
| Kernels | not measured | ||||||
All 102 YorkUrban images in the DT-LSD release. Each M-LSD mode uses 512-pixel short-side detection, cubic resizing, a score and length threshold of 0.1, and up to 200 segments. Structural AP matches endpoints on a 256 x 256 coordinate grid, testing both endpoint orders and matching each annotation at most once. Original = the authors’ code, same checkpoint and protocol. Measured on original fp32: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-18; portable fp32: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 3bf0bac7, 2026-11-18.
Closeness to the original
Beyond the test-set scores: the raw output of ours against the original’s fp32 output on the same input, pixel by pixel. CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.25 (absolute error of the output edges).
| Check | Tolerance | Measured |
|---|---|---|
| CPU fp32, max |diff| | ≤ 0.002 | not recorded |
| cpu_outliers | ≤ 0.001 | not recorded |
| GPU default dtypes, mean |diff| | ≤ 0.02 | 0 |
| GPU default dtypes, p99 |diff| | ≤ 0.25 | 0 |
Recorded for mlsd on RTX 5060 Ti, torch 2.10.0+cu130, 2026-11-17, with these kernels: cudnn_hacccutlass_convigev
Details
Mobile line segment detector (Gu et al., AAAI 2022; controlnet_aux `MLSDdetector`, ComfyUI's "M-LSD Lines"): a MobileNetV2 network that predicts segment centers and end-point displacements; returns the straight segments and the 1-pixel line map ControlNet's MLSD model takes.
- Input / output
- Input: one RGB image. Output: a line / edge map
[H, W]in [0, 1] at the input size (1 = line), the ControlNet preprocessors (line art, HED, PiDiNet, TEED / Anyline); M-LSD also returns straight line segments. - Input size
- any H x W >= 32, padded to a multiple of 32; tested up to 1080x1920 (127 MiB allocated)
- Options
short_side=512>= 64· changes the outputthr_v=0.10.0 to 1.0· changes the outputthr_d=0.1>= 0.0· changes the output
- Outputs
edges,lines- CUDA kernels
igevIGEV stereo: convolution epilogues, correlation lookups, GRU gatescutlass_convCUTLASS convolutions with the recurrent update (GRU) fused incudnn_haccconvolutions with fp16 accumulation (cuDNN)
- Apple (MPS)
- fp32, fp16
Full reference, generated from the repository
M-LSD (NAVER, AAAI 2022): finds the straight line segments of a photo (rooms, buildings) with a small MobileNetV2 network and draws them as a 1-pixel line map for the MLSD ControlNet; returns the segments too. Apache-2.0 code and weights.
Commercial use: yes.
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
mlsd | synthetic (1080x1920) | original portable kernels | 35.6 9.33 – | 185 / 212 127 / 170 – |
mlsd | synthetic (1024x1024) | original portable kernels portable + torch.compile TensorRT fp16 | 20.6 7.65 – 4.61 planned for 0.2.1 | 113 / 132 72 / 90 – 101 / 130 – |
Details
- Original code: https://github.com/huggingface/controlnet_aux at
b1aac318b15c - Code licence: Apache-2.0 (permissive); files:
LICENSE - Weights licence: Apache-2.0
- Licence evidence: https://github.com/lhwcv/mlsd_pytorch/blob/2312205254e66911703decf775f626995d260f17/LICENSE ; https://github.com/lhwcv/mlsd_pytorch/tree/2312205254e66911703decf775f626995d260f17/models
- Training data: Not stated for this checkpoint; the M-LSD paper trains on the Wireframe dataset (Huang et al. 2018, no licence stated).
- Options:
short_side= 512 (>= 64; alsoresolution; alsodetect_resolution),thr_v= 0.1 (0.0 to 1.0),thr_d= 0.1 (>= 0.0) - Kernel libraries:
igev,cutlass_conv,cudnn_hacc - Variants:
mlsd - Not in this release (planned for 0.2.1):
mlsdTensorRT export: TensorRT builds the engine but its CNN outputs diverge from portable FP32 on real images - Outputs: edges, lines
- Input size: any H x W >= 32, padded to a multiple of 32
- Tested up to (portable, fp16, RTX 5060 Ti 16 GB):
mlsd1080x1920 (0.1 GiB peak allocated, 0.2 reserved) mlsd1024x1024, first call: portable + torch.compile 11 s (cold Inductor cache).mlsdoriginal 1080x1920, original 1024x1024: torch.autocast(float16) around the call; huggingface/controlnet_aux@b1aac318b15c mlsd, host OpenCV resize, float post-processingmlsdload / first call: original 0.21 s / 0.057 s, portable 0.17 s / 0.011 s- Protocol, first call, 3 warm-up, peak reset, median of 3 rounds x median of 5
Measured
All 102 YorkUrban images in the DT-LSD release. Each M-LSD mode uses 512-pixel short-side detection, cubic resizing, a score and length threshold of 0.1, and up to 200 segments. Structural AP matches endpoints on a 256 x 256 coordinate grid, testing both endpoint orders and matching each annotation at most once. Original = the original code named under Details, same checkpoint and protocol.
| variant | dataset | original fp32 | portable fp32 | note |
|---|---|---|---|---|
mlsd | YorkUrban test (102 images), line segment sAP@5 / @10 / @15 % | 6.69 / 13.44 / 17.01 | 6.69 / 13.44 / 17.02 |
Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 3bf0bac7, 2026-11-18.
Parity
CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.25 (absolute error of the output edges).
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
mlsd | 0.0 / 0.0 | RTX 5060 Ti, torch 2.10.0+cu130 | cudnn_hacc@cudnn91501, cutlass_conv@988138e042cb:sm_120, igev@f075827d68c6:sm_120 |
Example output
No example rendered for this checkpoint yet.