如何快速上手swinv2_cr_small_ns_224.sw_in1k:图像分类完整指南
如何快速上手swinv2_cr_small_ns_224.sw_in1k图像分类完整指南【免费下载链接】swinv2_cr_small_ns_224.sw_in1k项目地址: https://ai.gitcode.com/hf_mirrors/timm/swinv2_cr_small_ns_224.sw_in1kswinv2_cr_small_ns_224.sw_in1k是一款基于Swin Transformer V2架构的图像分类模型由Christoph Reich和Ross Wightman合作开发在ImageNet-1k数据集上进行了预训练。该模型具有49.7M参数、9.1 GMACs计算量和50.3M激活值专为224x224分辨率图像设计是图像分类和特征提取任务的理想选择。 模型核心特性解析技术架构亮点改进型Swin Transformer V2采用独立实现的架构与官方版本相比有三处关键差异MLP日志相对位置偏差使用未归一化自然对数无缩放支持在每个阶段末尾应用LayerNormns变体每个阶段输出和最终特征默认使用NCHW张量布局性能参数概览输入规格3×224×224 RGB图像固定输入尺寸预处理参数均值[0.485, 0.456, 0.406]标准差[0.229, 0.224, 0.225]插值方式双三次bicubic裁剪比例0.9中心裁剪模式输出特性768维特征向量支持1000类ImageNet分类⚡ 快速安装与环境配置一键安装步骤# 克隆模型仓库 git clone https://gitcode.com/hf_mirrors/timm/swinv2_cr_small_ns_224.sw_in1k cd swinv2_cr_small_ns_224.sw_in1k # 安装依赖 pip install timm torch pillow urllib3环境要求Python 3.6PyTorch 1.7timm 0.5.4Pillow 8.0 三种核心使用场景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(swinv2_cr_small_ns_224.sw_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)) # 添加批次维度 top5_probabilities, top5_class_indices torch.topk(output.softmax(dim1) * 100, k5) # 输出结果 for prob, idx in zip(top5_probabilities[0], top5_class_indices[0]): print(f类别 {idx}: {prob:.2f}%)2. 特征图提取高级用法提取不同层级的特征图用于计算机视觉任务model timm.create_model( swinv2_cr_small_ns_224.sw_in1k, pretrainedTrue, features_onlyTrue, # 启用特征提取模式 ) 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)) # 输出各阶段特征图形状 for i, feature_map in enumerate(output): print(f特征图 {i1} 形状: {feature_map.shape})该模型将输出四个阶段的特征图形状分别为(1, 96, 56, 56)(1, 192, 28, 28)(1, 384, 14, 14)(1, 768, 7, 7)3. 图像嵌入向量生成生成图像的固定维度嵌入向量用于检索或下游任务# 方法一移除分类器 model timm.create_model( swinv2_cr_small_ns_224.sw_in1k, pretrainedTrue, num_classes0, # 移除分类头 ) # 方法二使用特征提取接口 model timm.create_model(swinv2_cr_small_ns_224.sw_in1k, pretrainedTrue) output model.forward_features(transforms(img).unsqueeze(0)) # 获取原始特征 output model.forward_head(output, pre_logitsTrue) # 转换为嵌入向量 print(f嵌入向量形状: {output.shape}) # 输出 (1, 768) 模型文件解析项目包含以下核心文件模型权重model.safetensors安全高效的模型权重存储格式pytorch_model.bin标准PyTorch权重文件配置文件config.json包含架构参数、预处理配置和输入规格configuration.json框架和任务元数据PyTorch/图像分类文档README.md完整的模型说明和使用示例 注意事项与最佳实践输入尺寸模型要求固定输入尺寸为224×224建议使用模型自带的变换函数确保正确预处理性能优化推理前设置model.eval()以禁用 dropout 和批量归一化训练模式对大量图像进行分类时使用批处理提高效率扩展应用可作为特征提取器用于目标检测、语义分割等下游任务通过微调适应特定领域数据集提升分类性能 引用与致谢如果您在研究中使用此模型请引用以下文献inproceedings{liu2021swinv2, title{Swin Transformer V2: Scaling Up Capacity and Resolution}, author{Ze Liu and Han Hu and Yutong Lin and Zhuliang Yao and Zhenda Xie and Yixuan Wei and Jia Ning and Yue Cao and Zheng Zhang and Li Dong and Furu Wei and Baining Guo}, booktitle{International Conference on Computer Vision and Pattern Recognition (CVPR)}, year{2022} } 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}} }本模型基于Christoph Reich和Ross Wightman的独立实现感谢他们在Swin Transformer V2架构上的贡献。【免费下载链接】swinv2_cr_small_ns_224.sw_in1k项目地址: https://ai.gitcode.com/hf_mirrors/timm/swinv2_cr_small_ns_224.sw_in1k创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考