文件最后提交记录最后更新时间
fix link validity Co-authored-by: frozenleaves<914814442@qq.com> # message auto-generated for no-merge-commit merge: !7517 merge master into master fix link validity Created-by: frozenn Commit-by: frozenleaves Merged-by: ascend-robot Description: ## Motivation Please describe the motivation of this PR and the goal you want to achieve through this PR. ## 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**: - [ ] The new code needs to comply with the Clean Code specification. - [ ] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [ ] CLA has been signed and all committers have signed the CLA in this PR. - [ ] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75171 个月前
!3416 segformer模型删除无效patch * update ACL_PyTorch/contrib/cv/segmentation/Segformer/Segformer.patch. 3 年前
!1794 [中山大学][高校贡献][PyTorch离线推理][Segformer]-初次提交 * update ACL_PyTorch/contrib/cv/segmentation/Segformer/README.md. * 删除文件 ACL_PyTorch/contrib/cv/segmentation/Segformer/.keep * update * 新建 Segformer 3 年前
!1794 [中山大学][高校贡献][PyTorch离线推理][Segformer]-初次提交 * update ACL_PyTorch/contrib/cv/segmentation/Segformer/README.md. * 删除文件 ACL_PyTorch/contrib/cv/segmentation/Segformer/.keep * update * 新建 Segformer 3 年前
!1794 [中山大学][高校贡献][PyTorch离线推理][Segformer]-初次提交 * update ACL_PyTorch/contrib/cv/segmentation/Segformer/README.md. * 删除文件 ACL_PyTorch/contrib/cv/segmentation/Segformer/.keep * update * 新建 Segformer 3 年前
!3570 [Pytorch][CV] Segformer模型 Readme整改 * change to use OnnxGraph only * update requirements.txt * update readme * use auto_optimizer 3 年前
!3570 [Pytorch][CV] Segformer模型 Readme整改 * change to use OnnxGraph only * update requirements.txt * update readme * use auto_optimizer 3 年前
README.md

Segformer 模型-推理指导

概述

Segformer是一个简单、高效但功能强大的语义分割框架,它将 Transformer 与轻量级多层感知 (MLP) 解码器相结合。SegFormer 有两个吸引人的特性:1) SegFormer 包含一个新颖的分层结构的 Transformer 编码器,它输出多尺度特征。它不需要位置编码,从而避免了在测试分辨率与训练不同时导致性能下降的位置编码插值。2) SegFormer 避免了复杂的解码器。所提出的 MLP 解码器聚合来自不同层的信息,从而结合局部注意力和全局注意力来呈现强大的表示。

  • 参考实现:

    url=https://github.com/open-mmlab/mmsegmentation/tree/master/configs/segformer
    branch=master
    commit_id=0e37281884193838417a43802bb7a4c854d2067e
    model_name=Segformer-MIT-B0
    

输入输出数据

  • 输入数据

    输入数据 数据类型 大小 数据排布格式
    input RGB_FP32 batchsize x 3 x 1024 x 2048 NCHW
  • 输出数据

    输出数据 大小 数据类型 数据排布格式
    output 1 x batchsize x 1024 x 2048 INT64 NCHW

推理环境准备[所有版本]

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

    表 1 版本配套表

配套 版本 环境准备指导
固件与驱动 22.0.4 Pytorch框架推理环境准备
CANN 6.0.RC1 -
Python 3.7.5 -
PyTorch 1.7.0 -
说明:Atlas 300I Duo 推理卡请以CANN版本选择实际固件与驱动版本。 \ \

快速上手

获取源码

  1. 获取本仓代码

    git clone https://gitcode.com/ascend/ModelZoo-PyTorch.git
    cd ModelZoo-PyTorch/ACL_PyTorch/contrib/cv/segmentation/Segformer
    

    文件说明

    Segformer
      ├── README.md                  # 此文档
      ├── Segformer_preprocess.py    # 数据集预处理脚本
      ├── Segformer_postprocess.py   # 推理结果后处理脚本
      ├── Segformer.patch            # 修改模型源码的patch文件
      └── optimize_onnx.py           # ONNX模型优化脚本
    
  2. 获取模型源码

    git clone https://github.com/open-mmlab/mmsegmentation.git
    cd mmsegmentation
    git reset --hard 0e37281884193838417a43802bb7a4c854d2067e
    patch -p1 < ../Segformer.patch
    pip3 install -v -e .
    cd ..
    
  3. 安装依赖。

    安装Python依赖库

    pip3 install -r requirements.txt
    

    从源码安装mmcv-full

    git clone https://github.com/open-mmlab/mmcv.git
    cd mmcv
    git reset --hard ccdc61c0878d27ac7cccfecd7b474320817f0bbf
    pip3 install -r requirements.txt
    MMCV_WITH_OPS=1 pip3 install -v -e .
    cd ..
    

    安装改图工具

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

准备数据集

  1. 获取原始数据集。

    下载 cityscpaes数据集,解压后数据集目录结构如下:

    cityscapes
      ├── gtFine
      │     ├── test
      │     ├── train
      │     └── val
      └── leftImg8bit
            ├── test
            ├── train
            └── val
    
  2. 数据预处理。

    将原始数据集转换为模型输入的二进制数据。

    执行Segformer_preprocess.py脚本,完成预处理。

    python3 ./Segformer_preprocess.py ${data_path}/cityscapes/leftImg8bit/val ./prep_dataset
    
    • 参数说明:
      • ${data_path}/cityscapes/leftImg8bit/val:验证集路径。
      • ./prep_dataset:预处理后的 bin 文件存放路径。

模型推理

  1. 模型转换。

    使用PyTorch将模型权重文件.pth转换为.onnx文件,再使用ATC工具将.onnx文件转为离线推理模型文件.om文件。

    1. 获取权重文件。
    wget https://download.openmmlab.com/mmsegmentation/v0.5/segformer/segformer_mit-b0_8x1_1024x1024_160k_cityscapes/segformer_mit-b0_8x1_1024x1024_160k_cityscapes_20211208_101857-e7f88502.pth
    
    1. 导出onnx文件。

      1. 使用pytorch2onnx.py导出onnx文件。

        运行pytorch2onnx.py脚本。

        python3 mmsegmentation/tools/pytorch2onnx.py \
        mmsegmentation/configs/segformer/segformer_mit-b0_8x1_1024x1024_160k_cityscapes.py \
        --checkpoint segformer_mit-b0_8x1_1024x1024_160k_cityscapes_20211208_101857-e7f88502.pth \
        --output-file segformer_dynamicbs.onnx \
        --shape 1024 2048 \
        --dynamic-export
        

        获得 segformer_dynamicbs.onnx 文件。

      2. 优化ONNX文件。

        使用onnx-simplifier简化onnx模型

        onnxsim segformer_dynamicbs.onnx segformer_dynamicbs_sim.onnx
        

        使用optimize_onnx.py优化onnx模型

        python3 optimize_onnx.py segformer_dynamicbs_sim.onnx segformer_dynamicbs_sim_opt.onnx
        

        获得 segformer_dynamicbs_sim_opt.onnx 文件。

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

      1. 配置环境变量。

        source /usr/local/Ascend/ascend-toolkit/set_env.sh
        

        说明: 该脚本中环境变量仅供参考,请以实际安装环境配置环境变量。详细介绍请参见《CANN 开发辅助工具指南 (推理)》。

      2. 执行命令查看芯片名称(${chip_name})。

        npu-smi info
        # 该设备芯片名为 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命令。

        atc --framework=5 \
        --model=segformer_dynamicbs_sim_opt.onnx \
        --output=segformer_bs${batch_size} \
        --input_format=NCHW \
        --input_shape="input:${batch_size},3,1024,2048" \
        --soc_version=Ascend${chip_name} \
        --log=error
        
        • 参数说明:

          • --model:为ONNX模型文件。
          • --framework:5代表ONNX模型。
          • --output:输出的OM模型。
          • --input_format:输入数据的格式。
          • --input_shape:输入数据的shape,${batch_size}的值可取:1,4。
          • --log:日志级别。
          • --soc_version:处理器型号。

          运行成功后生成 segformer_bs${batch_size}.om 模型文件。

  2. 开始推理验证。

    a. 安装ais_bench推理工具。

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

    b. 执行推理。

    python3 -m ais_bench \
    --model=./segformer_bs${batch_size}.om \
    --input=./prep_dataset \
    --output=./ \
    --outfmt=BIN
    
    • 参数说明:

      • --model:om文件路径。
      • --input:预处理后的数据集路径。
      • --output:输出文件保存路径。
      • --outfmt:输出文件格式。

    ${batch_size} 表示不同 batch 的 om 模型,该模型支持的batchsize为:1,4。

    推理后的输出默认在当前工作目录下,其目录命名格式为xxxx_xx_xx-xx_xx_xx(年_月_日-时_分_秒),如2022_08_30-08_50_53

    c. 精度验证。

    我们将后处理与精度验证一起放在了Segformer_postprocess.py文件中,脚本执行完毕后即可生成精度结果。

    python3 Segformer_postprocess.py --json_path=./xxxx_xx_xx-xx_xx_xx_sumary.json --dataset_path=${data_path}
    
    • 参数说明:

      • --json_path:ais_bench推理工具生成的json文件路径
      • --dataset_path:cityscpaes数据集所在父路径;比如:若cityscpaes存放在/opt/npu/cityscpaes,则--dataset_path=/opt/npu/

    d. 性能验证。

    使用ais_bench推理工具的纯推理模式验证不同batch_size的om模型的性能,命令如下:

    python3 -m ais_bench --model=./segformer_bs${batch_size}.om --loop=50 --batchsize=${batch_size}
    

模型推理性能&精度

调用ACL接口推理计算,性能参考下列数据。

芯片型号 Batch Size 数据集 精度 性能 基准性能
300I PRO 1 cityscapes mIoU = 75.94 6.01 fps 10.61 fps
300I PRO 4 cityscapes mIoU = 75.94 5.94 fps 10.65 fps

注:该模型支持的batchsize为1,4。