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

发布时间:2026/8/7 20:52:51
终极指南: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_in1kconvnext_tiny.in12k_ft_in1k 是一款基于 ConvNeXt 架构的图像分类模型由 Ross Wightman 在 timm 库中实现。该模型先在 ImageNet-12k包含 11821 个类别的 ImageNet-22k 子集上进行预训练然后在 ImageNet-1k 上进行微调非常适合图像分类、特征提取等计算机视觉任务。模型核心特性概览 关键技术参数模型类型图像分类/特征骨干网络参数量28.6M计算量GMACs4.5激活值M13.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, pretrainedTrue) model model.eval() # 获取模型专用数据转换自动处理归一化和尺寸调整 data_config timm.data.resolve_model_data_config(model) transforms timm.data.create_transform(**data_config, is_trainingFalse) # 执行推理 output model(transforms(img).unsqueeze(0)) # 添加 batch 维度 top5_prob, top5_idx torch.topk(output.softmax(dim1)*100, k5) # 输出结果 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, pretrainedTrue, features_onlyTrue, # 启用特征提取模式 ) model model.eval() output model(transforms(img).unsqueeze(0)) # 输出为特征图列表 # 打印各层特征图形状 for i, feature_map in enumerate(output): print(f特征层 {i1} 形状: {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, pretrainedTrue, num_classes0, # 设为 0 移除最终分类层 ) # 方法二显式调用特征提取接口 output model.forward_features(transforms(img).unsqueeze(0)) # 未池化特征 output model.forward_head(output, pre_logitsTrue) # 池化后特征向量 print(f图像嵌入向量形状: {output.shape}) # 输出: torch.Size([1, 768])进阶配置优化推理性能 ⚡调整输入尺寸根据硬件性能和精度需求可修改测试输入尺寸data_config[input_size] (3, 384, 384) # 增大尺寸可能提升精度但增加计算量 transforms timm.data.create_transform(**data_config, is_trainingFalse)使用混合精度推理在支持的 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.7convnext_small.in12k_ft_in1k85.17%50.21474.3convnext_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),仅供参考