From 52b1955ca9d3148191a2c3d797430ce3a86072aa Mon Sep 17 00:00:00 2001 From: Guanzhong Chen Date: Thu, 16 Nov 2023 18:55:18 +0800 Subject: [PATCH] 1 --- .../classification/EfficientNetV2/README.md | 128 ++++++++++++++++++ .../classification/EfficientNetV2/export.py | 47 +++++++ .../EfficientNetV2/requirements.txt | 3 + .../cv/classification/EfficientNetV2/run.py | 77 +++++++++++ 4 files changed, 255 insertions(+) create mode 100644 AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/README.md create mode 100644 AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/export.py create mode 100644 AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/requirements.txt create mode 100644 AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/run.py diff --git a/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/README.md b/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/README.md new file mode 100644 index 0000000000..e53a0ee091 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/README.md @@ -0,0 +1,128 @@ +# EfficientNetV2 + +- [概述](#ZH-CN_TOPIC_0000001172161501) + +- [环境准备](#ZH-CN_TOPIC_0000001126281702) + +- [快速上手](#ZH-CN_TOPIC_0000001126281700) + +- [模型推理精度](#ZH-CN_TOPIC_0000001172201573) + + ****** + + + +# 概述 + +EfficientNetV2是一系列图像分类模型,与现有技术相比,其实现了更好的参数效率和更快的训练速度。基于EfficientNetV1,Efficient NetV2模型使用神经架构搜索(NAS)来联合优化模型大小和训练速度,并以更快的训练和推理速度进行扩展。 + + +- 参考实现: + +``` +url=https://github.com/rwightman/pytorch-image-models +``` + +## 输入输出数据 + +- 输入数据 + + | 输入数据 | 数据类型 | 大小 | 数据排布格式 | + | -------- | -------- | ------------------------- | ------------ | + | input | RGB_FP32 | batchsize x 3 x 288 x 288 | NCHW | + + +- 输出数据 + + | 输出数据 | 数据类型 | 大小 | 数据排布格式 | + | -------- | -------- | -------- | ------------ | + | output1 | FLOAT32 | batchsize x 1000 | ND | + + + +# 推理环境准备 + +- 该模型需要以下依赖 + + **表 1** 版本配套表 + +| 配套 | 版本 | +|-----------------------|-----------------| +| CANN | 6.3.RC2.alph002 | - | +| Python | 3.9.0 | +| PyTorch | 2.0.1 | +| torchVison | 0.15.2 |- +| timm | 0.6.7 | - | +| tqdm | - | - | +| Ascend-cann-torch-aie | - +| Ascend-cann-aie | - +| 芯片类型 | Ascend310P3 | - | + +# 快速上手 +## 安装CANN包 + + ``` + chmod +x Ascend-cann-toolkit_6.3.RC2.alpha002_linux-aarch64.run +./Ascend-cann-toolkit_6.3.RC2.alpha002_linux-aarch64.run --install + ``` +下载Ascend-cann-torch-aie和Ascend-cann-aie得到run包和压缩包 +## 安装Ascend-cann-aie + ``` + chmod +x Ascend-cann-aie_6.3.T200_linux-aarch64.run + ./Ascend-cann-aie_6.3.T200_linux-aarch64.run --install + cd Ascend-cann-aie + source set_env.sh + ``` +## 安装Ascend-cann-torch-aie + ``` + tar -zxvf Ascend-cann-torch-aie-6.3.T200-linux_aarch64.tar.gz + pip3 install torch-aie-6.3.T200-linux_aarch64.whl + ``` + +## 安装其他依赖 +``` +pip3 install -r requirements.txt +``` + + +## 准备数据集 + +1. 获取原始数据集。(解压命令参考tar –xvf \*.tar与 unzip \*.zip) + + 本模型使用ImageNet官网的5万张验证集进行测试。用户需自行获取数据集(或给出明确下载链接),将数据集解压并上传到源码包路径下。目录结构如下: + + ``` + val + ├── n02071294 + ├── ILSVRC2012_val_00010966.JPEG + ├── ... + ├── n04532670 + ├── ... + ``` + + +## 模型推理 + +1. 导出原始torchscript模型,用于编译优化。 + ``` + python3 export.py --ts_save_path efficientnetv2.ts + ``` + 导出模型后,会在当前目录下生成`efficientnetv2.ts`文件。 +2. 运行Python推理样例 + ``` + python3 run.py --dataset_path ./imagenet/val/ --ts_model_path ./efficientnetv2.ts + ``` + 在上面的命令中,"./efficientnetv2.ts"是原始torchscript模型路径,"./imagenet/val/"是上一部分要求准备的ImageNet数据集。 + + 运行结束后,可以看到命令行打印如下信息,说明 top1 精度为 86.3621% + ``` + top1 is 86.3621, step is 50000 + ``` + +# 模型推理性能及精度 + +调用torch-aie推理计算,精度参考下列数据。 + +| 芯片型号 | Batch Size | 数据集 | 精度 | +| --------- |---| ---------- |------------------------------------- | +| 310P3 | 1 | ImageNet | top-1: 86.3621% | diff --git a/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/export.py b/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/export.py new file mode 100644 index 0000000000..4f9754a68f --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/export.py @@ -0,0 +1,47 @@ +# Copyright(C) 2023. Huawei Technologies Co.,Ltd. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse + +import timm +import torch + + +def parse_args(): + parser = argparse.ArgumentParser(description='Export EfficientNetV2 .ts model file') + parser.add_argument('--ts_save_path', help='EfficientNetV2 torch script model save path', type=str, + default='efficientnetv2.ts') + + args = parser.parse_args() + return args + +def trace_ts_model(ts_save_path): + # load model + model = timm.create_model('efficientnetv2_rw_t', pretrained=True) + model.eval() + + # trace model + input_data = torch.ones(1, 3, 288, 288) + ts_model = torch.jit.trace(model, input_data) + ts_model.save(ts_save_path) + print(f'EfficientNetV2 torch script model saved to {ts_save_path}') + + +if __name__ == '__main__': + print('Start to export EfficientNetV2 torch script model') + opts = parse_args() + + # load & trace model + trace_ts_model(opts.ts_save_path) + print("Finish Tracing EfficientNetV2 model") \ No newline at end of file diff --git a/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/requirements.txt b/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/requirements.txt new file mode 100644 index 0000000000..40449a9fb1 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/requirements.txt @@ -0,0 +1,3 @@ +numpy>=1.21.2 +torch==1.11.0 +timm==0.6.7 diff --git a/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/run.py b/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/run.py new file mode 100644 index 0000000000..d09b029299 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/classification/EfficientNetV2/run.py @@ -0,0 +1,77 @@ +import argparse + +import torch +import timm +from timm.data import ImageDataset, create_loader, resolve_data_config +from tqdm.auto import tqdm + +import torch_aie +from torch_aie import _enums + + +def compute_acc(y_pred, y_true, topk_list=(1, 5)): + maxk = max(topk_list) + batch_size = y_true.size(0) + + _, pred = y_pred.topk(maxk, 1, True, True) + pred = pred.t() + correct = pred.eq(y_true.view(1, -1).expand_as(pred)) + + res = [] + for k in topk_list: + correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) + res.append(correct_k.mul_(100.0 / batch_size)) + return res + +def validate(torch_aie_model, dataset_path): + + print("Enter validate...") + model = timm.create_model('efficientnetv2_rw_t', pretrained=True) + model.eval() + config = resolve_data_config({}, model=model) + print("Get config...") + + torch.multiprocessing.set_sharing_strategy('file_system') + + loader = create_loader( + ImageDataset(dataset_path), + input_size=(3, 288, 288), + batch_size=1, + use_prefetcher=False, + interpolation=config['interpolation'], + mean=config['mean'], + std=config['std'], + num_workers=2, + crop_pct=config['crop_pct'] + ) + + top1 = 1 + avg_top1 = 0 + for i, (data, label) in enumerate(tqdm(loader)): + pred = torch_aie_model(data) + acc = compute_acc(pred, label, topk_list=(top1,)) + avg_top1 += acc[0].item() + + step = i + 1 + if step % 100 == 0: + print(f'top1 is {avg_top1 / step}, step is {step}') + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='EfficientNetV2 validation') + parser.add_argument('--dataset_path', type=str, default='./imagenet/val/', help='dataset path') + parser.add_argument('--ts_model_path', type=str, default='./efficientnetv2.ts', + help='Original TorchScript model path') + args = parser.parse_args() + + ts_model = torch.jit.load(args.ts_model_path) + input_info = [torch_aie.Input((1, 3, 288, 288))] + torchaie_model = torch_aie.compile( + ts_model, + inputs=input_info, + precision_policy=_enums.PrecisionPolicy.FP16, + soc_version='Ascend310P3' + ) + torchaie_model.eval() + + validate(torchaie_model, args.dataset_path) \ No newline at end of file -- Gitee