Swin Transformer
Swin Transformer V1:一个分层的 vision transformer,注意力在移位的局部窗口(shifted local windows)内计算,而不是在整张图上计算。LibreYOLO 为图像分类提供四种尺寸。
- 任务
- classify
- 尺寸
- t, s, b, l at 224 px
- 安装
pip install libreyolo- 支持层级
- 仅推理,自 v 起。仅支持预测、验证和导出。训练相关的功能不适用。
- 许可
- 代码采用 Apache-2.0,权重采用 MIT。商用
安装
Swin 不需要任何可选 extra。它导入的所有东西都在基础安装里。
pip install libreyolo预测
权重在首次使用时从 Hugging Face 下载,并缓存在本地。
from libreyolo import LibreYOLO, SAMPLE_IMAGE model = LibreYOLO("LibreSwint-cls.pt")result = model(SAMPLE_IMAGE, save=True) probs = result.probsprint(probs.top1, probs.top1conf)print(probs.top5, probs.top5conf)libreyolo predict model=LibreSwint-cls.pt source=https://raw.githubusercontent.com/LibreYOLO/libreyolo/release/libreyolo/assets/parkour.jpg save=True分类器返回的是 result.probs 而不是 result.boxes:top1 和 top5 给出类别索引,top1conf 和 top5conf 给出它们的置信度。每种尺寸都固定为 224px 输入,因为最后一个注意力阶段就是按这个分辨率搭的;预测、验证和导出只要传入不同的 imgsz 都会报错。关于输入源、流式处理和结果处理,见预测。
变体
四种尺寸,从 tiny 到 large,由同一套移位窗口堆叠结构搭出来,区别在嵌入向量宽度和各阶段深度。large 在 ImageNet-22k 上预训练,再在 ImageNet-1k 上微调;另外三种直接在 ImageNet-1k 上训练。LibreYOLO 以仅推理的形式收录这个家族:预测、ImageNet 风格的 top-1/top-5 验证和导出都支持,上游的 ImageNet 训练配方没有实现。
验证
val() 在 ImageFolder 风格的划分上运行(一个带 train/ 和 val/ 子目录的目录,每个类别一个文件夹),返回 top-1 和 top-5 精度。
from libreyolo import LibreYOLO model = LibreYOLO("LibreSwint-cls.pt") # data 是一个带 train/ 和 val/ 类别文件夹划分的目录根# (ImageFolder 布局),不是数据集 YAMLmetrics = model.val(data="imagenet-1k/") print(metrics["metrics/accuracy_top1"])print(metrics["metrics/accuracy_top5"])libreyolo val model=LibreSwint-cls.pt data=imagenet-1k/导出
| 任务 | ONNX | TorchScript | ExecuTorch | TensorRT | OpenVINO | Paddle | MNN | RKNN | ncnn | TFLite | CoreML | Core AI |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| classify | classify to ONNX:支持 | classify to TorchScript:支持 | classify to ExecuTorch:支持 | classify to TensorRT:支持 | classify to OpenVINO:支持 | classify to Paddle:不支持 | classify to MNN:不支持 | classify to RKNN:不支持 | classify to ncnn:支持 | classify to TFLite:不支持 | classify to CoreML:不支持 | classify to Core AI:不支持 |
导出产物会按文件后缀通过 LibreYOLO() 重新加载回来,所以一个 .onnx 或 .engine 文件的表现就像一个检查点(checkpoint),返回同样的 Results。导出列出了每种格式都接受的参数,以及少数格式额外加上的参数。
from libreyolo import LibreYOLO model = LibreYOLO("LibreSwint-cls.pt")model.export(format="onnx")model.export(format="tensorrt", half=True)libreyolo export model=LibreSwint-cls.pt format=onnxlibreyolo export model=LibreSwint-cls.pt format=tensorrt half=Truefrom libreyolo import LibreYOLO, SAMPLE_IMAGE # 工厂按文件后缀路由,所以导出产物会像任何检查点一样加载,# 并返回同一个 Results 对象model = LibreYOLO("LibreSwint-cls.onnx")result = model(SAMPLE_IMAGE) print(result.probs.top1)检查点
这个家族已发布的每一个权重文件。
| 文件 | 输入(px) | 权重许可 |
|---|---|---|
| classify | ||
| LibreSwint-cls.pt | 224 | mit |
| LibreSwins-cls.pt | 224 | mit |
| LibreSwinb-cls.pt | 224 | mit |
| LibreSwinl-cls.pt | 224 | mit |
上面的每个文件目前都在 LibreYOLO 组织中,并会在首次使用时下载。
许可证
请检查你所下载的具体权重在 Hugging Face 仓库中的许可。LibreYOLO 组织里的每个检查点都附有许可,同一家族内也不一定相同。该仓库是权威来源;以下摘要说明本页上次验证时适用的情况。
这里只说明涉及的许可证,不构成法律意见。如果答案对商用很重要,请自行阅读许可证并咨询法律顾问。
- 原始工作
- Swin Transformer, Microsoft Research
- 上游许可
- MIT
- LibreYOLO 代码
- MIT
- 权重
- 采用 MIT 许可,重新发布在 huggingface.co/LibreYOLO
- 解读
- MIT is a permissive license, so these weights can be used in commercial and closed-source products. It asks only that you keep the license text and copyright notice with any copy you redistribute, and it carries no explicit patent grant. LibreYOLO's runtime code for this family is a derived port of the Apache-2.0 timm Swin implementation (Ross Wightman, huggingface/pytorch-image-models), kept parameter-name compatible so the tensors load unchanged; the four released Tiny/Small/Base/Large checkpoints are Microsoft's own MIT-licensed patch-4/window-7 classifiers. Code and weights therefore sit under two different permissive licenses, both of which allow commercial use.
引用
@inproceedings{liu2021Swin,
title={Swin Transformer: Hierarchical Vision Transformer using Shifted Windows},
author={Liu, Ze and Lin, Yutong and Cao, Yue and Hu, Han and Wei, Yixuan and Zhang, Zheng and Lin, Stephen and Guo, Baining},
booktitle={Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV)},
year={2021}
}复制自作者在 github.com/microsoft/Swin-Transformer#citing-swin-transformer 上提供的引用块。