查看 Markdown

SegFormer

SegFormer 是一个语义分割 transformer,它把分层的 Mix Transformer(MiT)编码器和一个轻量的 all-MLP 解码 head 配在一起,避开了早期分割 transformer 需要的笨重解码器和固定位置编码。LibreYOLO 只把它用于一个任务,语义分割,覆盖六种尺寸。

任务
semantic
尺寸
安装
pip install libreyolo
支持层级
已支持,自 v 起。起支撑作用的可训练家族:在 CI 里保持绿色,功能视情况落地。
上游
SegFormer,由 NVIDIA 发布,采用 NVIDIA Source Code License (non-commercial, research or evaluation only) 许可。论文源码
许可
代码采用 Apache-2.0,权重采用 NVIDIA Source Code License (non-commercial, research or evaluation only)。商用

安装

SegFormer 不需要任何可选 extra。它导入的一切都在基础安装里。

bash
pip install libreyolo

预测

权重在首次使用时从 Hugging Face 下载,并缓存在本地。

Python
from libreyolo import LibreYOLO, SAMPLE_IMAGE model = LibreYOLO("LibreSegformerb0-sem.pt")result = model(SAMPLE_IMAGE, save=True) mask = result.semantic_maskprint(mask.data.shape, mask.classes)
CLI
libreyolo predict model=LibreSegformerb0-sem.pt source=https://raw.githubusercontent.com/LibreYOLO/libreyolo/release/libreyolo/assets/parkour.jpg save=True

result.semantic_mask 携带稠密的类别图:.data 是一个按原图尺寸给出的 (H, W) 类别 id 张量,.classes 列出实际出现的类别 id。result.boxesNone,因为没有逐实例的检测结果。confiou 为了 API 一致而被接受,但不会 改变输出:模型给每个像素返回一个类别,而不是需要过滤或去重的逐实例检测。数据源、 流式处理和结果处理见预测

变体

六种尺寸,b0 到 b5,每一步都把 Mix Transformer 编码器加宽、加深,同时保持同样的 all-MLP 解码 head 设计。

上面的每个文件目前都在 LibreYOLO 组织中,并会在首次使用时下载。

训练

train() 默认对已发布的检查点(checkpoint)做微调。改为不给 LibreSegformer(...)model_path,它就会用随机初始化的编码器和 head 来构建, 从零开始训练,这是拿到不带任何预训练检查点非商用限制的权重的唯一途径(见 许可证)。

Python(微调)
from libreyolo import LibreYOLO model = LibreYOLO("LibreSegformerb0-sem.pt")model.train(data="my-dataset.yaml", epochs=160, imgsz=512, batch=8)
CLI
libreyolo train model=LibreSegformerb0-sem.pt data=my-dataset.yaml \  epochs=160 imgsz=512 batch=8
从零训练
from libreyolo.models.segformer.model import LibreSegformer # 不传 model_path:随机初始化,什么都不下载。这是拿到不带预训练# 检查点非商用条款的权重的唯一途径model = LibreSegformer(size="b0", nb_classes=150)model.train(data="my-dataset.yaml", epochs=160, imgsz=512, batch=8)
多卡训练
libreyolo train model=LibreSegformerb0-sem.pt data=my-dataset.yaml \  epochs=160 device=0,1 batch=16

不做改动的话,训练器遵循 SegFormer 论文的 ADE20K 配方:AdamW,骨干用一个基础 学习率,解码 head 用它的 10 倍学习率训练,除 LayerNorm 和 Mix-FFN 位置卷积之外 处处施加权重衰减,再配一个带预热的线性衰减调度。更大的几个尺寸,b3 到 b5,收敛性 尚未端到端验证。

数据集、数据增强、多卡训练和日志器见训练

验证

val() 返回一个由 metrics/ 开头的键组成的字典:mIoU 和像素精度,在任何采用你 训练所用格式的数据集上测量。

Python
from libreyolo import LibreYOLO model = LibreYOLO("LibreSegformerb0-sem.pt")metrics = model.val(data="my-dataset.yaml") print(metrics["metrics/mIoU"])print(metrics["metrics/pixel_accuracy"])
CLI
libreyolo val model=LibreSegformerb0-sem.pt data=my-dataset.yaml

导出

任务ONNXTorchScriptExecuTorchTensorRTOpenVINOPaddleMNNRKNNncnnTFLiteCoreMLCore AI
semanticsemantic to ONNX:支持semantic to TorchScript:支持semantic to ExecuTorch:支持semantic to TensorRT:支持semantic to OpenVINO:支持semantic to Paddle:不支持semantic to MNN:不支持semantic to RKNN:不支持semantic to ncnn:不支持semantic to TFLite:不支持semantic to CoreML:不支持semantic to Core AI:不支持

导出的产物按文件后缀经由 LibreYOLO() 重新加载,所以一个 .onnx.engine 文件的表现和检查点一样,返回同样的 Results导出列出了每种格式 接受的参数。

Python
from libreyolo import LibreYOLO model = LibreYOLO("LibreSegformerb0-sem.pt")model.export(format="onnx", imgsz=512)model.export(format="tensorrt", imgsz=512, half=True)
CLI
libreyolo export model=LibreSegformerb0-sem.pt format=onnx imgsz=512libreyolo export model=LibreSegformerb0-sem.pt format=tensorrt imgsz=512 half=True
使用导出的文件
from libreyolo import LibreYOLO, SAMPLE_IMAGE # 工厂按文件后缀分发,所以导出的产物加载方式和任何检查点一样,# 返回的也是同一个 Results 对象model = LibreYOLO("LibreSegformerb0-sem.onnx")result = model(SAMPLE_IMAGE) print(result.semantic_mask.data.shape)

检查点

这个家族已发布的全部权重文件。

上面的每个文件目前都在 LibreYOLO 组织中,并会在首次使用时下载。

许可证

请检查你所下载的具体权重在 Hugging Face 仓库中的许可。LibreYOLO 组织里的每个检查点都附有许可,同一家族内也不一定相同。该仓库是权威来源;以下摘要说明本页上次验证时适用的情况。

这里只说明涉及的许可证,不构成法律意见。如果答案对商用很重要,请自行阅读许可证并咨询法律顾问。

原始工作
SegFormer, NVIDIA
上游许可
NVIDIA Source Code License (non-commercial, research or evaluation only)
LibreYOLO 代码
MIT
权重
采用 NVIDIA Source Code License (non-commercial, research or evaluation only) 许可,重新发布在 huggingface.co/LibreYOLO
解读
The pretrained ADE20K checkpoints LibreYOLO hosts for this family are converted from NVIDIA's official SegFormer release under the NVIDIA Source Code License. That license permits redistributing the weights and derivative works, provided the license text and attribution notices travel with them, but it limits USE to non-commercial research or evaluation, a restriction its Section 3.2 carries forward into every derivative and that cannot be relicensed away: these weights are NOT covered by LibreYOLO's normal permissive terms, and that limitation binds you, not just LibreYOLO. LibreYOLO's own SegFormer implementation is a separate Apache-2.0 port of Hugging Face Transformers' code, unrelated to NVIDIA's repository, so a model you train from scratch with LibreSegformer(...).train(...) carries none of this restriction.

LibreSegformer 的编码器和解码 head 是对 Hugging Face Transformers 中采用 Apache-2.0 许可的 SegFormer 实现的 PyTorch 移植,而不是对 NVlabs/SegFormer 的 移植:英伟达的原始仓库从未被阅读或复制,这里列出它只是为了向论文作者致谢。只有 上面那些预训练检查点带有英伟达的非商用限制;架构和 LibreYOLO 自己的代码自始至终 都是 MIT。

引用

@inproceedings{xie2021segformer,
  title={SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers},
  author={Xie, Enze and Wang, Wenhai and Yu, Zhiding and Anandkumar, Anima and Alvarez, Jose M and Luo, Ping},
  booktitle={Neural Information Processing Systems (NeurIPS)},
  year={2021}
}

复制自作者在 github.com/NVlabs/SegFormer#citation 上提供的引用块。

已针对 LibreYOLO v1.5.0 验证。本页的支持表、检查点和基准测试数据由已发布的库和权重生成,并非手工编写。