ViT
经典的 Vision Transformer:一个纯 transformer,作用在固定大小的图像 patch 上,带一个学习得到的 class token,不含卷积。LibreYOLO 收录了四种 AugReg 预训练尺寸,用于图像分类。
- 任务
- classify
- 尺寸
- ti, s, b, l at 224 px
- 安装
pip install libreyolo- 支持层级
- 仅推理,自 v 起。仅支持预测、验证和导出。训练相关的功能不适用。
- 许可
- 代码采用 Apache-2.0,权重采用 Apache-2.0。商用
安装
ViT 不需要任何可选 extra。它导入的所有东西都在基础安装里。
pip install libreyolo预测
权重在首次使用时从 Hugging Face 下载,并缓存在本地。
from libreyolo import LibreYOLO, SAMPLE_IMAGE model = LibreYOLO("LibreViTti-cls.pt")result = model(SAMPLE_IMAGE, save=True) probs = result.probsprint(probs.top1, probs.top1conf)print(probs.top5, probs.top5conf)libreyolo predict model=LibreViTti-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 输入,用的是 timm 的 AugReg 评估配方:双三次插值,裁剪比例 0.9。数据源、
流式处理和结果处理见 预测。
变体
四种尺寸,从 tiny 到 large,共用同一张固定 224px、patch-16 的计算图,区别在于 嵌入向量宽度和 transformer 深度。LibreYOLO 以仅推理的形式收录这个家族:预测、 ImageNet 风格的 top-1/top-5 验证和导出都支持,AugReg 的微调配方没有实现。
验证
val() 跑在 ImageFolder 风格的划分上——一个带 train/ 和 val/ 子文件夹的
目录,每个类别一个文件夹——返回 top-1 和 top-5 精度。
from libreyolo import LibreYOLO model = LibreYOLO("LibreViTti-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=LibreViTti-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("LibreViTti-cls.pt")model.export(format="onnx")model.export(format="tensorrt", half=True)libreyolo export model=LibreViTti-cls.pt format=onnxlibreyolo export model=LibreViTti-cls.pt format=tensorrt half=Truefrom libreyolo import LibreYOLO, SAMPLE_IMAGE # 工厂按文件后缀分发,所以导出的产物加载方式和任何检查点一样,# 返回的也是同一个 Results 对象model = LibreYOLO("LibreViTti-cls.onnx")result = model(SAMPLE_IMAGE) print(result.probs.top1)检查点
这个家族已发布的全部权重文件。
| 文件 | 输入(px) | 权重许可 |
|---|---|---|
| classify | ||
| LibreViTti-cls.pt | 224 | apache-2.0 |
| LibreViTs-cls.pt | 224 | apache-2.0 |
| LibreViTb-cls.pt | 224 | apache-2.0 |
| LibreViTl-cls.pt | 224 | apache-2.0 |
上面的每个文件目前都在 LibreYOLO 组织中,并会在首次使用时下载。
许可证
请检查你所下载的具体权重在 Hugging Face 仓库中的许可。LibreYOLO 组织里的每个检查点都附有许可,同一家族内也不一定相同。该仓库是权威来源;以下摘要说明本页上次验证时适用的情况。
这里只说明涉及的许可证,不构成法律意见。如果答案对商用很重要,请自行阅读许可证并咨询法律顾问。
- 原始工作
- ViT, Google Research
- 上游许可
- Apache-2.0
- LibreYOLO 代码
- MIT
- 权重
- 采用 Apache-2.0 许可,重新发布在 huggingface.co/LibreYOLO
- 解读
- Apache-2.0 is a permissive license, so these weights can be used in commercial and closed-source products. It asks you to keep its license text and attribution notices with any copy of the weights you redistribute, and it grants a patent license. It places no obligation on your own application code. LibreYOLO's runtime code for this family is a derived port of the Apache-2.0 timm Vision Transformer implementation (Ross Wightman, huggingface/pytorch-image-models), kept checkpoint-compatible with the shipped tensors. The four AugReg checkpoints themselves are timm's Apache-2.0 conversion of Google Research's own AugReg pretraining, so the code and the weights carry the same permissive terms end to end.
引用
@article{dosovitskiy2020vit,
title={An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale},
author={Dosovitskiy, Alexey and Beyer, Lucas and Kolesnikov, Alexander and Weissenborn, Dirk and Zhai, Xiaohua and Unterthiner, Thomas and Dehghani, Mostafa and Minderer, Matthias and Heigold, Georg and Gelly, Sylvain and Uszkoreit, Jakob and Houlsby, Neil},
journal={ICLR},
year={2021}
}
@article{steiner2021augreg,
title={How to train your ViT? Data, Augmentation, and Regularization in Vision Transformers},
author={Steiner, Andreas and Kolesnikov, Alexander and and Zhai, Xiaohua and Wightman, Ross and Uszkoreit, Jakob and Beyer, Lucas},
journal={arXiv preprint arXiv:2106.10270},
year={2021}
}复制自作者在 github.com/google-research/vision_transformer#bibtex 上提供的引用块。