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 0000000000000000000000000000000000000000..e53a0ee0919156643bd87a0875c5ec6196103706
--- /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 0000000000000000000000000000000000000000..4f9754a68f13a578ada24d6a2ceea7fcb6d5be92
--- /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 0000000000000000000000000000000000000000..40449a9fb1b9c2e78cc9fbc69f4436626cdd5427
--- /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 0000000000000000000000000000000000000000..d09b029299b45aabe00b832ad7fd78131b76f48d
--- /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
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/README.md b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/README.md
new file mode 100644
index 0000000000000000000000000000000000000000..5580149c28e035ef09380e900573f659ca8348a8
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/README.md
@@ -0,0 +1,35 @@
+## 构建虚拟环境
+
+`conda create --name mobilenetV1 python=3.9`
+激活:`conda activate mobilenetV1`
+
+## 安装依赖
+
+`pip3 install -r requirements.txt`
+
+编译pt插件,在dist目录下安装torh_aie
+
+## 下载pth模型
+
+自行下载模型tar文件并放置在mobilenetv1路径下
+链接:https://ascend-repo-modelzoo.obs.cn-east-2.myhuaweicloud.com/model/1_PyTorch_PTH/MobileNetV1/PTH/mobilenet_sgd_rmsprop_69.526.tar
+
+## trace得到ts文件
+
+将model_path改为自己tar模型的路径
+`python3 export.py --model_path=./mobilenet_sgd_rmsprop_69.526.tar`
+
+## 数据预处理
+
+将src_path改为自己目录下数据集图片的路径
+`python3 imagenet_torch_preprocess.py --mode_type resnet --src_path /home/pttest_models/imagenet/val --save_path ./prep_dataset`
+
+## 模型推理 - 获取精度
+
+将annotation_file_path改为自己目录下数据集label的路径
+`python3 run.py --annotation_file_path /home/pttest_models/imagenet/val_label.txt --result_json_path ./ --json_file_name result_torchaie.json --ts_model_path ./mobilenetv1.ts --input_bin_folder_path ./prep_dataset`
+
+## 推理性能 - ts
+
+将--ts_path改为自己目录下的ts路径
+`python3 perf.py --mode=ts --ts_path=/onnx/mobilenetv1/mobilenetv1.ts`
\ No newline at end of file
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/export.py b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/export.py
new file mode 100644
index 0000000000000000000000000000000000000000..8521411b51e32df4c0657bd27a09b9416ca25b3d
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/export.py
@@ -0,0 +1,109 @@
+import os
+import sys
+import argparse
+
+import torch
+import torch.nn as nn
+from collections import OrderedDict
+
+def parse_args():
+ parser = argparse.ArgumentParser(description='Export MobilenetV1 .ts model file')
+ parser.add_argument('--model_path',help='MobilenetV1 pth file path', type=str,
+ default='/onnx/mobilenetv1/mobilenet_sgd_rmsprop_69.526.tar'
+ )
+ parser.add_argument('--ts_save_path', help='MobilenetV1 torch script model save path', type=str,
+ default='mobilenetv1.ts')
+
+ args = parser.parse_args()
+ return args
+
+def check_args(args):
+ if not os.path.exists(args.model_path):
+ raise FileNotFoundError(f'MobilenetV1 model file {args.model_path} not exists')
+
+class Net(nn.Module):
+ def __init__(self):
+ super(Net, self).__init__()
+
+ def conv_bn(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True)
+ )
+
+ def conv_dw(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
+ nn.BatchNorm2d(inp),
+ nn.ReLU(inplace=True),
+
+ nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True),
+ )
+
+ self.model = nn.Sequential(
+ conv_bn(3, 32, 2),
+ conv_dw(32, 64, 1),
+ conv_dw(64, 128, 2),
+ conv_dw(128, 128, 1),
+ conv_dw(128, 256, 2),
+ conv_dw(256, 256, 1),
+ conv_dw(256, 512, 2),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 1024, 2),
+ conv_dw(1024, 1024, 1),
+ nn.AvgPool2d(7),
+ )
+ self.fc = nn.Linear(1024, 1000)
+
+ def forward(self, x):
+ x = self.model(x)
+ x = x.view(-1, 1024)
+ x = self.fc(x)
+ return x
+
+
+def proc_nodes_module(checkpoint, AttrName):
+ new_state_dict = OrderedDict()
+ for k, v in checkpoint[AttrName].items():
+ if k[0:7] == "module.":
+ name = k[7:]
+ else:
+ name = k[0:]
+ new_state_dict[name] = v
+ return new_state_dict
+
+def trace_ts_model(model_path, ts_save_path):
+
+ checkpoint = torch.load(model_path, map_location="cpu")
+
+ new_state_dict = OrderedDict()
+ for k, v in checkpoint['state_dict'].items():
+ if k[0:7] == "module.":
+ name = k[7:]
+ else:
+ name = k[0:]
+ new_state_dict[name] = v
+
+ model = Net()
+ if new_state_dict:
+ model.load_state_dict(new_state_dict)
+ model.eval()
+
+ input_data = torch.ones(1, 3, 224, 224)
+ ts_model = torch.jit.trace(model, input_data)
+ ts_model.save(ts_save_path)
+ print(f'MobilenetV1 torch script model saved to {ts_save_path}')
+
+
+if __name__ == "__main__":
+ args = parse_args()
+ check_args(args)
+ trace_ts_model(args.model_path, args.ts_save_path)
+ print("Finish Tracing MobilenetV1 model")
\ No newline at end of file
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/imagenet_acc_eval.py b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/imagenet_acc_eval.py
new file mode 100644
index 0000000000000000000000000000000000000000..583340a19f2fc6e99faed85526c906f8bd12d7ba
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/imagenet_acc_eval.py
@@ -0,0 +1,184 @@
+# Copyright 2020 Huawei Technologies Co., Ltd
+#
+# 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 os
+import sys
+import json
+import numpy as np
+import time
+
+np.set_printoptions(threshold=sys.maxsize)
+
+LABEL_FILE = "HiAI_label.json"
+
+
+def gen_file_name(img_name):
+ full_name = img_name.split('/')[-1]
+ index = full_name.rfind('.')
+ return full_name[:index]
+
+
+def cre_groundtruth_dict(gtfile_path):
+ """
+ :param filename: file contains the imagename and label number
+ :return: dictionary key imagename, value is label number
+ """
+ img_gt_dict = {}
+ for gtfile in os.listdir(gtfile_path):
+ if (gtfile != LABEL_FILE):
+ with open(os.path.join(gtfile_path, gtfile), 'r') as f:
+ gt = json.load(f)
+ ret = gt["image"]["annotations"][0]["category_id"]
+ img_gt_dict[gen_file_name(gtfile)] = ret
+ return img_gt_dict
+
+
+def cre_groundtruth_dict_fromtxt(gtfile_path):
+ """
+ :param filename: file contains the imagename and label number
+ :return: dictionary key imagename, value is label number
+ """
+ img_gt_dict = {}
+ with open(gtfile_path, 'r')as f:
+ for line in f.readlines():
+ temp = line.strip().split(" ")
+ imgName = temp[0].split(".")[0]
+ imgLab = temp[1]
+ img_gt_dict[imgName] = imgLab
+ return img_gt_dict
+
+
+def load_statistical_predict_result(filepath):
+ """
+ function:
+ the prediction esult file data extraction
+ input:
+ result file:filepath
+ output:
+ n_label:numble of label
+ data_vec: the probabilitie of prediction in the 1000
+ :return: probabilities, numble of label, in_type, color
+ """
+ with open(filepath, 'r')as f:
+ data = f.readline()
+ temp = data.strip().split(" ")
+ n_label = len(temp)
+ if data == '':
+ n_label = 0
+ data_vec = np.zeros((n_label), dtype=np.float32)
+ in_type = ''
+ color = ''
+ if n_label == 0:
+ in_type = f.readline()
+ color = f.readline()
+ else:
+ for ind, prob in enumerate(temp):
+ data_vec[ind] = np.float32(prob)
+ return data_vec, n_label, in_type, color
+
+
+def create_visualization_statistical_result(prediction_file_path,
+ result_store_path, json_file_name,
+ img_gt_dict, topn=5):
+ """
+ :param prediction_file_path:
+ :param result_store_path:
+ :param json_file_name:
+ :param img_gt_dict:
+ :param topn:
+ :return:
+ """
+ writer = open(os.path.join(result_store_path, json_file_name), 'w')
+ table_dict = {}
+ table_dict["title"] = "Overall statistical evaluation"
+ table_dict["value"] = []
+
+ count = 0
+ resCnt = 0
+ n_labels = 0
+ count_hit = np.zeros(topn)
+ for tfile_name in os.listdir(prediction_file_path):
+ count += 1
+ temp = tfile_name.split('.')[0]
+ index = temp.rfind('_')
+ img_name = temp[:index]
+ filepath = os.path.join(prediction_file_path, tfile_name)
+ ret = load_statistical_predict_result(filepath)
+ prediction = ret[0]
+ n_labels = ret[1]
+ sort_index = np.argsort(-prediction)
+ gt = img_gt_dict[img_name]
+ if (n_labels == 1000):
+ realLabel = int(gt)
+ elif (n_labels == 1001):
+ realLabel = int(gt) + 1
+ else:
+ realLabel = int(gt)
+
+ resCnt = min(len(sort_index), topn)
+ for i in range(resCnt):
+ if (str(realLabel) == str(sort_index[i])):
+ count_hit[i] += 1
+ break
+
+ if 'value' not in table_dict.keys():
+ print("the item value does not exist!")
+ else:
+ table_dict["value"].extend(
+ [{"key": "Number of images", "value": str(count)},
+ {"key": "Number of classes", "value": str(n_labels)}])
+ if count == 0:
+ accuracy = 0
+ else:
+ accuracy = np.cumsum(count_hit) / count
+ for i in range(resCnt):
+ table_dict["value"].append({"key": "Top" + str(i + 1) + " accuracy",
+ "value": str(
+ round(accuracy[i] * 100, 2)) + '%'})
+ json.dump(table_dict, writer)
+ writer.close()
+
+
+if __name__ == '__main__':
+ start = time.time()
+ try:
+ # txt file path
+ folder_davinci_target = sys.argv[1]
+ # annotation files path, "val_label.txt"
+ annotation_file_path = sys.argv[2]
+ # the path to store the results json path
+ result_json_path = sys.argv[3]
+ # result json file name
+ json_file_name = sys.argv[4]
+ except IndexError:
+ print("Stopped!")
+ exit(1)
+
+ if not (os.path.exists(folder_davinci_target)):
+ print("target file folder does not exist.")
+
+ if not (os.path.exists(annotation_file_path)):
+ print("Ground truth file does not exist.")
+
+ if not (os.path.exists(result_json_path)):
+ print("Result folder doesn't exist.")
+
+ img_label_dict = cre_groundtruth_dict_fromtxt(annotation_file_path)
+ create_visualization_statistical_result(folder_davinci_target,
+ result_json_path, json_file_name,
+ img_label_dict, topn=5)
+
+ elapsed = (time.time() - start)
+ print("Time used:", elapsed)
+
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/imagenet_torch_preprocess.py b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/imagenet_torch_preprocess.py
new file mode 100644
index 0000000000000000000000000000000000000000..3938928ac1ada0a0d3e0892da9ede0144432b87a
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/imagenet_torch_preprocess.py
@@ -0,0 +1,132 @@
+# Copyright 2020 Huawei Technologies Co., Ltd
+#
+# 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 os
+import sys
+import glob
+import argparse
+
+from PIL import Image
+import numpy as np
+import multiprocessing
+
+
+model_config = {
+ 'resnet': {
+ 'resize': 256,
+ 'centercrop': 224,
+ 'mean': [0.485, 0.456, 0.406],
+ 'std': [0.229, 0.224, 0.225],
+ },
+ 'inceptionv3': {
+ 'resize': 342,
+ 'centercrop': 299,
+ 'mean': [0.485, 0.456, 0.406],
+ 'std': [0.229, 0.224, 0.225],
+ },
+ 'inceptionv4': {
+ 'resize': 342,
+ 'centercrop': 299,
+ 'mean': [0.5, 0.5, 0.5],
+ 'std': [0.5, 0.5, 0.5],
+ },
+}
+
+
+def center_crop(img, output_size):
+ if isinstance(output_size, int):
+ output_size = (int(output_size), int(output_size))
+ image_width, image_height = img.size
+ crop_height, crop_width = output_size
+ crop_top = int(round((image_height - crop_height) / 2.))
+ crop_left = int(round((image_width - crop_width) / 2.))
+ return img.crop((crop_left, crop_top, crop_left + crop_width, crop_top + crop_height))
+
+
+def resize(img, size, interpolation=Image.BILINEAR):
+ if isinstance(size, int):
+ w, h = img.size
+ if (w <= h and w == size) or (h <= w and h == size):
+ return img
+ if w < h:
+ ow = size
+ oh = int(size * h / w)
+ return img.resize((ow, oh), interpolation)
+ else:
+ oh = size
+ ow = int(size * w / h)
+ return img.resize((ow, oh), interpolation)
+ else:
+ return img.resize(size[::-1], interpolation)
+
+
+def gen_input_bin(mode_type_name, file_batches, batch, save_path_name):
+ i = 0
+ for files in file_batches[batch]:
+ i = i + 1
+ print("batch", batch, files, "===", i)
+
+ # RGBA to RGB
+ image = Image.open(os.path.join(src_path, files)).convert('RGB')
+ image = resize(image, model_config[mode_type_name]['resize']) # Resize
+ image = center_crop(image, model_config[mode_type_name]['centercrop']) # CenterCrop
+ img = np.array(image, dtype=np.float32)
+ img = img.transpose(2, 0, 1) # ToTensor: HWC -> CHW
+ img = img / 255. # ToTensor: div 255
+ img -= np.array(model_config[mode_type_name]['mean'], dtype=np.float32)[:, None, None] # Normalize: mean
+ img /= np.array(model_config[mode_type_name]['std'], dtype=np.float32)[:, None, None] # Normalize: std
+ print(img.shape)
+ print(img.dtype)
+ img.tofile(os.path.join(save_path_name, files.split('.')[0].split('/')[-1] + ".bin"))
+
+
+def preprocess(mode_type_name, src_path_name, save_path_name):
+ files = glob.glob(f"{src_path_name}/*.JPEG") + glob.glob(f"{src_path_name}/*/*.JPEG")
+ file_batches = [files[i:i + 500] for i in range(0, 50000, 500) if files[i:i + 500] != []]
+ print(len(file_batches))
+ thread_pool = multiprocessing.Pool(int(multiprocessing.cpu_count()))
+ for batch in range(len(file_batches)):
+ thread_pool.apply_async(gen_input_bin, args=(mode_type_name, file_batches, batch, save_path_name))
+ thread_pool.close()
+ thread_pool.join()
+ print("in thread, except will not report! please ensure bin files generated.")
+
+
+if __name__ == '__main__':
+ # if len(sys.argv) < 4:
+ # raise Exception("usage: python3 xxx.py [model_type] [src_path] [save_path]")
+ # mode_type = sys.argv[1]
+ # src_path = sys.argv[2]
+ # save_path = sys.argv[3]
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument("--mode_type", default = "resnet", type=str)
+ parser.add_argument("--src_path", default="/home/ascend/imagenet/val")
+ parser.add_argument("--save_path", default="./prep_dataset")
+ args = parser.parse_args()
+ mode_type = args.mode_type
+ src_path = args.src_path
+ save_path = args.save_path
+
+ src_path = os.path.realpath(src_path)
+ if mode_type not in model_config:
+ model_type_help = "model type: "
+ for key in model_config.keys():
+ model_type_help += key
+ model_type_help += ' '
+ raise Exception(model_type_help)
+ if not os.path.isdir(save_path):
+ os.makedirs(os.path.realpath(save_path))
+ preprocess(mode_type, src_path, save_path)
+
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/mobilenet-v1_pth2onnx.py b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/mobilenet-v1_pth2onnx.py
new file mode 100644
index 0000000000000000000000000000000000000000..f254c26ddfc1cf0b7fafd9d8a34eeda0813d9609
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/mobilenet-v1_pth2onnx.py
@@ -0,0 +1,107 @@
+# Copyright 2020 Huawei Technologies Co., Ltd
+#
+# 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 os
+import sys
+from collections import OrderedDict
+
+import torch
+import torch.nn as nn
+import torch.onnx
+
+
+class Net(nn.Module):
+ def __init__(self):
+ super(Net, self).__init__()
+
+ def conv_bn(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True)
+ )
+
+ def conv_dw(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
+ nn.BatchNorm2d(inp),
+ nn.ReLU(inplace=True),
+
+ nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True),
+ )
+
+ self.model = nn.Sequential(
+ conv_bn(3, 32, 2),
+ conv_dw(32, 64, 1),
+ conv_dw(64, 128, 2),
+ conv_dw(128, 128, 1),
+ conv_dw(128, 256, 2),
+ conv_dw(256, 256, 1),
+ conv_dw(256, 512, 2),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 1024, 2),
+ conv_dw(1024, 1024, 1),
+ nn.AvgPool2d(7),
+ )
+ self.fc = nn.Linear(1024, 1000)
+
+ def forward(self, x):
+ x = self.model(x)
+ x = x.view(-1, 1024)
+ x = self.fc(x)
+ return x
+
+
+def proc_nodes_module(checkpoint, AttrName):
+ new_state_dict = OrderedDict()
+ for k, v in checkpoint[AttrName].items():
+ if k[0:7] == "module.":
+ name = k[7:]
+ else:
+ name = k[0:]
+ new_state_dict[name] = v
+ return new_state_dict
+
+
+def convert_model_to_onnx(model_state, output_file):
+ model = Net()
+ if model_state:
+ model.load_state_dict(model_state)
+ model.eval()
+ input_names = ["image"]
+ output_names = ["class"]
+ dynamic_axes = {'image': {0: '-1'}, 'class': {0: '-1'}}
+ dummy_input = torch.randn(32, 3, 224, 224) # (batch_size, channels, width, height)
+ torch.onnx.export(model, dummy_input, output_file, input_names=input_names, dynamic_axes=dynamic_axes,
+ output_names=output_names, opset_version=11, verbose=True)
+
+
+if __name__ == '__main__':
+ checkpoint_file = sys.argv[1]
+ output_file = sys.argv[2]
+
+ if os.path.isfile(checkpoint_file):
+ checkpoint = torch.load(checkpoint_file, map_location='cpu')
+ print("{} successfully loaded.".format(checkpoint_file))
+ model_state = proc_nodes_module(checkpoint, 'state_dict')
+ else:
+ print("Failed to load checkpoint from {}! Output model with initial state.".format(checkpoint_file))
+ model_state = OrderedDict()
+ convert_model_to_onnx(model_state, output_file)
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/perf.py b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/perf.py
new file mode 100644
index 0000000000000000000000000000000000000000..fb499e304f74a3c0e15e0f98282c8efb784da4e6
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/perf.py
@@ -0,0 +1,125 @@
+"""
+切python2.0.1环境
+"""
+
+import argparse
+import time
+from tqdm import tqdm
+
+import torch
+import numpy as np
+
+import torch_aie
+from torch_aie import _enums
+
+from ais_bench.infer.interface import InferSession
+
+# OM_PATH = "/home/devkit1/cgznb1/vgg16-torchaie/vgg16_bs1.om" # revise static
+# OM_PATH = "/onnx/mobilenetv1/mobilenet-v1_bs1.om" # revise dynm
+# TS_PATH = "/onnx/mobilenetv1/mobilenetv1.ts" # revise
+INPUT_WIDTH = 224 # revise
+INPUT_HEIGHT = 224 # revise
+
+def parse_args():
+ args = argparse.ArgumentParser(description="A program that operates in 'om' or 'ts' mode.")
+ args.add_argument("--mode", choices=["om", "ts"], required=True, help="Specify the mode ('om' or 'ts').")
+ args.add_argument('--om_path',help='MobilenetV1 om file path', type=str,
+ default='/onnx/mobilenetv1/mobilenet-v1_bs1.om'
+ )
+ args.add_argument('--ts_path',help='MobilenetV1 ts file path', type=str,
+ default='/onnx/mobilenetv1/mobilenetv1.ts'
+ )
+ return args.parse_args()
+
+if __name__ == '__main__':
+ infer_times = 100
+ om_cost = 0
+ pt_cost = 0
+ opts = parse_args()
+ OM_PATH = opts.om_path
+ TS_PATH = opts.ts_path
+ batch_size = 1
+ warm_ctr = 10
+
+ if opts.mode == "om":
+ om_model = InferSession(0, OM_PATH)
+
+ while warm_ctr:
+ dummy_input = np.random.randn(1, 3, INPUT_WIDTH, INPUT_HEIGHT).astype(np.float32)
+ output = om_model.infer([dummy_input], 'static', custom_sizes=90000000)
+ warm_ctr -= 1
+
+ for _ in tqdm(range(0, infer_times)):
+ dummy_input = np.random.randn(1, 3, INPUT_WIDTH, INPUT_HEIGHT).astype(np.float32)
+ start = time.time()
+ output = om_model.infer([dummy_input], 'static', custom_sizes=90000000) # revise static
+ # output = om_model.infer([dummy_input], 'dymshape', custom_sizes=4000) # revise dynm fp32为4个字节,输出为1x1000
+ cost = time.time() - start
+ om_cost += cost
+ print(f"fps: {infer_times} * {batch_size} / {om_cost : .3f} samples/s")
+ print("om fps: ", infer_times * batch_size / om_cost)
+
+ if opts.mode == "ts":
+ ts_model = torch.jit.load(TS_PATH)
+
+ # revise static
+ input_info = [torch_aie.Input((1, 3, INPUT_WIDTH, INPUT_HEIGHT))]
+
+ # revise dynm
+ # min_shape = (1, 3, INPUT_WIDTH, INPUT_HEIGHT)
+ # max_shape = (32, 3, INPUT_WIDTH, INPUT_HEIGHT)
+ # input_info = [torch_aie.Input(min_shape=(1,3,INPUT_WIDTH,INPUT_HEIGHT), max_shape=(32,3,INPUT_WIDTH,INPUT_HEIGHT))]
+
+ # checkpoint = torch.load("/onnx/psenet/PSENet_for_PyTorch_1.2.pth", map_location='cpu')
+ # checkpoint['state_dict'] = proc_nodes_module(checkpoint, 'state_dict')
+ # # model = mobilenet.mobilenet_v2(pretrained = False)
+ # model = resnet50()
+ # model.load_state_dict(checkpoint['state_dict'])
+ # model.eval()
+ # print(model.forward(torch.ones(1, 3, 704, 1216)).shape)
+
+ torch_aie.set_device(0)
+ print("start compile")
+ torchaie_model = torch_aie.compile(
+ ts_model,
+ inputs=input_info,
+ precision_policy=_enums.PrecisionPolicy.FP32,
+ # allow_tensor_replace_int=True,
+ soc_version='Ascend310P3',
+ # optimization_level=2
+ )
+ print("end compile")
+ torchaie_model.eval()
+
+ dummy_input = np.random.randn(1, 3, INPUT_WIDTH, INPUT_HEIGHT).astype(np.float32)
+ input_tensor = torch.Tensor(dummy_input)
+ loops = 100
+ warm_ctr = 10
+ batch_size = 1
+
+ default_stream = torch_aie.npu.default_stream()
+ time_cost = 0
+
+ while warm_ctr:
+ _ = torchaie_model(input_tensor)
+ default_stream.synchronize()
+ warm_ctr -= 1
+
+ print("send to npu")
+ input_tensor = input_tensor.to("npu")
+ print("finish sent")
+ for i in range(loops):
+ t0 = time.time()
+ _ = torchaie_model(input_tensor)
+ default_stream.synchronize()
+ t1 = time.time()
+ time_cost += (t1 - t0)
+ print(i)
+
+ print(f"fps: {loops} * {batch_size} / {time_cost : .3f} samples/s")
+ print("torch_aie fps: ", loops * batch_size / time_cost)
+
+
+ # print(f'om avg cost: {om_cost/infer_times*1000} ms')
+
+
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/README.md b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/README.md
new file mode 100644
index 0000000000000000000000000000000000000000..08c74cbfc147d6d0ea435a20a0392790e7954485
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/README.md
@@ -0,0 +1,81 @@
+Implementation of MobileNet, modified from https://github.com/pytorch/examples/tree/master/imagenet.
+imagenet data is processed [as described here](https://github.com/facebook/fb.resnet.torch/blob/master/INSTALL.md#download-the-imagenet-dataset)
+
+
+nohup python main.py -a mobilenet ImageNet-Folder > log.txt &
+
+Results
+- sgd : top1 68.848 top5 88.740 [download](https://pan.baidu.com/s/1nuRcK3Z)
+- rmsprop: top1 0.104 top5 0.494
+- rmsprop init from sgd : top1 69.526 top5 88.978 [donwload](https://pan.baidu.com/s/1eRCxYKU)
+- paper: top1 70.6
+
+Benchmark:
+
+Titan-X, batchsize = 16
+```
+ resnet18 : 0.004030
+ alexnet : 0.001395
+ vgg16 : 0.002310
+squeezenet : 0.009848
+ mobilenet : 0.073611
+```
+Titan-X, batchsize = 1
+```
+ resnet18 : 0.003688
+ alexnet : 0.001179
+ vgg16 : 0.002055
+squeezenet : 0.003385
+ mobilenet : 0.076977
+```
+
+---------
+
+```
+class Net(nn.Module):
+ def __init__(self):
+ super(Net, self).__init__()
+
+ def conv_bn(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True)
+ )
+
+ def conv_dw(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
+ nn.BatchNorm2d(inp),
+ nn.ReLU(inplace=True),
+
+ nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True),
+ )
+
+ self.model = nn.Sequential(
+ conv_bn( 3, 32, 2),
+ conv_dw( 32, 64, 1),
+ conv_dw( 64, 128, 2),
+ conv_dw(128, 128, 1),
+ conv_dw(128, 256, 2),
+ conv_dw(256, 256, 1),
+ conv_dw(256, 512, 2),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 1024, 2),
+ conv_dw(1024, 1024, 1),
+ nn.AvgPool2d(7),
+ )
+ self.fc = nn.Linear(1024, 1000)
+
+ def forward(self, x):
+ x = self.model(x)
+ x = x.view(-1, 1024)
+ x = self.fc(x)
+ return x
+```
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/benchmark.py b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/benchmark.py
new file mode 100644
index 0000000000000000000000000000000000000000..7db54cf0cce865ffeb0dd360bd0373299d66f2f2
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/benchmark.py
@@ -0,0 +1,81 @@
+import time
+import torch
+import torch.nn as nn
+import torch.backends.cudnn as cudnn
+import torchvision.models as models
+from torch.autograd import Variable
+
+class MobileNet(nn.Module):
+ def __init__(self):
+ super(MobileNet, self).__init__()
+
+ def conv_bn(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True)
+ )
+
+ def conv_dw(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
+ nn.BatchNorm2d(inp),
+ nn.ReLU(inplace=True),
+
+ nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True),
+ )
+
+ self.model = nn.Sequential(
+ conv_bn( 3, 32, 2),
+ conv_dw( 32, 64, 1),
+ conv_dw( 64, 128, 2),
+ conv_dw(128, 128, 1),
+ conv_dw(128, 256, 2),
+ conv_dw(256, 256, 1),
+ conv_dw(256, 512, 2),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 1024, 2),
+ conv_dw(1024, 1024, 1),
+ nn.AvgPool2d(7),
+ )
+ self.fc = nn.Linear(1024, 1000)
+
+ def forward(self, x):
+ x = self.model(x)
+ x = x.view(-1, 1024)
+ x = self.fc(x)
+ return x
+
+def speed(model, name):
+ t0 = time.time()
+ input = torch.rand(1,3,224,224).cuda()
+ input = Variable(input, volatile = True)
+ t1 = time.time()
+
+ model(input)
+ t2 = time.time()
+
+ model(input)
+ t3 = time.time()
+
+ print('%10s : %f' % (name, t3 - t2))
+
+if __name__ == '__main__':
+ #cudnn.benchmark = True # This will make network slow ??
+ resnet18 = models.resnet18().cuda()
+ alexnet = models.alexnet().cuda()
+ vgg16 = models.vgg16().cuda()
+ squeezenet = models.squeezenet1_0().cuda()
+ mobilenet = MobileNet().cuda()
+
+ speed(resnet18, 'resnet18')
+ speed(alexnet, 'alexnet')
+ speed(vgg16, 'vgg16')
+ speed(squeezenet, 'squeezenet')
+ speed(mobilenet, 'mobilenet')
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/main.py b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/main.py
new file mode 100644
index 0000000000000000000000000000000000000000..0a0c43e587b5ddd6eebf72d8e3805a17f29cc06f
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/main.py
@@ -0,0 +1,346 @@
+import argparse
+import os
+import shutil
+import time
+
+import torch
+import torch.nn as nn
+import torch.nn.parallel
+import torch.backends.cudnn as cudnn
+import torch.optim
+import torch.utils.data
+import torchvision.transforms as transforms
+import torchvision.datasets as datasets
+import torchvision.models as models
+
+
+model_names = sorted(name for name in models.__dict__
+ if name.islower() and not name.startswith("__")
+ and callable(models.__dict__[name]))
+
+model_names.append('mobilenet')
+
+parser = argparse.ArgumentParser(description='PyTorch ImageNet Training')
+parser.add_argument('--data', metavar='DIR', default = "/home/jcwang/dataset/imagenet-data",
+ help='path to dataset')
+parser.add_argument('--arch', '-a', metavar='ARCH', default='mobilenet',
+ choices=model_names,
+ help='model architecture: ' +
+ ' | '.join(model_names) +
+ ' (default: resnet18)')
+parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
+ help='number of data loading workers (default: 4)')
+parser.add_argument('--epochs', default=90, type=int, metavar='N',
+ help='number of total epochs to run')
+parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
+ help='manual epoch number (useful on restarts)')
+parser.add_argument('-b', '--batch_size', default=64, type=int,
+ metavar='N', help='mini-batch size (default: 256)')
+parser.add_argument('--lr', '--learning-rate', default=0.1, type=float,
+ metavar='LR', help='initial learning rate')
+parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
+ help='momentum')
+parser.add_argument('--weight-decay', '--wd', default=1e-4, type=float,
+ metavar='W', help='weight decay (default: 1e-4)')
+parser.add_argument('--print-freq', '-p', default=10, type=int,
+ metavar='N', help='print frequency (default: 10)')
+parser.add_argument('--resume', default='./mobilenet_sgd_68.848.pth.tar', type=str, metavar='PATH',
+ help='path to latest checkpoint (default: none)')
+parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true',
+ default = 1,
+ help='evaluate model on validation set')
+parser.add_argument('--pretrained', dest='pretrained', action='store_true',
+ help='use pre-trained model')
+
+best_prec1 = 0
+
+class Net(nn.Module):
+ def __init__(self):
+ super(Net, self).__init__()
+
+ def conv_bn(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True)
+ )
+
+ def conv_dw(inp, oup, stride):
+ return nn.Sequential(
+ nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
+ nn.BatchNorm2d(inp),
+ nn.ReLU(inplace=True),
+
+ nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
+ nn.BatchNorm2d(oup),
+ nn.ReLU(inplace=True),
+ )
+
+ self.model = nn.Sequential(
+ conv_bn( 3, 32, 2),
+ conv_dw( 32, 64, 1),
+ conv_dw( 64, 128, 2),
+ conv_dw(128, 128, 1),
+ conv_dw(128, 256, 2),
+ conv_dw(256, 256, 1),
+ conv_dw(256, 512, 2),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 512, 1),
+ conv_dw(512, 1024, 2),
+ conv_dw(1024, 1024, 1),
+ nn.AvgPool2d(7),
+ )
+ self.fc = nn.Linear(1024, 1000)
+
+ def forward(self, x):
+ x = self.model(x)
+ x = x.view(-1, 1024)
+ x = self.fc(x)
+ return x
+
+def mobilenet(path="./checkpoint.pth.tar"):
+ net = Net()
+ state_dict = torch.load(path)
+ net.load_state_dict(state_dict)
+ return net
+
+
+def main():
+ global args, best_prec1
+ args = parser.parse_args()
+
+ # create model
+ if args.pretrained:
+ print("=> using pre-trained model '{}'".format(args.arch))
+ model = models.__dict__[args.arch](pretrained=True)
+ else:
+ print("=> creating model '{}'".format(args.arch))
+ if args.arch.startswith('mobilenet'):
+ model = Net()
+ print(model)
+ else:
+ model = models.__dict__[args.arch]()
+
+ if args.arch.startswith('alexnet') or args.arch.startswith('vgg'):
+ model.features = torch.nn.DataParallel(model.features)
+ model.cuda()
+ else:
+ model = torch.nn.DataParallel(model).cuda()
+
+ # define loss function (criterion) and optimizer
+ criterion = nn.CrossEntropyLoss().cuda()
+
+ optimizer = torch.optim.SGD(model.parameters(), args.lr,
+ momentum=args.momentum,
+ weight_decay=args.weight_decay)
+
+ # optionally resume from a checkpoint
+ if args.resume:
+ if os.path.isfile(args.resume):
+ print("=> loading checkpoint '{}'".format(args.resume))
+ checkpoint = torch.load(args.resume)
+ args.start_epoch = checkpoint['epoch']
+ best_prec1 = checkpoint['best_prec1']
+ model.load_state_dict(checkpoint['state_dict'])
+ optimizer.load_state_dict(checkpoint['optimizer'])
+ print("=> loaded checkpoint '{}' (epoch {})"
+ .format(args.resume, checkpoint['epoch']))
+ else:
+ print("=> no checkpoint found at '{}'".format(args.resume))
+
+ cudnn.benchmark = True
+
+ # Data loading code
+ traindir = os.path.join(args.data, 'train')
+ valdir = os.path.join(args.data, 'val')
+ normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
+ std=[0.229, 0.224, 0.225])
+ if args.evaluate:
+ val_loader = torch.utils.data.DataLoader(
+ datasets.ImageFolder(valdir, transforms.Compose([
+ transforms.Resize(256),
+ transforms.CenterCrop(224),
+ transforms.ToTensor(),
+ normalize,
+ ])),
+ batch_size=args.batch_size, shuffle=False,
+ num_workers=args.workers, pin_memory=True)
+ validate(val_loader, model, criterion)
+ return
+ else:
+ train_loader = torch.utils.data.DataLoader(
+ datasets.ImageFolder(traindir, transforms.Compose([
+ transforms.RandomSizedCrop(224),
+ transforms.RandomHorizontalFlip(),
+ transforms.ToTensor(),
+ normalize,
+ ])),
+ batch_size=args.batch_size, shuffle=True,
+ num_workers=args.workers, pin_memory=True)
+
+ for epoch in range(args.start_epoch, args.epochs):
+ adjust_learning_rate(optimizer, epoch)
+
+ # train for one epoch
+ train(train_loader, model, criterion, optimizer, epoch)
+
+ # evaluate on validation set
+ prec1 = validate(val_loader, model, criterion)
+
+ # remember best prec@1 and save checkpoint
+ is_best = prec1 > best_prec1
+ best_prec1 = max(prec1, best_prec1)
+ save_checkpoint({
+ 'epoch': epoch + 1,
+ 'arch': args.arch,
+ 'state_dict': model.state_dict(),
+ 'best_prec1': best_prec1,
+ 'optimizer' : optimizer.state_dict(),
+ }, is_best)
+
+
+def train(train_loader, model, criterion, optimizer, epoch):
+ batch_time = AverageMeter()
+ data_time = AverageMeter()
+ losses = AverageMeter()
+ top1 = AverageMeter()
+ top5 = AverageMeter()
+
+ # switch to train mode
+ model.train()
+
+ end = time.time()
+ for i, (input, target) in enumerate(train_loader):
+ # measure data loading time
+ data_time.update(time.time() - end)
+
+ target = target.cuda(async=True)
+ input_var = torch.autograd.Variable(input)
+ target_var = torch.autograd.Variable(target)
+
+ # compute output
+ output = model(input_var)
+ loss = criterion(output, target_var)
+
+ # measure accuracy and record loss
+ prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
+ losses.update(loss.data[0], input.size(0))
+ top1.update(prec1[0], input.size(0))
+ top5.update(prec5[0], input.size(0))
+
+ # compute gradient and do SGD step
+ optimizer.zero_grad()
+ loss.backward()
+ optimizer.step()
+
+ # measure elapsed time
+ batch_time.update(time.time() - end)
+ end = time.time()
+
+ if i % args.print_freq == 0:
+ print('Epoch: [{0}][{1}/{2}]\t'
+ 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
+ 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
+ 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
+ 'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
+ 'Prec@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
+ epoch, i, len(train_loader), batch_time=batch_time,
+ data_time=data_time, loss=losses, top1=top1, top5=top5))
+
+
+def validate(val_loader, model, criterion):
+ batch_time = AverageMeter()
+ losses = AverageMeter()
+ top1 = AverageMeter()
+ top5 = AverageMeter()
+
+ # switch to evaluate mode
+ model.eval()
+
+ end = time.time()
+ for i, (input, target) in enumerate(val_loader):
+ target = target.cuda(async=True)
+ input_var = input.cuda()
+ target_var = target
+
+ # compute output
+ output = model(input_var)
+ loss = criterion(output, target_var)
+
+ # measure accuracy and record loss
+ prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
+ losses.update(loss.item(), input.size(0))
+ top1.update(prec1.item(), input.size(0))
+ top5.update(prec5.item(), input.size(0))
+
+ # measure elapsed time
+ batch_time.update(time.time() - end)
+ end = time.time()
+
+ if i % args.print_freq == 0:
+ print('Test: [{0}/{1}]\t'
+ 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
+ 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
+ 'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
+ 'Prec@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
+ i, len(val_loader), batch_time=batch_time, loss=losses,
+ top1=top1, top5=top5))
+
+ print(' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}'
+ .format(top1=top1, top5=top5))
+
+ return top1.avg
+
+
+def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
+ torch.save(state, filename)
+ if is_best:
+ shutil.copyfile(filename, 'model_best.pth.tar')
+
+
+class AverageMeter(object):
+ """Computes and stores the average and current value"""
+ def __init__(self):
+ self.reset()
+
+ def reset(self):
+ self.val = 0
+ self.avg = 0
+ self.sum = 0
+ self.count = 0
+
+ def update(self, val, n=1):
+ self.val = val
+ self.sum += val * n
+ self.count += n
+ self.avg = self.sum / self.count
+
+
+def adjust_learning_rate(optimizer, epoch):
+ """Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
+ lr = args.lr * (0.1 ** (epoch // 30))
+ for param_group in optimizer.param_groups:
+ param_group['lr'] = lr
+
+
+def accuracy(output, target, topk=(1,)):
+ """Computes the precision@k for the specified values of k"""
+ maxk = max(topk)
+ batch_size = target.size(0)
+
+ _, pred = output.topk(maxk, 1, True, True)
+ pred = pred.t()
+ correct = pred.eq(target.view(1, -1).expand_as(pred))
+
+ res = []
+ for k in topk:
+ correct_k = correct[:k].view(-1).float().sum(0)
+ res.append(correct_k.mul_(100.0 / batch_size))
+ return res
+
+
+if __name__ == '__main__':
+ main()
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/run.sh b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/run.sh
new file mode 100644
index 0000000000000000000000000000000000000000..f31ea31a7226955dd8bf6128abcad821126543e1
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/pytorch-mobilenet-v1-master/run.sh
@@ -0,0 +1,2 @@
+#python main.py -a alexnet /home/xiaohang/ImageNet/
+nohup python main.py -a mobilenet /home/xiaohang/ImageNet/ > log.txt &
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/requirements.txt b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/requirements.txt
new file mode 100644
index 0000000000000000000000000000000000000000..796465d58538f4133ce49fd6d42c9d66f4bf3506
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/requirements.txt
@@ -0,0 +1,254 @@
+absl-py==1.4.0
+addict==2.4.0
+aenum==3.1.15
+aiohttp==3.8.6
+aiosignal==1.3.1
+antlr4-python3-runtime==4.9.3
+anyio==4.0.0
+appdirs==1.4.4
+argon2-cffi==23.1.0
+argon2-cffi-bindings==21.2.0
+arrow==1.3.0
+ascendie==6.3rc2
+astor==0.8.1
+asttokens==2.2.1
+astunparse==1.6.3
+async-lru==2.0.4
+async-timeout==4.0.3
+attrs==23.1.0
+Babel==2.13.0
+backcall==0.2.0
+bce-python-sdk==0.8.95
+beautifulsoup4==4.12.2
+black==21.4b2
+bleach==6.1.0
+blinker==1.7.0
+cachetools==5.3.1
+certifi==2023.5.7
+cffi==1.15.1
+chainer==7.8.1
+charset-normalizer==3.1.0
+click==8.1.7
+click-aliases==1.0.1
+cloudpickle==2.2.1
+cmake==3.26.4
+coloredlogs==15.0.1
+comm==0.1.4
+contourpy==1.2.0
+custom-passes-reduce==0.0.0
+cycler==0.12.1
+debugpy==1.8.0
+decorator==5.1.1
+defusedxml==0.7.1
+easydict==1.7
+exceptiongroup==1.1.3
+executing==1.2.0
+fastjsonschema==2.18.1
+filelock==3.12.2
+Flask==3.0.0
+flask-babel==4.0.0
+flatbuffers==1.12
+fonttools==4.44.0
+fqdn==1.5.1
+frozenlist==1.4.0
+fsspec==2023.9.0
+future==0.18.3
+fvcore==0.1.5.post20221221
+gast==0.4.0
+gitdb==4.0.10
+GitPython==3.1.31
+google-auth==2.22.0
+google-auth-oauthlib==0.4.6
+google-pasta==0.2.0
+greenlet==3.0.1
+grpcio==1.59.2
+h11==0.14.0
+h5py==3.9.0
+httpcore==1.0.2
+httpx==0.25.1
+huggingface-hub==0.17.1
+humanfriendly==10.0
+hydra-core==1.3.2
+idna==3.4
+imageio==2.31.5
+imgaug==0.4.0
+importlib-metadata==6.8.0
+importlib-resources==6.1.1
+iopath==0.1.8
+ipykernel==6.25.2
+ipython==8.14.0
+isoduration==20.11.0
+itsdangerous==2.1.2
+jedi==0.18.2
+Jinja2==3.1.2
+joblib==1.3.2
+json-tricks==3.17.3
+json5==0.9.14
+jsonpointer==2.4
+jsonschema==4.19.1
+jsonschema-specifications==2023.7.1
+jupyter-events==0.8.0
+jupyter-lsp==2.2.0
+jupyter_client==8.4.0
+jupyter_core==5.4.0
+jupyter_server==2.8.0
+jupyter_server_terminals==0.4.4
+jupyterlab==4.0.7
+jupyterlab-pygments==0.2.2
+jupyterlab_server==2.25.0
+keras==2.9.0
+Keras-Preprocessing==1.1.2
+kiwisolver==1.4.5
+lazy_loader==0.3
+libclang==16.0.6
+lit==16.0.5.post0
+lmdb==1.4.1
+loguru==0.7.2
+Markdown==3.4.4
+markdown-it-py==3.0.0
+MarkupSafe==2.1.3
+matplotlib==3.8.1
+matplotlib-inline==0.1.6
+mdurl==0.1.2
+mistune==3.0.2
+mmcv==2.0.0rc4
+mmdeploy==1.3.0
+mmdeploy-runtime==1.3.0
+mmdeploy-runtime-gpu==1.3.0
+mmdet==3.2.0
+mmengine==0.9.1
+mpmath==1.3.0
+multidict==6.0.4
+mypy-extensions==1.0.0
+nbclient==0.8.0
+nbconvert==7.9.2
+nbformat==5.9.2
+nest-asyncio==1.5.8
+networkx==3.1
+ninja==1.11.1.1
+notebook_shim==0.2.3
+numpy==1.26.1
+oauthlib==3.2.2
+omegaconf==2.3.0
+onnx==1.15.0
+onnx-simplifier==0.4.33
+onnxconverter-common==1.14.0
+onnxoptimizer==0.3.13
+onnxruntime==1.16.1
+onnxruntime-tools==1.7.0
+onnxsim==0.4.10
+opencv-python==4.8.1.78
+opt-einsum==3.3.0
+overrides==7.4.0
+packaging==23.2
+paddle2onnx==1.1.0
+paddlepaddle==2.5.2
+pandas==2.0.2
+pandocfilters==1.5.0
+parso==0.8.3
+pathlib2==2.3.7.post1
+pathspec==0.11.2
+pexpect==4.8.0
+pickleshare==0.7.5
+Pillow==10.1.0
+platformdirs==3.11.0
+portalocker==2.8.2
+prettytable==3.9.0
+prometheus-client==0.17.1
+prompt-toolkit==3.0.39
+protobuf==3.20.2
+psutil==5.9.5
+ptyprocess==0.7.0
+pure-eval==0.2.2
+py-cpuinfo==9.0.0
+py3nvml==0.2.7
+pyarrow==13.0.0
+pyascendie==0.0.0
+pyasn1==0.5.0
+pyasn1-modules==0.3.0
+pyclipper==1.3.0.post5
+pycocotools==2.0.7
+pycparser==2.21
+pycryptodome==3.19.0
+pydot==1.4.2
+Pygments==2.16.1
+pyparsing==3.1.1
+python-dateutil==2.8.2
+python-json-logger==2.0.7
+pytz==2023.3
+PyYAML==6.0.1
+pyzmq==25.1.1
+rarfile==4.1
+referencing==0.30.2
+regex==2023.10.3
+requests==2.31.0
+requests-oauthlib==1.3.1
+responses==0.18.0
+rfc3339-validator==0.1.4
+rfc3986-validator==0.1.1
+rich==13.6.0
+rpds-py==0.10.6
+rsa==4.9
+safetensors==0.3.3
+scikit-image==0.22.0
+scikit-learn==1.3.1
+scipy==1.10.1
+seaborn==0.12.2
+Send2Trash==1.8.2
+sentry-sdk==1.31.0
+Shapely==1.6.4
+six==1.16.0
+skl2onnx==1.15.0
+sklearn==0.0
+smmap==5.0.0
+sniffio==1.3.0
+soupsieve==2.5
+stack-data==0.6.2
+sympy==1.12
+synr==0.5.0
+tabulate==0.9.0
+termcolor==2.3.0
+terminado==0.17.1
+terminaltables==3.1.10
+tf2onnx==1.15.1
+thop==0.1.1.post2209072238
+threadpoolctl==3.2.0
+tifffile==2023.8.30
+timm==0.6.13
+tinycss2==1.2.1
+tokenizers==0.14.1
+toml==0.10.2
+tomli==2.0.1
+torch==2.0.1
+torchaudio==2.0.2
+torchsummary==1.5.1
+torchvision==0.15.2
+tornado==6.3.2
+tqdm==4.64.0
+traitlets==5.9.0
+transformers==4.34.0
+triton==2.0.0
+typeguard==2.13.3
+types-python-dateutil==2.8.19.14
+typing_extensions==4.5.0
+tzdata==2023.3
+uri-template==1.3.0
+urllib3==1.26.16
+virtualenv==20.24.5
+visualdl==2.5.3
+wcwidth==0.2.9
+webcolors==1.13
+webencodings==0.5.1
+websocket-client==1.6.4
+websockets==12.0
+Werkzeug==3.0.1
+wikiextractor==3.0.6
+wrapt==1.15.0
+xmltodict==0.13.0
+xxhash==3.4.1
+yacs==0.1.8
+yapf==0.40.2
+yarl==1.9.2
+zipp==3.17.0
+zope.event==5.0
+zope.interface==6.1
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/result_torchaie.json b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/result_torchaie.json
new file mode 100644
index 0000000000000000000000000000000000000000..c130fbd73174179f60f88bc76caf3f3e6a2cf805
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/result_torchaie.json
@@ -0,0 +1 @@
+{"title": "Overall statistical evaluation", "value": [{"key": "Number of images", "value": "50000"}, {"key": "Number of classes", "value": "1000"}, {"key": "Top1 accuracy", "value": "69.6%"}, {"key": "Top2 accuracy", "value": "80.52%"}, {"key": "Top3 accuracy", "value": "84.88%"}, {"key": "Top4 accuracy", "value": "87.35%"}, {"key": "Top5 accuracy", "value": "89.06%"}]}
\ No newline at end of file
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/run.py b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/run.py
new file mode 100644
index 0000000000000000000000000000000000000000..3c793447c986a9440ab0d2fe9a65a9efd81b991e
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/mobilenetv1/run.py
@@ -0,0 +1,207 @@
+# Copyright 2020 Huawei Technologies Co., Ltd
+#
+# 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 os
+import sys
+import json
+import time
+from tqdm import tqdm
+import argparse
+
+import numpy as np
+import torch
+
+import torch_aie
+from torch_aie import _enums
+
+np.set_printoptions(threshold=sys.maxsize)
+
+LABEL_FILE = "HiAI_label.json"
+
+
+def gen_file_name(img_name):
+ full_name = img_name.split('/')[-1]
+ index = full_name.rfind('.')
+ return full_name[:index]
+
+
+def cre_groundtruth_dict(gtfile_path):
+ """
+ :param filename: file contains the imagename and label number
+ :return: dictionary key imagename, value is label number
+ """
+ img_gt_dict = {}
+ for gtfile in os.listdir(gtfile_path):
+ if (gtfile != LABEL_FILE):
+ with open(os.path.join(gtfile_path, gtfile), 'r') as f:
+ gt = json.load(f)
+ ret = gt["image"]["annotations"][0]["category_id"]
+ img_gt_dict[gen_file_name(gtfile)] = ret
+ return img_gt_dict
+
+
+def cre_groundtruth_dict_fromtxt(gtfile_path):
+ """
+ :param filename: file contains the imagename and label number
+ :return: dictionary key imagename, value is label number
+ """
+ img_gt_dict = {}
+ with open(gtfile_path, 'r')as f:
+ for line in f.readlines():
+ temp = line.strip().split(" ")
+ imgName = temp[0].split(".")[0]
+ imgLab = temp[1]
+ img_gt_dict[imgName] = imgLab
+ return img_gt_dict
+
+
+def load_statistical_predict_result(filepath):
+ """
+ function:
+ the prediction esult file data extraction
+ input:
+ result file:filepath
+ output:
+ n_label:numble of label
+ data_vec: the probabilitie of prediction in the 1000
+ :return: probabilities, numble of label, in_type, color
+ """
+ with open(filepath, 'r')as f:
+ data = f.readline()
+ temp = data.strip().split(" ")
+ n_label = len(temp)
+ if data == '':
+ n_label = 0
+ data_vec = np.zeros((n_label), dtype=np.float32)
+ in_type = ''
+ color = ''
+ if n_label == 0:
+ in_type = f.readline()
+ color = f.readline()
+ else:
+ for ind, prob in enumerate(temp):
+ data_vec[ind] = np.float32(prob)
+ return data_vec, n_label, in_type, color
+
+
+def create_visualization_statistical_result(result_store_path, json_file_name,
+ img_gt_dict, ts_model_path, input_bin_folder_path, topn=5):
+ """
+ :param prediction_file_path:
+ :param result_store_path:
+ :param json_file_name:
+ :param img_gt_dict:
+ :param topn:
+ :return:
+ """
+ writer = open(os.path.join(result_store_path, json_file_name), 'w')
+ table_dict = {}
+ table_dict["title"] = "Overall statistical evaluation"
+ table_dict["value"] = []
+
+ count = 0
+ resCnt = 0
+ n_labels = 1000
+ count_hit = np.zeros(topn)
+
+ ts_model = torch.jit.load(ts_model_path)
+ input_info = [torch_aie.Input((1, 3, 224, 224))]
+ torch_aie.set_device(0)
+ print("Start compiling...")
+ torchaie_model = torch_aie.compile(
+ ts_model,
+ inputs=input_info,
+ precision_policy=_enums.PrecisionPolicy.FP32,
+ soc_version='Ascend310P3'
+ )
+ print("Compile finished!")
+ torchaie_model.eval()
+
+ list_files_input_bin = os.listdir(input_bin_folder_path)
+ list_files_input_bin.sort()
+
+ for binfile_name in tqdm(list_files_input_bin):
+ count += 1
+ input_bin_filepath = os.path.join(input_bin_folder_path, binfile_name)
+ input_np_arr = np.fromfile(input_bin_filepath, dtype=np.float32).reshape((1, 3, 224, 224)) # 保存进去就是C在最后一维度,因此读出也要保持一致!
+ input_tensor = torch.tensor(input_np_arr, dtype=torch.float32)
+ # pred = ts_model.forward(input_tensor)
+ pred = torchaie_model.forward(input_tensor)
+ sort_index = np.argsort(-pred.detach().numpy())[0]
+
+ img_name = binfile_name.split('.')[0]
+ gt = img_gt_dict[img_name]
+ realLabel = int(gt)
+ resCnt = min(len(sort_index), topn)
+ for i in range(resCnt):
+ if (str(realLabel) == str(sort_index[i])):
+ count_hit[i] += 1
+ break
+
+ if 'value' not in table_dict.keys():
+ print("the item value does not exist!")
+ else:
+ table_dict["value"].extend(
+ [{"key": "Number of images", "value": str(count)},
+ {"key": "Number of classes", "value": str(n_labels)}])
+ if count == 0:
+ accuracy = 0
+ else:
+ accuracy = np.cumsum(count_hit) / count
+ for i in range(resCnt):
+ table_dict["value"].append({"key": "Top" + str(i + 1) + " accuracy",
+ "value": str(
+ round(accuracy[i] * 100, 2)) + '%'})
+ json.dump(table_dict, writer)
+ writer.close()
+
+
+if __name__ == '__main__':
+ # try:
+ # # txt file path
+ # folder_davinci_target = sys.argv[1]
+ # # annotation files path, "val_label.txt"
+ # annotation_file_path = sys.argv[2]
+ # # the path to store the results json path
+ # result_json_path = sys.argv[3]
+ # # result json file name
+ # json_file_name = sys.argv[4]
+ # except IndexError:
+ # print("Stopped!")
+ # exit(1)
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument("--annotation_file_path", default = "/home/ascend/imagenet/val_label.txt", type=str, help="annotation file path")
+ parser.add_argument("--result_json_path", default="./")
+ parser.add_argument("--json_file_name", default="result_torchaie.json")
+ parser.add_argument("--ts_model_path", default="./mobilenetv1.ts")
+ parser.add_argument("--input_bin_folder_path", default="./prep_dataset")
+ args = parser.parse_args()
+ annotation_file_path = args.annotation_file_path
+ result_json_path = args.result_json_path
+ json_file_name = args.json_file_name
+ ts_model_path = args.ts_model_path
+ input_bin_folder_path = args.input_bin_folder_path
+
+ if not (os.path.exists(annotation_file_path)):
+ print("Ground truth file does not exist.")
+
+ if not (os.path.exists(result_json_path)):
+ print("Result folder doesn't exist.")
+
+ img_label_dict = cre_groundtruth_dict_fromtxt(annotation_file_path)
+ create_visualization_statistical_result(result_json_path, json_file_name,
+ img_label_dict, ts_model_path, input_bin_folder_path, topn=5)
+
+
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/README.md b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/README.md
new file mode 100644
index 0000000000000000000000000000000000000000..8c36228e87834e278f5dae5a7d43e0b03c98da3b
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/README.md
@@ -0,0 +1,40 @@
+## 构建虚拟环境
+
+`conda create --name repvgg python=3.9`
+激活:`conda activate repvgg`
+
+## 安装依赖
+
+`pip3 install -r requirements.txt`
+
+编译pt插件,在dist目录下安装torh_aie
+
+## 更改路径
+
+在`export.py`函数中的第七行,更改`github-src/RepVGG-main`为当前环境的路径,如:
+`sys.path.append(r"/onnx/github-src/RepVGG-main/")`
+
+## 下载pth模型
+
+自行前往下面google drive链接,下载模型文件`RepVGG-A0-train.pth`并放置在`repvgg`路径下
+链接:https://drive.google.com/drive/folders/1Avome4KvNp0Lqh2QwhXO6L5URQjzCjUq
+
+## trace得到ts文件
+
+将model_path改为自己tar模型的路径
+`python3 export.py --model_path=./RepVGG_trace.ts`
+
+## 数据预处理
+
+将src_path改为自己目录下数据集图片的路径
+`python3 RepVGG_preprocess.py --src_path /home/pttest_models/imagenet/val --save_path ./prep_dataset`
+
+## 模型推理 - 获取精度
+
+将`gtfile_path`改为自己目录下数据集label的路径
+`python3 run.py --ts_model_path ./RepVGG_trace.ts --input_bin_folder_path ./prep_dataset --gtfile_path /home/pttest_models/imagenet/val_label.txt`
+
+## 推理性能 - ts
+
+将--ts_path改为自己目录下的ts路径
+`python3 perf_right_one.py --mode=ts --ts_path=./RepVGG_trace.ts`
\ No newline at end of file
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/RepVGG_preprocess.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/RepVGG_preprocess.py
new file mode 100644
index 0000000000000000000000000000000000000000..5f037b9a7b24c148badbb655aa31d6d1e9b129e0
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/RepVGG_preprocess.py
@@ -0,0 +1,105 @@
+# Copyright 2022 Huawei Technologies Co., Ltd
+#
+# 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 os
+import sys
+import multiprocessing
+from PIL import Image
+import numpy as np
+from tqdm import tqdm
+
+
+transforms = {
+ 'resize': 256,
+ 'centercrop': 224,
+ 'mean': [0.485, 0.456, 0.406],
+ 'std': [0.229, 0.224, 0.225],
+}
+
+
+def center_crop(img, output_size):
+ if isinstance(output_size, int):
+ output_size = (int(output_size), int(output_size))
+ image_width, image_height = img.size
+ crop_height, crop_width = output_size
+ crop_top = int(round((image_height - crop_height) / 2.))
+ crop_left = int(round((image_width - crop_width) / 2.))
+ img = img.crop((crop_left, crop_top, crop_left + \
+ crop_width, crop_top + crop_height))
+ return img
+
+
+def resize(img, size, interpolation=Image.BILINEAR):
+ if isinstance(size, int):
+ w, h = img.size
+ if (w <= h and w == size) or (h <= w and h == size):
+ return img
+ if w < h:
+ ow = size
+ oh = int(size * h / w)
+ return img.resize((ow, oh), interpolation)
+ else:
+ oh = size
+ ow = int(size * w / h)
+ return img.resize((ow, oh), interpolation)
+ else:
+ return img.resize(size[::-1], interpolation)
+
+
+def gen_input_bin(file_batches, batch):
+ for file in file_batches[batch]:
+ # RGBA to RGB
+ print(file)
+ image = Image.open(os.path.join(src_path, file)).convert('RGB')
+ image = resize(image, transforms['resize']) # Resize
+ image = center_crop(image, transforms['centercrop']) # CenterCrop
+ img = np.array(image, dtype=np.float32)
+ img = img.transpose(2, 0, 1) # ToTensor: HWC -> CHW
+ img = img / 255. # ToTensor: div 255
+ img -= np.array(transforms['mean'], dtype=np.float32)[:, None, None]
+ img /= np.array(transforms['std'], dtype=np.float32)[:, None, None]
+ # print(img.dtype)
+ # print(img.shape)
+ img.tofile(os.path.join(save_path, file.split('.')[0] + ".bin"))
+
+
+def preprocess(src_path, save_path):
+ files = os.listdir(src_path)
+ file_batches = [
+ files[i:i + 500] for i in range(0, 50000, 500)
+ if files[i:i + 500] != []
+ ]
+ thread_pool = multiprocessing.Pool(len(file_batches))
+ for batch in range(len(file_batches)):
+ thread_pool.apply_async(gen_input_bin, args=(file_batches, batch))
+ thread_pool.close()
+ thread_pool.join()
+ print("Done! please ensure bin files generated.")
+
+
+if __name__ == '__main__':
+ import argparse
+ parser = argparse.ArgumentParser('data preprocess.')
+ parser.add_argument('--src_path', type=str, required=True,
+ help='path to original dataset.')
+ parser.add_argument('--save_path', type=str, required=True,
+ help='a directory to save bin files.')
+ args = parser.parse_args()
+
+ src_path = os.path.realpath(args.src_path)
+ save_path = os.path.realpath(args.save_path)
+ if not os.path.isdir(save_path):
+ os.makedirs(save_path)
+ preprocess(src_path, save_path)
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/export.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/export.py
new file mode 100644
index 0000000000000000000000000000000000000000..0c1c8b2412103b117d03c385ff83d93b83c6955c
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/export.py
@@ -0,0 +1,61 @@
+# 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 sys
+import os
+import argparse
+
+import torch
+
+sys.path.append(r"./github-src/RepVGG-main/")
+from repvgg import create_RepVGG_A0
+
+
+def parse_args():
+ parser = argparse.ArgumentParser(description='Export RepVGG .ts model file')
+ parser.add_argument('--model_path',help='RepVGG pth file path', type=str,
+ default='./RepVGG-A0-train.pth'
+ )
+ parser.add_argument('--ts_save_path', help='RepVGG torch script model save path', type=str,
+ default='RepVGG_trace.ts') # revise
+
+ args = parser.parse_args()
+ return args
+
+def check_args(args):
+ if not os.path.exists(args.model_path):
+ raise FileNotFoundError(f'RepVGG model file {args.model_path} not exists')
+
+def trace_ts_model(model_path, ts_save_path):
+
+ # trace model
+ input_data = torch.ones(1, 3, 224, 224)
+ ts_model = torch.jit.trace(model, input_data)
+ ts_model.save(ts_save_path)
+ print(f'RepVGG torch script model saved to {ts_save_path}')
+
+
+if __name__ == '__main__':
+ print('Start to export RepVGG torch script model')
+ opts = parse_args()
+ check_args(opts)
+
+ checkpoint = torch.load(opts.model_path, map_location='cpu')
+ model = create_RepVGG_A0()
+ model.load_state_dict(checkpoint)
+ model.eval()
+
+ # load & trace model
+ trace_ts_model(opts.model_path, opts.ts_save_path)
+ print("Finish Tracing RepVGG model")
\ No newline at end of file
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/.gitignore b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..f8d7bf1e82d27ec8cdc1f869511449a396431dcf
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/.gitignore
@@ -0,0 +1,2 @@
+.idea/
+*nori*
\ No newline at end of file
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/LICENSE b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/LICENSE
new file mode 100644
index 0000000000000000000000000000000000000000..9b7d31a349b645c2323b96fb4ed7912eabce3884
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2020 DingXiaoH
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/README.md b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/README.md
new file mode 100644
index 0000000000000000000000000000000000000000..d37d21e4cc61f4b3e0c831b25aca131f9cf49987
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/README.md
@@ -0,0 +1,334 @@
+# RepVGG: Making VGG-style ConvNets Great Again (CVPR-2021) (PyTorch)
+
+## Highlights (Sep. 1st, 2022)
+
+RepVGG and the methodology of re-parameterization have been used in **YOLOv6** ([paper](https://arxiv.org/abs/2209.02976), [code](https://github.com/meituan/YOLOv6)) and **YOLOv7** ([paper](https://arxiv.org/abs/2207.02696), [code](https://github.com/WongKinYiu/yolov7)).
+
+I have re-organized this repository and released the RepVGGplus-L2pse model with 84.06% ImageNet accuracy. Will release more RepVGGplus models in this month.
+
+## Introduction
+
+This is a super simple ConvNet architecture that achieves over **84% top-1 accuracy on ImageNet** with a VGG-like architecture! This repo contains the **pretrained models**, code for building the model, training, and the conversion from training-time model to inference-time, and **an example of using RepVGG for semantic segmentation**.
+
+[The MegEngine version](https://github.com/megvii-model/RepVGG)
+
+[TensorRT implemention with C++ API by @upczww](https://github.com/upczww/TensorRT-RepVGG). Great work!
+
+[Another PyTorch implementation by @zjykzj](https://github.com/ZJCV/ZCls). He also presented detailed benchmarks [here](https://zcls.readthedocs.io/en/latest/benchmark-repvgg/). Nice work!
+
+Included in a famous PyTorch model zoo https://github.com/rwightman/pytorch-image-models.
+
+[Objax implementation and models by @benjaminjellis](https://github.com/benjaminjellis/Objax-RepVGG). Great work!
+
+Included in the [MegEngine Basecls model zoo](https://github.com/megvii-research/basecls/tree/main/zoo/public/repvgg).
+
+Citation:
+
+ @inproceedings{ding2021repvgg,
+ title={Repvgg: Making vgg-style convnets great again},
+ author={Ding, Xiaohan and Zhang, Xiangyu and Ma, Ningning and Han, Jungong and Ding, Guiguang and Sun, Jian},
+ booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
+ pages={13733--13742},
+ year={2021}
+ }
+
+
+## From RepVGG to RepVGGplus
+
+We have released an improved architecture named RepVGGplus on top of the original version presented in the CVPR-2021 paper.
+
+1. RepVGGplus is deeper
+
+2. RepVGGplus has auxiliary classifiers during training, which can also be removed for inference
+
+3. (Optional) RepVGGplus uses Squeeze-and-Excitation blocks to further improve the performance.
+
+RepVGGplus outperformed several recent visual transformers with a top-1 accuracy of **84.06%** and higher throughput. Our training script is based on [codebase of Swin Transformer](https://github.com/microsoft/Swin-Transformer/). The throughput is tested with the Swin codebase as well. We would like to thank the authors of [Swin](https://arxiv.org/abs/2103.14030) for their clean and well-structured code.
+
+| Model | Train image size | Test size | ImageNet top-1 | Throughput (examples/second), 320, batchsize=128, 2080Ti) |
+| ------------- |:-------------:| -----:| -----:| -----:|
+| RepVGGplus-L2pse | 256 | 320 | **84.06%** |**147** |
+| Swin Transformer | 320 | 320 | 84.0% |102 |
+
+("pse" means Squeeze-and-Excitation blocks after ReLU.)
+
+Download this model: [Google Drive](https://drive.google.com/file/d/1x8VNLpfuLzg0xXDVIZv9yIIgqnSMoK-W/view?usp=sharing) or [Baidu Cloud](https://pan.baidu.com/s/19YwKCTSPVgJu5Ueg0Q78-w?pwd=rvgg).
+
+To train or finetune it, slightly change your training code like this:
+```
+ # Build model and data loader as usual
+ for samples, targets in enumerate(train_data_loader):
+ # ......
+ outputs = model(samples) # Your original code
+ if type(outputs) is dict:
+ # A training-time RepVGGplus outputs a dict. The items are:
+ # 'main': the output of the final layer
+ # '*aux*': the output of auxiliary classifiers
+ loss = 0
+ for name, pred in outputs.items():
+ if 'aux' in name:
+ loss += 0.1 * criterion(pred, targets) # Assume "criterion" is cross-entropy for classification
+ else:
+ loss += criterion(pred, targets)
+ else:
+ loss = criterion(outputs, targets) # Your original code
+ # Backward as usual
+ # ......
+```
+
+To use it for downstream tasks like semantic segmentation, just discard the aux classifiers and the final FC layer.
+
+Pleased note that the custom weight decay trick I described last year turned out to be insignificant in our recent experiments (84.16% ImageNet acc and negligible improvements on other tasks), so I decided to stop using it as a new feature of RepVGGplus. You may try it optionally on your task. Please refer to the last part of this page for details.
+
+
+## Use our pretrained model
+
+You may download _all_ of the ImageNet-pretrained models reported in the paper from Google Drive (https://drive.google.com/drive/folders/1Avome4KvNp0Lqh2QwhXO6L5URQjzCjUq?usp=sharing) or Baidu Cloud (https://pan.baidu.com/s/1nCsZlMynnJwbUBKn0ch7dQ, the access code is "rvgg"). For the ease of transfer learning on other tasks, they are all training-time models (with identity and 1x1 branches). You may test the accuracy by running
+```
+python -m torch.distributed.launch --nproc_per_node 1 --master_port 12349 main.py --arch [model name] --data-path [/path/to/imagenet] --batch-size 32 --tag test --eval --resume [/path/to/weights/file] --opts DATA.DATASET imagenet DATA.IMG_SIZE [224 or 320]
+```
+The valid model names include
+```
+RepVGGplus-L2pse, RepVGG-A0, RepVGG-A1, RepVGG-A2, RepVGG-B0, RepVGG-B1, RepVGG-B1g2, RepVGG-B1g4, RepVGG-B2, RepVGG-B2g2, RepVGG-B2g4, RepVGG-B3, RepVGG-B3g2, RepVGG-B3g4
+```
+
+## Convert a training-time RepVGG into the inference-time structure
+
+For a RepVGG model or a model with RepVGG as one of its components (e.g., the backbone), you can convert the whole model by simply calling **switch_to_deploy** of every RepVGG block. This is the recommended way. Examples are shown in ```tools/convert.py``` and ```example_pspnet.py```.
+```
+ for module in model.modules():
+ if hasattr(module, 'switch_to_deploy'):
+ module.switch_to_deploy()
+```
+We have also released a script for the conversion. For example,
+```
+python convert.py RepVGGplus-L2pse-train256-acc84.06.pth RepVGGplus-L2pse-deploy.pth -a RepVGGplus-L2pse
+```
+Then you may build the inference-time model with ```--deploy```, load the converted weights and test
+```
+python -m torch.distributed.launch --nproc_per_node 1 --master_port 12349 main.py --arch RepVGGplus-L2pse --data-path [/path/to/imagenet] --batch-size 32 --tag test --eval --resume RepVGGplus-L2pse-deploy.pth --deploy --opts DATA.DATASET imagenet DATA.IMG_SIZE [224 or 320]
+```
+
+Except for the final conversion after training, you may want to get the equivalent kernel and bias **during training** in a **differentiable** way at any time (```get_equivalent_kernel_bias``` in ```repvgg.py```). This may help training-based pruning or quantization.
+
+## Train from scratch
+
+### Reproduce RepVGGplus-L2pse (not presented in the paper)
+
+To train the recently released RepVGGplus-L2pse from scratch, activate mixup and use ```--AUG.PRESET raug15``` for RandAug.
+```
+python -m torch.distributed.launch --nproc_per_node 8 --master_port 12349 main.py --arch RepVGGplus-L2pse --data-path [/path/to/imagenet] --batch-size 32 --tag train_from_scratch --output-dir /path/to/save/the/log/and/checkpoints --opts TRAIN.EPOCHS 300 TRAIN.BASE_LR 0.1 TRAIN.WEIGHT_DECAY 4e-5 TRAIN.WARMUP_EPOCHS 5 MODEL.LABEL_SMOOTHING 0.1 AUG.PRESET raug15 AUG.MIXUP 0.2 DATA.DATASET imagenet DATA.IMG_SIZE 256 DATA.TEST_SIZE 320
+```
+
+### Reproduce original RepVGG results reported in the paper
+
+To reproduce the models reported in the CVPR-2021 paper, use no mixup nor RandAug.
+```
+python -m torch.distributed.launch --nproc_per_node 8 --master_port 12349 main.py --arch [model name] --data-path [/path/to/imagenet] --batch-size 32 --tag train_from_scratch --output-dir /path/to/save/the/log/and/checkpoints --opts TRAIN.EPOCHS 300 TRAIN.BASE_LR 0.1 TRAIN.WEIGHT_DECAY 1e-4 TRAIN.WARMUP_EPOCHS 5 MODEL.LABEL_SMOOTHING 0.1 AUG.PRESET weak AUG.MIXUP 0.0 DATA.DATASET imagenet DATA.IMG_SIZE 224
+```
+The original RepVGG models were trained in 120 epochs with cosine learning rate decay from 0.1 to 0. We used 8 GPUs, global batch size of 256, weight decay of 1e-4 (no weight decay on fc.bias, bn.bias, rbr_dense.bn.weight and rbr_1x1.bn.weight) (weight decay on rbr_identity.weight makes little difference, and it is better to use it in most of the cases), and the same simple data preprocssing as the PyTorch official example:
+```
+ trans = transforms.Compose([
+ transforms.RandomResizedCrop(224),
+ transforms.RandomHorizontalFlip(),
+ transforms.ToTensor(),
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
+```
+
+
+
+
+
+
+
+## Other released models not presented in the paper
+
+***Apr 25, 2021*** A deeper RepVGG model achieves **83.55\% top-1 accuracy on ImageNet** with [SE](https://openaccess.thecvf.com/content_cvpr_2018/html/Hu_Squeeze-and-Excitation_Networks_CVPR_2018_paper.html) blocks and an input resolution of 320x320 (and a wider version achieves **83.67\% accuracy** _without SE_). Note that it is trained with 224x224 but tested with 320x320, so that it is still trainable with a global batch size of 256 on a single machine with 8 1080Ti GPUs. If you test it with 224x224, the top-1 accuracy will be 81.82%. It has 1, 8, 14, 24, 1 layers in the 5 stages respectively. The width multipliers are a=2.5 and b=5 (the same as RepVGG-B2). The model name is "RepVGG-D2se". The code for building the model (repvgg.py) and testing with 320x320 (the testing example below) has been updated and the weights have been released at Google Drive and Baidu Cloud. Please check the links below.
+
+
+## Example 1: use Structural Re-parameterization like this in your own code
+```
+from repvgg import repvgg_model_convert, create_RepVGG_A0
+train_model = create_RepVGG_A0(deploy=False)
+train_model.load_state_dict(torch.load('RepVGG-A0-train.pth')) # or train from scratch
+# do whatever you want with train_model
+deploy_model = repvgg_model_convert(train_model, save_path='RepVGG-A0-deploy.pth')
+# do whatever you want with deploy_model
+```
+or
+```
+deploy_model = create_RepVGG_A0(deploy=True)
+deploy_model.load_state_dict(torch.load('RepVGG-A0-deploy.pth'))
+# do whatever you want with deploy_model
+```
+If you use RepVGG as a component of another model, the conversion is as simple as calling **switch_to_deploy** of every RepVGG block.
+
+
+## Example 2: use RepVGG as the backbone for downstream tasks
+
+I would suggest you use popular frameworks like MMDetection and MMSegmentation. The features from any stage or layer of RepVGG can be fed into the task-specific heads. If you are not familiar with such frameworks and just would like to see a simple example, please check ```example_pspnet.py```, which shows how to use RepVGG as the backbone of PSPNet for semantic segmentation: 1) build a PSPNet with RepVGG backbone, 2) load the ImageNet-pretrained weights, 3) convert the whole model with **switch_to_deploy**, 4) save and use the converted model for inference.
+
+
+
+## Quantization
+
+RepVGG works fine with FP16 but the accuracy may decrease when directly quantized to INT8. If IN8 quantization is essential to your application, we suggest three practical solutions.
+
+### Solution A: RepOptimizer
+
+I strongly recommend trying RepOptimizer if quantization is essential to your application. RepOptimizer directly trains a VGG-like model via Gradient Re-parameterization without any structural conversions. Quantizing a VGG-like model trained with RepOptimizer is as easy as quantizing a regular model. RepOptimizer has already been used in YOLOv6.
+
+Paper: https://arxiv.org/abs/2205.15242
+
+Code: https://github.com/DingXiaoH/RepOptimizers
+
+Tutorial provided by the authors of YOLOv6: https://github.com/meituan/YOLOv6/blob/main/docs/tutorial_repopt.md. Great work! Many thanks!
+
+### Solution B: custom quantization-aware training
+
+Another choice is is to constrain the equivalent kernel (get_equivalent_kernel_bias() in repvgg.py) to be low-bit (e.g., make every param in {-127, -126, .., 126, 127} for int8), instead of constraining the params of every kernel separately for an ordinary model.
+
+### Solution C: use the off-the-shelf toolboxes
+
+(TODO: check and refactor the code of this example)
+
+For the simplicity, we can also use the off-the-shelf quantization toolboxes to quantize RepVGG. We use the simple QAT (quantization-aware training) tool in torch.quantization as an example.
+
+1. Given the base model converted into the inference-time structure. We insert BN after the converted 3x3 conv layers because QAT with torch.quantization requires BN. Specifically, we run the model on ImageNet training set and record the mean/std statistics and use them to initialize the BN layers, and initialize BN.gamma/beta accordingly so that the saved model has the same outputs as the inference-time model.
+
+```
+python quantization/convert.py RepVGG-A0.pth RepVGG-A0_base.pth -a RepVGG-A0
+python quantization/insert_bn.py [imagenet-folder] RepVGG-A0_base.pth RepVGG-A0_withBN.pth -a RepVGG-A0 -b 32 -n 40000
+```
+
+2. Build the model, prepare it for QAT (torch.quantization.prepare_qat), and conduct QAT. This is only an example and the hyper-parameters may not be optimal.
+```
+python quantization/quant_qat_train.py [imagenet-folder] -j 32 --epochs 20 -b 256 --lr 1e-3 --weight-decay 4e-5 --base-weights RepVGG-A0_withBN.pth --tag quanttest
+```
+
+
+## FAQs
+
+**Q**: Is the inference-time model's output the _same_ as the training-time model?
+
+**A**: Yes. You can verify that by
+```
+python tools/verify.py
+```
+
+**Q**: How to use the pretrained RepVGG models for other tasks?
+
+**A**: It is better to finetune the training-time RepVGG models on your datasets. Then you should do the conversion after finetuning and before you deploy the models. For example, say you want to use PSPNet for semantic segmentation, you should build a PSPNet with a training-time RepVGG model as the backbone, load pre-trained weights into the backbone, and finetune the PSPNet on your segmentation dataset. Then you should convert the backbone following the code provided in this repo and keep the other task-specific structures (the PSPNet parts, in this case). The pseudo code will be like
+```
+# train_backbone = create_RepVGG_B2(deploy=False)
+# train_backbone.load_state_dict(torch.load('RepVGG-B2-train.pth'))
+# train_pspnet = build_pspnet(backbone=train_backbone)
+# segmentation_train(train_pspnet)
+# deploy_pspnet = repvgg_model_convert(train_pspnet)
+# segmentation_test(deploy_pspnet)
+```
+There is an example in **example_pspnet.py**.
+
+Finetuning with a converted RepVGG also makes sense if you insert a BN after each conv (please see the quantization example), but the performance may be slightly lower.
+
+**Q**: I tried to finetune your model with multiple GPUs but got an error. Why are the names of params like "stage1.0.rbr_dense.conv.weight" in the downloaded weight file but sometimes like "module.stage1.0.rbr_dense.conv.weight" (shown by nn.Module.named_parameters()) in my model?
+
+**A**: DistributedDataParallel may prefix "module." to the name of params and cause a mismatch when loading weights by name. The simplest solution is to load the weights (model.load_state_dict(...)) before DistributedDataParallel(model). Otherwise, you may insert "module." before the names like this
+```
+checkpoint = torch.load(...) # This is just a name-value dict
+ckpt = {('module.' + k) : v for k, v in checkpoint.items()}
+model.load_state_dict(ckpt)
+```
+Likewise, if the param names in the checkpoint file start with "module." but those in your model do not, you may strip the names like line 50 in test.py.
+```
+ckpt = {k.replace('module.', ''):v for k,v in checkpoint.items()} # strip the names
+model.load_state_dict(ckpt)
+```
+**Q**: So a RepVGG model derives the equivalent 3x3 kernels before each forwarding to save computations?
+
+**A**: No! More precisely, we do the conversion only once right after training. Then the training-time model can be discarded, and the resultant model only has 3x3 kernels. We only save and use the resultant model.
+
+
+## An optional trick with a custom weight decay (deprecated)
+
+This is deprecated. Please check ```repvggplus_custom_L2.py```. The intuition is to add regularization on the equivalent kernel. It may work in some cases.
+
+The trained model can be downloaded at [Google Drive](https://drive.google.com/file/d/14I1jWU4rS4y0wdxm03SnEVP1Tx6GGfKu/view?usp=sharing) or [Baidu Cloud](https://pan.baidu.com/s/1qFGmgJ6Ir6W3wAcCBQb9-w?pwd=rvgg)
+
+The training code should be changed like this:
+```
+ # Build model and data loader as usual
+ for samples, targets in enumerate(train_data_loader):
+ # ......
+ outputs = model(samples) # Your original code
+ if type(outputs) is dict:
+ # A training-time RepVGGplus outputs a dict. The items are:
+ # 'main': the output of the final layer
+ # '*aux*': the output of auxiliary classifiers
+ # 'L2': the custom L2 regularization term
+ loss = WEIGHT_DECAY * 0.5 * outputs['L2']
+ for name, pred in outputs.items():
+ if name == 'L2':
+ pass
+ elif 'aux' in name:
+ loss += 0.1 * criterion(pred, targets) # Assume "criterion" is cross-entropy for classification
+ else:
+ loss += criterion(pred, targets)
+ else:
+ loss = criterion(outputs, targets) # Your original code
+ # Backward as usual
+ # ......
+```
+
+
+
+## Contact
+
+**xiaohding@gmail.com** (The original Tsinghua mailbox dxh17@mails.tsinghua.edu.cn will expire in several months)
+
+Google Scholar Profile: https://scholar.google.com/citations?user=CIjw0KoAAAAJ&hl=en
+
+Homepage: https://dingxiaohan.xyz/
+
+My open-sourced papers and repos:
+
+The **Structural Re-parameterization Universe**:
+
+1. RepLKNet (CVPR 2022) **Powerful efficient architecture with very large kernels (31x31) and guidelines for using large kernels in model CNNs**\
+[Scaling Up Your Kernels to 31x31: Revisiting Large Kernel Design in CNNs](https://arxiv.org/abs/2203.06717)\
+[code](https://github.com/DingXiaoH/RepLKNet-pytorch).
+
+2. **RepOptimizer** (ICLR 2023) uses **Gradient Re-parameterization** to train powerful models efficiently. The training-time **RepOpt-VGG** is **as simple as the inference-time**. It also addresses the problem of quantization.\
+[Re-parameterizing Your Optimizers rather than Architectures](https://arxiv.org/pdf/2205.15242.pdf)\
+[code](https://github.com/DingXiaoH/RepOptimizers).
+
+3. RepVGG (CVPR 2021) **A super simple and powerful VGG-style ConvNet architecture**. Up to **84.16%** ImageNet top-1 accuracy!\
+[RepVGG: Making VGG-style ConvNets Great Again](https://arxiv.org/abs/2101.03697)\
+[code](https://github.com/DingXiaoH/RepVGG).
+
+4. RepMLP (CVPR 2022) **MLP-style building block and Architecture**\
+[RepMLPNet: Hierarchical Vision MLP with Re-parameterized Locality](https://arxiv.org/abs/2112.11081)\
+[code](https://github.com/DingXiaoH/RepMLP).
+
+5. ResRep (ICCV 2021) **State-of-the-art** channel pruning (Res50, 55\% FLOPs reduction, 76.15\% acc)\
+[ResRep: Lossless CNN Pruning via Decoupling Remembering and Forgetting](https://openaccess.thecvf.com/content/ICCV2021/papers/Ding_ResRep_Lossless_CNN_Pruning_via_Decoupling_Remembering_and_Forgetting_ICCV_2021_paper.pdf)\
+[code](https://github.com/DingXiaoH/ResRep).
+
+6. ACB (ICCV 2019) is a CNN component without any inference-time costs. The first work of our Structural Re-parameterization Universe.\
+[ACNet: Strengthening the Kernel Skeletons for Powerful CNN via Asymmetric Convolution Blocks](http://openaccess.thecvf.com/content_ICCV_2019/papers/Ding_ACNet_Strengthening_the_Kernel_Skeletons_for_Powerful_CNN_via_Asymmetric_ICCV_2019_paper.pdf).\
+[code](https://github.com/DingXiaoH/ACNet).
+
+7. DBB (CVPR 2021) is a CNN component with higher performance than ACB and still no inference-time costs. Sometimes I call it ACNet v2 because "DBB" is 2 bits larger than "ACB" in ASCII (lol).\
+[Diverse Branch Block: Building a Convolution as an Inception-like Unit](https://arxiv.org/abs/2103.13425)\
+[code](https://github.com/DingXiaoH/DiverseBranchBlock).
+
+**Model compression and acceleration**:
+
+1. (CVPR 2019) Channel pruning: [Centripetal SGD for Pruning Very Deep Convolutional Networks with Complicated Structure](http://openaccess.thecvf.com/content_CVPR_2019/html/Ding_Centripetal_SGD_for_Pruning_Very_Deep_Convolutional_Networks_With_Complicated_CVPR_2019_paper.html)\
+[code](https://github.com/DingXiaoH/Centripetal-SGD)
+
+2. (ICML 2019) Channel pruning: [Approximated Oracle Filter Pruning for Destructive CNN Width Optimization](http://proceedings.mlr.press/v97/ding19a.html)\
+[code](https://github.com/DingXiaoH/AOFP)
+
+3. (NeurIPS 2019) Unstructured pruning: [Global Sparse Momentum SGD for Pruning Very Deep Neural Networks](http://papers.nips.cc/paper/8867-global-sparse-momentum-sgd-for-pruning-very-deep-neural-networks.pdf)\
+[code](https://github.com/DingXiaoH/GSM-SGD)
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/__init__.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..70c633ce61849c1600e3effbd1bf46f29f29cb80
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/__init__.py
@@ -0,0 +1 @@
+from .build import build_loader
\ No newline at end of file
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/build.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/build.py
new file mode 100644
index 0000000000000000000000000000000000000000..607869ca8859d341eec7c96f6fa5ab5c820db288
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/build.py
@@ -0,0 +1,184 @@
+# --------------------------------------------------------
+# RepVGG: Making VGG-style ConvNets Great Again (https://openaccess.thecvf.com/content/CVPR2021/papers/Ding_RepVGG_Making_VGG-Style_ConvNets_Great_Again_CVPR_2021_paper.pdf)
+# Github source: https://github.com/DingXiaoH/RepVGG
+# Licensed under The MIT License [see LICENSE for details]
+# The training script is based on the code of Swin Transformer (https://github.com/microsoft/Swin-Transformer)
+# --------------------------------------------------------
+import torch
+import numpy as np
+import torch.distributed as dist
+from torchvision import datasets, transforms
+from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
+from timm.data import Mixup
+from timm.data import create_transform
+try:
+ from timm.data.transforms import str_to_pil_interp as _pil_interp
+except:
+ from timm.data.transforms import _pil_interp
+from .cached_image_folder import CachedImageFolder
+from .samplers import SubsetRandomSampler
+
+
+def build_loader(config):
+ config.defrost()
+ dataset_train, config.MODEL.NUM_CLASSES = build_dataset(is_train=True, config=config)
+ config.freeze()
+ print(f"local rank {config.LOCAL_RANK} / global rank {dist.get_rank()} successfully build train dataset")
+ dataset_val, _ = build_dataset(is_train=False, config=config)
+ print(f"local rank {config.LOCAL_RANK} / global rank {dist.get_rank()} successfully build val dataset")
+
+ num_tasks = dist.get_world_size()
+ global_rank = dist.get_rank()
+ if config.DATA.ZIP_MODE and config.DATA.CACHE_MODE == 'part':
+ indices = np.arange(dist.get_rank(), len(dataset_train), dist.get_world_size())
+ sampler_train = SubsetRandomSampler(indices)
+ else:
+ sampler_train = torch.utils.data.DistributedSampler(
+ dataset_train, num_replicas=num_tasks, rank=global_rank, shuffle=True
+ )
+
+ if dataset_val is None:
+ sampler_val = None
+ else:
+ indices = np.arange(dist.get_rank(), len(dataset_val), dist.get_world_size()) #TODO
+ sampler_val = SubsetRandomSampler(indices)
+
+ data_loader_train = torch.utils.data.DataLoader(
+ dataset_train, sampler=sampler_train,
+ batch_size=config.DATA.BATCH_SIZE,
+ num_workers=config.DATA.NUM_WORKERS,
+ pin_memory=config.DATA.PIN_MEMORY,
+ drop_last=True,
+ )
+
+ if dataset_val is None:
+ data_loader_val = None
+ else:
+ data_loader_val = torch.utils.data.DataLoader(
+ dataset_val, sampler=sampler_val,
+ batch_size=config.DATA.TEST_BATCH_SIZE,
+ shuffle=False,
+ num_workers=config.DATA.NUM_WORKERS,
+ pin_memory=config.DATA.PIN_MEMORY,
+ drop_last=False
+ )
+
+ # setup mixup / cutmix
+ mixup_fn = None
+ mixup_active = config.AUG.MIXUP > 0 or config.AUG.CUTMIX > 0. or config.AUG.CUTMIX_MINMAX is not None
+ if mixup_active:
+ mixup_fn = Mixup(
+ mixup_alpha=config.AUG.MIXUP, cutmix_alpha=config.AUG.CUTMIX, cutmix_minmax=config.AUG.CUTMIX_MINMAX,
+ prob=config.AUG.MIXUP_PROB, switch_prob=config.AUG.MIXUP_SWITCH_PROB, mode=config.AUG.MIXUP_MODE,
+ label_smoothing=config.MODEL.LABEL_SMOOTHING, num_classes=config.MODEL.NUM_CLASSES)
+
+ return dataset_train, dataset_val, data_loader_train, data_loader_val, mixup_fn
+
+
+def build_dataset(is_train, config):
+ if config.DATA.DATASET == 'imagenet':
+ transform = build_transform(is_train, config)
+ prefix = 'train' if is_train else 'val'
+ if config.DATA.ZIP_MODE:
+ ann_file = prefix + "_map.txt"
+ prefix = prefix + ".zip@/"
+ dataset = CachedImageFolder(config.DATA.DATA_PATH, ann_file, prefix, transform,
+ cache_mode=config.DATA.CACHE_MODE if is_train else 'part')
+ else:
+ import torchvision
+ print('use raw ImageNet data')
+ dataset = torchvision.datasets.ImageNet(root=config.DATA.DATA_PATH, split='train' if is_train else 'val', transform=transform)
+ nb_classes = 1000
+
+ elif config.DATA.DATASET == 'cf100':
+ mean = [0.5070751592371323, 0.48654887331495095, 0.4409178433670343]
+ std = [0.2673342858792401, 0.2564384629170883, 0.27615047132568404]
+ if is_train:
+ transform = transforms.Compose([
+ transforms.RandomCrop(32, padding=4),
+ transforms.RandomHorizontalFlip(),
+ transforms.ToTensor(),
+ transforms.Normalize(mean, std)
+ ])
+ dataset = datasets.CIFAR100(root=config.DATA.DATA_PATH, train=True, download=True, transform=transform)
+ else:
+ transform = transforms.Compose(
+ [transforms.ToTensor(),
+ transforms.Normalize(mean, std)])
+ dataset = datasets.CIFAR100(root=config.DATA.DATA_PATH, train=False, download=True, transform=transform)
+ nb_classes = 100
+
+ else:
+ raise NotImplementedError("We only support ImageNet and CIFAR-100 now.")
+
+ return dataset, nb_classes
+
+
+def build_transform(is_train, config):
+ resize_im = config.DATA.IMG_SIZE > 32
+ if is_train:
+ # this should always dispatch to transforms_imagenet_train
+
+ if config.AUG.PRESET is None:
+ transform = create_transform(
+ input_size=config.DATA.IMG_SIZE,
+ is_training=True,
+ color_jitter=config.AUG.COLOR_JITTER if config.AUG.COLOR_JITTER > 0 else None,
+ auto_augment=config.AUG.AUTO_AUGMENT if config.AUG.AUTO_AUGMENT != 'none' else None,
+ re_prob=config.AUG.REPROB,
+ re_mode=config.AUG.REMODE,
+ re_count=config.AUG.RECOUNT,
+ interpolation=config.DATA.INTERPOLATION,
+ )
+ print('=============================== original AUG! ', config.AUG.AUTO_AUGMENT)
+ if not resize_im:
+ # replace RandomResizedCropAndInterpolation with
+ # RandomCrop
+ transform.transforms[0] = transforms.RandomCrop(config.DATA.IMG_SIZE, padding=4)
+
+ elif config.AUG.PRESET.strip() == 'raug15':
+ from train.randaug import RandAugPolicy
+ transform = transforms.Compose([
+ transforms.RandomResizedCrop(config.DATA.IMG_SIZE),
+ transforms.RandomHorizontalFlip(),
+ RandAugPolicy(magnitude=15),
+ transforms.ToTensor(),
+ transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD),
+ ])
+ print('---------------------- RAND AUG 15 distortion!')
+
+ elif config.AUG.PRESET.strip() == 'weak':
+ transform = transforms.Compose([
+ transforms.RandomResizedCrop(config.DATA.IMG_SIZE),
+ transforms.RandomHorizontalFlip(),
+ transforms.ToTensor(),
+ transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD),
+ ])
+ elif config.AUG.PRESET.strip() == 'none':
+ transform = transforms.Compose([
+ transforms.Resize(config.DATA.IMG_SIZE, interpolation=_pil_interp(config.DATA.INTERPOLATION)),
+ transforms.CenterCrop(config.DATA.IMG_SIZE),
+ transforms.ToTensor(),
+ transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD),
+ ])
+ else:
+ raise ValueError('???' + config.AUG.PRESET)
+ print(transform)
+ return transform
+
+ t = []
+ if resize_im:
+ if config.TEST.CROP:
+ size = int((256 / 224) * config.DATA.TEST_SIZE)
+ t.append(transforms.Resize(size, interpolation=_pil_interp(config.DATA.INTERPOLATION)),
+ # to maintain same ratio w.r.t. 224 images
+ )
+ t.append(transforms.CenterCrop(config.DATA.TEST_SIZE))
+ else:
+ # default for testing
+ t.append(transforms.Resize(config.DATA.TEST_SIZE, interpolation=_pil_interp(config.DATA.INTERPOLATION)))
+ t.append(transforms.CenterCrop(config.DATA.TEST_SIZE))
+ t.append(transforms.ToTensor())
+ t.append(transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD))
+ trans = transforms.Compose(t)
+ return trans
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/cached_image_folder.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/cached_image_folder.py
new file mode 100644
index 0000000000000000000000000000000000000000..2f3d013a67f5aac9ca5fac635044ecef8fdbca5a
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/cached_image_folder.py
@@ -0,0 +1,244 @@
+import io
+import os
+import time
+import torch.distributed as dist
+import torch.utils.data as data
+from PIL import Image
+
+from .zipreader import is_zip_path, ZipReader
+
+
+def has_file_allowed_extension(filename, extensions):
+ """Checks if a file is an allowed extension.
+ Args:
+ filename (string): path to a file
+ Returns:
+ bool: True if the filename ends with a known image extension
+ """
+ filename_lower = filename.lower()
+ return any(filename_lower.endswith(ext) for ext in extensions)
+
+
+def find_classes(dir):
+ classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
+ classes.sort()
+ class_to_idx = {classes[i]: i for i in range(len(classes))}
+ return classes, class_to_idx
+
+
+def make_dataset(dir, class_to_idx, extensions):
+ images = []
+ dir = os.path.expanduser(dir)
+ for target in sorted(os.listdir(dir)):
+ d = os.path.join(dir, target)
+ if not os.path.isdir(d):
+ continue
+
+ for root, _, fnames in sorted(os.walk(d)):
+ for fname in sorted(fnames):
+ if has_file_allowed_extension(fname, extensions):
+ path = os.path.join(root, fname)
+ item = (path, class_to_idx[target])
+ images.append(item)
+
+ return images
+
+
+def make_dataset_with_ann(ann_file, img_prefix, extensions):
+ images = []
+ with open(ann_file, "r") as f:
+ contents = f.readlines()
+ for line_str in contents:
+ path_contents = [c for c in line_str.split('\t')]
+ im_file_name = path_contents[0]
+ class_index = int(path_contents[1])
+
+ assert str.lower(os.path.splitext(im_file_name)[-1]) in extensions
+ item = (os.path.join(img_prefix, im_file_name), class_index)
+
+ images.append(item)
+
+ return images
+
+
+class DatasetFolder(data.Dataset):
+ """A generic data loader where the samples are arranged in this way: ::
+ root/class_x/xxx.ext
+ root/class_x/xxy.ext
+ root/class_x/xxz.ext
+ root/class_y/123.ext
+ root/class_y/nsdf3.ext
+ root/class_y/asd932_.ext
+ Args:
+ root (string): Root directory path.
+ loader (callable): A function to load a sample given its path.
+ extensions (list[string]): A list of allowed extensions.
+ transform (callable, optional): A function/transform that takes in
+ a sample and returns a transformed version.
+ E.g, ``transforms.RandomCrop`` for images.
+ target_transform (callable, optional): A function/transform that takes
+ in the target and transforms it.
+ Attributes:
+ samples (list): List of (sample path, class_index) tuples
+ """
+
+ def __init__(self, root, loader, extensions, ann_file='', img_prefix='', transform=None, target_transform=None,
+ cache_mode="no"):
+ # image folder mode
+ if ann_file == '':
+ _, class_to_idx = find_classes(root)
+ samples = make_dataset(root, class_to_idx, extensions)
+ # zip mode
+ else:
+ samples = make_dataset_with_ann(os.path.join(root, ann_file),
+ os.path.join(root, img_prefix),
+ extensions)
+
+ if len(samples) == 0:
+ raise (RuntimeError("Found 0 files in subfolders of: " + root + "\n" +
+ "Supported extensions are: " + ",".join(extensions)))
+
+ self.root = root
+ self.loader = loader
+ self.extensions = extensions
+
+ self.samples = samples
+ self.labels = [y_1k for _, y_1k in samples]
+ self.classes = list(set(self.labels))
+
+ self.transform = transform
+ self.target_transform = target_transform
+
+ self.cache_mode = cache_mode
+ if self.cache_mode != "no":
+ self.init_cache()
+
+ def init_cache(self):
+ assert self.cache_mode in ["part", "full"]
+ n_sample = len(self.samples)
+ global_rank = dist.get_rank()
+ world_size = dist.get_world_size()
+
+ samples_bytes = [None for _ in range(n_sample)]
+ start_time = time.time()
+ for index in range(n_sample):
+ if index % (n_sample // 10) == 0:
+ t = time.time() - start_time
+ print(f'global_rank {dist.get_rank()} cached {index}/{n_sample} takes {t:.2f}s per block')
+ start_time = time.time()
+ path, target = self.samples[index]
+ if self.cache_mode == "full":
+ samples_bytes[index] = (ZipReader.read(path), target)
+ elif self.cache_mode == "part" and index % world_size == global_rank:
+ samples_bytes[index] = (ZipReader.read(path), target)
+ else:
+ samples_bytes[index] = (path, target)
+ self.samples = samples_bytes
+
+ def __getitem__(self, index):
+ """
+ Args:
+ index (int): Index
+ Returns:
+ tuple: (sample, target) where target is class_index of the target class.
+ """
+ path, target = self.samples[index]
+ sample = self.loader(path)
+ if self.transform is not None:
+ sample = self.transform(sample)
+ if self.target_transform is not None:
+ target = self.target_transform(target)
+
+ return sample, target
+
+ def __len__(self):
+ return len(self.samples)
+
+ def __repr__(self):
+ fmt_str = 'Dataset ' + self.__class__.__name__ + '\n'
+ fmt_str += ' Number of datapoints: {}\n'.format(self.__len__())
+ fmt_str += ' Root Location: {}\n'.format(self.root)
+ tmp = ' Transforms (if any): '
+ fmt_str += '{0}{1}\n'.format(tmp, self.transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
+ tmp = ' Target Transforms (if any): '
+ fmt_str += '{0}{1}'.format(tmp, self.target_transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
+ return fmt_str
+
+
+IMG_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif']
+
+
+def pil_loader(path):
+ # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
+ if isinstance(path, bytes):
+ img = Image.open(io.BytesIO(path))
+ elif is_zip_path(path):
+ data = ZipReader.read(path)
+ img = Image.open(io.BytesIO(data))
+ else:
+ with open(path, 'rb') as f:
+ img = Image.open(f)
+ return img.convert('RGB')
+
+
+def accimage_loader(path):
+ import accimage
+ try:
+ return accimage.Image(path)
+ except IOError:
+ # Potentially a decoding problem, fall back to PIL.Image
+ return pil_loader(path)
+
+
+def default_img_loader(path):
+ from torchvision import get_image_backend
+ if get_image_backend() == 'accimage':
+ return accimage_loader(path)
+ else:
+ return pil_loader(path)
+
+
+class CachedImageFolder(DatasetFolder):
+ """A generic data loader where the images are arranged in this way: ::
+ root/dog/xxx.png
+ root/dog/xxy.png
+ root/dog/xxz.png
+ root/cat/123.png
+ root/cat/nsdf3.png
+ root/cat/asd932_.png
+ Args:
+ root (string): Root directory path.
+ transform (callable, optional): A function/transform that takes in an PIL image
+ and returns a transformed version. E.g, ``transforms.RandomCrop``
+ target_transform (callable, optional): A function/transform that takes in the
+ target and transforms it.
+ loader (callable, optional): A function to load an image given its path.
+ Attributes:
+ imgs (list): List of (image path, class_index) tuples
+ """
+
+ def __init__(self, root, ann_file='', img_prefix='', transform=None, target_transform=None,
+ loader=default_img_loader, cache_mode="no"):
+ super(CachedImageFolder, self).__init__(root, loader, IMG_EXTENSIONS,
+ ann_file=ann_file, img_prefix=img_prefix,
+ transform=transform, target_transform=target_transform,
+ cache_mode=cache_mode)
+ self.imgs = self.samples
+
+ def __getitem__(self, index):
+ """
+ Args:
+ index (int): Index
+ Returns:
+ tuple: (image, target) where target is class_index of the target class.
+ """
+ path, target = self.samples[index]
+ image = self.loader(path)
+ if self.transform is not None:
+ img = self.transform(image)
+ else:
+ img = image
+ if self.target_transform is not None:
+ target = self.target_transform(target)
+
+ return img, target
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/lmdb_dataset.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/lmdb_dataset.py
new file mode 100644
index 0000000000000000000000000000000000000000..640d1824d34be5f1f522918500156fdf498a22df
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/lmdb_dataset.py
@@ -0,0 +1,164 @@
+import os
+import os.path as osp
+from PIL import Image
+import six
+import lmdb
+import warnings
+warnings.simplefilter(action='ignore', category=FutureWarning)
+import pyarrow as pa
+import numpy as np
+import torch.utils.data as data
+from torch.utils.data import DataLoader
+from torchvision.datasets import ImageFolder
+
+train_lmdb_path = '/apdcephfs/share_1290939/0_public_datasets/imageNet_2012/train.lmdb'
+val_lmdb_path = '/apdcephfs/share_1290939/0_public_datasets/imageNet_2012/val.lmdb'
+
+# from data.lmdb_dataset import ImageFolderLMDB, train_lmdb_path, val_lmdb_path
+# lmdb_path = train_lmdb_path if is_train else val_lmdb_path
+# dataset = ImageFolderLMDB(db_path=lmdb_path, transform=transform)
+
+def loads_pyarrow(buf):
+ """
+ Args:
+ buf: the output of `dumps`.
+ """
+ return pa.deserialize(buf)
+
+
+class ImageFolderLMDB(data.Dataset):
+ def __init__(self, db_path, transform=None, target_transform=None):
+ self.db_path = db_path
+ self.env = lmdb.open(db_path, subdir=osp.isdir(db_path),
+ readonly=True, lock=False,
+ readahead=False, meminit=False)
+ with self.env.begin(write=False) as txn:
+ self.length = loads_pyarrow(txn.get(b'__len__'))
+ self.keys = loads_pyarrow(txn.get(b'__keys__'))
+
+ self.transform = transform
+ self.target_transform = target_transform
+
+ def __getstate__(self):
+ state = self.__dict__
+ state["env"] = None
+ return state
+
+ def __setstate__(self, state):
+ self.__dict__ = state
+ self.env = lmdb.open(self.db_path, subdir=osp.isdir(self.db_path),
+ readonly=True, lock=False,
+ readahead=False, meminit=False)
+ with self.env.begin(write=False) as txn:
+ self.length = loads_pyarrow(txn.get(b'__len__'))
+ self.keys = loads_pyarrow(txn.get(b'__keys__'))
+
+ def __getitem__(self, index):
+ env = self.env
+ with env.begin(write=False) as txn:
+ byteflow = txn.get(self.keys[index])
+
+ unpacked = loads_pyarrow(byteflow)
+
+ # load img
+ imgbuf = unpacked[0]
+ buf = six.BytesIO()
+ buf.write(imgbuf)
+ buf.seek(0)
+ img = Image.open(buf).convert('RGB')
+ if self.transform is not None:
+ img = self.transform(img)
+
+ # load label
+ target = unpacked[1]
+ if self.target_transform is not None:
+ target = self.transform(target)
+
+ return img, target
+# if self.transform is not None:
+# img = self.transform(img)
+#
+# # im2arr = np.array(img)
+#
+# if self.target_transform is not None:
+# target = self.target_transform(target)
+#
+# return img, target
+ # return im2arr, target
+
+ def __len__(self):
+ return self.length
+
+ def __repr__(self):
+ return self.__class__.__name__ + ' (' + self.db_path + ')'
+
+
+def raw_reader(path):
+ with open(path, 'rb') as f:
+ bin_data = f.read()
+ return bin_data
+
+
+def dumps_pyarrow(obj):
+ """
+ Serialize an object.
+ Returns:
+ Implementation-dependent bytes-like object
+ """
+ return pa.serialize(obj).to_buffer()
+
+
+def folder2lmdb(dpath, name="train", write_frequency=5000):
+ directory = osp.expanduser(osp.join(dpath, name))
+ print("Loading dataset from %s" % directory)
+ dataset = ImageFolder(directory, loader=raw_reader)
+ data_loader = DataLoader(dataset, num_workers=4, collate_fn=lambda x: x)
+
+ lmdb_path = osp.join(dpath, "%s.lmdb" % name)
+ isdir = os.path.isdir(lmdb_path)
+
+ print("Generate LMDB to %s" % lmdb_path)
+ db = lmdb.open(lmdb_path, subdir=isdir,
+ map_size=1099511627776 * 2, readonly=False,
+ meminit=False, map_async=True)
+
+ txn = db.begin(write=True)
+ for idx, data in enumerate(data_loader):
+ image, label = data[0]
+
+ txn.put(u'{}'.format(idx).encode('ascii'), dumps_pyarrow((image, label)))
+ if idx % write_frequency == 0:
+ print("[%d/%d]" % (idx, len(data_loader)))
+ txn.commit()
+ txn = db.begin(write=True)
+
+ # finish iterating through dataset
+ txn.commit()
+ keys = [u'{}'.format(k).encode('ascii') for k in range(idx + 1)]
+ with db.begin(write=True) as txn:
+ txn.put(b'__keys__', dumps_pyarrow(keys))
+ txn.put(b'__len__', dumps_pyarrow(len(keys)))
+
+ print("Flushing database ...")
+ db.sync()
+ db.close()
+
+
+
+
+if __name__ == "__main__":
+ # lmdb_path = '/apdcephfs/share_1016399/0_public_datasets/imageNet_2012/train.lmdb'
+ # from lmdb_dataset import ImageFolderLMDB
+ # dataset = ImageFolderLMDB(db_path=lmdb_path)
+ # for x, y in dataset:
+ # print(type(x), type(y))
+ # exit()
+
+ import argparse
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--dir', type=str, required=True, help="The dataset directory to process")
+ args = parser.parse_args()
+ # generate lmdb
+ path = args.dir
+ folder2lmdb(path, name="train")
+ folder2lmdb(path, name="val")
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/samplers.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/samplers.py
new file mode 100644
index 0000000000000000000000000000000000000000..fed54b4e27b2df268670ace4cda64687209a5380
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/samplers.py
@@ -0,0 +1,21 @@
+import torch
+
+class SubsetRandomSampler(torch.utils.data.Sampler):
+ r"""Samples elements randomly from a given list of indices, without replacement.
+
+ Arguments:
+ indices (sequence): a sequence of indices
+ """
+
+ def __init__(self, indices):
+ self.epoch = 0
+ self.indices = indices
+
+ def __iter__(self):
+ return (self.indices[i] for i in torch.randperm(len(self.indices)))
+
+ def __len__(self):
+ return len(self.indices)
+
+ def set_epoch(self, epoch):
+ self.epoch = epoch
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/zipreader.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/zipreader.py
new file mode 100644
index 0000000000000000000000000000000000000000..9d773c3c4e91eae4435faf9d9b297a2d3c21a3d2
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/data/zipreader.py
@@ -0,0 +1,96 @@
+import os
+import zipfile
+import io
+import numpy as np
+from PIL import Image
+from PIL import ImageFile
+
+ImageFile.LOAD_TRUNCATED_IMAGES = True
+
+
+def is_zip_path(img_or_path):
+ """judge if this is a zip path"""
+ return '.zip@' in img_or_path
+
+
+class ZipReader(object):
+ """A class to read zipped files"""
+ zip_bank = dict()
+
+ def __init__(self):
+ super(ZipReader, self).__init__()
+
+ @staticmethod
+ def get_zipfile(path):
+ zip_bank = ZipReader.zip_bank
+ if path not in zip_bank:
+ zfile = zipfile.ZipFile(path, 'r')
+ zip_bank[path] = zfile
+ return zip_bank[path]
+
+ @staticmethod
+ def split_zip_style_path(path):
+ pos_at = path.index('@')
+ assert pos_at != -1, "character '@' is not found from the given path '%s'" % path
+
+ zip_path = path[0: pos_at]
+ folder_path = path[pos_at + 1:]
+ folder_path = str.strip(folder_path, '/')
+ return zip_path, folder_path
+
+ @staticmethod
+ def list_folder(path):
+ zip_path, folder_path = ZipReader.split_zip_style_path(path)
+
+ zfile = ZipReader.get_zipfile(zip_path)
+ folder_list = []
+ for file_foler_name in zfile.namelist():
+ file_foler_name = str.strip(file_foler_name, '/')
+ if file_foler_name.startswith(folder_path) and \
+ len(os.path.splitext(file_foler_name)[-1]) == 0 and \
+ file_foler_name != folder_path:
+ if len(folder_path) == 0:
+ folder_list.append(file_foler_name)
+ else:
+ folder_list.append(file_foler_name[len(folder_path) + 1:])
+
+ return folder_list
+
+ @staticmethod
+ def list_files(path, extension=None):
+ if extension is None:
+ extension = ['.*']
+ zip_path, folder_path = ZipReader.split_zip_style_path(path)
+
+ zfile = ZipReader.get_zipfile(zip_path)
+ file_lists = []
+ for file_foler_name in zfile.namelist():
+ file_foler_name = str.strip(file_foler_name, '/')
+ if file_foler_name.startswith(folder_path) and \
+ str.lower(os.path.splitext(file_foler_name)[-1]) in extension:
+ if len(folder_path) == 0:
+ file_lists.append(file_foler_name)
+ else:
+ file_lists.append(file_foler_name[len(folder_path) + 1:])
+
+ return file_lists
+
+ @staticmethod
+ def read(path):
+ zip_path, path_img = ZipReader.split_zip_style_path(path)
+ zfile = ZipReader.get_zipfile(zip_path)
+ data = zfile.read(path_img)
+ return data
+
+ @staticmethod
+ def imread(path):
+ zip_path, path_img = ZipReader.split_zip_style_path(path)
+ zfile = ZipReader.get_zipfile(zip_path)
+ data = zfile.read(path_img)
+ try:
+ im = Image.open(io.BytesIO(data))
+ except:
+ print("ERROR IMG LOADED: ", path_img)
+ random_img = np.random.rand(224, 224, 3) * 255
+ im = Image.fromarray(np.uint8(random_img))
+ return im
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/example_pspnet.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/example_pspnet.py
new file mode 100644
index 0000000000000000000000000000000000000000..19ff9abdce2ac9b80d637c5033fb568351564796
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/example_pspnet.py
@@ -0,0 +1,161 @@
+import torch
+from torch import nn
+import torch.nn.functional as F
+from repvgg import get_RepVGG_func_by_name
+
+# The PSPNet parts are from
+# https://github.com/hszhao/semseg
+
+class PPM(nn.Module):
+ def __init__(self, in_dim, reduction_dim, bins, BatchNorm):
+ super(PPM, self).__init__()
+ self.features = []
+ for bin in bins:
+ self.features.append(nn.Sequential(
+ nn.AdaptiveAvgPool2d(bin),
+ nn.Conv2d(in_dim, reduction_dim, kernel_size=1, bias=False),
+ BatchNorm(reduction_dim),
+ nn.ReLU(inplace=True)
+ ))
+ self.features = nn.ModuleList(self.features)
+
+ def forward(self, x):
+ x_size = x.size()
+ out = [x]
+ for f in self.features:
+ out.append(F.interpolate(f(x), x_size[2:], mode='bilinear', align_corners=True))
+ return torch.cat(out, 1)
+
+
+class PSPNet(nn.Module):
+ def __init__(self,
+ backbone_name, backbone_file, deploy,
+ bins=(1, 2, 3, 6), dropout=0.1, classes=2,
+ zoom_factor=8, use_ppm=True, criterion=nn.CrossEntropyLoss(ignore_index=255), BatchNorm=nn.BatchNorm2d,
+ pretrained=True):
+ super(PSPNet, self).__init__()
+ assert 2048 % len(bins) == 0
+ assert classes > 1
+ assert zoom_factor in [1, 2, 4, 8]
+ self.zoom_factor = zoom_factor
+ self.use_ppm = use_ppm
+ self.criterion = criterion
+
+ repvgg_fn = get_RepVGG_func_by_name(backbone_name)
+ backbone = repvgg_fn(deploy)
+ if pretrained:
+ checkpoint = torch.load(backbone_file)
+ if 'state_dict' in checkpoint:
+ checkpoint = checkpoint['state_dict']
+ ckpt = {k.replace('module.', ''): v for k, v in checkpoint.items()} # strip the names
+ backbone.load_state_dict(ckpt)
+
+ self.layer0, self.layer1, self.layer2, self.layer3, self.layer4 = backbone.stage0, backbone.stage1, backbone.stage2, backbone.stage3, backbone.stage4
+
+ # The last two stages should have stride=1 for semantic segmentation
+ # Note that the stride of 1x1 should be the same as the 3x3
+ # Use dilation following the implementation of PSPNet
+ secondlast_channel = 0
+ for n, m in self.layer3.named_modules():
+ if ('rbr_dense' in n or 'rbr_reparam' in n) and isinstance(m, nn.Conv2d):
+ m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1)
+ print('change dilation, padding, stride of ', n)
+ secondlast_channel = m.out_channels
+ elif 'rbr_1x1' in n and isinstance(m, nn.Conv2d):
+ m.stride = (1, 1)
+ print('change stride of ', n)
+ last_channel = 0
+ for n, m in self.layer4.named_modules():
+ if ('rbr_dense' in n or 'rbr_reparam' in n) and isinstance(m, nn.Conv2d):
+ m.dilation, m.padding, m.stride = (4, 4), (4, 4), (1, 1)
+ print('change dilation, padding, stride of ', n)
+ last_channel = m.out_channels
+ elif 'rbr_1x1' in n and isinstance(m, nn.Conv2d):
+ m.stride = (1, 1)
+ print('change stride of ', n)
+
+ fea_dim = last_channel
+ aux_in = secondlast_channel
+
+ if use_ppm:
+ self.ppm = PPM(fea_dim, int(fea_dim/len(bins)), bins, BatchNorm)
+ fea_dim *= 2
+
+ self.cls = nn.Sequential(
+ nn.Conv2d(fea_dim, 512, kernel_size=3, padding=1, bias=False),
+ BatchNorm(512),
+ nn.ReLU(inplace=True),
+ nn.Dropout2d(p=dropout),
+ nn.Conv2d(512, classes, kernel_size=1)
+ )
+ if self.training:
+ self.aux = nn.Sequential(
+ nn.Conv2d(aux_in, 256, kernel_size=3, padding=1, bias=False),
+ BatchNorm(256),
+ nn.ReLU(inplace=True),
+ nn.Dropout2d(p=dropout),
+ nn.Conv2d(256, classes, kernel_size=1)
+ )
+
+ def forward(self, x, y=None):
+ x_size = x.size()
+ assert (x_size[2]-1) % 8 == 0 and (x_size[3]-1) % 8 == 0
+ h = int((x_size[2] - 1) / 8 * self.zoom_factor + 1)
+ w = int((x_size[3] - 1) / 8 * self.zoom_factor + 1)
+
+ x = self.layer0(x)
+ x = self.layer1(x)
+ x = self.layer2(x)
+ x_tmp = self.layer3(x)
+ x = self.layer4(x_tmp)
+
+ if self.use_ppm:
+ x = self.ppm(x)
+ x = self.cls(x)
+ if self.zoom_factor != 1:
+ x = F.interpolate(x, size=(h, w), mode='bilinear', align_corners=True)
+
+ if self.training:
+ aux = self.aux(x_tmp)
+ if self.zoom_factor != 1:
+ aux = F.interpolate(aux, size=(h, w), mode='bilinear', align_corners=True)
+ main_loss = self.criterion(x, y)
+ aux_loss = self.criterion(aux, y)
+ return x.max(1)[1], main_loss, aux_loss
+ else:
+ return x
+
+
+if __name__ == '__main__':
+ # 1. Build the PSPNet with RepVGG backbone. Download the ImageNet-pretrained weight file and load it.
+ model = PSPNet(backbone_name='RepVGG-A0', backbone_file='RepVGG-A0-train.pth', deploy=False, classes=19, pretrained=True)
+
+ # 2. Train it
+ # seg_train(model)
+
+ # 3. Convert and check the equivalence
+ input = torch.rand(4, 3, 713, 713)
+ model.eval()
+ print(model)
+ y_train = model(input)
+ for module in model.modules():
+ if hasattr(module, 'switch_to_deploy'):
+ module.switch_to_deploy()
+ y_deploy = model(input)
+ print('output is ', y_deploy.size())
+ print('=================== The diff is')
+ print(((y_deploy - y_train) ** 2).sum())
+
+ # 4. Save the converted model
+ torch.save(model.state_dict(), 'PSPNet-RepVGG-A0-deploy.pth')
+ del model # Or do whatever you want with it
+
+ # 5. For inference, load the saved model. There is no need to load the ImageNet-pretrained weights again.
+ deploy_model = PSPNet(backbone_name='RepVGG-A0', backbone_file=None, deploy=True, classes=19, pretrained=False)
+ deploy_model.eval()
+ deploy_model.load_state_dict(torch.load('PSPNet-RepVGG-A0-deploy.pth'))
+
+ # 6. Check again or do whatever you want
+ y_deploy = deploy_model(input)
+ print('=================== The diff is')
+ print(((y_deploy - y_train) ** 2).sum())
\ No newline at end of file
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/jizhi_submit_train_repvgg.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/jizhi_submit_train_repvgg.py
new file mode 100644
index 0000000000000000000000000000000000000000..8debce4d26aada27fcad1d47148c04510499636f
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/jizhi_submit_train_repvgg.py
@@ -0,0 +1,34 @@
+import argparse
+import datetime
+import os
+import json
+
+parser = argparse.ArgumentParser('JIZHI submit', add_help=False)
+parser.add_argument('arch', default=None, type=str)
+parser.add_argument('tag', default=None, type=str)
+parser.add_argument('--config', default='/apdcephfs_cq2/share_1290939/xiaohanding/cnt/default_V100x8_elastic_config.json', type=str,
+ help='config file')
+
+
+args = parser.parse_args()
+run_dir = f'{args.arch}_{args.tag}'
+
+cmd = f'python3 -m torch.distributed.launch --nproc_per_node 8 --master_port 12349 main.py ' \
+ f'--arch {args.arch} --batch-size 32 --tag {args.tag} --output-dir /apdcephfs_cq2/share_1290939/xiaohanding/swin_exps/{args.arch}_{args.tag} --opts TRAIN.EPOCHS 120 TRAIN.BASE_LR 0.1 TRAIN.WEIGHT_DECAY 4e-5 TRAIN.WARMUP_EPOCHS 5 MODEL.LABEL_SMOOTHING 0.1 AUG.PRESET raug15 DATA.DATASET imagenet'
+
+os.system('cd /apdcephfs_cq2/share_1290939/xiaohanding/RepVGG/')
+os.system(f'mkdir runs/{run_dir}')
+with open(f'runs/{run_dir}/start.sh', 'w') as f:
+ f.write(cmd)
+with open(args.config, 'r') as f:
+ json_content = json.load(f)
+json_content['model_local_file_path'] = f'/apdcephfs_cq2/share_1290939/xiaohanding/RepVGG/runs/{run_dir}'
+config_file_path = f'/apdcephfs_cq2/share_1290939/xiaohanding/RepVGG/runs/{run_dir}/config.json'
+with open(config_file_path, 'w') as f:
+ json.dump(json_content, f)
+
+os.system(f'cp *.py runs/{run_dir}/')
+os.system(f'cp -r data runs/{run_dir}/')
+os.system(f'cp -r train runs/{run_dir}/')
+os.system(f'cd runs/{run_dir}')
+os.system(f'jizhi_client start -scfg {config_file_path}')
\ No newline at end of file
diff --git a/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/main.py b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/main.py
new file mode 100644
index 0000000000000000000000000000000000000000..c721c14bd81484896b0a4f4f72558af96af29790
--- /dev/null
+++ b/AscendIE/TorchAIE/built-in/cv/classification/repvgg/github-src/RepVGG-main/main.py
@@ -0,0 +1,414 @@
+# --------------------------------------------------------
+# RepVGG: Making VGG-style ConvNets Great Again (https://openaccess.thecvf.com/content/CVPR2021/papers/Ding_RepVGG_Making_VGG-Style_ConvNets_Great_Again_CVPR_2021_paper.pdf)
+# Github source: https://github.com/DingXiaoH/RepVGG
+# Licensed under The MIT License [see LICENSE for details]
+# The training script is based on the code of Swin Transformer (https://github.com/microsoft/Swin-Transformer)
+# --------------------------------------------------------
+import time
+import argparse
+import datetime
+import numpy as np
+import torch
+import torch.backends.cudnn as cudnn
+import torch.distributed as dist
+from timm.loss import LabelSmoothingCrossEntropy, SoftTargetCrossEntropy
+from timm.utils import accuracy, AverageMeter
+from train.config import get_config
+from data import build_loader
+from train.lr_scheduler import build_scheduler
+from train.logger import create_logger
+from utils import load_checkpoint, save_checkpoint, get_grad_norm, auto_resume_helper, reduce_tensor, save_latest, update_model_ema, unwrap_model
+import copy
+from train.optimizer import build_optimizer
+from repvggplus import create_RepVGGplus_by_name
+
+try:
+ # noinspection PyUnresolvedReferences
+ from apex import amp
+except ImportError:
+ amp = None
+
+def parse_option():
+ parser = argparse.ArgumentParser('RepOpt-VGG training script built on the codebase of Swin Transformer', add_help=False)
+ parser.add_argument(
+ "--opts",
+ help="Modify config options by adding 'KEY VALUE' pairs. ",
+ default=None,
+ nargs='+',
+ )
+
+ # easy config modification
+ parser.add_argument('--arch', default=None, type=str, help='arch name')
+ parser.add_argument('--batch-size', default=128, type=int, help="batch size for single GPU")
+ parser.add_argument('--data-path', default='/your/path/to/dataset', type=str, help='path to dataset')
+ parser.add_argument('--scales-path', default=None, type=str, help='path to the trained Hyper-Search model')
+ parser.add_argument('--zip', action='store_true', help='use zipped dataset instead of folder dataset')
+ parser.add_argument('--cache-mode', type=str, default='part', choices=['no', 'full', 'part'],
+ help='no: no cache, '
+ 'full: cache all data, '
+ 'part: sharding the dataset into nonoverlapping pieces and only cache one piece')
+ parser.add_argument('--resume', help='resume from checkpoint')
+ parser.add_argument('--accumulation-steps', type=int, help="gradient accumulation steps")
+ parser.add_argument('--use-checkpoint', action='store_true',
+ help="whether to use gradient checkpointing to save memory")
+ parser.add_argument('--amp-opt-level', type=str, default='O0', choices=['O0', 'O1', 'O2'], #TODO Note: use amp if you have it
+ help='mixed precision opt level, if O0, no amp is used')
+ parser.add_argument('--output', default='/your/path/to/save/dir', type=str, metavar='PATH',
+ help='root of output folder, the full path is