ResNet
ResNet 是一个由残差块搭出来的图像分类器,残差块就是那些跳跃连接(skip connection),它让网络可以多堆很多层,而不会出现很深的普通卷积堆叠原本会有的精度下降。LibreYOLO 只支持它的一个任务:分类。
- 任务
- classify
- 尺寸
- 18, 34, 50, 101 at 224 px
- 安装
pip install libreyolo- 支持层级
- 已支持,自 v 起。起支撑作用的可训练家族:在 CI 里保持绿色,功能视情况落地。
- 许可
- 代码采用 Apache-2.0,权重采用 Apache-2.0。商用
安装
ResNet 不需要任何可选 extra。它导入的所有东西都在基础安装里。
pip install libreyolo预测
权重在首次使用时从 Hugging Face 下载,并缓存在本地。
from libreyolo import LibreYOLO, SAMPLE_IMAGE model = LibreYOLO("LibreResNet50-cls.pt")result = model(SAMPLE_IMAGE, save=True) print(result.probs.top1, result.probs.top1conf)print(result.probs.top5)libreyolo predict model=LibreResNet50-cls.pt source=cat.jpg save=True返回的 Results 对象和每个家族返回的都是同一个,所以换成另一个模型只是一行的改动。分类器不带检测框,也不带掩码:result.probs 保存整图的预测,包含 top1、top5、top1conf 和 top5conf。conf、iou 和 max_det 出于 API 一致性会被接受,但不起任何作用,因为在单个概率向量上既没有可以卡阈值的东西,也没有可以抑制的东西。关于输入源、流式处理和结果处理,见预测。
变体
四种深度,全部以同样的方式训练和评测,所以选哪个就是一次直接的参数量换精度的取舍。任务是固定的:每种尺寸都只覆盖分类。每种尺寸的权重文件名都以 -cls.pt 结尾,工厂读的正是这个后缀,据此路由到这个家族;不需要 task= 参数。
训练
微调从已发布的 ImageNet 骨干开始,并自动把最后的分类层重建成目标数据集的类别数。
from libreyolo import LibreYOLO model = LibreYOLO("LibreResNet50-cls.pt")model.train(data="imagenette160", epochs=5)libreyolo train model=LibreResNet50-cls.pt data=imagenette160 epochs=5libreyolo train model=LibreResNet50-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 组件,而 ResNet 一个都没有。
关于数据集、数据增强、多卡训练和 logger,见训练。
验证
val() 返回一个由 metrics/ 键组成的字典。对分类来说就是验证集上的 top-1 和 top-5 精度。
from libreyolo import LibreYOLO model = LibreYOLO("LibreResNet50-cls.pt")metrics = model.val(data="imagenette160") print(metrics["metrics/accuracy_top1"])print(metrics["metrics/accuracy_top5"])libreyolo val model=LibreResNet50-cls.pt data=imagenette160导出
| 任务 | 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("LibreResNet50-cls.pt")model.export(format="onnx")model.export(format="tensorrt", half=True)libreyolo export model=LibreResNet50-cls.pt format=onnxlibreyolo export model=LibreResNet50-cls.pt format=tensorrt half=Truefrom libreyolo import LibreYOLO, SAMPLE_IMAGE # 工厂按文件后缀路由,所以导出产物会像任何检查点一样加载,# 并返回同一个 Results 对象model = LibreYOLO("LibreResNet50-cls.onnx")result = model(SAMPLE_IMAGE) print(result.probs.top1)检查点
这个家族已发布的每一个权重文件。
| 文件 | 输入(px) | 权重许可 |
|---|---|---|
| classify | ||
| LibreResNet18-cls.pt | 224 | apache-2.0 |
| LibreResNet34-cls.pt | 224 | apache-2.0 |
| LibreResNet50-cls.pt | 224 | apache-2.0 |
| LibreResNet101-cls.pt | 224 | apache-2.0 |
上面的每个文件目前都在 LibreYOLO 组织中,并会在首次使用时下载。
许可证
请检查你所下载的具体权重在 Hugging Face 仓库中的许可。LibreYOLO 组织里的每个检查点都附有许可,同一家族内也不一定相同。该仓库是权威来源;以下摘要说明本页上次验证时适用的情况。
这里只说明涉及的许可证,不构成法律意见。如果答案对商用很重要,请自行阅读许可证并咨询法律顾问。
- 原始工作
- ResNet, Microsoft Research Asia
- 上游许可
- 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 the original Microsoft Research Asia design; the pretrained weights LibreYOLO ships are timm's resnet{18,34,50,101}.a1_in1k reproduction (the "ResNet Strikes Back" A1 recipe), trained by Ross Wightman and the timm contributors on ImageNet-1k and licensed Apache-2.0 there. LibreYOLO's module and attribute names mirror timm so the state dict loads unchanged and inference matches bit-identically.
引用
@article{He2015,
author = {Kaiming He and Xiangyu Zhang and Shaoqing Ren and Jian Sun},
title = {Deep Residual Learning for Image Recognition},
journal = {arXiv preprint arXiv:1512.03385},
year = {2015}
}复制自作者在 github.com/KaimingHe/deep-residual-networks#citation 上提供的引用块。