ohhoohho/convnext_tiny.in12k_ft_in1k
模型介绍
文件和版本
Pull Requests
讨论
分析

<<<<<<< HEAD

convnext_tiny.in12k_ft_in1k — 昇腾 NPU 部署

基于 ConvNeXt-Tiny 的图像分类模型,适配华为 Ascend 910B/910C NPU。

原模型: timm/convnext_tiny.in12k_ft_in1k

镜像: ai.gitcode.com/hf_mirrors/timm/convnext_tiny.in12k_ft_in1k

论文: A ConvNet for the 2020s


模型简介

属性说明
任务图像分类(ImageNet-1k 1000 类)
架构ConvNeXt-Tiny
输入尺寸224 × 224 RGB
输出1000 类 logits → Softmax 概率
参数量28.6M
Top-1 准确率84.186%
Top-5 准确率97.124%
预训练数据ImageNet-12k (11,821 类子集) → ImageNet-1k 微调
许可证Apache 2.0

环境准备

1. 昇腾 NPU 驱动与 CANN

确保已安装昇腾 NPU 驱动和 CANN 软件包,版本建议 CANN 7.0.RC1 及以上。

# 验证 NPU 可用
npu-smi info

2. Python 环境

# 创建虚拟环境(推荐 Python 3.9+)
python3 -m venv venv
source venv/bin/activate

# 安装依赖
pip install -r requirements.txt

torch_npu 需根据 CANN 版本安装,参考 Ascend PyTorch:

pip install torch-npu==2.1.0.post8

3. 验证 NPU 可用

import torch
import torch_npu  # noqa: F401
print(torch.npu.is_available())  # 应输出 True
print(torch.npu.device_count())  # NPU 卡数量

快速推理

单张图像推理

python inference.py --image_path /path/to/image.jpg

默认从 HuggingFace 镜像 timm/convnext_tiny.in12k_ft_in1k 自动下载模型权重。

指定模型来源

# 从 HuggingFace Hub
python inference.py --image_path test.jpg --model_path timm/convnext_tiny.in12k_ft_in1k

# 从镜像源
python inference.py --image_path test.jpg --model_path ai.gitcode.com/hf_mirrors/timm/convnext_tiny.in12k_ft_in1k

# 从本地目录
python inference.py --image_path test.jpg --model_path ./model_weights

# 强制 CPU 推理
python inference.py --image_path test.jpg --cpu

# 显示 Top-3 结果
python inference.py --image_path test.jpg --topk 3

输出示例

[INFO] 推理设备: npu:0
[INFO] 加载模型: timm/convnext_tiny.in12k_ft_in1k
[INFO] 模型加载完成,参数量: 28,589,416
[INFO] 输入尺寸: (3, 224, 224)
[INFO] 输入图像: test.jpg  (1024×768)
============================================================
  Top-5 预测结果
============================================================
  #1: golden retriever (207) — 92.35%
  #2: Labrador retriever (208) — 3.21%
  #3: English setter (170) — 1.12%
  #4: flat-coated retriever (209) — 0.89%
  #5: curly-coated retriever (210) — 0.67%
============================================================
  推理耗时: 12.34 ms
  设备: npu:0
============================================================

项目结构

convnext_tiny.in12k_ft_in1k/
├── inference.py           # 推理脚本(Ascend NPU 适配)
├── README.md              # 本部署说明文档
├── requirements.txt       # 环境依赖清单
└── assets/                # 截图素材
    ├── agent_workflow.png # 部署/推理流程截图
    ├── npu_device_call.png# NPU 设备调用截图
    └── model_result.png   # 推理结果截图

推理流程

输入图像 ──→ 预处理 (Resize 256 + CenterCrop 224 + Normalize) ──→ ConvNeXt-Tiny ──→ Softmax ──→ Top-5 预测
                                                                       ↑
                                                                Ascend NPU (torch_npu)

预处理细节

  • Resize: 256px(保持宽高比)
  • CenterCrop: 224 × 224
  • Normalize: mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225]

在 AtomGit 上部署

本仓库已集成至 AtomGit 模型仓,支持一键拉取:

git clone https://ai.atomgit.com/ohhoohho/convnext_tiny.in12k_ft_in1k.git
cd convnext_tiny.in12k_ft_in1k
pip install -r requirements.txt
python inference.py --image_path example.jpg

参考

  • ConvNeXt: A ConvNet for the 2020s (CVPR 2022)
  • timm: PyTorch Image Models
  • Ascend PyTorch (torch_npu)

许可证

Apache 2.0 License