查看 Markdown

EfficientNetV2

EfficientNetV2 是一个图像分类器,它的深度、宽度以及每个阶段的模块选择都是由神经架构搜索找出来的,同时优化精度和训练速度,而不是只优化精度。LibreYOLO 只支持它的一个任务:分类。

任务
classify
尺寸
b0, b1, b2, b3 at 224 to 300 px
安装
pip install libreyolo
支持层级
已支持,自 v 起。起支撑作用的可训练家族:在 CI 里保持绿色,功能视情况落地。
上游
EfficientNetV2,由 Google 发布,采用 Apache-2.0 许可。论文源码
许可
代码采用 Apache-2.0,权重采用 Apache-2.0。商用

安装

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

bash
pip install libreyolo

预测

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

Python
from libreyolo import LibreYOLO, SAMPLE_IMAGE model = LibreYOLO("LibreEfficientNetV2b0-cls.pt")result = model(SAMPLE_IMAGE, save=True) print(result.probs.top1, result.probs.top1conf)print(result.probs.top5)
CLI
libreyolo predict model=LibreEfficientNetV2b0-cls.pt source=cat.jpg save=True

返回的 Results 对象和每个家族返回的都是同一个,所以换成另一个模型只是一行的改动。分类器不带检测框,也不带掩码:result.probs 保存整图的预测,包含 top1top5top1conftop5confconfioumax_det 出于 API 一致性会被接受,但不起任何作用,因为在单个概率向量上既没有可以卡阈值的东西,也没有可以抑制的东西。关于输入源、流式处理和结果处理,见预测

变体

四种尺寸,b0 到 b3,每一种都在自己的分辨率和裁剪比例下评测,而不是整个家族共用一个输入尺寸。选哪个尺寸就是一次直接的参数量换精度的取舍。任务是固定的:每种尺寸都只覆盖分类。每种尺寸的权重文件名都以 -cls.pt 结尾,工厂读的正是这个后缀,据此路由到这个家族;不需要 task= 参数。

训练

微调从已发布的 ImageNet 骨干开始,并自动把最后的分类层重建成目标数据集的类别数。除非显式设置,imgsz 默认取该尺寸自己的评测分辨率。

Python
from libreyolo import LibreYOLO model = LibreYOLO("LibreEfficientNetV2b0-cls.pt")model.train(data="imagenette160", epochs=5)
CLI
libreyolo train model=LibreEfficientNetV2b0-cls.pt data=imagenette160 epochs=5
Multi-GPU
libreyolo train model=LibreEfficientNetV2b0-cls.pt data=imagenette160 \  epochs=50 device=0,1 batch=-1

不去动它的话,训练器会用 AdamW 跑 100 轮,lr0=1e-3,批大小 64,连续 50 轮没有提升就早停。data 接受一个数据集根目录(train/val/,每个类别一个文件夹)、一个像 imagenette160 这样的已知短名称,或者一个 .zip URL。这里不支持 lora=True,传进去会抛异常,因为 LibreYOLO 里的 LoRA 针对的是带 nn.Linear 层的 transformer 组件,而这个家族的 MBConv 模块里没有这种层。

关于数据集、数据增强、多卡训练和 logger,见训练

验证

val() 返回一个由 metrics/ 键组成的字典。对分类来说就是验证集上的 top-1 和 top-5 精度。

Python
from libreyolo import LibreYOLO model = LibreYOLO("LibreEfficientNetV2b0-cls.pt")metrics = model.val(data="imagenette160") print(metrics["metrics/accuracy_top1"])print(metrics["metrics/accuracy_top5"])
CLI
libreyolo val model=LibreEfficientNetV2b0-cls.pt data=imagenette160

导出

任务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 文件的表现就像一个检查点,返回同样的 Results导出列出了每种格式都接受的参数,以及少数格式额外加上的参数。

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

检查点

这个家族已发布的每一个权重文件。

文件输入(px)权重许可
classify
LibreEfficientNetV2b0-cls.pt224apache-2.0
LibreEfficientNetV2b1-cls.pt240apache-2.0
LibreEfficientNetV2b2-cls.pt260apache-2.0
LibreEfficientNetV2b3-cls.pt300apache-2.0

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

许可证

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

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

原始工作
EfficientNetV2, Google
上游许可
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, and weights you train yourself on your own data are yours. The architecture is Google's design, whose reference implementation at google/automl is also Apache-2.0; LibreYOLO's implementation follows the block definitions, TensorFlow "SAME" padding and naming in timm, whose tf_efficientnetv2_b{0,1,2,3} ImageNet-1k weights (ported by Ross Wightman, no ImageNet-21k or extra data) are licensed Apache-2.0 and are what LibreYOLO ships.

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