当前位置: 首页 > news >正文

终极指南:convnext_tiny.in12k_ft_in1k 快速上手教程(附完整代码)

终极指南:convnext_tiny.in12k_ft_in1k 快速上手教程(附完整代码)

【免费下载链接】convnext_tiny.in12k_ft_in1k项目地址: https://ai.gitcode.com/hf_mirrors/timm/convnext_tiny.in12k_ft_in1k

convnext_tiny.in12k_ft_in1k 是一款基于 ConvNeXt 架构的图像分类模型,由 Ross Wightman 在 timm 库中实现。该模型先在 ImageNet-12k(包含 11821 个类别的 ImageNet-22k 子集)上进行预训练,然后在 ImageNet-1k 上进行微调,非常适合图像分类、特征提取等计算机视觉任务。

模型核心特性概览 🚀

关键技术参数

  • 模型类型:图像分类/特征骨干网络
  • 参数量:28.6M
  • 计算量(GMACs):4.5
  • 激活值(M):13.4
  • 输入尺寸:训练时 224×224,测试时 288×288
  • 支持数据集:ImageNet-1k(微调)、ImageNet-12k(预训练)

性能优势

在 RTX 3090 显卡上,该模型以 256 batch size 运行时可达到2433.7 样本/秒的推理速度,Top-1 准确率为 84.186%,Top-5 准确率为 97.124%,在轻量级模型中表现出色。

快速开始:环境准备 ⚙️

安装必要依赖

# 克隆仓库 git clone https://gitcode.com/hf_mirrors/timm/convnext_tiny.in12k_ft_in1k cd convnext_tiny.in12k_ft_in1k # 安装依赖 pip install timm torch pillow urllib3

模型文件说明

项目目录下包含以下核心文件:

  • 模型权重:model.safetensors、pytorch_model.bin
  • 配置文件:config.json(包含输入尺寸、均值/标准差等关键参数)
  • 说明文档:README.md(完整技术细节)

实战教程:三大核心功能 🔥

1. 图像分类(最常用场景)

通过以下代码可快速实现对任意图像的分类:

from urllib.request import urlopen from PIL import Image import timm import torch # 加载图像(可替换为本地图片路径) img = Image.open(urlopen( 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' )) # 加载预训练模型 model = timm.create_model('convnext_tiny.in12k_ft_in1k', pretrained=True) model = model.eval() # 获取模型专用数据转换(自动处理归一化和尺寸调整) data_config = timm.data.resolve_model_data_config(model) transforms = timm.data.create_transform(**data_config, is_training=False) # 执行推理 output = model(transforms(img).unsqueeze(0)) # 添加 batch 维度 top5_prob, top5_idx = torch.topk(output.softmax(dim=1)*100, k=5) # 输出结果 print("Top 5 预测类别及概率:") for prob, idx in zip(top5_prob[0], top5_idx[0]): print(f"类别 {idx}: {prob:.2f}%")

2. 特征图提取(用于可视化或下游任务)

提取模型中间层特征,可用于目标检测、语义分割等任务:

model = timm.create_model( 'convnext_tiny.in12k_ft_in1k', pretrained=True, features_only=True, # 启用特征提取模式 ) model = model.eval() output = model(transforms(img).unsqueeze(0)) # 输出为特征图列表 # 打印各层特征图形状 for i, feature_map in enumerate(output): print(f"特征层 {i+1} 形状: {feature_map.shape}") # 输出示例: # 特征层 1 形状: torch.Size([1, 96, 56, 56]) # 特征层 2 形状: torch.Size([1, 192, 28, 28]) # 特征层 3 形状: torch.Size([1, 384, 14, 14]) # 特征层 4 形状: torch.Size([1, 768, 7, 7])

3. 图像嵌入向量生成(用于相似度计算)

生成图像的固定长度向量表示,可用于检索、聚类等任务:

# 方法一:移除分类头直接输出特征 model = timm.create_model( 'convnext_tiny.in12k_ft_in1k', pretrained=True, num_classes=0, # 设为 0 移除最终分类层 ) # 方法二:显式调用特征提取接口 output = model.forward_features(transforms(img).unsqueeze(0)) # 未池化特征 output = model.forward_head(output, pre_logits=True) # 池化后特征向量 print(f"图像嵌入向量形状: {output.shape}") # 输出: torch.Size([1, 768])

进阶配置:优化推理性能 ⚡

调整输入尺寸

根据硬件性能和精度需求,可修改测试输入尺寸:

data_config['input_size'] = (3, 384, 384) # 增大尺寸可能提升精度,但增加计算量 transforms = timm.data.create_transform(**data_config, is_training=False)

使用混合精度推理

在支持的 GPU 上启用 AMP 加速:

with torch.cuda.amp.autocast(): output = model(transforms(img).unsqueeze(0).cuda())

模型对比:为何选择 convnext_tiny.in12k_ft_in1k?

模型Top1 准确率参数量(M)速度(样本/秒)
convnext_tiny.in12k_ft_in1k84.19%28.62433.7
convnext_small.in12k_ft_in1k85.17%50.21474.3
convnext_base.fb_in1k83.82%88.61054.0

相比同系列模型,convnext_tiny.in12k_ft_in1k 在速度与精度间取得了极佳平衡,适合边缘设备和实时应用场景。

引用与致谢

如果使用本模型,请引用以下论文:

@misc{rw2019timm, author = {Ross Wightman}, title = {PyTorch Image Models}, year = {2019}, publisher = {GitHub}, journal = {GitHub repository}, doi = {10.5281/zenodo.4414861}, howpublished = {\url{https://github.com/huggingface/pytorch-image-models}} } @article{liu2022convnet, author = {Zhuang Liu and Hanzi Mao and Chao-Yuan Wu and Christoph Feichtenhofer and Trevor Darrell and Saining Xie}, title = {A ConvNet for the 2020s}, journal = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)}, year = {2022}, }

本模型的训练得到了 TRC 项目和 Lambda Labs 云服务的支持。

【免费下载链接】convnext_tiny.in12k_ft_in1k项目地址: https://ai.gitcode.com/hf_mirrors/timm/convnext_tiny.in12k_ft_in1k

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

http://www.jsqmd.com/news/1348567/

相关文章:

  • 8.6 label
  • 上海全屋定制工厂店实地验证指南——设备、车间、报价的三维度判断 - 精彩城市
  • Brisk核心功能解析:双因素认证、附件上传与Radar保存技巧
  • 《Docker 容器化镜像安全管理 线上高并发排障实战》
  • 终极开源字体方案:如何用Montserrat打造3个免费的专业级设计
  • caj转pdf用哪个软件好?实测7款覆盖日常与学术场景的格式转换工具盘点 - 软件小管家
  • Mermaid Live Editor:零成本重塑你的图表创作体验,让想法秒变可视化
  • Scroll Reverser终极指南:解决macOS滚动方向混乱的完美方案
  • 5分钟上手ComfyUI:MiniMax-H3-GGUF工作流配置与优化技巧
  • 证件照换底色App怎么操作?这几款工具几分钟搞定 - 提词匠
  • Moirai-1.0-R-Base实战案例:用Python预测股票价格的完整流程
  • 7大开源数据集强强联合:JoyAI-Image-OpenSpatial背后的数据源揭秘
  • 从小白到高手:easy-canvas事件系统完全指南
  • 电机三维温度场自动建模:从数据到可视化模型的完整实现路径
  • 3分钟解决Windows远程桌面多用户连接问题:RDPWrap.ini配置指南
  • 盘点6款批量修改图片尺寸工具,覆盖网页端、电脑自带与微信小程序 - 软件小管家
  • Mission Planner:免费开源的ArduPilot无人机地面站完整指南
  • nemo-nano-codec-22khz-1.89kbps-21.5fps的伦理考量:安全、隐私与负责任AI实践
  • LFM2.5-2.6B-nvfp4 vs 原版模型:nvfp4量化带来的10倍性能提升与质量对比
  • 10分钟上手Wine Staging:新手必备的Windows程序兼容工具
  • 磁盘空间告急?5个实用技巧让Krokiet帮你快速清理重复文件和相似图片
  • 如何快速上手JoyAI-Image-OpenSpatial:3行代码玩转230万空间问答样本
  • 从安装到预测:Moirai-1.0-R-Large完整部署指南(含代码示例)
  • ADR安全审计:企业AI代理防护效果评估
  • 提升Vue.js应用性能的7个秘诀:vuejs-advanced-learning最佳实践
  • 2026合肥大闸蟹店选择参考各区域靠谱门店汇总 - 滚动商讯
  • Codex多分支开发为什么越来越容易冲突?用Git工作流减少重复合并
  • Claude Composer与MCP服务器集成:扩展AI工具能力的高级配置指南
  • 从论文到实践:PatchTST-ETTh1-Pretrain模型背后的7大技术创新
  • 稿定AI使用指南:核心功能与场景应用解析