ViT

经典的 Vision Transformer:一个纯 transformer,作用在固定大小的图像 patch 上,带一个学习得到的 class token,不含卷积。LibreYOLO 收录了四种 AugReg 预训练尺寸,用于图像分类。

任务
classify
尺寸
ti, s, b, l at 224 px
安装
pip install libreyolo
支持层级
仅推理,自 v 起。仅支持预测、验证和导出。训练相关的功能不适用。
上游
ViT,由 Google Research 发布,采用 Apache-2.0 许可。论文源码
许可
代码采用 Apache-2.0,权重采用 Apache-2.0。商用

安装

ViT 不需要任何可选 extra。它导入的所有东西都在基础安装里。

bash
pip install libreyolo

预测

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

Python
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)
CLI
libreyolo predict model=LibreViTti-cls.pt source=https://raw.githubusercontent.com/LibreYOLO/libreyolo/release/libreyolo/assets/parkour.jpg save=True

分类器返回的是 result.probs 而不是 result.boxestop1top5 给出类别 索引,top1conftop5conf 给出对应的置信度。预处理会缩放并中心裁剪到固定的 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 精度。

Python
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"])
CLI
libreyolo val model=LibreViTti-cls.pt data=imagenet-1k/

导出

任务ONNXTorchScriptExecuTorchTensorRTOpenVINOPaddleMNNRKNNncnnTFLiteCoreMLCore AI
classifyclassify 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导出 列出了每种格式都接受的参数,以及其中少数几种额外加的参数。

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

检查点

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

文件输入(px)权重许可
classify
LibreViTti-cls.pt224apache-2.0
LibreViTs-cls.pt224apache-2.0
LibreViTb-cls.pt224apache-2.0
LibreViTl-cls.pt224apache-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 上提供的引用块。

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