文件最后提交记录最后更新时间
!6368 [built-in][Pytorch] 添加vit-l模型改图功能 Merge pull request !6368 from zhanfeiran/master 1 年前
【fix】完善小模型Atlas 300I DUO硬件描述 Co-authored-by: Niushiya<niushiya1@huawei.com> # message auto-generated for no-merge-commit merge: !7587 merge master into master 【fix】完善小模型Atlas 300I DUO硬件描述 Created-by: niushiya Commit-by: Niushiya Merged-by: ascend-robot Description: ## Motivation 1、完善小模型Atlas 300I DUO硬件描述,补充单芯字段; ## Modification Please briefly describe what modification is made in this PR. ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [x] The new code needs to comply with the Clean Code specification. - [x] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [x] CLA has been signed and all committers have signed the CLA in this PR. - [x] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75876 天前
!6368 [built-in][Pytorch] 添加vit-l模型改图功能 Merge pull request !6368 from zhanfeiran/master 1 年前
!6368 [built-in][Pytorch] 添加vit-l模型改图功能 Merge pull request !6368 from zhanfeiran/master 1 年前
!6368 [built-in][Pytorch] 添加vit-l模型改图功能 Merge pull request !6368 from zhanfeiran/master 1 年前
!6368 [built-in][Pytorch] 添加vit-l模型改图功能 Merge pull request !6368 from zhanfeiran/master 1 年前
!6368 [built-in][Pytorch] 添加vit-l模型改图功能 Merge pull request !6368 from zhanfeiran/master 1 年前
!6368 [built-in][Pytorch] 添加vit-l模型改图功能 Merge pull request !6368 from zhanfeiran/master 1 年前
【fix】完善小模型Atlas 300I DUO硬件描述 Co-authored-by: Niushiya<niushiya1@huawei.com> # message auto-generated for no-merge-commit merge: !7587 merge master into master 【fix】完善小模型Atlas 300I DUO硬件描述 Created-by: niushiya Commit-by: Niushiya Merged-by: ascend-robot Description: ## Motivation 1、完善小模型Atlas 300I DUO硬件描述,补充单芯字段; ## Modification Please briefly describe what modification is made in this PR. ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [x] The new code needs to comply with the Clean Code specification. - [x] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [x] CLA has been signed and all committers have signed the CLA in this PR. - [x] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75876 天前
更新VIT readme Co-authored-by: zch777<zhuangchenghao@huawei.com> # message auto-generated for no-merge-commit merge: !7486 merge update_vit into master 更新VIT readme Created-by: zch777 Commit-by: zch777 Merged-by: ascend-robot Description: ## Motivation Please describe the motivation of this PR and the goal you want to achieve through this PR. 更新VIT readme ## Modification Please briefly describe what modification is made in this PR. 更新VIT readme ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [x] The new code needs to comply with the Clean Code specification. - [x] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [x] CLA has been signed and all committers have signed the CLA in this PR. - [x] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!74863 个月前
README.md

ViT模型-推理指导

概述

Transformer 架构已广泛应用于自然语言处理领域。本模型的作者发现,Vision Transformer(ViT)模型在计算机视觉领域中对CNN的依赖不是必需的,直接将其应用于图像块序列来进行图像分类时,也能得到和目前卷积网络相媲美的准确率。

  • 参考实现:
url=https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/vision_transformer.py
mode_name = [
   vit_base_patch8_224,
   vit_base_patch16_224,
   vit_base_patch16_384,
   vit_base_patch32_224,
   vit_base_patch32_384,
   vit_large_patch16_224,
]

输入输出数据

  • 输入数据

    1. 对于 vit_base_patch8_224、vit_base_patch16_224、vit_base_patch32_224 和 vit_large_patch16_224

      输入数据 数据类型 大小 数据排布格式
      input FLOAT32 batchsize x 3 x 224 x 224 NCHW
    2. 对于 vit_base_patch16_384 和 vit_base_patch32_384

      输入数据 数据类型 大小 数据排布格式
      input FLOAT32 batchsize x 3 x 384 x 384 NCHW
  • 输出数据

    输出数据 数据类型 大小 数据排布格式
    output FLOAT32 batchsize x num_class ND

推理环境准备

  • 该模型需要以下插件与驱动:

    表 1 版本配套表

    配套 版本 环境准备指导
    固件与驱动 25.3.rc1 Pytorch框架推理环境准备
    CANN 8.3.RC2 -
    Python 3.11.10 -
    PyTorch 2.1.0 -
    Ascend Extension PyTorch 2.1.0.post13 -
    说明:Atlas 300I Duo 推理卡请以CANN版本选择实际固件与驱动版本。 \ \

快速上手

获取源码

  1. 获取源码。

    git clone https://gitcode.com/ascend/ModelZoo-PyTorch.git        # 克隆仓库的代码
    git checkout master                                            # 切换到对应分支
    cd ACL_PyTorch/contrib/cv/classfication/ViT                    # 切换到模型的代码仓目录
    
  2. 安装依赖。

    pip3 install -r requirements.txt
    
  3. 安装改图工具 auto-optimizer

    git clone https://github.com/Ascend/msadvisor.git
    cd msadvisor/auto-optimizer
    pip3 install -r requirements.txt
    python3 setup.py install
    cd ../..
    

准备数据集

本模型使用ImageNet 50000张图片的验证集,请前往ImageNet官网下载数据集:

```
├── ImageNet
|   ├── val
|   |    ├── ILSVRC2012_val_00000001.JPEG
│   |    ├── ILSVRC2012_val_00000002.JPEG
│   |    ├── ......
|   ├── val_label.txt
```

执行预处理脚本:

# img_size为预处理输出的图像尺寸,可选224或384,需要和模型相对应。
img_size=224

python3 Vit_preprocess.py --data_path ImageNet/val/ --store_path ./prep_dataset/${img_size} --image_size ${img_size}
  • 参数说明:
    • --data_path: 数据集路径
    • --store_path: 预处理结果保存路径
    • --image_size: 图像尺寸

模型推理

  1. 模型转换。

    获取模型权重。 下载链接可参考:https://github.com/rwightman/pytorch-image-models/blob/main/timm/models/vision_transformer.py

    模型变体较多,可按需下载。根据下表通过搜索文件名找到对应的权重文件下载地址,下载到当前目录下。

    模型变体 文件名
    vit_base_patch8_224 B_8-i21k-300ep-lr_0.001-aug_medium1-wd_0.1-do_0.0-sd_0.0--imagenet2012-steps_20k-lr_0.01-res_224.npz
    vit_base_patch16_224 B_16-i21k-300ep-lr_0.001-aug_medium1-wd_0.1-do_0.0-sd_0.0--imagenet2012-steps_20k-lr_0.01-res_224.npz
    vit_base_patch16_384 B_16-i21k-300ep-lr_0.001-aug_medium1-wd_0.1-do_0.0-sd_0.0--imagenet2012-steps_20k-lr_0.01-res_384.npz
    vit_base_patch32_224 B_32-i21k-300ep-lr_0.001-aug_medium1-wd_0.03-do_0.0-sd_0.0--imagenet2012-steps_20k-lr_0.03-res_224.npz
    vit_base_patch32_384 B_32-i21k-300ep-lr_0.001-aug_light1-wd_0.1-do_0.0-sd_0.0--imagenet2012-steps_20k-lr_0.03-res_384.npz
    vit_large_patch16_224 L_16-i21k-300ep-lr_0.001-aug_medium1-wd_0.1-do_0.1-sd_0.1--imagenet2012-steps_20k-lr_0.01-res_224.npz

    然后将权重文件重命名为模型变体名称.npz

    # 以 vit_base_patch8_224 为例
    mv B_8-i21k-300ep-lr_0.001-aug_medium1-wd_0.1-do_0.0-sd_0.0--imagenet2012-steps_20k-lr_0.01-res_224.npz vit_base_patch8_224.npz
    
    1. 导出onnx文件。

      1. 使用以下脚本导出onnx文件:

        # bs为Batch Size,可根据需要设置,此处以1为例
        bs=1
        
        # model_name为模型变体名称,可根据需要设置,此处以 vit_base_patch8_224 为例
        model_name=vit_base_patch8_224
        
        python3 Vit_pth2onnx.py --batch_size ${bs} --model_path ${model_name}.npz --save_dir models/onnx --model_name ${model_name}
        
        • 参数说明:
          • --batch_size: 批次大小
          • --model_path: 模型权重npz文件路径
          • --save_dir: 保存onnx文件的目录
          • --model_name: 模型变体名称

        获得vit_base_patch8_224_bs1.onnx文件。

      2. 优化ONNX文件。

        python3 -m onnxsim models/onnx/${model_name}_bs${bs}.onnx models/onnx/${model_name}_bs${bs}_sim.onnx
        
        # 输入参数: 1.原始模型文件路径 2.优化模型文件路径 3.模型变体名称
        python3 opt_vit.py models/onnx/${model_name}_bs${bs}_sim.onnx models/onnx/${model_name}_bs${bs}_opt.onnx ${model_name}
        

        获得vit_base_patch8_224_bs1_opt.onnx文件。

    2. 使用ATC工具将ONNX模型转OM模型。

      1. 配置环境变量。

        source /usr/local/Ascend/ascend-toolkit/set_env.sh
        
      2. 执行命令查看芯片名称(${chip_name})。

        npu-smi info
        chip_name=Ascend310P3 #该设备芯片名为Ascend310P3 (自行替换)
        回显如下:
        +-------------------|-----------------|------------------------------------------------------+
        | NPU     Name      | Health          | Power(W)     Temp(C)           Hugepages-Usage(page) |
        | Chip    Device    | Bus-Id          | AICore(%)    Memory-Usage(MB)                        |
        +===================+=================+======================================================+
        | 0       310P3     | OK              | 15.8         42                0    / 0              |
        | 0       0         | 0000:82:00.0    | 0            1074 / 21534                            |
        +===================+=================+======================================================+
        | 1       310P3     | OK              | 15.4         43                0    / 0              |
        | 0       1         | 0000:89:00.0    | 0            1070 / 21534                            |
        +===================+=================+======================================================+
        
      3. 执行ATC命令。

        mkdir -p models/om
        atc --framework=5 --model=models/onnx/${model_name}_bs${bs}_opt.onnx --output=models/om/${model_name}_bs${bs} --input_format=NCHW --input_shape="input:${bs},3,${img_size},${img_size}" --log=debug --soc_version=${chip_name} --enable_small_channel=1 --optypelist_for_implmode="Gelu" --op_select_implmode=high_performance
        
        • 参数说明:

          • --model:为ONNX模型文件。
          • --framework:5代表ONNX模型。
          • --output:输出的OM模型。
          • --input_format:输入数据的格式。
          • --log:日志级别。
          • --soc_version:处理器型号。

        运行成功后生成vit_base_patch8_224_bs1.om模型文件。

  2. 开始推理验证。

    1. 安装ais_bench推理工具。

      请访问ais_bench推理工具代码仓,根据readme文档进行工具安装。

    2. 执行推理。

      python3 -m ais_bench --model models/om/${model_name}_bs${bs}.om --input prep_dataset/${img_size} --output outputs/ --output_dir ${model_name}_bs${bs} --device 1
      
      • 参数说明:
        • --model:om文件路径
        • --input:输入文件
        • --output:输出目录
        • --device:NPU设备编号

      推理后的输出默认在当前目录outputs/${model_name}_bs${bs}下。

    3. 精度验证。

      调用脚本与GT label,可以获得精度数据:

      python3 Vit_postprocess.py --save_path result_${model_name}_bs${bs}.json --input_dir ./outputs/${model_name}_bs${bs} --label_path ImageNet/val_label.txt
      
      • 参数说明:
        • --input_dir:为生成推理结果所在路径
        • --label_path:为标签数据路径
        • --save_path: 结果保存路径
    4. 执行纯推理验证性能。

      python3 -m ais_bench --model models/om/${model_name}_bs${bs}.om --device 1 --loop 100
      
      • 参数说明:
        • --model:om文件路径
        • --device:NPU设备编号
        • --loop: 纯推理次数

模型推理性能&精度

芯片型号 模型变体 Batch Size 数据集 参考精度 NPU精度 性能(fps)
300I Pro ViT_base_patch8_224 1 ImageNet top1: 85.80 top1: 85.58 76.42
300I Pro ViT_base_patch8_224 8 (最优性能) ImageNet top1: 85.80 top1: 85.58 98.48
300I Pro ViT_base_patch16_224 1 ImageNet top1: 84.20 top1: 84.16 342.34
300I Pro ViT_base_patch16_224 16 (最优性能) ImageNet top1: 84.20 top1: 84.16 660.64
300I Pro ViT_base_patch16_384 1 ImageNet top1: 85.83 top1: 85.84 108.87
300I Pro ViT_base_patch16_384 8 (最优性能) ImageNet top1: 85.83 top1: 85.84 151.01
300I Pro ViT_base_patch32_224 1 ImageNet top1: 80.72 top1: 80.63 431.89
300I Pro ViT_base_patch32_224 64 (最优性能) ImageNet top1: 80.72 top1: 80.63 1679.63
300I Pro ViT_base_patch32_384 1 ImageNet top1: 83.35 top1: 83.29 267.01
300I Pro ViT_base_patch32_384 32 (最优性能) ImageNet top1: 83.35 top1: 83.29 596.55
300I Pro ViT_large_patch16_224 1 ImageNet top1: 85.84 top1: 85.62 119.80
300I Pro ViT_large_patch16_224 4 (最优性能) ImageNet top1: 85.84 top1: 85.62 166.43
300I DUO(单芯) ViT_base_patch16_224 1 ImageNet top1: 84.20 top1: 84.16 351.32
300I DUO(单芯) ViT_base_patch16_224 8 (最优性能) ImageNet top1: 84.20 top1: 84.16 661.05
300I DUO(单芯) ViT_base_patch16_384 1 ImageNet top1: 85.83 top1: 85.84 110.54
300I DUO(单芯) ViT_base_patch16_384 8 (最优性能) ImageNet top1: 85.83 top1: 85.84 153.29
310P RC ViT_base_patch16_224 1 ImageNet top1: 84.20 top1: 84.16 364.69
310P RC ViT_base_patch16_224 8 (最优性能) ImageNet top1: 84.20 top1: 84.16 683.75
310P RC ViT_base_patch16_384 1 ImageNet top1: 85.83 top1: 85.84 112.73
310P RC ViT_base_patch16_384 8 (最优性能) ImageNet top1: 85.83 top1: 85.84 158.43

完整性能数据请查阅文件:performances.md