终极指南: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_in1k | 84.19% | 28.6 | 2433.7 |
| convnext_small.in12k_ft_in1k | 85.17% | 50.2 | 1474.3 |
| convnext_base.fb_in1k | 83.82% | 88.6 | 1054.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),仅供参考
