lm3_leaf_segmenter__birefnet_hr_swinl_1024_leaf
BiRefNet (Swin-L backbone) fine-tuned to segment the lamina and petiole as one mask on single-leaf crops from herbarium specimens (binary mask, foreground = lamina + petiole). Not a LeafMachine3 default model.
LeafMachine3 segments the lamina, petiole, and leaf holes with YOLO26x-seg (lm3_leaf_segmenter__yolo26x_seg_1024), but we also experimented with BiRefNet, which is designed to segment thin, fine structures precisely. BiRefNet is a semantic segmentation architecture, not an instance segmentation architecture like YOLO26, so it is harder to integrate into the overall LeafMachine3 workflow. We provide these models anyway because they are still useful. There are two: one finds only the lamina (lm3_leaf_segmenter__birefnet_hr_swinl_1024_leaf_no_petiole), and one finds the lamina and petiole as one mask (lm3_leaf_segmenter__birefnet_hr_swinl_1024_leaf). We also experimented with combining the two to isolate the lamina from the petiole (last column of the Examples), but BiRefNet is already a heavy model, and its precision gain did not make the extra computational cost worthwhile for LeafMachine3. Do whatever you want with them.
LM3 metadata
| Field | Value |
|---|---|
| LM3 action | leaf_segmenter (alternate; no LeafMachine3 stage runs it) |
| LM3_default_model | false |
| LM3_version | none (no LeafMachine3 release loads this model) |
| Model key | birefnet_hr_swinl_1024_leaf (training run birefnet-hr-1024-leaf-it2, checkpoint epoch 170) |
| Input | single-leaf crop, resized (not letterboxed) to 1024 x 1024, RGB scaled to [0, 1]; ImageNet mean/std normalization is inside the graph; tensor input_rgb01 [1, 3, 1024, 1024] float32 |
| Output | prob [1, 1, 1024, 1024], foreground probability (sigmoid applied); resize to the crop, threshold 0.5 |
| Foreground | lamina + petiole (one mask; holes are background) |
| Trained | 2026-10-15 |
| Iteration | 2 |
Architecture
| Component | Detail |
|---|---|
| Network | BiRefNet (bilateral reference): localization module plus a reconstruction decoder guided by in-image and gradient references, with deformable-convolution ASPP blocks |
| Backbone | Swin Transformer Large (swin_v1_l) |
| Starting weights | BiRefNet_HR-general-epoch_130 (general high-resolution background removal, trained at 2048 x 2048) |
| Fine-tuning | full network, at 1024 x 1024 |
| Output | multi-scale predictions during training; the final full-resolution map at inference |
Credits
- BiRefNet (Peng Zheng et al., MIT): architecture, training code and the
BiRefNet_HR-generalweights this model starts from. https://github.com/ZhengPeng7/BiRefNet - Zheng et al., "Bilateral Reference for High-Resolution Dichotomous Image Segmentation", CAAI Artificial Intelligence Research, 2024.
- Swin Transformer: Liu et al., "Swin Transformer: Hierarchical Vision Transformer using Shifted Windows", 2021.
Files
| Folder | File | Use |
|---|---|---|
pytorch/ | epoch_170.pth | BiRefNet state dict; load with the BiRefNet repo (swin_v1_l, size 1024); retraining |
onnx/ | model.onnx | onnxruntime, opset 17, batch 1; deformable convolutions decomposed into standard ops (no custom ops) |
torchscript/ | model.torchscript | torch.jit.load(..., map_location="cuda"); traced on CUDA and runs on CUDA only; needs torchvision's ops (import torchvision first) |
openvino/ | model_openvino_model/ | OpenVINO IR (fp32), converted from the ONNX |
docs/ | panel_*.jpg | example panels (see Examples) |
SHA-256 of every file is in manifest.json. There is no CoreML export: coremltools cannot convert BiRefNet's deformable convolutions.
Training data
Dataset LM3_Leaf-birefnet-leafbg, derived from LM3_Leaf (built 2026-06-26 from 5 Labelbox segmentation projects, 16,895 single-leaf crops, frozen 80/10/10 split, salt lm3, class hash 7f2c49ca64d9): one binary mask per crop, foreground = Leaf and Petiole polygons minus Hole polygons. Crops with no foreground are skipped. Train and val built 2026-08-09; test built 2026-10-21 from the LM3_Leaf test split for the evaluation below. Images and masks are not distributed.
| Split | Crops |
|---|---|
| train | 13,477 |
| val | 1,709 |
| test | 1,703 |
Training
Fine-tuned from epoch 130 to 170 (40 epochs) on the train split, batch 4, 1024 x 1024, AdamW, learning rate 1e-5, BiRefNet's general background-removal loss preset (binary cross-entropy 30, IoU 0.5, MAE 100, SSIM 10), with the contour-friendly loss switch over the final 20 epochs. Published checkpoint: the final epoch.
Evaluation
Scored against the hand-drawn LM3_Leaf polygons rasterized to this model's foreground, at native crop resolution, through the exported ONNX (fp32, onnxruntime CUDA); metrics are means over crops.
| Split | Crops | Dice (mean) | Dice (median) | IoU (mean) | Precision | Recall | MAE |
|---|---|---|---|---|---|---|---|
| val | 1,709 | 0.978 | 0.988 | 0.959 | 0.977 | 0.980 | 0.0160 |
| test | 1,703 | 0.978 | 0.988 | 0.960 | 0.979 | 0.979 | 0.0159 |
0.1% of test crops score below 0.5 Dice. Export parity with the PyTorch checkpoint (mask IoU at threshold 0.5, worst crop): ONNX 0.99995 (24 val crops, CPU and CUDA), TorchScript 1.00000 and OpenVINO 0.99996 (8 val crops).
Examples
The same eight Leaf_WHOLE crops on both BiRefNet cards: plant_detector crops from LeafMachine3 example runs, four of them the crops on the lm3_leaf_segmenter__yolo26x_seg_1024 card. Columns: the crop | the lamina + petiole model | the lamina-only model | the two combined: lamina (green) from the lamina-only model, petiole (magenta) = the bulk of what the lamina + petiole model adds, after a morphological opening removes the thin ring where the two models' edges disagree. Small crops are shown enlarged.
Use in LeafMachine3
No LeafMachine3 release runs this model; the leaf_segmenter stage runs YOLO26x-seg. To run it on a leaf crop:
import cv2, numpy as np, onnxruntime as ort
from huggingface_hub import hf_hub_download
path = hf_hub_download("phyloforfun/lm3_leaf_segmenter__birefnet_hr_swinl_1024_leaf", "onnx/model.onnx")
sess = ort.InferenceSession(path, providers=["CUDAExecutionProvider", "CPUExecutionProvider"])
bgr = cv2.imread("leaf_crop.jpg")
x = cv2.resize(cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB), (1024, 1024)).astype(np.float32) / 255.0
prob = sess.run(None, {"input_rgb01": x.transpose(2, 0, 1)[None]})[0][0, 0]
mask = cv2.resize(prob, (bgr.shape[1], bgr.shape[0])) > 0.5
The fp32 ONNX is about 1 GB; with onnxruntime's CUDA provider it uses about 17 GB of GPU memory (about 4 GB with the TensorRT provider), and on a CPU it takes several seconds per crop.
Citation
Weaver, W. N. (2026) LeafMachine3. https://leafmachine.org