diff --git a/models/cv/classification/alexnet/igie/README.md b/models/cv/classification/alexnet/igie/README.md index 59be111f70b9e78cf3defff7230a0dbb1b3d33ae..a5985877a36ddb22954cf2d5824ed921539a6bf9 100644 --- a/models/cv/classification/alexnet/igie/README.md +++ b/models/cv/classification/alexnet/igie/README.md @@ -25,19 +25,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight alexnet-owt-7be5be79.pth --output alexnet.onnx +python3 ../../igie_common/export.py --model-name alexnet --weight alexnet-owt-7be5be79.pth --output alexnet.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/alexnet/igie/ci/prepare.sh b/models/cv/classification/alexnet/igie/ci/prepare.sh index f79ad70d611b5c78f75587f5f962a48f14119b47..56267d4ab05cc7ac45fba48daba4382e6e10eea1 100644 --- a/models/cv/classification/alexnet/igie/ci/prepare.sh +++ b/models/cv/classification/alexnet/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight alexnet-owt-7be5be79.pth --output alexnet.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name alexnet --weight alexnet-owt-7be5be79.pth --output alexnet.onnx \ No newline at end of file diff --git a/models/cv/classification/alexnet/igie/export.py b/models/cv/classification/alexnet/igie/export.py deleted file mode 100644 index 543676ee6aeac7991d13bef6da514b5257f3a1b0..0000000000000000000000000000000000000000 --- a/models/cv/classification/alexnet/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.alexnet() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/alexnet/igie/scripts/infer_alexnet_fp16_accuracy.sh b/models/cv/classification/alexnet/igie/scripts/infer_alexnet_fp16_accuracy.sh index eee72a15b633495a1308ef8cc3aa6c17893b4c05..5e7525f02af8b1c2c2648a21e6f502740b1e90ff 100644 --- a/models/cv/classification/alexnet/igie/scripts/infer_alexnet_fp16_accuracy.sh +++ b/models/cv/classification/alexnet/igie/scripts/infer_alexnet_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="alexnet.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine alexnet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/alexnet/igie/scripts/infer_alexnet_fp16_performance.sh b/models/cv/classification/alexnet/igie/scripts/infer_alexnet_fp16_performance.sh index 5ea9adb9ab7eabaaaf50a0e63ef267fb24844708..ab1b19ceb3b85d4fde87816fce6bd319ba4b77f3 100644 --- a/models/cv/classification/alexnet/igie/scripts/infer_alexnet_fp16_performance.sh +++ b/models/cv/classification/alexnet/igie/scripts/infer_alexnet_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="alexnet.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine alexnet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/alexnet/igie/scripts/infer_alexnet_int8_accuracy.sh b/models/cv/classification/alexnet/igie/scripts/infer_alexnet_int8_accuracy.sh index e34643572d47c43e074482a2551172c497ab39d6..2bd275af424e9ed0dc3280be8a09756847e59e33 100644 --- a/models/cv/classification/alexnet/igie/scripts/infer_alexnet_int8_accuracy.sh +++ b/models/cv/classification/alexnet/igie/scripts/infer_alexnet_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path alexnet_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine alexnet_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/alexnet/igie/scripts/infer_alexnet_int8_performance.sh b/models/cv/classification/alexnet/igie/scripts/infer_alexnet_int8_performance.sh index 0fab8e8474a609e84ca15022f25adb579f206224..d85fff445455d73a74e4777820ef4c7fc0c33bbc 100644 --- a/models/cv/classification/alexnet/igie/scripts/infer_alexnet_int8_performance.sh +++ b/models/cv/classification/alexnet/igie/scripts/infer_alexnet_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path alexnet_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine alexnet_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/clip/igie/README.md b/models/cv/classification/clip/igie/README.md index 1fd0c7891723db69110b99a49ed407fc7dacfb01..380d95bad46b068e7db1e1a947a3f20c4dd7073c 100644 --- a/models/cv/classification/clip/igie/README.md +++ b/models/cv/classification/clip/igie/README.md @@ -14,15 +14,15 @@ CLIP (Contrastive Language-Image Pre-Training) is a neural network trained on a ### Prepare Resources -Pretrained model: +Pretrained model: + +Dataset: to download the validation dataset. ```bash git lfs install git clone https://huggingface.co/openai/clip-vit-base-patch32 clip-vit-base-patch32 ``` -Dataset: to download the validation dataset. - ### Install Dependencies ```bash diff --git a/models/cv/classification/convnext_base/igie/README.md b/models/cv/classification/convnext_base/igie/README.md index c59e893351f350f1fe8161d6f68a2f391eb036b9..2a60872fab9fb96376fc57de1ec37adb972f0f73 100644 --- a/models/cv/classification/convnext_base/igie/README.md +++ b/models/cv/classification/convnext_base/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight convnext_base-6075fbad.pth --output convnext_base.onnx +python3 ../../igie_common/export.py --model-name convnext_base --weight convnext_base-6075fbad.pth --output convnext_base.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/convnext_base/igie/build_engine.py b/models/cv/classification/convnext_base/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_base/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/convnext_base/igie/ci/prepare.sh b/models/cv/classification/convnext_base/igie/ci/prepare.sh index 17d287d5d57be04e154bc1057da20ca5f5248768..f820b393380506c29559764e5c039a2b3fbbfacc 100644 --- a/models/cv/classification/convnext_base/igie/ci/prepare.sh +++ b/models/cv/classification/convnext_base/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight convnext_base-6075fbad.pth --output convnext_base.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name convnext_base --weight convnext_base-6075fbad.pth --output convnext_base.onnx \ No newline at end of file diff --git a/models/cv/classification/convnext_base/igie/export.py b/models/cv/classification/convnext_base/igie/export.py deleted file mode 100644 index d9a2fe01e69085ca3c53782d32080c54cbbd1e5a..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_base/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.convnext_base() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/convnext_base/igie/inference.py b/models/cv/classification/convnext_base/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_base/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/convnext_base/igie/requirements.txt b/models/cv/classification/convnext_base/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_base/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/convnext_base/igie/scripts/infer_convnext_base_fp16_accuracy.sh b/models/cv/classification/convnext_base/igie/scripts/infer_convnext_base_fp16_accuracy.sh index 42575772c1d678ecc0b9b0f51a6f827480ab1dd2..1e0b7e3b2d7dd2b4239fe2ada29000eabbe684bd 100644 --- a/models/cv/classification/convnext_base/igie/scripts/infer_convnext_base_fp16_accuracy.sh +++ b/models/cv/classification/convnext_base/igie/scripts/infer_convnext_base_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="convnext_base.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine convnext_base_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/convnext_base/igie/scripts/infer_convnext_base_fp16_performance.sh b/models/cv/classification/convnext_base/igie/scripts/infer_convnext_base_fp16_performance.sh index d5ae06496b606011a35726082ac331df296195ae..87c3d1ee58ef2ea8b4c82efb2bd1eb5f9c3f05de 100644 --- a/models/cv/classification/convnext_base/igie/scripts/infer_convnext_base_fp16_performance.sh +++ b/models/cv/classification/convnext_base/igie/scripts/infer_convnext_base_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="convnext_base.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine convnext_base_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/convnext_s/igie/README.md b/models/cv/classification/convnext_s/igie/README.md index b7ed3c622c06f37d5db131c14dca069366740c4d..9222a9599af09fda79f09f4684b68cf065907f3a 100644 --- a/models/cv/classification/convnext_s/igie/README.md +++ b/models/cv/classification/convnext_s/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/convnext/convnext-small_32xb128_in1k.py --weight convnext-small_3rdparty_32xb128_in1k_20220124-d39b5192.pth --output convnext_s.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/convnext/convnext-small_32xb128_in1k.py --weight convnext-small_3rdparty_32xb128_in1k_20220124-d39b5192.pth --output convnext_s.onnx # Use onnxsim optimize onnx model onnxsim convnext_s.onnx convnext_s_opt.onnx @@ -48,6 +49,7 @@ onnxsim convnext_s.onnx convnext_s_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/convnext_s/igie/build_engine.py b/models/cv/classification/convnext_s/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_s/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/convnext_s/igie/ci/prepare.sh b/models/cv/classification/convnext_s/igie/ci/prepare.sh index 52f2253afd3a4ca167e2c302acf3d99c26e6e0a3..29082e3d1a7c83603555fe61c5cbde331bd05a09 100644 --- a/models/cv/classification/convnext_s/igie/ci/prepare.sh +++ b/models/cv/classification/convnext_s/igie/ci/prepare.sh @@ -24,13 +24,14 @@ else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 # git clone mmpretrain # git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ # export onnx model -python3 export.py --cfg mmpretrain/configs/convnext/convnext-small_32xb128_in1k.py --weight convnext-small_3rdparty_32xb128_in1k_20220124-d39b5192.pth --output convnext_s.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/convnext/convnext-small_32xb128_in1k.py --weight convnext-small_3rdparty_32xb128_in1k_20220124-d39b5192.pth --output convnext_s.onnx # Use onnxsim optimize onnx model onnxsim convnext_s.onnx convnext_s_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/convnext_s/igie/export.py b/models/cv/classification/convnext_s/igie/export.py deleted file mode 100644 index 662c6a01b1fcf102b4c7962b1d7e45b94da229d4..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_s/igie/export.py +++ /dev/null @@ -1,77 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - out_head = self.model.head.fc(feat[0]) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/convnext_s/igie/inference.py b/models/cv/classification/convnext_s/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_s/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/convnext_s/igie/requirements.txt b/models/cv/classification/convnext_s/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_s/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/convnext_s/igie/scripts/infer_convnext_s_fp16_accuracy.sh b/models/cv/classification/convnext_s/igie/scripts/infer_convnext_s_fp16_accuracy.sh index 48d67214a118fea1224ad783dafb3d57dc5a6c65..4cbdc4a1765662f111f6b4344006499535c624aa 100644 --- a/models/cv/classification/convnext_s/igie/scripts/infer_convnext_s_fp16_accuracy.sh +++ b/models/cv/classification/convnext_s/igie/scripts/infer_convnext_s_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="convnext_s_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine convnext_s_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/convnext_s/igie/scripts/infer_convnext_s_fp16_performance.sh b/models/cv/classification/convnext_s/igie/scripts/infer_convnext_s_fp16_performance.sh index f09fcc9cc8fab2d223b6c54d9a7992b9d8495c2f..3a9b08ea5ff2f3abbb9a088a6191d79a500b0b4d 100644 --- a/models/cv/classification/convnext_s/igie/scripts/infer_convnext_s_fp16_performance.sh +++ b/models/cv/classification/convnext_s/igie/scripts/infer_convnext_s_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="convnext_s_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine convnext_s_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/convnext_small/igie/README.md b/models/cv/classification/convnext_small/igie/README.md index 8a62ba22dbbc9b703432b19f8d333c215913cb99..c665dcb6f0f3f6177048e30010142d469c27207b 100644 --- a/models/cv/classification/convnext_small/igie/README.md +++ b/models/cv/classification/convnext_small/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight convnext_small-0c510722.pth --output convnext_small.onnx +python3 ../../igie_common/export.py --model-name convnext_small --weight convnext_small-0c510722.pth --output convnext_small.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/convnext_small/igie/build_engine.py b/models/cv/classification/convnext_small/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_small/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/convnext_small/igie/ci/prepare.sh b/models/cv/classification/convnext_small/igie/ci/prepare.sh index 6c67be93b5f3ff1be1fcd93ed1f8bc77ec2404f1..2303d1ec2b2401856ab499ad431e73c20fd8b9d3 100644 --- a/models/cv/classification/convnext_small/igie/ci/prepare.sh +++ b/models/cv/classification/convnext_small/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight convnext_small-0c510722.pth --output convnext_small.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name convnext_small --weight convnext_small-0c510722.pth --output convnext_small.onnx \ No newline at end of file diff --git a/models/cv/classification/convnext_small/igie/export.py b/models/cv/classification/convnext_small/igie/export.py deleted file mode 100644 index 1ec39771ca57ad629d1ebbd5dac4ee71cd3d303c..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_small/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.convnext_small() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/convnext_small/igie/inference.py b/models/cv/classification/convnext_small/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_small/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/convnext_small/igie/requirements.txt b/models/cv/classification/convnext_small/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_small/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/convnext_small/igie/scripts/infer_convnext_small_fp16_accuracy.sh b/models/cv/classification/convnext_small/igie/scripts/infer_convnext_small_fp16_accuracy.sh index c7837d790c90b4aa6af02ecc15e517d82acfc9c4..37d7b59c86b4ef4a4f48545e813d1cd86a32a50c 100644 --- a/models/cv/classification/convnext_small/igie/scripts/infer_convnext_small_fp16_accuracy.sh +++ b/models/cv/classification/convnext_small/igie/scripts/infer_convnext_small_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="convnext_small.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine convnext_small_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/convnext_small/igie/scripts/infer_convnext_small_fp16_performance.sh b/models/cv/classification/convnext_small/igie/scripts/infer_convnext_small_fp16_performance.sh index cde8da5aaf4e74f7d6681cc43915e0faf6d1bbfc..12956b0e0efb71bcbbd3e4ecce1f0d44c1b317b0 100644 --- a/models/cv/classification/convnext_small/igie/scripts/infer_convnext_small_fp16_performance.sh +++ b/models/cv/classification/convnext_small/igie/scripts/infer_convnext_small_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="convnext_small.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine convnext_small_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/convnext_tiny/igie/README.md b/models/cv/classification/convnext_tiny/igie/README.md index 5608999f5ef7aa5fc40d8eb690b88934cf22087b..3d0831352122b0981f1a3fbc58c4aaf0ed6229c2 100644 --- a/models/cv/classification/convnext_tiny/igie/README.md +++ b/models/cv/classification/convnext_tiny/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight convnext_tiny-983f1562.pth --output convnext_tiny.onnx +python3 ../../igie_common/export.py --model-name convnext_tiny --weight convnext_tiny-983f1562.pth --output convnext_tiny.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/convnext_tiny/igie/build_engine.py b/models/cv/classification/convnext_tiny/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_tiny/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/convnext_tiny/igie/ci/prepare.sh b/models/cv/classification/convnext_tiny/igie/ci/prepare.sh index 9710375eed96d2ab6dc8b14a86c7eb2942a71f2b..1ec6f531f50c028b9fdb5d8f35566fa417907dff 100644 --- a/models/cv/classification/convnext_tiny/igie/ci/prepare.sh +++ b/models/cv/classification/convnext_tiny/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight convnext_tiny-983f1562.pth --output convnext_tiny.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name convnext_tiny --weight convnext_tiny-983f1562.pth --output convnext_tiny.onnx \ No newline at end of file diff --git a/models/cv/classification/convnext_tiny/igie/export.py b/models/cv/classification/convnext_tiny/igie/export.py deleted file mode 100644 index 3364e7be4125161504e40d51c3e90017da614fee..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_tiny/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.convnext_tiny() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/convnext_tiny/igie/inference.py b/models/cv/classification/convnext_tiny/igie/inference.py deleted file mode 100644 index 0563f72e11c44bceb5acf127981e225746aa9155..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_tiny/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/convnext_tiny/igie/requirements.txt b/models/cv/classification/convnext_tiny/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/convnext_tiny/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/convnext_tiny/igie/scripts/infer_convnext_tiny_fp16_accuracy.sh b/models/cv/classification/convnext_tiny/igie/scripts/infer_convnext_tiny_fp16_accuracy.sh index c095775781344f40ed2bd5bfaeb36c88d43fd816..9c6cfe460ce8f742e4f2b32d83dd5be5110739ff 100644 --- a/models/cv/classification/convnext_tiny/igie/scripts/infer_convnext_tiny_fp16_accuracy.sh +++ b/models/cv/classification/convnext_tiny/igie/scripts/infer_convnext_tiny_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="convnext_tiny.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine convnext_tiny_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/convnext_tiny/igie/scripts/infer_convnext_tiny_fp16_performance.sh b/models/cv/classification/convnext_tiny/igie/scripts/infer_convnext_tiny_fp16_performance.sh index 36a26f5a16189cdf0451a44c3b66870b64b2bbd2..5571d5e9d843f8571fca4ad64ff1149263111469 100644 --- a/models/cv/classification/convnext_tiny/igie/scripts/infer_convnext_tiny_fp16_performance.sh +++ b/models/cv/classification/convnext_tiny/igie/scripts/infer_convnext_tiny_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="convnext_tiny.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine convnext_tiny_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/cspdarknet53/igie/README.md b/models/cv/classification/cspdarknet53/igie/README.md index c7c1624b64acf75ba25056e852daa942159a4157..07da984c34c040f7d3f9e3b5862eef19b63cdbe3 100644 --- a/models/cv/classification/cspdarknet53/igie/README.md +++ b/models/cv/classification/cspdarknet53/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -40,7 +41,7 @@ git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git ## cspdarknet50 is actually cspdarknet53 wget -O cspdarknet53_3rdparty_8xb32_in1k_20220329-bd275287.pth https://download.openmmlab.com/mmclassification/v0/cspnet/ -python3 export.py --cfg mmpretrain/configs/cspnet/cspdarknet50_8xb32_in1k.py --weight cspdarknet53_3rdparty_8xb32_in1k_20220329-bd275287.pth --output cspdarknet53.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/cspnet/cspdarknet50_8xb32_in1k.py --weight cspdarknet53_3rdparty_8xb32_in1k_20220329-bd275287.pth --output cspdarknet53.onnx # Use onnxsim optimize onnx model onnxsim cspdarknet53.onnx cspdarknet53_opt.onnx @@ -50,6 +51,7 @@ onnxsim cspdarknet53.onnx cspdarknet53_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/cspdarknet53/igie/build_engine.py b/models/cv/classification/cspdarknet53/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspdarknet53/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/cspdarknet53/igie/ci/prepare.sh b/models/cv/classification/cspdarknet53/igie/ci/prepare.sh index 6b99b1299505b4e74fa6d808905ca7788c73d805..782aa45f7dfabbc813edc99a17f6e3acb9f5e04e 100644 --- a/models/cv/classification/cspdarknet53/igie/ci/prepare.sh +++ b/models/cv/classification/cspdarknet53/igie/ci/prepare.sh @@ -24,7 +24,8 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ -python3 export.py --cfg mmpretrain/configs/cspnet/cspdarknet50_8xb32_in1k.py --weight cspdarknet53_3rdparty_8xb32_in1k_20220329-bd275287.pth --output cspdarknet53.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/cspnet/cspdarknet50_8xb32_in1k.py --weight cspdarknet53_3rdparty_8xb32_in1k_20220329-bd275287.pth --output cspdarknet53.onnx onnxsim cspdarknet53.onnx cspdarknet53_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/cspdarknet53/igie/inference.py b/models/cv/classification/cspdarknet53/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspdarknet53/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/cspdarknet53/igie/requirements.txt b/models/cv/classification/cspdarknet53/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspdarknet53/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/cspdarknet53/igie/scripts/infer_cspdarknet53_fp16_accuracy.sh b/models/cv/classification/cspdarknet53/igie/scripts/infer_cspdarknet53_fp16_accuracy.sh index 3838127cc28ecc22a417e2a17f6146c4f9621c84..c0c7ea34980979a34cd3d5060b63ab0b4ccf2a7c 100644 --- a/models/cv/classification/cspdarknet53/igie/scripts/infer_cspdarknet53_fp16_accuracy.sh +++ b/models/cv/classification/cspdarknet53/igie/scripts/infer_cspdarknet53_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="cspdarknet53_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine cspdarknet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/cspdarknet53/igie/scripts/infer_cspdarknet53_fp16_performance.sh b/models/cv/classification/cspdarknet53/igie/scripts/infer_cspdarknet53_fp16_performance.sh index 0c1a847bd5a803467a48bb47088014499c0b270c..0a79497787a3b010e44f9bb93b308dd4ce3517f0 100644 --- a/models/cv/classification/cspdarknet53/igie/scripts/infer_cspdarknet53_fp16_performance.sh +++ b/models/cv/classification/cspdarknet53/igie/scripts/infer_cspdarknet53_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="cspdarknet53_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine cspdarknet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/cspresnet50/igie/README.md b/models/cv/classification/cspresnet50/igie/README.md index b16f992cd2144bb7f9c08f13bbe623fc82554918..5c01bbd147810e72ef3b66a6632e53261bd427a7 100644 --- a/models/cv/classification/cspresnet50/igie/README.md +++ b/models/cv/classification/cspresnet50/igie/README.md @@ -29,7 +29,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -39,7 +40,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/cspnet/cspresnet50_8xb32_in1k.py --weight cspresnet50_3rdparty_8xb32_in1k_20220329-dd6dddfb.pth --output cspresnet50.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/cspnet/cspresnet50_8xb32_in1k.py --weight cspresnet50_3rdparty_8xb32_in1k_20220329-dd6dddfb.pth --output cspresnet50.onnx # Use onnxsim optimize onnx model onnxsim cspresnet50.onnx cspresnet50_opt.onnx @@ -50,6 +51,7 @@ onnxsim cspresnet50.onnx cspresnet50_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/cspresnet50/igie/build_engine.py b/models/cv/classification/cspresnet50/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspresnet50/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/cspresnet50/igie/ci/prepare.sh b/models/cv/classification/cspresnet50/igie/ci/prepare.sh index f35c6c7b67f92dc77b90521129fbd731952718f5..f1ae8f103c8f9901ebb9185632ed6a9249de2261 100644 --- a/models/cv/classification/cspresnet50/igie/ci/prepare.sh +++ b/models/cv/classification/cspresnet50/igie/ci/prepare.sh @@ -24,10 +24,11 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ # export onnx model -python3 export.py --cfg mmpretrain/configs/cspnet/cspresnet50_8xb32_in1k.py --weight cspresnet50_3rdparty_8xb32_in1k_20220329-dd6dddfb.pth --output cspresnet50.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/cspnet/cspresnet50_8xb32_in1k.py --weight cspresnet50_3rdparty_8xb32_in1k_20220329-dd6dddfb.pth --output cspresnet50.onnx # Use onnxsim optimize onnx model onnxsim cspresnet50.onnx cspresnet50_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/cspresnet50/igie/export.py b/models/cv/classification/cspresnet50/igie/export.py deleted file mode 100644 index 6eafb7b164d52da54d010aeb1d92b838215c7702..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspresnet50/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat[0]) - out_head = self.model.head.fc(feat) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/cspresnet50/igie/inference.py b/models/cv/classification/cspresnet50/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspresnet50/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/cspresnet50/igie/requirements.txt b/models/cv/classification/cspresnet50/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspresnet50/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/cspresnet50/igie/scripts/infer_cspresnet50_fp16_accuracy.sh b/models/cv/classification/cspresnet50/igie/scripts/infer_cspresnet50_fp16_accuracy.sh index 47a37e73cfa365e1316e89877a8e79bf54c444f7..398a1ceebeb76bf83623e0d9b29dd28e43651a95 100644 --- a/models/cv/classification/cspresnet50/igie/scripts/infer_cspresnet50_fp16_accuracy.sh +++ b/models/cv/classification/cspresnet50/igie/scripts/infer_cspresnet50_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="cspresnet50_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine cspresnet50_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/cspresnet50/igie/scripts/infer_cspresnet50_fp16_performance.sh b/models/cv/classification/cspresnet50/igie/scripts/infer_cspresnet50_fp16_performance.sh index a1ea0cc5bb2b21f0649956eb9baba49cc444967c..4fcab323904779b8ebe3e70fff44561341bcb1ae 100644 --- a/models/cv/classification/cspresnet50/igie/scripts/infer_cspresnet50_fp16_performance.sh +++ b/models/cv/classification/cspresnet50/igie/scripts/infer_cspresnet50_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="cspresnet50_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine cspresnet50_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/cspresnext50/igie/README.md b/models/cv/classification/cspresnext50/igie/README.md index 4f56b0f0c6c93f15636154a7483228cf74eeb80b..7a8ddc1e2dfcbb803fec896a84b0ef4c61d74991 100644 --- a/models/cv/classification/cspresnext50/igie/README.md +++ b/models/cv/classification/cspresnext50/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/cspnet/cspresnext50_8xb32_in1k.py --weight cspresnext50_3rdparty_8xb32_in1k_20220329-2cc84d21.pth --output cspresnext50.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/cspnet/cspresnext50_8xb32_in1k.py --weight cspresnext50_3rdparty_8xb32_in1k_20220329-2cc84d21.pth --output cspresnext50.onnx # Use onnxsim optimize onnx model onnxsim cspresnext50.onnx cspresnext50_opt.onnx @@ -48,6 +49,7 @@ onnxsim cspresnext50.onnx cspresnext50_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/cspresnext50/igie/build_engine.py b/models/cv/classification/cspresnext50/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspresnext50/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/cspresnext50/igie/ci/prepare.sh b/models/cv/classification/cspresnext50/igie/ci/prepare.sh index 5f18b3798cc8e96b07fccb3f8357be1bd51c6afa..5f3a564fe92e160188b50dc3c0a02245ba56a4cf 100644 --- a/models/cv/classification/cspresnext50/igie/ci/prepare.sh +++ b/models/cv/classification/cspresnext50/igie/ci/prepare.sh @@ -24,10 +24,11 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ # export onnx model -python3 export.py --cfg mmpretrain/configs/cspnet/cspresnext50_8xb32_in1k.py --weight cspresnext50_3rdparty_8xb32_in1k_20220329-2cc84d21.pth --output cspresnext50.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/cspnet/cspresnext50_8xb32_in1k.py --weight cspresnext50_3rdparty_8xb32_in1k_20220329-2cc84d21.pth --output cspresnext50.onnx # Use onnxsim optimize onnx model onnxsim cspresnext50.onnx cspresnext50_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/cspresnext50/igie/export.py b/models/cv/classification/cspresnext50/igie/export.py deleted file mode 100644 index c7681b6883fffd4e126cc01d946e7abfb69df315..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspresnext50/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat[0]) - out_head = self.model.head.fc(feat) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/cspresnext50/igie/inference.py b/models/cv/classification/cspresnext50/igie/inference.py deleted file mode 100644 index b0a8ed039512863abde9bcb6ade316900a9fdaac..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspresnext50/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/cspresnext50/igie/requirements.txt b/models/cv/classification/cspresnext50/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/cspresnext50/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/cspresnext50/igie/scripts/infer_cspresnext50_fp16_accuracy.sh b/models/cv/classification/cspresnext50/igie/scripts/infer_cspresnext50_fp16_accuracy.sh index 7086d1e1833663877dc06946aa741d8c4425fcc7..b711f2eefe3fb9c4c3de30d477041d7463798e99 100644 --- a/models/cv/classification/cspresnext50/igie/scripts/infer_cspresnext50_fp16_accuracy.sh +++ b/models/cv/classification/cspresnext50/igie/scripts/infer_cspresnext50_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="cspresnext50_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine cspresnext50_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/cspresnext50/igie/scripts/infer_cspresnext50_fp16_performance.sh b/models/cv/classification/cspresnext50/igie/scripts/infer_cspresnext50_fp16_performance.sh index 1716d61c8eeffcbe9555bb1a68215bd019b02e9e..f1810a258bc125aafa8460d13d3f505be68b55e8 100644 --- a/models/cv/classification/cspresnext50/igie/scripts/infer_cspresnext50_fp16_performance.sh +++ b/models/cv/classification/cspresnext50/igie/scripts/infer_cspresnext50_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="cspresnext50_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine cspresnext50_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/deit_tiny/igie/README.md b/models/cv/classification/deit_tiny/igie/README.md index 1f703580134713db790a3eff5c672ded67af522b..439a3cc76972601d3314e2fdceb732004a0bf97d 100644 --- a/models/cv/classification/deit_tiny/igie/README.md +++ b/models/cv/classification/deit_tiny/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/deit/deit-tiny_pt-4xb256_in1k.py --weight deit-tiny_pt-4xb256_in1k_20220218-13b382a0.pth --output deit_tiny.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/deit/deit-tiny_pt-4xb256_in1k.py --weight deit-tiny_pt-4xb256_in1k_20220218-13b382a0.pth --output deit_tiny.onnx # Use onnxsim optimize onnx model onnxsim deit_tiny.onnx deit_tiny_opt.onnx @@ -47,6 +48,7 @@ onnxsim deit_tiny.onnx deit_tiny_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/deit_tiny/igie/build_engine.py b/models/cv/classification/deit_tiny/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/deit_tiny/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/deit_tiny/igie/ci/prepare.sh b/models/cv/classification/deit_tiny/igie/ci/prepare.sh index d310ee2630abc2ed57b86000c44f50db595f9e4e..223bd4edbb7d32fd820fa0b49de61448176d4aae 100644 --- a/models/cv/classification/deit_tiny/igie/ci/prepare.sh +++ b/models/cv/classification/deit_tiny/igie/ci/prepare.sh @@ -24,7 +24,8 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ -python3 export.py --cfg mmpretrain/configs/deit/deit-tiny_pt-4xb256_in1k.py --weight deit-tiny_pt-4xb256_in1k_20220218-13b382a0.pth --output deit_tiny.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/deit/deit-tiny_pt-4xb256_in1k.py --weight deit-tiny_pt-4xb256_in1k_20220218-13b382a0.pth --output deit_tiny.onnx onnxsim deit_tiny.onnx deit_tiny_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/deit_tiny/igie/export.py b/models/cv/classification/deit_tiny/igie/export.py deleted file mode 100644 index 0078670ffbf4bbcce3358d4a2cedc42ce61176f5..0000000000000000000000000000000000000000 --- a/models/cv/classification/deit_tiny/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - head = self.model.head.pre_logits(feat) - out_head = self.model.head.layers.head(head) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/deit_tiny/igie/inference.py b/models/cv/classification/deit_tiny/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/deit_tiny/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/deit_tiny/igie/requirements.txt b/models/cv/classification/deit_tiny/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/deit_tiny/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/deit_tiny/igie/scripts/infer_deit_tiny_fp16_accuracy.sh b/models/cv/classification/deit_tiny/igie/scripts/infer_deit_tiny_fp16_accuracy.sh index f6f9a8fb070df948cbf9dbe3e6e3c043c6270063..800804530187fd54df00311e81819f7d1fd0be9e 100644 --- a/models/cv/classification/deit_tiny/igie/scripts/infer_deit_tiny_fp16_accuracy.sh +++ b/models/cv/classification/deit_tiny/igie/scripts/infer_deit_tiny_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="deit_tiny_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine deit_tiny_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/deit_tiny/igie/scripts/infer_deit_tiny_fp16_performance.sh b/models/cv/classification/deit_tiny/igie/scripts/infer_deit_tiny_fp16_performance.sh index a21f13b246ba7a65399c12d1613055be39d73e81..91fc101ca9d4260abf9117c4fe4319557dd97c70 100644 --- a/models/cv/classification/deit_tiny/igie/scripts/infer_deit_tiny_fp16_performance.sh +++ b/models/cv/classification/deit_tiny/igie/scripts/infer_deit_tiny_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="deit_tiny_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine deit_tiny_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/densenet121/igie/README.md b/models/cv/classification/densenet121/igie/README.md index 3f8d0c2e38d1e949979c0ad1707fb18c47a137d7..e3ab597d787865ca0de5fb42abf50ef39d48805c 100644 --- a/models/cv/classification/densenet121/igie/README.md +++ b/models/cv/classification/densenet121/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight densenet121-a639ec97.pth --output densenet121.onnx +python3 ../../igie_common/export.py --model-name densenet121 --weight densenet121-a639ec97.pth --output densenet121.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/densenet121/igie/build_engine.py b/models/cv/classification/densenet121/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet121/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/densenet121/igie/ci/prepare.sh b/models/cv/classification/densenet121/igie/ci/prepare.sh index d97d1a7ffc3720ac0333e0501a9e79b8d3beda9b..fbd8c730940b06886b9f10e1929789ddcfa5f3af 100644 --- a/models/cv/classification/densenet121/igie/ci/prepare.sh +++ b/models/cv/classification/densenet121/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight densenet121-a639ec97.pth --output densenet121.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name densenet121 --weight densenet121-a639ec97.pth --output densenet121.onnx \ No newline at end of file diff --git a/models/cv/classification/densenet121/igie/export.py b/models/cv/classification/densenet121/igie/export.py deleted file mode 100644 index 456fe29e90109461d5ec237372d9b3542ecb35ae..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet121/igie/export.py +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse -import re - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.densenet121(weights=False) - - state_dict = torch.load(args.weight) - - pattern = re.compile(r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$' - ) - for key in list(state_dict.keys()): - res = pattern.match(key) - if res: - new_key = res.group(1) + res.group(2) - state_dict[new_key] = state_dict[key] - del state_dict[key] - - model.load_state_dict(state_dict) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/densenet121/igie/inference.py b/models/cv/classification/densenet121/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet121/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/densenet121/igie/requirements.txt b/models/cv/classification/densenet121/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet121/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/densenet121/igie/scripts/infer_densenet121_fp16_accuracy.sh b/models/cv/classification/densenet121/igie/scripts/infer_densenet121_fp16_accuracy.sh index 72aff4063c8ce794054c19f8ef354d165a70c204..e499ceefe691205ecdbadd24ada6d7a7fa905cd7 100644 --- a/models/cv/classification/densenet121/igie/scripts/infer_densenet121_fp16_accuracy.sh +++ b/models/cv/classification/densenet121/igie/scripts/infer_densenet121_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="densenet121.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine densenet121_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/densenet121/igie/scripts/infer_densenet121_fp16_performance.sh b/models/cv/classification/densenet121/igie/scripts/infer_densenet121_fp16_performance.sh index 50db35bb6578f3de5570c92bc53b785fc148e832..8cd546218bf8720e22795c3a37e0151dbda194c7 100644 --- a/models/cv/classification/densenet121/igie/scripts/infer_densenet121_fp16_performance.sh +++ b/models/cv/classification/densenet121/igie/scripts/infer_densenet121_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="densenet121.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine densenet121_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/densenet161/igie/README.md b/models/cv/classification/densenet161/igie/README.md index 26e7f1d4a9b94634ef6c12d8c54d0b1c01143f63..c2f5a294b9fcf31e16ebc8fef8427fdf636b8775 100644 --- a/models/cv/classification/densenet161/igie/README.md +++ b/models/cv/classification/densenet161/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight densenet161-8d451a50.pth --output densenet161.onnx +python3 ../../igie_common/export.py --model-name densenet161 --weight densenet161-8d451a50.pth --output densenet161.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/densenet161/igie/build_engine.py b/models/cv/classification/densenet161/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet161/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/densenet161/igie/ci/prepare.sh b/models/cv/classification/densenet161/igie/ci/prepare.sh index 575a4e0667aea9d89260987c2775ab45ee80fd6c..09c727cd8cffdbce233d1d17c9f0d6c28f84957c 100644 --- a/models/cv/classification/densenet161/igie/ci/prepare.sh +++ b/models/cv/classification/densenet161/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight densenet161-8d451a50.pth --output densenet161.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name densenet161 --weight densenet161-8d451a50.pth --output densenet161.onnx \ No newline at end of file diff --git a/models/cv/classification/densenet161/igie/inference.py b/models/cv/classification/densenet161/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet161/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/densenet161/igie/requirements.txt b/models/cv/classification/densenet161/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet161/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/densenet161/igie/scripts/infer_densenet161_fp16_accuracy.sh b/models/cv/classification/densenet161/igie/scripts/infer_densenet161_fp16_accuracy.sh index 7ad5d57e2ec3501d199bbfb3b59ec11b096088b5..407c1c51c5d59901b6ab0fc151cb3831b5be9d97 100644 --- a/models/cv/classification/densenet161/igie/scripts/infer_densenet161_fp16_accuracy.sh +++ b/models/cv/classification/densenet161/igie/scripts/infer_densenet161_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="densenet161.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine densenet161_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/densenet161/igie/scripts/infer_densenet161_fp16_performance.sh b/models/cv/classification/densenet161/igie/scripts/infer_densenet161_fp16_performance.sh index 96548717a4f8b21681b844547946b9356fda56bd..c192f2a973dd9c0ae3d858c93f21ee8cddf60536 100644 --- a/models/cv/classification/densenet161/igie/scripts/infer_densenet161_fp16_performance.sh +++ b/models/cv/classification/densenet161/igie/scripts/infer_densenet161_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="densenet161.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine densenet161_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/densenet169/igie/README.md b/models/cv/classification/densenet169/igie/README.md index 507fa0704e94694d0589aaba2c84ced6603cb091..5e961ca42a647ef04328e17a0eb1d62e4a0abf4d 100644 --- a/models/cv/classification/densenet169/igie/README.md +++ b/models/cv/classification/densenet169/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight densenet169-b2777c0a.pth --output densenet169.onnx +python3 ../../igie_common/export.py --model-name densenet169 --weight densenet169-b2777c0a.pth --output densenet169.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/densenet169/igie/build_engine.py b/models/cv/classification/densenet169/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet169/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/densenet169/igie/ci/prepare.sh b/models/cv/classification/densenet169/igie/ci/prepare.sh index 0f87783649fbbd5d27723eab5ccaea87b0b48769..b6bb73a7317b56562acb2d2bb8d7d631ef0e65da 100644 --- a/models/cv/classification/densenet169/igie/ci/prepare.sh +++ b/models/cv/classification/densenet169/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight densenet169-b2777c0a.pth --output densenet169.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name densenet169 --weight densenet169-b2777c0a.pth --output densenet169.onnx \ No newline at end of file diff --git a/models/cv/classification/densenet169/igie/export.py b/models/cv/classification/densenet169/igie/export.py deleted file mode 100644 index 44c7269055407350e6af92f9a85d8d9bc5e5bab5..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet169/igie/export.py +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse -import re - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.densenet169(weights=False) - - state_dict = torch.load(args.weight) - - pattern = re.compile(r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$' - ) - for key in list(state_dict.keys()): - res = pattern.match(key) - if res: - new_key = res.group(1) + res.group(2) - state_dict[new_key] = state_dict[key] - del state_dict[key] - - model.load_state_dict(state_dict) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/densenet169/igie/inference.py b/models/cv/classification/densenet169/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet169/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/densenet169/igie/requirements.txt b/models/cv/classification/densenet169/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet169/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/densenet169/igie/scripts/infer_densenet169_fp16_accuracy.sh b/models/cv/classification/densenet169/igie/scripts/infer_densenet169_fp16_accuracy.sh index 6475ddcdd58e63bfd7cd911dc3be1801026d6097..a2ab73db5447a4f80845154ad82adb75e26c2870 100644 --- a/models/cv/classification/densenet169/igie/scripts/infer_densenet169_fp16_accuracy.sh +++ b/models/cv/classification/densenet169/igie/scripts/infer_densenet169_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="densenet169.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine densenet169_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/densenet169/igie/scripts/infer_densenet169_fp16_performance.sh b/models/cv/classification/densenet169/igie/scripts/infer_densenet169_fp16_performance.sh index 8a36fc2f0129a71e0fd3ef678e06ee4eb3651fa3..ca965a5fe7aa61733cbb638ac3e8a040c0abbcf3 100644 --- a/models/cv/classification/densenet169/igie/scripts/infer_densenet169_fp16_performance.sh +++ b/models/cv/classification/densenet169/igie/scripts/infer_densenet169_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="densenet169.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine densenet169_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/densenet201/igie/README.md b/models/cv/classification/densenet201/igie/README.md index 9707e577169eaf3b595b456624ee4fa98e28072f..fc54b25baf7227341683e2bb7ed80c10e64082c4 100644 --- a/models/cv/classification/densenet201/igie/README.md +++ b/models/cv/classification/densenet201/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight densenet201-c1103571.pth --output densenet201.onnx +python3 ../../igie_common/export.py --model-name densenet201 --weight densenet201-c1103571.pth --output densenet201.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/densenet201/igie/build_engine.py b/models/cv/classification/densenet201/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet201/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/densenet201/igie/ci/prepare.sh b/models/cv/classification/densenet201/igie/ci/prepare.sh index 7a4eb046ddb0f6ca7dbc15690f21fea94b216466..1f9515b46a1ebb3e2e4d2d3445c352bd6ff58ff0 100644 --- a/models/cv/classification/densenet201/igie/ci/prepare.sh +++ b/models/cv/classification/densenet201/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight densenet201-c1103571.pth --output densenet201.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name densenet201 --weight densenet201-c1103571.pth --output densenet201.onnx \ No newline at end of file diff --git a/models/cv/classification/densenet201/igie/export.py b/models/cv/classification/densenet201/igie/export.py deleted file mode 100644 index 66019547bf1101889f3fd699581741a114890959..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet201/igie/export.py +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse -import re - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.densenet201(weights=False) - - state_dict = torch.load(args.weight) - - pattern = re.compile(r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$' - ) - for key in list(state_dict.keys()): - res = pattern.match(key) - if res: - new_key = res.group(1) + res.group(2) - state_dict[new_key] = state_dict[key] - del state_dict[key] - - model.load_state_dict(state_dict) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/densenet201/igie/inference.py b/models/cv/classification/densenet201/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet201/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/densenet201/igie/requirements.txt b/models/cv/classification/densenet201/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/densenet201/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/densenet201/igie/scripts/infer_densenet201_fp16_accuracy.sh b/models/cv/classification/densenet201/igie/scripts/infer_densenet201_fp16_accuracy.sh index 470b285a4038a285110f5b5d6ecce509062d517b..521eb073c6fb817c4639295c8ef8f3ba5acde516 100644 --- a/models/cv/classification/densenet201/igie/scripts/infer_densenet201_fp16_accuracy.sh +++ b/models/cv/classification/densenet201/igie/scripts/infer_densenet201_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="densenet201.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine densenet201_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/densenet201/igie/scripts/infer_densenet201_fp16_performance.sh b/models/cv/classification/densenet201/igie/scripts/infer_densenet201_fp16_performance.sh index e1ad69b7a3b1bbcb4da314ab792448aea440ea87..99834cc857d637814d0cba3ea311bb65aa35fb2d 100644 --- a/models/cv/classification/densenet201/igie/scripts/infer_densenet201_fp16_performance.sh +++ b/models/cv/classification/densenet201/igie/scripts/infer_densenet201_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="densenet201.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine densenet201_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b0/igie/README.md b/models/cv/classification/efficientnet_b0/igie/README.md index cd111f438c98f821cffdaec5106783afa7a5102e..9bf513dcbebe6da5a8f2196483226a0db27aeb7d 100644 --- a/models/cv/classification/efficientnet_b0/igie/README.md +++ b/models/cv/classification/efficientnet_b0/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight efficientnet_b0_rwightman-7f5810bc.pth --output efficientnet_b0.onnx +python3 ../../igie_common/export.py --model-name efficientnet_b0 --weight efficientnet_b0_rwightman-7f5810bc.pth --output efficientnet_b0.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/efficientnet_b0/igie/build_engine.py b/models/cv/classification/efficientnet_b0/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b0/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b0/igie/ci/prepare.sh b/models/cv/classification/efficientnet_b0/igie/ci/prepare.sh index 9fa9a304e3fa3ced16254685e532ec72f9ee9f2f..b305db606f2ecce66c4f95df09f9aca634b50265 100644 --- a/models/cv/classification/efficientnet_b0/igie/ci/prepare.sh +++ b/models/cv/classification/efficientnet_b0/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight efficientnet_b0_rwightman-7f5810bc.pth --output efficientnet_b0.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name efficientnet_b0 --weight efficientnet_b0_rwightman-7f5810bc.pth --output efficientnet_b0.onnx \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b0/igie/export.py b/models/cv/classification/efficientnet_b0/igie/export.py deleted file mode 100644 index f01809d7fb2ef9a45ee8cee0043af9a8f881c6ea..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b0/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.efficientnet_b0() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/efficientnet_b0/igie/inference.py b/models/cv/classification/efficientnet_b0/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b0/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b0/igie/requirements.txt b/models/cv/classification/efficientnet_b0/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b0/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/efficientnet_b0/igie/scripts/infer_efficientnet_b0_fp16_accuracy.sh b/models/cv/classification/efficientnet_b0/igie/scripts/infer_efficientnet_b0_fp16_accuracy.sh index 5785456aa2f023a433fb511a118e52e6557f16fd..77106c12aea50e1bcb0c19282ddb5e3bd77789a3 100644 --- a/models/cv/classification/efficientnet_b0/igie/scripts/infer_efficientnet_b0_fp16_accuracy.sh +++ b/models/cv/classification/efficientnet_b0/igie/scripts/infer_efficientnet_b0_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b0/igie/scripts/infer_efficientnet_b0_fp16_performance.sh b/models/cv/classification/efficientnet_b0/igie/scripts/infer_efficientnet_b0_fp16_performance.sh index b26ffa3d47086eea3ed3647df0f63f0e5ddf83b8..d6303c594362163262d001081de89f0ef9448216 100644 --- a/models/cv/classification/efficientnet_b0/igie/scripts/infer_efficientnet_b0_fp16_performance.sh +++ b/models/cv/classification/efficientnet_b0/igie/scripts/infer_efficientnet_b0_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b1/igie/README.md b/models/cv/classification/efficientnet_b1/igie/README.md index 315c6486a801c8d76bc73aaa03ae93aa2fc7c52d..7825dc7dc6b35311bda893cabdf0321ab0c19366 100644 --- a/models/cv/classification/efficientnet_b1/igie/README.md +++ b/models/cv/classification/efficientnet_b1/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight efficientnet_b1-c27df63c.pth --output efficientnet_b1.onnx +python3 ../../igie_common/export.py --model-name efficientnet_b1 --weight efficientnet_b1-c27df63c.pth --output efficientnet_b1.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/efficientnet_b1/igie/build_engine.py b/models/cv/classification/efficientnet_b1/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b1/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b1/igie/ci/prepare.sh b/models/cv/classification/efficientnet_b1/igie/ci/prepare.sh index 4ba1c5d4a1a230970af2cfc284138098e38b87a1..7e0c8231753060a05664275a50f3bf6982d4e511 100644 --- a/models/cv/classification/efficientnet_b1/igie/ci/prepare.sh +++ b/models/cv/classification/efficientnet_b1/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight efficientnet_b1-c27df63c.pth --output efficientnet_b1.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name efficientnet_b1 --weight efficientnet_b1-c27df63c.pth --output efficientnet_b1.onnx \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b1/igie/export.py b/models/cv/classification/efficientnet_b1/igie/export.py deleted file mode 100644 index ec2d6de9fddf54ed07cace4124d6c6b240c4ac35..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b1/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.efficientnet_b1() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/efficientnet_b1/igie/inference.py b/models/cv/classification/efficientnet_b1/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b1/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b1/igie/requirements.txt b/models/cv/classification/efficientnet_b1/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b1/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/efficientnet_b1/igie/scripts/infer_efficientnet_b1_fp16_accuracy.sh b/models/cv/classification/efficientnet_b1/igie/scripts/infer_efficientnet_b1_fp16_accuracy.sh index 5d57cdf1862de0816873501e9aaca4f92daf8be2..0e64b3826ac22b108bfcaa32727293b94cdf7d1d 100644 --- a/models/cv/classification/efficientnet_b1/igie/scripts/infer_efficientnet_b1_fp16_accuracy.sh +++ b/models/cv/classification/efficientnet_b1/igie/scripts/infer_efficientnet_b1_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b1.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b1_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b1/igie/scripts/infer_efficientnet_b1_fp16_performance.sh b/models/cv/classification/efficientnet_b1/igie/scripts/infer_efficientnet_b1_fp16_performance.sh index 69c2ad2fe43bd28ac6fa3a2e8873e7bcdda291ca..0ceaa988cccffa3cca74c144539e61e64602ae52 100644 --- a/models/cv/classification/efficientnet_b1/igie/scripts/infer_efficientnet_b1_fp16_performance.sh +++ b/models/cv/classification/efficientnet_b1/igie/scripts/infer_efficientnet_b1_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b1.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b1_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b2/igie/README.md b/models/cv/classification/efficientnet_b2/igie/README.md index d0a0069ab141d535db412be78b1e09756af2268e..2da29aff2099e05cb3893544b6e80b94b25d4d11 100644 --- a/models/cv/classification/efficientnet_b2/igie/README.md +++ b/models/cv/classification/efficientnet_b2/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight efficientnet_b2_rwightman-c35c1473.pth --output efficientnet_b2.onnx +python3 ../../igie_common/export.py --model-name efficientnet_b2 --weight efficientnet_b2_rwightman-c35c1473.pth --output efficientnet_b2.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/efficientnet_b2/igie/build_engine.py b/models/cv/classification/efficientnet_b2/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b2/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b2/igie/ci/prepare.sh b/models/cv/classification/efficientnet_b2/igie/ci/prepare.sh index 7903f6231894cd5e6b132e555a4dee7180135dbc..8aac963500c8989507028b190169b9dd1be6b683 100644 --- a/models/cv/classification/efficientnet_b2/igie/ci/prepare.sh +++ b/models/cv/classification/efficientnet_b2/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight efficientnet_b2_rwightman-c35c1473.pth --output efficientnet_b2.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name efficientnet_b2 --weight efficientnet_b2_rwightman-c35c1473.pth --output efficientnet_b2.onnx \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b2/igie/export.py b/models/cv/classification/efficientnet_b2/igie/export.py deleted file mode 100644 index 7761ffaca5fd02798d898e12392f3e0501aef0ec..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b2/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.efficientnet_b2() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/efficientnet_b2/igie/inference.py b/models/cv/classification/efficientnet_b2/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b2/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b2/igie/requirements.txt b/models/cv/classification/efficientnet_b2/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b2/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/efficientnet_b2/igie/scripts/infer_efficientnet_b2_fp16_accuracy.sh b/models/cv/classification/efficientnet_b2/igie/scripts/infer_efficientnet_b2_fp16_accuracy.sh index d4ed3ef0893017781d18c20c4387aa8c7f52851c..e1e4f855ff320746ec918e8071205a0cd0565454 100644 --- a/models/cv/classification/efficientnet_b2/igie/scripts/infer_efficientnet_b2_fp16_accuracy.sh +++ b/models/cv/classification/efficientnet_b2/igie/scripts/infer_efficientnet_b2_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b2.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b2_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b2/igie/scripts/infer_efficientnet_b2_fp16_performance.sh b/models/cv/classification/efficientnet_b2/igie/scripts/infer_efficientnet_b2_fp16_performance.sh index 4b9602218a6a2c5be91f8ee2f97e63df7abc85e4..c988d5c816ffe0f8c7490f9f1c6986bf198a72d6 100644 --- a/models/cv/classification/efficientnet_b2/igie/scripts/infer_efficientnet_b2_fp16_performance.sh +++ b/models/cv/classification/efficientnet_b2/igie/scripts/infer_efficientnet_b2_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b2.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b2_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b3/igie/README.md b/models/cv/classification/efficientnet_b3/igie/README.md index 072ab49375ffd4a4c406e025918cd4b432f67ce6..44c0fd3e4de2bc012e39a558ffdbc121c2f0a9e3 100644 --- a/models/cv/classification/efficientnet_b3/igie/README.md +++ b/models/cv/classification/efficientnet_b3/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight efficientnet_b3_rwightman-b3899882.pth --output efficientnet_b3.onnx +python3 ../../igie_common/export.py --model-name efficientnet_b3 --weight efficientnet_b3_rwightman-b3899882.pth --output efficientnet_b3.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/efficientnet_b3/igie/build_engine.py b/models/cv/classification/efficientnet_b3/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b3/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b3/igie/ci/prepare.sh b/models/cv/classification/efficientnet_b3/igie/ci/prepare.sh index c6da04a2b7d44a6d3faa391309223e71f7073e35..8cdfec1b65e0555ca97881fd909c24f53f32bca0 100644 --- a/models/cv/classification/efficientnet_b3/igie/ci/prepare.sh +++ b/models/cv/classification/efficientnet_b3/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight efficientnet_b3_rwightman-b3899882.pth --output efficientnet_b3.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name efficientnet_b3 --weight efficientnet_b3_rwightman-b3899882.pth --output efficientnet_b3.onnx \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b3/igie/export.py b/models/cv/classification/efficientnet_b3/igie/export.py deleted file mode 100644 index f66e2cb063c247300fde0cc0c8e772dd7dbceb79..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b3/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.efficientnet_b3() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/efficientnet_b3/igie/inference.py b/models/cv/classification/efficientnet_b3/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b3/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b3/igie/requirements.txt b/models/cv/classification/efficientnet_b3/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b3/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/efficientnet_b3/igie/scripts/infer_efficientnet_b3_fp16_accuracy.sh b/models/cv/classification/efficientnet_b3/igie/scripts/infer_efficientnet_b3_fp16_accuracy.sh index 36e9a874f7b3795ebe6aa80378978d384db3f6de..2f75ca4a9bf17718470e8ee66ff10d009f32bebf 100644 --- a/models/cv/classification/efficientnet_b3/igie/scripts/infer_efficientnet_b3_fp16_accuracy.sh +++ b/models/cv/classification/efficientnet_b3/igie/scripts/infer_efficientnet_b3_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b3.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b3_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b3/igie/scripts/infer_efficientnet_b3_fp16_performance.sh b/models/cv/classification/efficientnet_b3/igie/scripts/infer_efficientnet_b3_fp16_performance.sh index 27b13c90249c3b423d8a3dbb5f2f9d123fc5e9f5..5df9e54ed0e29f2cd75b268389c01510955e2543 100644 --- a/models/cv/classification/efficientnet_b3/igie/scripts/infer_efficientnet_b3_fp16_performance.sh +++ b/models/cv/classification/efficientnet_b3/igie/scripts/infer_efficientnet_b3_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b3.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b3_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b4/igie/README.md b/models/cv/classification/efficientnet_b4/igie/README.md index bd04f34a6e5704c313fe42fdc0296583a3c111c5..68a12a6a49f967597c0a5bdfb6449f68287c33d4 100644 --- a/models/cv/classification/efficientnet_b4/igie/README.md +++ b/models/cv/classification/efficientnet_b4/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight efficientnet_b4_rwightman-23ab8bcd.pth --output efficientnet_b4.onnx +python3 ../../igie_common/export.py --model-name efficientnet_b4 --weight efficientnet_b4_rwightman-23ab8bcd.pth --output efficientnet_b4.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/efficientnet_b4/igie/build_engine.py b/models/cv/classification/efficientnet_b4/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b4/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b4/igie/ci/prepare.sh b/models/cv/classification/efficientnet_b4/igie/ci/prepare.sh index 4303d2aa10ef0a583204eb6f6bae07dd14e6f990..9683493268bc838184130c402f56cc61aa820de1 100644 --- a/models/cv/classification/efficientnet_b4/igie/ci/prepare.sh +++ b/models/cv/classification/efficientnet_b4/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight efficientnet_b4_rwightman-23ab8bcd.pth --output efficientnet_b4.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name efficientnet_b4 --weight efficientnet_b4_rwightman-23ab8bcd.pth --output efficientnet_b4.onnx \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b4/igie/export.py b/models/cv/classification/efficientnet_b4/igie/export.py deleted file mode 100644 index bffe9c47470f1066bfc6b8c64a0b30509b944665..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b4/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.efficientnet_b4() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/efficientnet_b4/igie/inference.py b/models/cv/classification/efficientnet_b4/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b4/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b4/igie/requirements.txt b/models/cv/classification/efficientnet_b4/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b4/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/efficientnet_b4/igie/scripts/infer_efficientnet_b4_fp16_accuracy.sh b/models/cv/classification/efficientnet_b4/igie/scripts/infer_efficientnet_b4_fp16_accuracy.sh index 018dfb220020cbd3d849b9c14f315ce8296e782b..72b966dfb1cc65b1d846263ce0f0af893c58793a 100644 --- a/models/cv/classification/efficientnet_b4/igie/scripts/infer_efficientnet_b4_fp16_accuracy.sh +++ b/models/cv/classification/efficientnet_b4/igie/scripts/infer_efficientnet_b4_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b4.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b4_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b4/igie/scripts/infer_efficientnet_b4_fp16_performance.sh b/models/cv/classification/efficientnet_b4/igie/scripts/infer_efficientnet_b4_fp16_performance.sh index 6ad001c760ed0fd7a49c4939a9b9084285858303..6ac6a3c23d63f9396e1854b9a818f2b3fe147136 100644 --- a/models/cv/classification/efficientnet_b4/igie/scripts/infer_efficientnet_b4_fp16_performance.sh +++ b/models/cv/classification/efficientnet_b4/igie/scripts/infer_efficientnet_b4_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b4.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b4_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b5/igie/README.md b/models/cv/classification/efficientnet_b5/igie/README.md index f5ed4d10e00ea5648a3cdfd3c2b779c711d7d2f4..e2a626bfa60ae7ee4a34a538926b8a5e6a93cd40 100644 --- a/models/cv/classification/efficientnet_b5/igie/README.md +++ b/models/cv/classification/efficientnet_b5/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight efficientnet_b5_lukemelas-1a07897c.pth --output efficientnet_b5.onnx +python3 ../../igie_common/export.py --model-name efficientnet_b5 --weight efficientnet_b5_lukemelas-1a07897c.pth --output efficientnet_b5.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/efficientnet_b5/igie/build_engine.py b/models/cv/classification/efficientnet_b5/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b5/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b5/igie/ci/prepare.sh b/models/cv/classification/efficientnet_b5/igie/ci/prepare.sh index 13c3aed51c38f88ce9d311117a2095e0c8764ec7..ce45e7c117c59a61afd9299b900fc7112362f2b5 100644 --- a/models/cv/classification/efficientnet_b5/igie/ci/prepare.sh +++ b/models/cv/classification/efficientnet_b5/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight efficientnet_b5_lukemelas-1a07897c.pth --output efficientnet_b5.onnx +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name efficientnet_b5 --weight efficientnet_b5_lukemelas-1a07897c.pth --output efficientnet_b5.onnx diff --git a/models/cv/classification/efficientnet_b5/igie/export.py b/models/cv/classification/efficientnet_b5/igie/export.py deleted file mode 100644 index 7c4e85238173d0a6b18eabb34aefca55a55f5c6b..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b5/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.efficientnet_b5() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/efficientnet_b5/igie/inference.py b/models/cv/classification/efficientnet_b5/igie/inference.py deleted file mode 100644 index 0563f72e11c44bceb5acf127981e225746aa9155..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b5/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_b5/igie/requirements.txt b/models/cv/classification/efficientnet_b5/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_b5/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/efficientnet_b5/igie/scripts/infer_efficientnet_b5_fp16_accuracy.sh b/models/cv/classification/efficientnet_b5/igie/scripts/infer_efficientnet_b5_fp16_accuracy.sh index e7c1330bfacaddd368adb7c2f1c6a019b730cacd..2db3257aedcf4f1c4149dc18ec15bebcb6f31527 100644 --- a/models/cv/classification/efficientnet_b5/igie/scripts/infer_efficientnet_b5_fp16_accuracy.sh +++ b/models/cv/classification/efficientnet_b5/igie/scripts/infer_efficientnet_b5_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b5.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b5_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_b5/igie/scripts/infer_efficientnet_b5_fp16_performance.sh b/models/cv/classification/efficientnet_b5/igie/scripts/infer_efficientnet_b5_fp16_performance.sh index 60d83a376107ed2d9ce0481867edd5f080d26e57..3c42938cf3026ee0151857de61b9118ab7a87bbd 100644 --- a/models/cv/classification/efficientnet_b5/igie/scripts/infer_efficientnet_b5_fp16_performance.sh +++ b/models/cv/classification/efficientnet_b5/igie/scripts/infer_efficientnet_b5_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="efficientnet_b5.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_b5_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_v2/igie/README.md b/models/cv/classification/efficientnet_v2/igie/README.md index eab2fd142f2e9f2638f1feb5233597a32647db7a..752160a57dfe129aa1b6b6734dfc2f0e7028d9bb 100644 --- a/models/cv/classification/efficientnet_v2/igie/README.md +++ b/models/cv/classification/efficientnet_v2/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight efficientnet_v2_m-dc08266a.pth --output efficientnet_v2_m.onnx +python3 ../../igie_common/export.py --model-name efficientnet_v2_m --weight efficientnet_v2_m-dc08266a.pth --output efficientnet_v2_m.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/efficientnet_v2/igie/build_engine.py b/models/cv/classification/efficientnet_v2/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_v2/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_v2/igie/ci/prepare.sh b/models/cv/classification/efficientnet_v2/igie/ci/prepare.sh index 75133e4be3f986424c0a273fafa1c025cdca0fc9..ccda00ca3c9449e836a8560aaf92a7eb35803fdb 100644 --- a/models/cv/classification/efficientnet_v2/igie/ci/prepare.sh +++ b/models/cv/classification/efficientnet_v2/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight efficientnet_v2_m-dc08266a.pth --output efficientnet_v2_m.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name efficientnet_v2_m --weight efficientnet_v2_m-dc08266a.pth --output efficientnet_v2_m.onnx \ No newline at end of file diff --git a/models/cv/classification/efficientnet_v2/igie/export.py b/models/cv/classification/efficientnet_v2/igie/export.py deleted file mode 100644 index cfb9f76e7503ce443da7090c5739e4880652f194..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_v2/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.efficientnet_v2_m() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/efficientnet_v2/igie/inference.py b/models/cv/classification/efficientnet_v2/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_v2/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_v2/igie/requirements.txt b/models/cv/classification/efficientnet_v2/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_v2/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/efficientnet_v2/igie/scripts/infer_efficientnet_v2_fp16_accuracy.sh b/models/cv/classification/efficientnet_v2/igie/scripts/infer_efficientnet_v2_fp16_accuracy.sh index d7aa78a2d9b15370dc1e2a5fc5a9fd14ab68668d..57bf599d14d7fe2e5c736eb3e3607446474a0c05 100644 --- a/models/cv/classification/efficientnet_v2/igie/scripts/infer_efficientnet_v2_fp16_accuracy.sh +++ b/models/cv/classification/efficientnet_v2/igie/scripts/infer_efficientnet_v2_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="efficientnet_v2_m.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_v2_m_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_v2/igie/scripts/infer_efficientnet_v2_fp16_performance.sh b/models/cv/classification/efficientnet_v2/igie/scripts/infer_efficientnet_v2_fp16_performance.sh index 21073983ec294f1612356de6848ed2c08f9374d4..18714a42958e1e4383f83593137ed8c54ee0d7cc 100644 --- a/models/cv/classification/efficientnet_v2/igie/scripts/infer_efficientnet_v2_fp16_performance.sh +++ b/models/cv/classification/efficientnet_v2/igie/scripts/infer_efficientnet_v2_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="efficientnet_v2_m.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_v2_m_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_v2_s/igie/README.md b/models/cv/classification/efficientnet_v2_s/igie/README.md index 66f446c3c0e2187963f04157860cb829a5c4ce11..8a8fa2faee8ba23aad2dfa8a0d7412b5b60cbcbb 100644 --- a/models/cv/classification/efficientnet_v2_s/igie/README.md +++ b/models/cv/classification/efficientnet_v2_s/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight efficientnet_v2_s-dd5fe13b.pth --output efficientnet_v2_s.onnx +python3 ../../igie_common/export.py --model-name efficientnet_v2_s --weight efficientnet_v2_s-dd5fe13b.pth --output efficientnet_v2_s.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/efficientnet_v2_s/igie/build_engine.py b/models/cv/classification/efficientnet_v2_s/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_v2_s/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_v2_s/igie/ci/prepare.sh b/models/cv/classification/efficientnet_v2_s/igie/ci/prepare.sh index ac0106d5784afdfd7400f8c1daf9a4fddb14045a..93bcc6a0d6ade6ff9ef6719d29eb298691fee2eb 100644 --- a/models/cv/classification/efficientnet_v2_s/igie/ci/prepare.sh +++ b/models/cv/classification/efficientnet_v2_s/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight efficientnet_v2_s-dd5fe13b.pth --output efficientnet_v2_s.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name efficientnet_v2_s --weight efficientnet_v2_s-dd5fe13b.pth --output efficientnet_v2_s.onnx \ No newline at end of file diff --git a/models/cv/classification/efficientnet_v2_s/igie/export.py b/models/cv/classification/efficientnet_v2_s/igie/export.py deleted file mode 100644 index 63b1b4c8af7c406299a29d241c78506964011839..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_v2_s/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.efficientnet_v2_s() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/efficientnet_v2_s/igie/inference.py b/models/cv/classification/efficientnet_v2_s/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_v2_s/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnet_v2_s/igie/requirements.txt b/models/cv/classification/efficientnet_v2_s/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnet_v2_s/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/efficientnet_v2_s/igie/scripts/infer_efficientnet_v2_s_fp16_accuracy.sh b/models/cv/classification/efficientnet_v2_s/igie/scripts/infer_efficientnet_v2_s_fp16_accuracy.sh index 2e7d113328c3c605a3609e2b9b562663000f3334..22f2084e91547ac983c503e63a61cb06aceba7ce 100644 --- a/models/cv/classification/efficientnet_v2_s/igie/scripts/infer_efficientnet_v2_s_fp16_accuracy.sh +++ b/models/cv/classification/efficientnet_v2_s/igie/scripts/infer_efficientnet_v2_s_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="efficientnet_v2_s.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_v2_s_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnet_v2_s/igie/scripts/infer_efficientnet_v2_s_fp16_performance.sh b/models/cv/classification/efficientnet_v2_s/igie/scripts/infer_efficientnet_v2_s_fp16_performance.sh index 4c67dd99f550c7ff7e50181de53231e4b1ca9cb7..c6ef87e84edc86faa8e559ae9b8bc8feb28bd3c9 100644 --- a/models/cv/classification/efficientnet_v2_s/igie/scripts/infer_efficientnet_v2_s_fp16_performance.sh +++ b/models/cv/classification/efficientnet_v2_s/igie/scripts/infer_efficientnet_v2_s_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="efficientnet_v2_s.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnet_v2_s_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnetv2_rw_t/igie/README.md b/models/cv/classification/efficientnetv2_rw_t/igie/README.md index e0df0c12afc9ba76fa17ed4cd1918abb9ee751ea..cfdd1b9a9d36afd1a0faff05d8687a0021a55d96 100644 --- a/models/cv/classification/efficientnetv2_rw_t/igie/README.md +++ b/models/cv/classification/efficientnetv2_rw_t/igie/README.md @@ -21,19 +21,21 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install timm ``` ### Model Conversion ```bash -python3 export.py --weight efficientnetv2_t_agc-3620981a.pth --output efficientnetv2_rw_t.onnx +python3 ../../igie_common/export_timm.py --model-name efficientnetv2_rw_t --weight efficientnetv2_t_agc-3620981a.pth --output efficientnetv2_rw_t.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/efficientnetv2_rw_t/igie/build_engine.py b/models/cv/classification/efficientnetv2_rw_t/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnetv2_rw_t/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnetv2_rw_t/igie/ci/prepare.sh b/models/cv/classification/efficientnetv2_rw_t/igie/ci/prepare.sh index 1eee601b85f8dd992bfe4f461474b734a2e372a3..dc9e1c67a01df5120a44cef0bb4740f661897b2e 100644 --- a/models/cv/classification/efficientnetv2_rw_t/igie/ci/prepare.sh +++ b/models/cv/classification/efficientnetv2_rw_t/igie/ci/prepare.sh @@ -16,5 +16,6 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight efficientnetv2_t_agc-3620981a.pth --output efficientnetv2_rw_t.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +pip3 install timm +python3 ../../igie_common/export_timm.py --model-name efficientnetv2_rw_t --weight efficientnetv2_t_agc-3620981a.pth --output efficientnetv2_rw_t.onnx \ No newline at end of file diff --git a/models/cv/classification/efficientnetv2_rw_t/igie/inference.py b/models/cv/classification/efficientnetv2_rw_t/igie/inference.py deleted file mode 100644 index e33c91fa1de2d25402f0ad3318e15f372d829908..0000000000000000000000000000000000000000 --- a/models/cv/classification/efficientnetv2_rw_t/igie/inference.py +++ /dev/null @@ -1,183 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -from timm.data import create_dataset, create_loader - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - datasets = create_dataset(root=data_path, name="") - - dataloader = create_loader( - datasets, - input_size=(3, 288, 288), - batch_size=batch_size, - interpolation='bicubic', - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225), - crop_pct=1.0, - use_prefetcher = False, - num_workers = num_workers - ) - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/efficientnetv2_rw_t/igie/scripts/infer_efficientnetv2_rw_t_fp16_accuracy.sh b/models/cv/classification/efficientnetv2_rw_t/igie/scripts/infer_efficientnetv2_rw_t_fp16_accuracy.sh index e1e984147321a7a48a4ab6957611bd5c2efee372..9d96fcfb93ffd560a2a682f94068fd3322833a93 100644 --- a/models/cv/classification/efficientnetv2_rw_t/igie/scripts/infer_efficientnetv2_rw_t_fp16_accuracy.sh +++ b/models/cv/classification/efficientnetv2_rw_t/igie/scripts/infer_efficientnetv2_rw_t_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="efficientnetv2_rw_t.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,288,288 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnetv2_rw_t_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/efficientnetv2_rw_t/igie/scripts/infer_efficientnetv2_rw_t_fp16_performance.sh b/models/cv/classification/efficientnetv2_rw_t/igie/scripts/infer_efficientnetv2_rw_t_fp16_performance.sh index 797791fe30aa26d05bac953ca25079cedc1519e2..a3f9d58520cb98365e5c789fce1f07dba5627249 100644 --- a/models/cv/classification/efficientnetv2_rw_t/igie/scripts/infer_efficientnetv2_rw_t_fp16_performance.sh +++ b/models/cv/classification/efficientnetv2_rw_t/igie/scripts/infer_efficientnetv2_rw_t_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="efficientnetv2_rw_t.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,288,288 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine efficientnetv2_rw_t_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/googlenet/igie/README.md b/models/cv/classification/googlenet/igie/README.md index 7c6cb329f74b87caf4d6eebce87ae19285f195f7..2165ac5f921714ff1b0d612d39d3201124ddf740 100644 --- a/models/cv/classification/googlenet/igie/README.md +++ b/models/cv/classification/googlenet/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight googlenet-1378be20.pth --output googlenet.onnx +python3 ../../igie_common/export.py --model-name googlenet --weight googlenet-1378be20.pth --output googlenet.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/googlenet/igie/build_engine.py b/models/cv/classification/googlenet/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/googlenet/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/googlenet/igie/ci/prepare.sh b/models/cv/classification/googlenet/igie/ci/prepare.sh index e6fedc20c16f9603b20801fd6c733a8f6124a361..df6b792299f20437bc96e4bb5be31133b6e2bb39 100644 --- a/models/cv/classification/googlenet/igie/ci/prepare.sh +++ b/models/cv/classification/googlenet/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight googlenet-1378be20.pth --output googlenet.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name googlenet --weight googlenet-1378be20.pth --output googlenet.onnx \ No newline at end of file diff --git a/models/cv/classification/googlenet/igie/export.py b/models/cv/classification/googlenet/igie/export.py deleted file mode 100644 index ffd662e167331b6b023f047d954c5becc48c17c1..0000000000000000000000000000000000000000 --- a/models/cv/classification/googlenet/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.googlenet() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/googlenet/igie/inference.py b/models/cv/classification/googlenet/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/googlenet/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/googlenet/igie/quantize.py b/models/cv/classification/googlenet/igie/quantize.py deleted file mode 100644 index 1cdeb93fd3d5bd126324524d330be916b709d380..0000000000000000000000000000000000000000 --- a/models/cv/classification/googlenet/igie/quantize.py +++ /dev/null @@ -1,103 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 onnx -import psutil -import argparse -import numpy as np -from inference import get_dataloader -from onnxruntime.quantization import (CalibrationDataReader, QuantFormat, - quantize_static, QuantType, - CalibrationMethod) - -class CalibrationDataLoader(CalibrationDataReader): - def __init__(self, input_name, dataloader, cnt_limit=100): - self.cnt = 0 - self.input_name = input_name - self.cnt_limit = cnt_limit - self.iter = iter(dataloader) - - # avoid oom - @staticmethod - def _exceed_memory_upper_bound(upper_bound=80): - info = psutil.virtual_memory() - total_percent = info.percent - if total_percent >= upper_bound: - return True - return False - - def get_next(self): - if self._exceed_memory_upper_bound() or self.cnt >= self.cnt_limit: - return None - self.cnt += 1 - print(f"onnx calibration data count: {self.cnt}") - input_info = next(self.iter) - - ort_input = {k: np.array(v) for k, v in zip(self.input_name, input_info)} - return ort_input - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--out_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="calibration datasets path.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - model = onnx.load(args.model_path) - input_names = [input.name for input in model.graph.input] - - dataloader = get_dataloader(args.datasets, batch_size=1, num_workers=args.num_workers) - calibration = CalibrationDataLoader(input_names, dataloader, cnt_limit=20) - - quantize_static(args.model_path, - args.out_path, - calibration_data_reader=calibration, - quant_format=QuantFormat.QOperator, - per_channel=False, - activation_type=QuantType.QInt8, - weight_type=QuantType.QInt8, - use_external_data_format=False, - calibrate_method=CalibrationMethod.Percentile, - extra_options = { - 'ActivationSymmetric': True, - 'WeightSymmetric': True - } - ) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/googlenet/igie/requirements.txt b/models/cv/classification/googlenet/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/googlenet/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/googlenet/igie/scripts/infer_googlenet_fp16_accuracy.sh b/models/cv/classification/googlenet/igie/scripts/infer_googlenet_fp16_accuracy.sh index e546516f0260287d8f37410d676fed6e39865a3a..945acc1b27884c236226f6c47cd1ed58a29a23fa 100644 --- a/models/cv/classification/googlenet/igie/scripts/infer_googlenet_fp16_accuracy.sh +++ b/models/cv/classification/googlenet/igie/scripts/infer_googlenet_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="googlenet.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine googlenet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/googlenet/igie/scripts/infer_googlenet_fp16_performance.sh b/models/cv/classification/googlenet/igie/scripts/infer_googlenet_fp16_performance.sh index 54077315fae1326e2bf9666d45dce2f46e4d3556..0730255ff14122cbe87971780f76e6e50958c2fd 100644 --- a/models/cv/classification/googlenet/igie/scripts/infer_googlenet_fp16_performance.sh +++ b/models/cv/classification/googlenet/igie/scripts/infer_googlenet_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="googlenet.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine googlenet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/googlenet/igie/scripts/infer_googlenet_int8_accuracy.sh b/models/cv/classification/googlenet/igie/scripts/infer_googlenet_int8_accuracy.sh index f9be8585800f2638b609c1e6f1d1c31754383dfa..2f102e960c1791ef634f57970e6e2daba0cff665 100644 --- a/models/cv/classification/googlenet/igie/scripts/infer_googlenet_int8_accuracy.sh +++ b/models/cv/classification/googlenet/igie/scripts/infer_googlenet_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path googlenet_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine googlenet_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/googlenet/igie/scripts/infer_googlenet_int8_performance.sh b/models/cv/classification/googlenet/igie/scripts/infer_googlenet_int8_performance.sh index 064ab656eacc4ae8cb0bf59774eacb92fad203d3..5df60743106137c9af51f0ab1f56861647cd0f6c 100644 --- a/models/cv/classification/googlenet/igie/scripts/infer_googlenet_int8_performance.sh +++ b/models/cv/classification/googlenet/igie/scripts/infer_googlenet_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path googlenet_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine googlenet_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/hrnet_w18/igie/README.md b/models/cv/classification/hrnet_w18/igie/README.md index 7ae6a59fd85c724e6ad3a8625931b045ab5683ab..b27ad9d6bee383203241afe9c67f458fbf24ee9e 100644 --- a/models/cv/classification/hrnet_w18/igie/README.md +++ b/models/cv/classification/hrnet_w18/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/hrnet/hrnet-w18_4xb32_in1k.py --weight hrnet-w18_3rdparty_8xb32_in1k_20220120-0c10b180.pth --output hrnet_w18.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/hrnet/hrnet-w18_4xb32_in1k.py --weight hrnet-w18_3rdparty_8xb32_in1k_20220120-0c10b180.pth --output hrnet_w18.onnx # Use onnxsim optimize onnx model onnxsim hrnet_w18.onnx hrnet_w18_opt.onnx @@ -47,6 +48,7 @@ onnxsim hrnet_w18.onnx hrnet_w18_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/hrnet_w18/igie/build_engine.py b/models/cv/classification/hrnet_w18/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/hrnet_w18/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/hrnet_w18/igie/ci/prepare.sh b/models/cv/classification/hrnet_w18/igie/ci/prepare.sh index 65bfe18cad13d86d3eef75e65f90255d01569726..55010bdc4d7a92839237cb5912fa9dce492a851c 100644 --- a/models/cv/classification/hrnet_w18/igie/ci/prepare.sh +++ b/models/cv/classification/hrnet_w18/igie/ci/prepare.sh @@ -24,7 +24,8 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ -python3 export.py --cfg mmpretrain/configs/hrnet/hrnet-w18_4xb32_in1k.py --weight hrnet-w18_3rdparty_8xb32_in1k_20220120-0c10b180.pth --output hrnet_w18.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/hrnet/hrnet-w18_4xb32_in1k.py --weight hrnet-w18_3rdparty_8xb32_in1k_20220120-0c10b180.pth --output hrnet_w18.onnx onnxsim hrnet_w18.onnx hrnet_w18_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/hrnet_w18/igie/export.py b/models/cv/classification/hrnet_w18/igie/export.py deleted file mode 100644 index 7dc8d9fde531853bb9d25966dc7f706f2d9276dd..0000000000000000000000000000000000000000 --- a/models/cv/classification/hrnet_w18/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat) - out_head = self.model.head.fc(feat[0]) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/hrnet_w18/igie/inference.py b/models/cv/classification/hrnet_w18/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/hrnet_w18/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/hrnet_w18/igie/requirements.txt b/models/cv/classification/hrnet_w18/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/hrnet_w18/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/hrnet_w18/igie/scripts/infer_hrnet_w18_fp16_accuracy.sh b/models/cv/classification/hrnet_w18/igie/scripts/infer_hrnet_w18_fp16_accuracy.sh index 8d6849daf773be8918f98cbe35e414a0993b311f..2bd1d5ab8eb73617d6b54e29346dc44d02f39fc4 100644 --- a/models/cv/classification/hrnet_w18/igie/scripts/infer_hrnet_w18_fp16_accuracy.sh +++ b/models/cv/classification/hrnet_w18/igie/scripts/infer_hrnet_w18_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="hrnet_w18_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine hrnet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/hrnet_w18/igie/scripts/infer_hrnet_w18_fp16_performance.sh b/models/cv/classification/hrnet_w18/igie/scripts/infer_hrnet_w18_fp16_performance.sh index e6158cc746516f4433a2387f487cbd478d2dc780..1bf39bfbb810ed6f77733361906fd01c9f7ee76c 100644 --- a/models/cv/classification/hrnet_w18/igie/scripts/infer_hrnet_w18_fp16_performance.sh +++ b/models/cv/classification/hrnet_w18/igie/scripts/infer_hrnet_w18_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="hrnet_w18_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine hrnet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/alexnet/igie/build_engine.py b/models/cv/classification/igie_common/build_engine.py similarity index 100% rename from models/cv/classification/alexnet/igie/build_engine.py rename to models/cv/classification/igie_common/build_engine.py diff --git a/models/cv/classification/densenet161/igie/export.py b/models/cv/classification/igie_common/export.py similarity index 63% rename from models/cv/classification/densenet161/igie/export.py rename to models/cv/classification/igie_common/export.py index cb5466db48a519cb2267570bbfdb7c2e7009b018..7c4ff989d482bf3f309af04058cf83bcaa66e4e4 100644 --- a/models/cv/classification/densenet161/igie/export.py +++ b/models/cv/classification/igie_common/export.py @@ -21,6 +21,11 @@ import re def parse_args(): parser = argparse.ArgumentParser() + parser.add_argument("--model-name", + type=str, + required=True, + help="Name of the model from torchvision.models.") + parser.add_argument("--weight", type=str, required=True, @@ -36,19 +41,28 @@ def parse_args(): def main(): args = parse_args() - - model = torchvision.models.densenet161(weights=False) + print(f"Loading model: {args.model_name}...") + if args.model_name == "resnest50": + from resnest.torch import resnest50 + model = resnest50(pretrained=False) + else: + try: + model = getattr(torchvision.models, args.model_name)(pretrained=False) + except TypeError: + # Fallback for models that do not accept 'pretrained' parameter + model = getattr(torchvision.models, args.model_name)() state_dict = torch.load(args.weight) - pattern = re.compile(r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$' - ) - for key in list(state_dict.keys()): - res = pattern.match(key) - if res: - new_key = res.group(1) + res.group(2) - state_dict[new_key] = state_dict[key] - del state_dict[key] + if args.model_name in ["densenet201", "densenet161", "densenet169", "densenet121"]: + pattern = re.compile(r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$' + ) + for key in list(state_dict.keys()): + res = pattern.match(key) + if res: + new_key = res.group(1) + res.group(2) + state_dict[new_key] = state_dict[key] + del state_dict[key] model.load_state_dict(state_dict) model.eval() diff --git a/models/cv/classification/cspdarknet53/igie/export.py b/models/cv/classification/igie_common/export_mmcls.py similarity index 72% rename from models/cv/classification/cspdarknet53/igie/export.py rename to models/cv/classification/igie_common/export_mmcls.py index 7dc8d9fde531853bb9d25966dc7f706f2d9276dd..4b9e5bf79bc38ae65962e43aa0613c9dc7263a48 100644 --- a/models/cv/classification/cspdarknet53/igie/export.py +++ b/models/cv/classification/igie_common/export_mmcls.py @@ -24,8 +24,24 @@ class Model(torch.nn.Module): def forward(self, x): feat = self.model.backbone(x) - feat = self.model.neck(feat) - out_head = self.model.head.fc(feat[0]) + + if hasattr(self.model, 'neck') and callable(getattr(self.model, 'neck', None)): + feat = self.model.neck(feat) + + if hasattr(self.model.head, 'fc') and isinstance(self.model.head.fc, torch.nn.Module): + out_head = self.model.head.fc(feat[0]) + elif ( + hasattr(self.model.head, 'pre_logits') + and hasattr(self.model.head, 'layers') + and hasattr(self.model.head.layers, 'head') + ): + head = self.model.head.pre_logits(feat) + out_head = self.model.head.layers.head(head) + else: + raise NotImplementedError( + "当前模型 head 结构不被支持,请确保其包含 'fc' 或 'pre_logits + layers.head'" + ) + return out_head def parse_args(): @@ -60,6 +76,8 @@ def main(): output_names = ['output'] dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} dummy_input = torch.randn(1, 3, 224, 224) + if args.output == "mvitv2_base.onnx": + dummy_input = torch.randn(16, 3, 224, 224) torch.onnx.export( model, diff --git a/models/cv/classification/efficientnetv2_rw_t/igie/export.py b/models/cv/classification/igie_common/export_timm.py similarity index 85% rename from models/cv/classification/efficientnetv2_rw_t/igie/export.py rename to models/cv/classification/igie_common/export_timm.py index 3c3f579c02f06cec8be82f7ca1a797ecbab960c3..25e995a84c1195d2fae9aae6e4aa2d295733d0cc 100644 --- a/models/cv/classification/efficientnetv2_rw_t/igie/export.py +++ b/models/cv/classification/igie_common/export_timm.py @@ -20,6 +20,11 @@ import argparse def parse_args(): parser = argparse.ArgumentParser() + parser.add_argument("--model-name", + type=str, + required=True, + help="Name of the model.") + parser.add_argument("--weight", type=str, required=True, @@ -35,8 +40,9 @@ def parse_args(): def main(): args = parse_args() + print(f"Loading model: {args.model_name}...") - model = timm.create_model('efficientnetv2_rw_t', checkpoint_path=args.weight) + model = timm.create_model(args.model_name, checkpoint_path=args.weight) model.eval() dummy_input = torch.randn([32, 3, 288, 288]) diff --git a/models/cv/classification/alexnet/igie/inference.py b/models/cv/classification/igie_common/inference.py similarity index 100% rename from models/cv/classification/alexnet/igie/inference.py rename to models/cv/classification/igie_common/inference.py diff --git a/models/cv/classification/alexnet/igie/quantize.py b/models/cv/classification/igie_common/quantize.py similarity index 100% rename from models/cv/classification/alexnet/igie/quantize.py rename to models/cv/classification/igie_common/quantize.py diff --git a/models/cv/classification/alexnet/igie/requirements.txt b/models/cv/classification/igie_common/requirements.txt similarity index 100% rename from models/cv/classification/alexnet/igie/requirements.txt rename to models/cv/classification/igie_common/requirements.txt diff --git a/models/cv/classification/inception_v3/igie/README.md b/models/cv/classification/inception_v3/igie/README.md index c9e9ce2775e4a06b9addba4fcbb82f3111be0f62..a1c0fe407f5cb4ddb1097b912210a2a160a106cf 100644 --- a/models/cv/classification/inception_v3/igie/README.md +++ b/models/cv/classification/inception_v3/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight inception_v3_google-0cc3c7bd.pth --output inception_v3.onnx +python3 ../../igie_common/export.py --model-name inception_v3 --weight inception_v3_google-0cc3c7bd.pth --output inception_v3.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/inception_v3/igie/build_engine.py b/models/cv/classification/inception_v3/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/inception_v3/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/inception_v3/igie/ci/prepare.sh b/models/cv/classification/inception_v3/igie/ci/prepare.sh index 6214ee358de2ac2df62f998272888fe2c3f524b6..52aa92cc1ed181b3cd872fcdbe6fd3775d4a2488 100644 --- a/models/cv/classification/inception_v3/igie/ci/prepare.sh +++ b/models/cv/classification/inception_v3/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight inception_v3_google-0cc3c7bd.pth --output inception_v3.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name inception_v3 --weight inception_v3_google-0cc3c7bd.pth --output inception_v3.onnx \ No newline at end of file diff --git a/models/cv/classification/inception_v3/igie/export.py b/models/cv/classification/inception_v3/igie/export.py deleted file mode 100644 index 77ced0fd88fa097af7c4463578aec110b99cf37d..0000000000000000000000000000000000000000 --- a/models/cv/classification/inception_v3/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.inception_v3() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/inception_v3/igie/inference.py b/models/cv/classification/inception_v3/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/inception_v3/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/inception_v3/igie/quantize.py b/models/cv/classification/inception_v3/igie/quantize.py deleted file mode 100644 index 1cdeb93fd3d5bd126324524d330be916b709d380..0000000000000000000000000000000000000000 --- a/models/cv/classification/inception_v3/igie/quantize.py +++ /dev/null @@ -1,103 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 onnx -import psutil -import argparse -import numpy as np -from inference import get_dataloader -from onnxruntime.quantization import (CalibrationDataReader, QuantFormat, - quantize_static, QuantType, - CalibrationMethod) - -class CalibrationDataLoader(CalibrationDataReader): - def __init__(self, input_name, dataloader, cnt_limit=100): - self.cnt = 0 - self.input_name = input_name - self.cnt_limit = cnt_limit - self.iter = iter(dataloader) - - # avoid oom - @staticmethod - def _exceed_memory_upper_bound(upper_bound=80): - info = psutil.virtual_memory() - total_percent = info.percent - if total_percent >= upper_bound: - return True - return False - - def get_next(self): - if self._exceed_memory_upper_bound() or self.cnt >= self.cnt_limit: - return None - self.cnt += 1 - print(f"onnx calibration data count: {self.cnt}") - input_info = next(self.iter) - - ort_input = {k: np.array(v) for k, v in zip(self.input_name, input_info)} - return ort_input - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--out_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="calibration datasets path.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - model = onnx.load(args.model_path) - input_names = [input.name for input in model.graph.input] - - dataloader = get_dataloader(args.datasets, batch_size=1, num_workers=args.num_workers) - calibration = CalibrationDataLoader(input_names, dataloader, cnt_limit=20) - - quantize_static(args.model_path, - args.out_path, - calibration_data_reader=calibration, - quant_format=QuantFormat.QOperator, - per_channel=False, - activation_type=QuantType.QInt8, - weight_type=QuantType.QInt8, - use_external_data_format=False, - calibrate_method=CalibrationMethod.Percentile, - extra_options = { - 'ActivationSymmetric': True, - 'WeightSymmetric': True - } - ) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/inception_v3/igie/requirements.txt b/models/cv/classification/inception_v3/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/inception_v3/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_fp16_accuracy.sh b/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_fp16_accuracy.sh index 9598ad90be713a748026ec18376aa5e884347ff3..d20faeb38ffff90953999d92ecd2c0462563e36b 100644 --- a/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_fp16_accuracy.sh +++ b/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="inception_v3.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine inception_v3_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_fp16_performance.sh b/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_fp16_performance.sh index abf94b1d66b586b3810377bf8da216ffa152c4ab..9154fd25648b7ba6b2a2a01549aff783accbaa25 100644 --- a/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_fp16_performance.sh +++ b/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="inception_v3.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine inception_v3_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_int8_accuracy.sh b/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_int8_accuracy.sh index 444768faab520a1cfffeaf46683f697ff3d2b312..cb7ff46827c92c617485c01a208044adccf16871 100644 --- a/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_int8_accuracy.sh +++ b/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path inception_v3_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine inception_v3_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_int8_performance.sh b/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_int8_performance.sh index 3650229973f1434c45405ebcf055f5a3f3dc876d..736b18bb3465e1d6206ce8396901510474203953 100644 --- a/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_int8_performance.sh +++ b/models/cv/classification/inception_v3/igie/scripts/infer_inception_v3_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path inception_v3_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine inception_v3_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mlp_mixer_base/igie/README.md b/models/cv/classification/mlp_mixer_base/igie/README.md index 618116ba32b6a66dad290b0bb30af6eb644a3a47..de3fadf47577d6ca67b1b91724f01a69d52b53ca 100644 --- a/models/cv/classification/mlp_mixer_base/igie/README.md +++ b/models/cv/classification/mlp_mixer_base/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/mlp_mixer/mlp-mixer-base-p16_64xb64_in1k.py --weight mixer-base-p16_3rdparty_64xb64_in1k_20211124-1377e3e0.pth --output mlp_mixer_base.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/mlp_mixer/mlp-mixer-base-p16_64xb64_in1k.py --weight mixer-base-p16_3rdparty_64xb64_in1k_20211124-1377e3e0.pth --output mlp_mixer_base.onnx # Use onnxsim optimize onnx model onnxsim mlp_mixer_base.onnx mlp_mixer_base_opt.onnx @@ -48,6 +49,7 @@ onnxsim mlp_mixer_base.onnx mlp_mixer_base_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/mlp_mixer_base/igie/build_engine.py b/models/cv/classification/mlp_mixer_base/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/mlp_mixer_base/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mlp_mixer_base/igie/ci/prepare.sh b/models/cv/classification/mlp_mixer_base/igie/ci/prepare.sh index 79746d8e5476e0727554f9556ec8dea5e8d56cda..1e50e388d1dde5f6f7d25b9af807f7ce0dfb0263 100644 --- a/models/cv/classification/mlp_mixer_base/igie/ci/prepare.sh +++ b/models/cv/classification/mlp_mixer_base/igie/ci/prepare.sh @@ -25,10 +25,11 @@ else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ # export onnx model -python3 export.py --cfg mmpretrain/configs/mlp_mixer/mlp-mixer-base-p16_64xb64_in1k.py --weight mixer-base-p16_3rdparty_64xb64_in1k_20211124-1377e3e0.pth --output mlp_mixer_base.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/mlp_mixer/mlp-mixer-base-p16_64xb64_in1k.py --weight mixer-base-p16_3rdparty_64xb64_in1k_20211124-1377e3e0.pth --output mlp_mixer_base.onnx # Use onnxsim optimize onnx model onnxsim mlp_mixer_base.onnx mlp_mixer_base_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/mlp_mixer_base/igie/export.py b/models/cv/classification/mlp_mixer_base/igie/export.py deleted file mode 100644 index 6eafb7b164d52da54d010aeb1d92b838215c7702..0000000000000000000000000000000000000000 --- a/models/cv/classification/mlp_mixer_base/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat[0]) - out_head = self.model.head.fc(feat) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/mlp_mixer_base/igie/inference.py b/models/cv/classification/mlp_mixer_base/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/mlp_mixer_base/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/mlp_mixer_base/igie/requirements.txt b/models/cv/classification/mlp_mixer_base/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/mlp_mixer_base/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/mlp_mixer_base/igie/scripts/infer_mlp_mixer_base_fp16_accuracy.sh b/models/cv/classification/mlp_mixer_base/igie/scripts/infer_mlp_mixer_base_fp16_accuracy.sh index 448fe268f5c9cb05273181e27e42ca1d9736b5de..14b17e6ef613ba4b8a340ed9ae1f6136eb701ca6 100644 --- a/models/cv/classification/mlp_mixer_base/igie/scripts/infer_mlp_mixer_base_fp16_accuracy.sh +++ b/models/cv/classification/mlp_mixer_base/igie/scripts/infer_mlp_mixer_base_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="mlp_mixer_base_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mlp_mixer_base_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mlp_mixer_base/igie/scripts/infer_mlp_mixer_base_fp16_performance.sh b/models/cv/classification/mlp_mixer_base/igie/scripts/infer_mlp_mixer_base_fp16_performance.sh index c2c62600928826344befbc20b3557050052fe008..9bc0267ac8c9813e28e59789d88defdb33f19a97 100644 --- a/models/cv/classification/mlp_mixer_base/igie/scripts/infer_mlp_mixer_base_fp16_performance.sh +++ b/models/cv/classification/mlp_mixer_base/igie/scripts/infer_mlp_mixer_base_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="mlp_mixer_base_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mlp_mixer_base_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mnasnet0_5/igie/README.md b/models/cv/classification/mnasnet0_5/igie/README.md index da619b03fdbbe2a457293f78cb64e226bdaf4dfa..4847f2ce7e1f2f5315f16ddbf79720f3ac0295df 100644 --- a/models/cv/classification/mnasnet0_5/igie/README.md +++ b/models/cv/classification/mnasnet0_5/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight mnasnet0.5_top1_67.823-3ffadce67e.pth --output mnasnet0_5.onnx +python3 ../../igie_common/export.py --model-name mnasnet0_5 --weight mnasnet0.5_top1_67.823-3ffadce67e.pth --output mnasnet0_5.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/mnasnet0_5/igie/build_engine.py b/models/cv/classification/mnasnet0_5/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet0_5/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mnasnet0_5/igie/ci/prepare.sh b/models/cv/classification/mnasnet0_5/igie/ci/prepare.sh index b242e80ad5f71ac5197dab782c3e8b5346498644..020c8735d17cd5e02bbbb34bdcd6ea98f5fb04a9 100644 --- a/models/cv/classification/mnasnet0_5/igie/ci/prepare.sh +++ b/models/cv/classification/mnasnet0_5/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight mnasnet0.5_top1_67.823-3ffadce67e.pth --output mnasnet0_5.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name mnasnet0_5 --weight mnasnet0.5_top1_67.823-3ffadce67e.pth --output mnasnet0_5.onnx \ No newline at end of file diff --git a/models/cv/classification/mnasnet0_5/igie/export.py b/models/cv/classification/mnasnet0_5/igie/export.py deleted file mode 100644 index bd48e206871de1b3cc86f267e215d556b3bd3c6f..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet0_5/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.mnasnet0_5() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/mnasnet0_5/igie/inference.py b/models/cv/classification/mnasnet0_5/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet0_5/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mnasnet0_5/igie/requirements.txt b/models/cv/classification/mnasnet0_5/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet0_5/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/mnasnet0_5/igie/scripts/infer_mnasnet0_5_fp16_accuracy.sh b/models/cv/classification/mnasnet0_5/igie/scripts/infer_mnasnet0_5_fp16_accuracy.sh index a1c3b37a6198e500753759a7b437fb8c84399239..29da5778294e0be9b49800b0a90816ce8b07eba5 100644 --- a/models/cv/classification/mnasnet0_5/igie/scripts/infer_mnasnet0_5_fp16_accuracy.sh +++ b/models/cv/classification/mnasnet0_5/igie/scripts/infer_mnasnet0_5_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="mnasnet0_5.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mnasnet0_5_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mnasnet0_5/igie/scripts/infer_mnasnet0_5_fp16_performance.sh b/models/cv/classification/mnasnet0_5/igie/scripts/infer_mnasnet0_5_fp16_performance.sh index 89271d3380b9988acb3374b5ffaaa691b0496b3a..68ba295769238aede8b57a089c7f982bb3a96ecf 100644 --- a/models/cv/classification/mnasnet0_5/igie/scripts/infer_mnasnet0_5_fp16_performance.sh +++ b/models/cv/classification/mnasnet0_5/igie/scripts/infer_mnasnet0_5_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="mnasnet0_5.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mnasnet0_5_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mnasnet0_75/igie/README.md b/models/cv/classification/mnasnet0_75/igie/README.md index 29cf14fb04cec2122c617f22a240e55eb0a0b608..12bf5601fb392106255b4364e8bc88d8fd738466 100644 --- a/models/cv/classification/mnasnet0_75/igie/README.md +++ b/models/cv/classification/mnasnet0_75/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight mnasnet0_75-7090bc5f.pth --output mnasnet0_75.onnx +python3 ../../igie_common/export.py --model-name mnasnet0_75 --weight mnasnet0_75-7090bc5f.pth --output mnasnet0_75.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/mnasnet0_75/igie/build_engine.py b/models/cv/classification/mnasnet0_75/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet0_75/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mnasnet0_75/igie/ci/prepare.sh b/models/cv/classification/mnasnet0_75/igie/ci/prepare.sh index 96e6e152aecce2edb247442bd4743d38b0a8b861..ffdaed97443a33986787ab41705a7ee4ba07b197 100644 --- a/models/cv/classification/mnasnet0_75/igie/ci/prepare.sh +++ b/models/cv/classification/mnasnet0_75/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight mnasnet0_75-7090bc5f.pth --output mnasnet0_75.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name mnasnet0_75 --weight mnasnet0_75-7090bc5f.pth --output mnasnet0_75.onnx \ No newline at end of file diff --git a/models/cv/classification/mnasnet0_75/igie/export.py b/models/cv/classification/mnasnet0_75/igie/export.py deleted file mode 100644 index e1b69e4d5fab722ca5209115947d6616e408345c..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet0_75/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.mnasnet0_75() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/mnasnet0_75/igie/inference.py b/models/cv/classification/mnasnet0_75/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet0_75/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mnasnet0_75/igie/requirements.txt b/models/cv/classification/mnasnet0_75/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet0_75/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/mnasnet0_75/igie/scripts/infer_mnasnet0_75_fp16_accuracy.sh b/models/cv/classification/mnasnet0_75/igie/scripts/infer_mnasnet0_75_fp16_accuracy.sh index 9c5fb76d5f7d2ece46f6a49688423b8e0c96adab..2814192ec2d68df4dffbda433647cc107d608575 100644 --- a/models/cv/classification/mnasnet0_75/igie/scripts/infer_mnasnet0_75_fp16_accuracy.sh +++ b/models/cv/classification/mnasnet0_75/igie/scripts/infer_mnasnet0_75_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="mnasnet0_75.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mnasnet0_75_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mnasnet0_75/igie/scripts/infer_mnasnet0_75_fp16_performance.sh b/models/cv/classification/mnasnet0_75/igie/scripts/infer_mnasnet0_75_fp16_performance.sh index 014ac67a3f576d3685800eb44a0c0eb10169d907..c8b6a2b4730c8d3295588914e445f33d33c4377b 100644 --- a/models/cv/classification/mnasnet0_75/igie/scripts/infer_mnasnet0_75_fp16_performance.sh +++ b/models/cv/classification/mnasnet0_75/igie/scripts/infer_mnasnet0_75_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="mnasnet0_75.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mnasnet0_75_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mnasnet1_0/igie/README.md b/models/cv/classification/mnasnet1_0/igie/README.md index f4d0e339becdfa980ee1ebde7a7da438aa96b46d..cde6a03332c8df5ced8bb9d414d0e0a548ded5d7 100644 --- a/models/cv/classification/mnasnet1_0/igie/README.md +++ b/models/cv/classification/mnasnet1_0/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight mnasnet1.0_top1_73.512-f206786ef8.pth --output mnasnet1_0.onnx +python3 ../../igie_common/export.py --model-name mnasnet1_0 --weight mnasnet1.0_top1_73.512-f206786ef8.pth --output mnasnet1_0.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/mnasnet1_0/igie/build_engine.py b/models/cv/classification/mnasnet1_0/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet1_0/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mnasnet1_0/igie/ci/prepare.sh b/models/cv/classification/mnasnet1_0/igie/ci/prepare.sh index a78b6fedcb81002958706e0a5f7138359b1ec414..8c5519aaf9eba89254050898ae1945e68460ba87 100644 --- a/models/cv/classification/mnasnet1_0/igie/ci/prepare.sh +++ b/models/cv/classification/mnasnet1_0/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight mnasnet1.0_top1_73.512-f206786ef8.pth --output mnasnet1_0.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name mnasnet1_0 --weight mnasnet1.0_top1_73.512-f206786ef8.pth --output mnasnet1_0.onnx \ No newline at end of file diff --git a/models/cv/classification/mnasnet1_0/igie/export.py b/models/cv/classification/mnasnet1_0/igie/export.py deleted file mode 100644 index 9dbcdd4e545956d70ab6dfef7e917aec667a5b65..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet1_0/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.mnasnet1_0() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/mnasnet1_0/igie/inference.py b/models/cv/classification/mnasnet1_0/igie/inference.py deleted file mode 100644 index 0563f72e11c44bceb5acf127981e225746aa9155..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet1_0/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mnasnet1_0/igie/requirements.txt b/models/cv/classification/mnasnet1_0/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/mnasnet1_0/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/mnasnet1_0/igie/scripts/infer_mnasnet1_0_fp16_accuracy.sh b/models/cv/classification/mnasnet1_0/igie/scripts/infer_mnasnet1_0_fp16_accuracy.sh index 33e04e8f58d81966adabb519f4c6cdbc97e7582c..7fc5a71b9e28a043681472ed4b642998684e9c71 100644 --- a/models/cv/classification/mnasnet1_0/igie/scripts/infer_mnasnet1_0_fp16_accuracy.sh +++ b/models/cv/classification/mnasnet1_0/igie/scripts/infer_mnasnet1_0_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="mnasnet1_0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mnasnet1_0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mnasnet1_0/igie/scripts/infer_mnasnet1_0_fp16_performance.sh b/models/cv/classification/mnasnet1_0/igie/scripts/infer_mnasnet1_0_fp16_performance.sh index 5fbdcde9ddee7e68226fdb57035e604efb130a76..e8800ae0d352495bea82870df1c3620907f735a1 100644 --- a/models/cv/classification/mnasnet1_0/igie/scripts/infer_mnasnet1_0_fp16_performance.sh +++ b/models/cv/classification/mnasnet1_0/igie/scripts/infer_mnasnet1_0_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="mnasnet1_0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mnasnet1_0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mobilenet_v2/igie/README.md b/models/cv/classification/mobilenet_v2/igie/README.md index 4ef43425ec34c5e7d29a1f9cc83a6bc0be32a770..be32dd57dabeb531cbf46cddb08ed8d54642fea4 100644 --- a/models/cv/classification/mobilenet_v2/igie/README.md +++ b/models/cv/classification/mobilenet_v2/igie/README.md @@ -21,19 +21,21 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install onnxruntime-gpu==1.18.0 ``` ### Model Conversion ```bash -python3 export.py --weight mobilenet_v2-7ebf99e0.pth --output mobilenet_v2.onnx +python3 ../../igie_common/export.py --model-name mobilenet_v2 --weight mobilenet_v2-7ebf99e0.pth --output mobilenet_v2.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/mobilenet_v2/igie/build_engine.py b/models/cv/classification/mobilenet_v2/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v2/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v2/igie/ci/prepare.sh b/models/cv/classification/mobilenet_v2/igie/ci/prepare.sh index 736be770d85ee5ef5d98c571d32e1046c71f101d..74d130ecdfcbb62c92fdeb9f483b01974f6d05c7 100644 --- a/models/cv/classification/mobilenet_v2/igie/ci/prepare.sh +++ b/models/cv/classification/mobilenet_v2/igie/ci/prepare.sh @@ -16,5 +16,6 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight mobilenet_v2-7ebf99e0.pth --output mobilenet_v2.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +pip3 install onnxruntime-gpu==1.18.0 +python3 ../../igie_common/export.py --model-name mobilenet_v2 --weight mobilenet_v2-7ebf99e0.pth --output mobilenet_v2.onnx diff --git a/models/cv/classification/mobilenet_v2/igie/export.py b/models/cv/classification/mobilenet_v2/igie/export.py deleted file mode 100644 index 8cf121cc41b804f87f3f18a7d34dbd44303c63c9..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v2/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.mobilenet_v2() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/mobilenet_v2/igie/inference.py b/models/cv/classification/mobilenet_v2/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v2/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v2/igie/quantize.py b/models/cv/classification/mobilenet_v2/igie/quantize.py deleted file mode 100644 index c3311ad08ce5bc82684bdbf52c8bf81c82fd3589..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v2/igie/quantize.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 onnx -import psutil -import argparse -import numpy as np -from inference import get_dataloader -from onnxruntime.quantization import (CalibrationDataReader, QuantFormat, - quantize_static, QuantType, - CalibrationMethod) - -class CalibrationDataLoader(CalibrationDataReader): - def __init__(self, input_name, dataloader, cnt_limit=100): - self.cnt = 0 - self.input_name = input_name - self.cnt_limit = cnt_limit - self.iter = iter(dataloader) - - # avoid oom - @staticmethod - def _exceed_memory_upper_bound(upper_bound=80): - info = psutil.virtual_memory() - total_percent = info.percent - if total_percent >= upper_bound: - return True - return False - - def get_next(self): - if self._exceed_memory_upper_bound() or self.cnt >= self.cnt_limit: - return None - self.cnt += 1 - print(f"onnx calibration data count: {self.cnt}") - input_info = next(self.iter) - - ort_input = {k: np.array(v) for k, v in zip(self.input_name, input_info)} - return ort_input - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--out_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="calibration datasets path.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - model = onnx.load(args.model_path) - input_names = [input.name for input in model.graph.input] - - dataloader = get_dataloader(args.datasets, batch_size=1, num_workers=args.num_workers) - calibration = CalibrationDataLoader(input_names, dataloader, cnt_limit=10) - - quantize_static(args.model_path, - args.out_path, - calibration_data_reader=calibration, - quant_format=QuantFormat.QOperator, - per_channel=True, - activation_type=QuantType.QInt8, - weight_type=QuantType.QInt8, - use_external_data_format=False, - nodes_to_exclude=['/classifier/classifier.1/Gemm'], - calibrate_method=CalibrationMethod.Percentile, - extra_options = { - 'ActivationSymmetric': True, - 'WeightSymmetric': True - } - ) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v2/igie/requirements.txt b/models/cv/classification/mobilenet_v2/igie/requirements.txt deleted file mode 100644 index 08a0a972e3bf75e58f513f312b12d56d7a38d3b6..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v2/igie/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -onnx -tqdm -onnxruntime-gpu==1.18.0 \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_fp16_accuracy.sh b/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_fp16_accuracy.sh index 24547ddf6e21f858f9a3f9374832693f9286ae78..3b52593eb109e2734e6cdcb1517aff0969e2586b 100644 --- a/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_fp16_accuracy.sh +++ b/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="mobilenet_v2.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mobilenetv2_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_fp16_performance.sh b/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_fp16_performance.sh index 30b0b9ad54999961ebc5c6c0b7e103a3c5c8a2ac..d788d740589b4982f718f7044b2c7bc825a78276 100644 --- a/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_fp16_performance.sh +++ b/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="mobilenet_v2.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mobilenetv2_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_int8_accuracy.sh b/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_int8_accuracy.sh index e7f81cfda690f8e2e0196b6bb8e6e50c37a72263..4ff0d535294ca8f7c0b0e6fb45d2d6d479697644 100644 --- a/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_int8_accuracy.sh +++ b/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path mobilenetv2_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mobilenetv2_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_int8_performance.sh b/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_int8_performance.sh index 5c58794ab0eb9f35035d4e6395b02a52988313dd..c6900b1d4068f1647583f21e7be112df71c0265a 100644 --- a/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_int8_performance.sh +++ b/models/cv/classification/mobilenet_v2/igie/scripts/infer_mobilenet_v2_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path mobilenetv2_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mobilenetv2_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mobilenet_v3/igie/README.md b/models/cv/classification/mobilenet_v3/igie/README.md index 81a2c05f32351f026add5b29695ee4e9ea9a0784..9e7ebb8719b164cef28ba1c920ed9e67e88320d4 100644 --- a/models/cv/classification/mobilenet_v3/igie/README.md +++ b/models/cv/classification/mobilenet_v3/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight mobilenet_v3_small-047dcff4.pth --output mobilenetv3_small.onnx +python3 ../../igie_common/export.py --model-name mobilenet_v3_small --weight mobilenet_v3_small-047dcff4.pth --output mobilenetv3_small.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/mobilenet_v3/igie/build_engine.py b/models/cv/classification/mobilenet_v3/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v3/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v3/igie/ci/prepare.sh b/models/cv/classification/mobilenet_v3/igie/ci/prepare.sh index 8e30886cb372a477e976d5465b920ab76b0dfeb5..c597a33dc9043813f77726faa2869291c09ea5ed 100644 --- a/models/cv/classification/mobilenet_v3/igie/ci/prepare.sh +++ b/models/cv/classification/mobilenet_v3/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight mobilenet_v3_small-047dcff4.pth --output mobilenetv3_small.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name mobilenet_v3_small --weight mobilenet_v3_small-047dcff4.pth --output mobilenetv3_small.onnx \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v3/igie/export.py b/models/cv/classification/mobilenet_v3/igie/export.py deleted file mode 100644 index e02485e45eb22a8431f76508e81dca2b21e2a016..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v3/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.mobilenet_v3_small() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/mobilenet_v3/igie/inference.py b/models/cv/classification/mobilenet_v3/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v3/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v3/igie/requirements.txt b/models/cv/classification/mobilenet_v3/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v3/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/mobilenet_v3/igie/scripts/infer_mobilenet_v3_fp16_accuracy.sh b/models/cv/classification/mobilenet_v3/igie/scripts/infer_mobilenet_v3_fp16_accuracy.sh index 95a264809a3598137a163562c0f1f24bb5a89ad4..55146d89278c740b4a011f0c0c308d535ece1454 100644 --- a/models/cv/classification/mobilenet_v3/igie/scripts/infer_mobilenet_v3_fp16_accuracy.sh +++ b/models/cv/classification/mobilenet_v3/igie/scripts/infer_mobilenet_v3_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="mobilenetv3_small.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mobilenetv3_small_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mobilenet_v3/igie/scripts/infer_mobilenet_v3_fp16_performance.sh b/models/cv/classification/mobilenet_v3/igie/scripts/infer_mobilenet_v3_fp16_performance.sh index 05b9e1ec5363ce4d9ef9d9228572e368a9c69b13..7ba4d96320d0c695edd14154e978268b96af3476 100644 --- a/models/cv/classification/mobilenet_v3/igie/scripts/infer_mobilenet_v3_fp16_performance.sh +++ b/models/cv/classification/mobilenet_v3/igie/scripts/infer_mobilenet_v3_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="mobilenetv3_small.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mobilenetv3_small_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mobilenet_v3_large/igie/README.md b/models/cv/classification/mobilenet_v3_large/igie/README.md index 1d71e1ff7952aad05c0df78bc075d1c247380637..365d5297f8ca4fc7724a6016d9b37554ec57f199 100644 --- a/models/cv/classification/mobilenet_v3_large/igie/README.md +++ b/models/cv/classification/mobilenet_v3_large/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight mobilenet_v3_large-8738ca79.pth --output mobilenetv3_large.onnx +python3 ../../igie_common/export.py --model-name mobilenet_v3_large --weight mobilenet_v3_large-8738ca79.pth --output mobilenetv3_large.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/mobilenet_v3_large/igie/build_engine.py b/models/cv/classification/mobilenet_v3_large/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v3_large/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v3_large/igie/ci/prepare.sh b/models/cv/classification/mobilenet_v3_large/igie/ci/prepare.sh index 76d67fd8aa7320ee198e4cc83ef79c41106b9bbc..3584dfe165c3e59b0076d4091ac7c21a850d3d31 100644 --- a/models/cv/classification/mobilenet_v3_large/igie/ci/prepare.sh +++ b/models/cv/classification/mobilenet_v3_large/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight mobilenet_v3_large-8738ca79.pth --output mobilenetv3_large.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name mobilenet_v3_large --weight mobilenet_v3_large-8738ca79.pth --output mobilenetv3_large.onnx \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v3_large/igie/export.py b/models/cv/classification/mobilenet_v3_large/igie/export.py deleted file mode 100644 index 1e6c82ea868a1858f6d9dfe43cb37e26982aaebe..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v3_large/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.mobilenet_v3_large() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/mobilenet_v3_large/igie/inference.py b/models/cv/classification/mobilenet_v3_large/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v3_large/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mobilenet_v3_large/igie/requirements.txt b/models/cv/classification/mobilenet_v3_large/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/mobilenet_v3_large/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/mobilenet_v3_large/igie/scripts/infer_mobilenet_v3_large_fp16_accuracy.sh b/models/cv/classification/mobilenet_v3_large/igie/scripts/infer_mobilenet_v3_large_fp16_accuracy.sh index dff18e9b923d6b298acec748717b9272815fe7c7..5a20e7dc81959f3f75c45f8a6783fd1d3cc5eb2b 100644 --- a/models/cv/classification/mobilenet_v3_large/igie/scripts/infer_mobilenet_v3_large_fp16_accuracy.sh +++ b/models/cv/classification/mobilenet_v3_large/igie/scripts/infer_mobilenet_v3_large_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="mobilenetv3_large.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mobilenetv3_large_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mobilenet_v3_large/igie/scripts/infer_mobilenet_v3_large_fp16_performance.sh b/models/cv/classification/mobilenet_v3_large/igie/scripts/infer_mobilenet_v3_large_fp16_performance.sh index 49380c0a7d3534bd9e8bfac9cb69e2e20a238cad..503c71b89c68f3fb2a92e925349a8f2e9c4b5c58 100644 --- a/models/cv/classification/mobilenet_v3_large/igie/scripts/infer_mobilenet_v3_large_fp16_performance.sh +++ b/models/cv/classification/mobilenet_v3_large/igie/scripts/infer_mobilenet_v3_large_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="mobilenetv3_large.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mobilenetv3_large_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mvitv2_base/igie/README.md b/models/cv/classification/mvitv2_base/igie/README.md index 433d07652442c0750eaedbe3ea0add15e32440eb..75d1621235ac6712ad23c2814fb1563a5e68f0e9 100644 --- a/models/cv/classification/mvitv2_base/igie/README.md +++ b/models/cv/classification/mvitv2_base/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/mvit/mvitv2-base_8xb256_in1k.py --weight mvitv2-base_3rdparty_in1k_20220722-9c4f0a17.pth --output mvitv2_base.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/mvit/mvitv2-base_8xb256_in1k.py --weight mvitv2-base_3rdparty_in1k_20220722-9c4f0a17.pth --output mvitv2_base.onnx # Use onnxsim optimize onnx model onnxsim mvitv2_base.onnx mvitv2_base_opt.onnx @@ -48,6 +49,7 @@ onnxsim mvitv2_base.onnx mvitv2_base_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/mvitv2_base/igie/build_engine.py b/models/cv/classification/mvitv2_base/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/mvitv2_base/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/mvitv2_base/igie/ci/prepare.sh b/models/cv/classification/mvitv2_base/igie/ci/prepare.sh index 747bee51a69f51ee228ab6efd71c4ad276ad6e52..e073e9c7d8b501c5301ae85928d8eb4b6b042656 100644 --- a/models/cv/classification/mvitv2_base/igie/ci/prepare.sh +++ b/models/cv/classification/mvitv2_base/igie/ci/prepare.sh @@ -24,7 +24,8 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ -python3 export.py --cfg mmpretrain/configs/mvit/mvitv2-base_8xb256_in1k.py --weight mvitv2-base_3rdparty_in1k_20220722-9c4f0a17.pth --output mvitv2_base.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/mvit/mvitv2-base_8xb256_in1k.py --weight mvitv2-base_3rdparty_in1k_20220722-9c4f0a17.pth --output mvitv2_base.onnx onnxsim mvitv2_base.onnx mvitv2_base_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/mvitv2_base/igie/export.py b/models/cv/classification/mvitv2_base/igie/export.py deleted file mode 100644 index d78b898b57ab44fe968882f2beb27e5d655c509f..0000000000000000000000000000000000000000 --- a/models/cv/classification/mvitv2_base/igie/export.py +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - head = self.model.simple_test(x) - return head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dummy_input = torch.randn(16, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/mvitv2_base/igie/inference.py b/models/cv/classification/mvitv2_base/igie/inference.py deleted file mode 100644 index c42cf871274e316f7175ea3ad77c5468d2c9b936..0000000000000000000000000000000000000000 --- a/models/cv/classification/mvitv2_base/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/mvitv2_base/igie/requirements.txt b/models/cv/classification/mvitv2_base/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/mvitv2_base/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/mvitv2_base/igie/scripts/infer_mvitv2_base_fp16_accuracy.sh b/models/cv/classification/mvitv2_base/igie/scripts/infer_mvitv2_base_fp16_accuracy.sh index 85f66d502c91b6d1b5d4a371576dd6ece1167067..b32b237c0bd8d1cff65cb8c5dc606da3ee9ba9b2 100644 --- a/models/cv/classification/mvitv2_base/igie/scripts/infer_mvitv2_base_fp16_accuracy.sh +++ b/models/cv/classification/mvitv2_base/igie/scripts/infer_mvitv2_base_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="mvitv2_base_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mvitv2_base_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/mvitv2_base/igie/scripts/infer_mvitv2_base_fp16_performance.sh b/models/cv/classification/mvitv2_base/igie/scripts/infer_mvitv2_base_fp16_performance.sh index d54dac50ec287023085ef286b9642d1b4512b42e..a37b1285835fa109d35d5b466c24f7d78937ebf9 100644 --- a/models/cv/classification/mvitv2_base/igie/scripts/infer_mvitv2_base_fp16_performance.sh +++ b/models/cv/classification/mvitv2_base/igie/scripts/infer_mvitv2_base_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="mvitv2_base_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine mvitv2_base_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_x_16gf/igie/README.md b/models/cv/classification/regnet_x_16gf/igie/README.md index 9eb5779597b01bdd583f52525797c513cdc2d5af..f36c4241e67ee01881c88d30fc3fce28536558ae 100644 --- a/models/cv/classification/regnet_x_16gf/igie/README.md +++ b/models/cv/classification/regnet_x_16gf/igie/README.md @@ -22,19 +22,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight regnet_x_16gf-2007eb11.pth --output regnet_x_16gf.onnx +python3 ../../igie_common/export.py --model-name regnet_x_16gf --weight regnet_x_16gf-2007eb11.pth --output regnet_x_16gf.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/regnet_x_16gf/igie/build_engine.py b/models/cv/classification/regnet_x_16gf/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_16gf/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_x_16gf/igie/ci/prepare.sh b/models/cv/classification/regnet_x_16gf/igie/ci/prepare.sh index 385a3ae84c210e8064fa3de10a8bef2066a99709..adefc57b79efa5b36fb02510231a63404af73b38 100644 --- a/models/cv/classification/regnet_x_16gf/igie/ci/prepare.sh +++ b/models/cv/classification/regnet_x_16gf/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight regnet_x_16gf-2007eb11.pth --output regnet_x_16gf.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name regnet_x_16gf --weight regnet_x_16gf-2007eb11.pth --output regnet_x_16gf.onnx \ No newline at end of file diff --git a/models/cv/classification/regnet_x_16gf/igie/export.py b/models/cv/classification/regnet_x_16gf/igie/export.py deleted file mode 100644 index 7807f0c6ce2c20b948357e47ee9351922eca9070..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_16gf/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.regnet_x_16gf() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/regnet_x_16gf/igie/inference.py b/models/cv/classification/regnet_x_16gf/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_16gf/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_x_16gf/igie/requirements.txt b/models/cv/classification/regnet_x_16gf/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_16gf/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/regnet_x_16gf/igie/scripts/infer_regnet_x_16gf_fp16_accuracy.sh b/models/cv/classification/regnet_x_16gf/igie/scripts/infer_regnet_x_16gf_fp16_accuracy.sh index 5df1457c16ba2513c5843e3da8a7f807c9a795c3..badd00394b651a8da2f2d8fb60700c0734e39a7a 100644 --- a/models/cv/classification/regnet_x_16gf/igie/scripts/infer_regnet_x_16gf_fp16_accuracy.sh +++ b/models/cv/classification/regnet_x_16gf/igie/scripts/infer_regnet_x_16gf_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="regnet_x_16gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_x_16gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_x_16gf/igie/scripts/infer_regnet_x_16gf_fp16_performance.sh b/models/cv/classification/regnet_x_16gf/igie/scripts/infer_regnet_x_16gf_fp16_performance.sh index f6b4f481d79010e6018c3f25ae4823642383dacf..f9e681cb140494ed42f9505d08fdc8dceabd95d2 100644 --- a/models/cv/classification/regnet_x_16gf/igie/scripts/infer_regnet_x_16gf_fp16_performance.sh +++ b/models/cv/classification/regnet_x_16gf/igie/scripts/infer_regnet_x_16gf_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="regnet_x_16gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_x_16gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_x_1_6gf/igie/README.md b/models/cv/classification/regnet_x_1_6gf/igie/README.md index f2ff1a2c8c10d152c04ea81181b2db5ed8875513..1c2bbf496e4a74727e0ea63e70013bc81eb40e5d 100644 --- a/models/cv/classification/regnet_x_1_6gf/igie/README.md +++ b/models/cv/classification/regnet_x_1_6gf/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight regnet_x_1_6gf-a12f2b72.pth --output regnet_x_1_6gf.onnx +python3 ../../igie_common/export.py --model-name regnet_x_1_6gf --weight regnet_x_1_6gf-a12f2b72.pth --output regnet_x_1_6gf.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/regnet_x_1_6gf/igie/build_engine.py b/models/cv/classification/regnet_x_1_6gf/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_1_6gf/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_x_1_6gf/igie/ci/prepare.sh b/models/cv/classification/regnet_x_1_6gf/igie/ci/prepare.sh index 3e54150d64a0350eae60df80cd5cad85f0ad477e..b5cfc4ffbcf5257eacaa6a3f29ba7b9bef12565c 100644 --- a/models/cv/classification/regnet_x_1_6gf/igie/ci/prepare.sh +++ b/models/cv/classification/regnet_x_1_6gf/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight regnet_x_1_6gf-a12f2b72.pth --output regnet_x_1_6gf.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name regnet_x_1_6gf --weight regnet_x_1_6gf-a12f2b72.pth --output regnet_x_1_6gf.onnx \ No newline at end of file diff --git a/models/cv/classification/regnet_x_1_6gf/igie/export.py b/models/cv/classification/regnet_x_1_6gf/igie/export.py deleted file mode 100644 index 3e60b43abd193c32943ce659ffdbd390612d2446..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_1_6gf/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.regnet_x_1_6gf() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/regnet_x_1_6gf/igie/inference.py b/models/cv/classification/regnet_x_1_6gf/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_1_6gf/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_x_1_6gf/igie/requirements.txt b/models/cv/classification/regnet_x_1_6gf/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_1_6gf/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/regnet_x_1_6gf/igie/scripts/infer_regnet_x_1_6gf_fp16_accuracy.sh b/models/cv/classification/regnet_x_1_6gf/igie/scripts/infer_regnet_x_1_6gf_fp16_accuracy.sh index 4dda13dc4798953febb50feee706ad05bda39381..427665f918b23c75fdc5e0f7327a9e8b80d47c15 100644 --- a/models/cv/classification/regnet_x_1_6gf/igie/scripts/infer_regnet_x_1_6gf_fp16_accuracy.sh +++ b/models/cv/classification/regnet_x_1_6gf/igie/scripts/infer_regnet_x_1_6gf_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="regnet_x_1_6gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_x_1_6gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_x_1_6gf/igie/scripts/infer_regnet_x_1_6gf_fp16_performance.sh b/models/cv/classification/regnet_x_1_6gf/igie/scripts/infer_regnet_x_1_6gf_fp16_performance.sh index 2fa72b647ac1676845dda577ac8921caea03247a..9668fe1d59d2c9a01d13684ae993a26c62b4e959 100644 --- a/models/cv/classification/regnet_x_1_6gf/igie/scripts/infer_regnet_x_1_6gf_fp16_performance.sh +++ b/models/cv/classification/regnet_x_1_6gf/igie/scripts/infer_regnet_x_1_6gf_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="regnet_x_1_6gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_x_1_6gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_x_3_2gf/igie/README.md b/models/cv/classification/regnet_x_3_2gf/igie/README.md index 991b23f1a596d906387400eb734450a55b0559e9..89433371df9361279b879b7c65fd479d7b369b9e 100644 --- a/models/cv/classification/regnet_x_3_2gf/igie/README.md +++ b/models/cv/classification/regnet_x_3_2gf/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight regnet_x_3_2gf-f342aeae.pth --output regnet_x_3_2gf.onnx +python3 ../../igie_common/export.py --model-name regnet_x_3_2gf --weight regnet_x_3_2gf-f342aeae.pth --output regnet_x_3_2gf.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/regnet_x_3_2gf/igie/build_engine.py b/models/cv/classification/regnet_x_3_2gf/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_3_2gf/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_x_3_2gf/igie/ci/prepare.sh b/models/cv/classification/regnet_x_3_2gf/igie/ci/prepare.sh index 78f28f45ddc384eb978f15a44e0d1e6ecad730e1..13623e8265c9c666add62da52815c10b6ce17756 100644 --- a/models/cv/classification/regnet_x_3_2gf/igie/ci/prepare.sh +++ b/models/cv/classification/regnet_x_3_2gf/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight regnet_x_3_2gf-f342aeae.pth --output regnet_x_3_2gf.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name regnet_x_3_2gf --weight regnet_x_3_2gf-f342aeae.pth --output regnet_x_3_2gf.onnx \ No newline at end of file diff --git a/models/cv/classification/regnet_x_3_2gf/igie/export.py b/models/cv/classification/regnet_x_3_2gf/igie/export.py deleted file mode 100644 index c6e2019f34c1842c0344bf1d12dbc3eaf0d53e23..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_3_2gf/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.regnet_x_3_2gf() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/regnet_x_3_2gf/igie/inference.py b/models/cv/classification/regnet_x_3_2gf/igie/inference.py deleted file mode 100644 index 0563f72e11c44bceb5acf127981e225746aa9155..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_3_2gf/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_x_3_2gf/igie/requirements.txt b/models/cv/classification/regnet_x_3_2gf/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_x_3_2gf/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/regnet_x_3_2gf/igie/scripts/infer_regnet_x_3_2gf_fp16_accuracy.sh b/models/cv/classification/regnet_x_3_2gf/igie/scripts/infer_regnet_x_3_2gf_fp16_accuracy.sh index c68d181ce590e226b767ca1f9cc3d5d56119876a..1f88a01296c87c78d1825c2e4acad38bb3ed2382 100644 --- a/models/cv/classification/regnet_x_3_2gf/igie/scripts/infer_regnet_x_3_2gf_fp16_accuracy.sh +++ b/models/cv/classification/regnet_x_3_2gf/igie/scripts/infer_regnet_x_3_2gf_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="regnet_x_3_2gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_x_3_2gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_x_3_2gf/igie/scripts/infer_regnet_x_3_2gf_fp16_performance.sh b/models/cv/classification/regnet_x_3_2gf/igie/scripts/infer_regnet_x_3_2gf_fp16_performance.sh index 0e1fc7c4c189121fd47559268de574e0b5edfcdd..b2ca03fe2dbde6f73e88e54d6f98e220efddc560 100644 --- a/models/cv/classification/regnet_x_3_2gf/igie/scripts/infer_regnet_x_3_2gf_fp16_performance.sh +++ b/models/cv/classification/regnet_x_3_2gf/igie/scripts/infer_regnet_x_3_2gf_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="regnet_x_3_2gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_x_3_2gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_y_16gf/igie/README.md b/models/cv/classification/regnet_y_16gf/igie/README.md index 2ec70dd186fbe648bda7e936f34d8659496982b6..b8486e5592b240c8517ecd1c52a5041ca03a1750 100644 --- a/models/cv/classification/regnet_y_16gf/igie/README.md +++ b/models/cv/classification/regnet_y_16gf/igie/README.md @@ -22,19 +22,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight regnet_y_16gf-9e6ed7dd.pth --output regnet_y_16gf.onnx +python3 ../../igie_common/export.py --model-name regnet_y_16gf --weight regnet_y_16gf-9e6ed7dd.pth --output regnet_y_16gf.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/regnet_y_16gf/igie/build_engine.py b/models/cv/classification/regnet_y_16gf/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_y_16gf/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_y_16gf/igie/ci/prepare.sh b/models/cv/classification/regnet_y_16gf/igie/ci/prepare.sh index 8fc7ac11cf511111514cc227dd2db8ef49e7967a..fbdbe04c3c6d18d5b039adeaa263d6e8adf9806c 100644 --- a/models/cv/classification/regnet_y_16gf/igie/ci/prepare.sh +++ b/models/cv/classification/regnet_y_16gf/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight regnet_y_16gf-9e6ed7dd.pth --output regnet_y_16gf.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name regnet_y_16gf --weight regnet_y_16gf-9e6ed7dd.pth --output regnet_y_16gf.onnx \ No newline at end of file diff --git a/models/cv/classification/regnet_y_16gf/igie/export.py b/models/cv/classification/regnet_y_16gf/igie/export.py deleted file mode 100644 index 33f35b3dd04550d5bf362365c006fb3406c7daa4..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_y_16gf/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.regnet_y_16gf() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/regnet_y_16gf/igie/inference.py b/models/cv/classification/regnet_y_16gf/igie/inference.py deleted file mode 100644 index 0563f72e11c44bceb5acf127981e225746aa9155..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_y_16gf/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_y_16gf/igie/requirements.txt b/models/cv/classification/regnet_y_16gf/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_y_16gf/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/regnet_y_16gf/igie/scripts/infer_regnet_y_16gf_fp16_accuracy.sh b/models/cv/classification/regnet_y_16gf/igie/scripts/infer_regnet_y_16gf_fp16_accuracy.sh index d7f0adb28d30ffe9eb9d240fe3aaa619c1fedeba..f7f91ddab3f465625f478ed24a6c1da576d30743 100644 --- a/models/cv/classification/regnet_y_16gf/igie/scripts/infer_regnet_y_16gf_fp16_accuracy.sh +++ b/models/cv/classification/regnet_y_16gf/igie/scripts/infer_regnet_y_16gf_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="regnet_y_16gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_y_16gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_y_16gf/igie/scripts/infer_regnet_y_16gf_fp16_performance.sh b/models/cv/classification/regnet_y_16gf/igie/scripts/infer_regnet_y_16gf_fp16_performance.sh index 30dc217fc7214fcc392d1482fe07d151042a4d7d..7ede053bf5d67b767ac26b5b890e22f8ccc9f35f 100644 --- a/models/cv/classification/regnet_y_16gf/igie/scripts/infer_regnet_y_16gf_fp16_performance.sh +++ b/models/cv/classification/regnet_y_16gf/igie/scripts/infer_regnet_y_16gf_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="regnet_y_16gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_y_16gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_y_1_6gf/igie/README.md b/models/cv/classification/regnet_y_1_6gf/igie/README.md index e4972c223c9befdf02ceefab4144466a945455b9..2151fea729fa3bdc37f37fe55afbf3beeae0134d 100644 --- a/models/cv/classification/regnet_y_1_6gf/igie/README.md +++ b/models/cv/classification/regnet_y_1_6gf/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight regnet_y_1_6gf-b11a554e.pth --output regnet_y_1_6gf.onnx +python3 ../../igie_common/export.py --model-name regnet_y_1_6gf --weight regnet_y_1_6gf-b11a554e.pth --output regnet_y_1_6gf.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/regnet_y_1_6gf/igie/build_engine.py b/models/cv/classification/regnet_y_1_6gf/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_y_1_6gf/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_y_1_6gf/igie/ci/prepare.sh b/models/cv/classification/regnet_y_1_6gf/igie/ci/prepare.sh index e6346b744df00cf7d48ac7c027b7eb92d8f52647..541c682c48ee37a80b8a8ec06f290f57d41c48ba 100644 --- a/models/cv/classification/regnet_y_1_6gf/igie/ci/prepare.sh +++ b/models/cv/classification/regnet_y_1_6gf/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight regnet_y_1_6gf-b11a554e.pth --output regnet_y_1_6gf.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name regnet_y_1_6gf --weight regnet_y_1_6gf-b11a554e.pth --output regnet_y_1_6gf.onnx \ No newline at end of file diff --git a/models/cv/classification/regnet_y_1_6gf/igie/export.py b/models/cv/classification/regnet_y_1_6gf/igie/export.py deleted file mode 100644 index f2a9f0a2757b3a65852b9825b87ef756a08a7ec4..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_y_1_6gf/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.regnet_y_1_6gf() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/regnet_y_1_6gf/igie/inference.py b/models/cv/classification/regnet_y_1_6gf/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_y_1_6gf/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/regnet_y_1_6gf/igie/requirements.txt b/models/cv/classification/regnet_y_1_6gf/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/regnet_y_1_6gf/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/regnet_y_1_6gf/igie/scripts/infer_regnet_y_1_6gf_fp16_accuracy.sh b/models/cv/classification/regnet_y_1_6gf/igie/scripts/infer_regnet_y_1_6gf_fp16_accuracy.sh index 62d9cc115ed77d3bdc8260cdedc50cadd8bc5b9b..4f335412e60495fd60912da75f9ebda85935ddca 100644 --- a/models/cv/classification/regnet_y_1_6gf/igie/scripts/infer_regnet_y_1_6gf_fp16_accuracy.sh +++ b/models/cv/classification/regnet_y_1_6gf/igie/scripts/infer_regnet_y_1_6gf_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="regnet_y_1_6gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_y_1_6gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/regnet_y_1_6gf/igie/scripts/infer_regnet_y_1_6gf_fp16_performance.sh b/models/cv/classification/regnet_y_1_6gf/igie/scripts/infer_regnet_y_1_6gf_fp16_performance.sh index afe3a45083e873691b0a670864e7055a36c72587..c542815b00af85f6256b2106696958ffd6279800 100644 --- a/models/cv/classification/regnet_y_1_6gf/igie/scripts/infer_regnet_y_1_6gf_fp16_performance.sh +++ b/models/cv/classification/regnet_y_1_6gf/igie/scripts/infer_regnet_y_1_6gf_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="regnet_y_1_6gf.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine regnet_y_1_6gf_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/repvgg/igie/README.md b/models/cv/classification/repvgg/igie/README.md index 970e8de0da875680ad035403fe9c4117b973f2af..4a3c87d52848a87c6becefbf89ab343df49db95b 100644 --- a/models/cv/classification/repvgg/igie/README.md +++ b/models/cv/classification/repvgg/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/repvgg/repvgg-A0_4xb64-coslr-120e_in1k.py --weight repvgg-A0_8xb32_in1k_20221213-60ae8e23.pth --output repvgg.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/repvgg/repvgg-A0_4xb64-coslr-120e_in1k.py --weight repvgg-A0_8xb32_in1k_20221213-60ae8e23.pth --output repvgg.onnx ``` @@ -45,6 +46,7 @@ python3 export.py --cfg mmpretrain/configs/repvgg/repvgg-A0_4xb64-coslr-120e_in1 ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/repvgg/igie/build_engine.py b/models/cv/classification/repvgg/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/repvgg/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/repvgg/igie/ci/prepare.sh b/models/cv/classification/repvgg/igie/ci/prepare.sh index 4433e485e49e15ff38cb3466c31a196ebcbcaf97..2daf189bad5b341763db0258f8ebb0657e0e839e 100644 --- a/models/cv/classification/repvgg/igie/ci/prepare.sh +++ b/models/cv/classification/repvgg/igie/ci/prepare.sh @@ -24,6 +24,7 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ -python3 export.py --cfg mmpretrain/configs/repvgg/repvgg-A0_4xb64-coslr-120e_in1k.py --weight repvgg-A0_8xb32_in1k_20221213-60ae8e23.pth --output repvgg.onnx \ No newline at end of file +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/repvgg/repvgg-A0_4xb64-coslr-120e_in1k.py --weight repvgg-A0_8xb32_in1k_20221213-60ae8e23.pth --output repvgg.onnx \ No newline at end of file diff --git a/models/cv/classification/repvgg/igie/export.py b/models/cv/classification/repvgg/igie/export.py deleted file mode 100644 index 7dc8d9fde531853bb9d25966dc7f706f2d9276dd..0000000000000000000000000000000000000000 --- a/models/cv/classification/repvgg/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat) - out_head = self.model.head.fc(feat[0]) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/repvgg/igie/inference.py b/models/cv/classification/repvgg/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/repvgg/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/repvgg/igie/requirements.txt b/models/cv/classification/repvgg/igie/requirements.txt deleted file mode 100644 index 2bd089c8fdcd7755d0ca576c42f26d79e2e7c433..0000000000000000000000000000000000000000 --- a/models/cv/classification/repvgg/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -mmcv==1.5.3 -mmcls -mmengine diff --git a/models/cv/classification/repvgg/igie/scripts/infer_repvgg_fp16_accuracy.sh b/models/cv/classification/repvgg/igie/scripts/infer_repvgg_fp16_accuracy.sh index 37f6fcb2ab964b22bd9e29f0015a1b2900f5a560..ea8323e50f474b92ed6e2096af619e6663805206 100644 --- a/models/cv/classification/repvgg/igie/scripts/infer_repvgg_fp16_accuracy.sh +++ b/models/cv/classification/repvgg/igie/scripts/infer_repvgg_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="repvgg.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine repvgg_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/repvgg/igie/scripts/infer_repvgg_fp16_performance.sh b/models/cv/classification/repvgg/igie/scripts/infer_repvgg_fp16_performance.sh index ad29f598b8dbe3512e5288bfd7c757df89654b09..ebb2456c6a021a890fd8fe23f86ac3f55c12fd10 100644 --- a/models/cv/classification/repvgg/igie/scripts/infer_repvgg_fp16_performance.sh +++ b/models/cv/classification/repvgg/igie/scripts/infer_repvgg_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="repvgg.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine repvgg_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/res2net50/igie/README.md b/models/cv/classification/res2net50/igie/README.md index 59d0446a9403ac24e84c4d703564f0075cc75dc2..fc8fec18a82f9a92e27aad5084fd7e8d6cb4a117 100644 --- a/models/cv/classification/res2net50/igie/README.md +++ b/models/cv/classification/res2net50/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/res2net/res2net50-w14-s8_8xb32_in1k.py --weight res2net50-w14-s8_3rdparty_8xb32_in1k_20210927-bc967bf1.pth --output res2net50.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/res2net/res2net50-w14-s8_8xb32_in1k.py --weight res2net50-w14-s8_3rdparty_8xb32_in1k_20210927-bc967bf1.pth --output res2net50.onnx # Use onnxsim optimize onnx model onnxsim res2net50.onnx res2net50_opt.onnx @@ -48,6 +49,7 @@ onnxsim res2net50.onnx res2net50_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/res2net50/igie/build_engine.py b/models/cv/classification/res2net50/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/res2net50/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/res2net50/igie/ci/prepare.sh b/models/cv/classification/res2net50/igie/ci/prepare.sh index ddc583f282df11405010c09c7860d55166d61523..e2372d86856d5f99e22cfe5d49788a773aea4642 100644 --- a/models/cv/classification/res2net50/igie/ci/prepare.sh +++ b/models/cv/classification/res2net50/igie/ci/prepare.sh @@ -24,7 +24,8 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ -python3 export.py --cfg mmpretrain/configs/res2net/res2net50-w14-s8_8xb32_in1k.py --weight res2net50-w14-s8_3rdparty_8xb32_in1k_20210927-bc967bf1.pth --output res2net50.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/res2net/res2net50-w14-s8_8xb32_in1k.py --weight res2net50-w14-s8_3rdparty_8xb32_in1k_20210927-bc967bf1.pth --output res2net50.onnx onnxsim res2net50.onnx res2net50_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/res2net50/igie/export.py b/models/cv/classification/res2net50/igie/export.py deleted file mode 100644 index 7dc8d9fde531853bb9d25966dc7f706f2d9276dd..0000000000000000000000000000000000000000 --- a/models/cv/classification/res2net50/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat) - out_head = self.model.head.fc(feat[0]) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/res2net50/igie/inference.py b/models/cv/classification/res2net50/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/res2net50/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/res2net50/igie/requirements.txt b/models/cv/classification/res2net50/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/res2net50/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/res2net50/igie/scripts/infer_res2net50_fp16_accuracy.sh b/models/cv/classification/res2net50/igie/scripts/infer_res2net50_fp16_accuracy.sh index 155ec563624beccb6de835d797ad2741d7e1aa50..ca78878b8bbbe14e4f45f96d5be9fb796a76fca6 100644 --- a/models/cv/classification/res2net50/igie/scripts/infer_res2net50_fp16_accuracy.sh +++ b/models/cv/classification/res2net50/igie/scripts/infer_res2net50_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="res2net50_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine res2net50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/res2net50/igie/scripts/infer_res2net50_fp16_performance.sh b/models/cv/classification/res2net50/igie/scripts/infer_res2net50_fp16_performance.sh index c24472ba2c3c101f0e2cf4413b396a73a3491529..85ce84551ea410faa2ef6924157f31ce6c2f58dc 100644 --- a/models/cv/classification/res2net50/igie/scripts/infer_res2net50_fp16_performance.sh +++ b/models/cv/classification/res2net50/igie/scripts/infer_res2net50_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="res2net50_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine res2net50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnest50/igie/README.md b/models/cv/classification/resnest50/igie/README.md index d3ed08b423581b7c00742345890687d55171cde0..b39cc5ba7195594ecca590ed5c6a5afb1e62f6a2 100644 --- a/models/cv/classification/resnest50/igie/README.md +++ b/models/cv/classification/resnest50/igie/README.md @@ -27,14 +27,15 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install git+https://github.com/zhanghang1989/ResNeSt ``` ### Model Conversion ```bash # export onnx model -python3 export.py --weight resnest50-528c19ca.pth --output resnest50.onnx +python3 ../../igie_common/export.py --model-name resnest50 --weight resnest50-528c19ca.pth --output resnest50.onnx # Use onnxsim optimize onnx model onnxsim resnest50.onnx resnest50_opt.onnx @@ -45,6 +46,7 @@ onnxsim resnest50.onnx resnest50_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/resnest50/igie/build_engine.py b/models/cv/classification/resnest50/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnest50/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnest50/igie/ci/prepare.sh b/models/cv/classification/resnest50/igie/ci/prepare.sh index 21f3114af083d4bb1ef03f6cb2ab82cf5106bce6..1a67b423156609ea97149900175c9e6e68c9febb 100644 --- a/models/cv/classification/resnest50/igie/ci/prepare.sh +++ b/models/cv/classification/resnest50/igie/ci/prepare.sh @@ -24,7 +24,8 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip install -r requirements.txt +pip install -r ../../igie_common/requirements.txt +pip3 install git+https://github.com/zhanghang1989/ResNeSt # export onnx model -python3 export.py --weight resnest50-528c19ca.pth --output resnest50.onnx +python3 ../../igie_common/export.py --model-name resnest50 --weight resnest50-528c19ca.pth --output resnest50.onnx onnxsim resnest50.onnx resnest50_opt.onnx diff --git a/models/cv/classification/resnest50/igie/export.py b/models/cv/classification/resnest50/igie/export.py deleted file mode 100644 index 9514777b8c42ce5d46d11791a4c54729a2f7695e..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnest50/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from resnest.torch import resnest50 - - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = resnest50(pretrained=False) - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/models/cv/classification/resnest50/igie/inference.py b/models/cv/classification/resnest50/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnest50/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/resnest50/igie/requirements.txt b/models/cv/classification/resnest50/igie/requirements.txt deleted file mode 100644 index b6cbf88fb82dea30f99d75e35623ebbb72eeeaa7..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnest50/igie/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -onnx -tqdm -onnxsim -git+https://github.com/zhanghang1989/ResNeSt diff --git a/models/cv/classification/resnest50/igie/scripts/infer_resnest50_fp16_accuracy.sh b/models/cv/classification/resnest50/igie/scripts/infer_resnest50_fp16_accuracy.sh index c5a33efeaa7dfb5c397ca1cf5806455500656aaa..c59ea3d73bc716f293475d70cb908ff055e623ef 100644 --- a/models/cv/classification/resnest50/igie/scripts/infer_resnest50_fp16_accuracy.sh +++ b/models/cv/classification/resnest50/igie/scripts/infer_resnest50_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="resnest50_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnest50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnest50/igie/scripts/infer_resnest50_fp16_performance.sh b/models/cv/classification/resnest50/igie/scripts/infer_resnest50_fp16_performance.sh index 82c32e89fcf08e5ad8063cbdbab752c29f6957a3..8146397ac9d89dc989077dd179155bda64888f32 100644 --- a/models/cv/classification/resnest50/igie/scripts/infer_resnest50_fp16_performance.sh +++ b/models/cv/classification/resnest50/igie/scripts/infer_resnest50_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="resnest50_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnest50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet101/igie/README.md b/models/cv/classification/resnet101/igie/README.md index 0a986582ddb819e5d4816eaec68906d784f6adaf..2aaa15947d60cfa9c551ca930d35761d3227a125 100644 --- a/models/cv/classification/resnet101/igie/README.md +++ b/models/cv/classification/resnet101/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight resnet101-63fe2227.pth --output resnet101.onnx +python3 ../../igie_common/export.py --model-name resnet101 --weight resnet101-63fe2227.pth --output resnet101.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/resnet101/igie/build_engine.py b/models/cv/classification/resnet101/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet101/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet101/igie/ci/prepare.sh b/models/cv/classification/resnet101/igie/ci/prepare.sh index 2e4e45799cc705f0fed9ce602f4952fe8e836718..a1e32ba09c919c97484837d37557c0eb7a5aa914 100644 --- a/models/cv/classification/resnet101/igie/ci/prepare.sh +++ b/models/cv/classification/resnet101/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight resnet101-63fe2227.pth --output resnet101.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name resnet101 --weight resnet101-63fe2227.pth --output resnet101.onnx \ No newline at end of file diff --git a/models/cv/classification/resnet101/igie/export.py b/models/cv/classification/resnet101/igie/export.py deleted file mode 100644 index a9cd5b05692f23e74c35144bb1e402720aa83834..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet101/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.resnet101() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/resnet101/igie/inference.py b/models/cv/classification/resnet101/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet101/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet101/igie/quantize.py b/models/cv/classification/resnet101/igie/quantize.py deleted file mode 100644 index e851dcc16e57c9531c37f577a98a593ad2bbb46f..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet101/igie/quantize.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 onnx -import psutil -import argparse -import numpy as np -from inference import get_dataloader -from onnxruntime.quantization import (CalibrationDataReader, QuantFormat, - quantize_static, QuantType, - CalibrationMethod) - -class CalibrationDataLoader(CalibrationDataReader): - def __init__(self, input_name, dataloader, cnt_limit=100): - self.cnt = 0 - self.input_name = input_name - self.cnt_limit = cnt_limit - self.iter = iter(dataloader) - - # avoid oom - @staticmethod - def _exceed_memory_upper_bound(upper_bound=80): - info = psutil.virtual_memory() - total_percent = info.percent - if total_percent >= upper_bound: - return True - return False - - def get_next(self): - if self._exceed_memory_upper_bound() or self.cnt >= self.cnt_limit: - return None - self.cnt += 1 - print(f"onnx calibration data count: {self.cnt}") - input_info = next(self.iter) - - ort_input = {k: np.array(v) for k, v in zip(self.input_name, input_info)} - return ort_input - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--out_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="calibration datasets path.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - model = onnx.load(args.model_path) - input_names = [input.name for input in model.graph.input] - - dataloader = get_dataloader(args.datasets, batch_size=1, num_workers=args.num_workers) - calibration = CalibrationDataLoader(input_names, dataloader, cnt_limit=20) - - quantize_static(args.model_path, - args.out_path, - calibration_data_reader=calibration, - quant_format=QuantFormat.QOperator, - per_channel=False, - activation_type=QuantType.QInt8, - weight_type=QuantType.QInt8, - use_external_data_format=False, - calibrate_method=CalibrationMethod.Percentile, - nodes_to_exclude=['/Softmax'], - extra_options = { - 'ActivationSymmetric': True, - 'WeightSymmetric': True - } - ) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet101/igie/requirements.txt b/models/cv/classification/resnet101/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet101/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/resnet101/igie/scripts/infer_resnet101_fp16_accuracy.sh b/models/cv/classification/resnet101/igie/scripts/infer_resnet101_fp16_accuracy.sh index 3175d05b7e7804c39afe7f777bee46bef5daf927..2445a04ed320a3b9b31589f78a6b4fcd123f27de 100644 --- a/models/cv/classification/resnet101/igie/scripts/infer_resnet101_fp16_accuracy.sh +++ b/models/cv/classification/resnet101/igie/scripts/infer_resnet101_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="resnet101.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet101_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet101/igie/scripts/infer_resnet101_fp16_performance.sh b/models/cv/classification/resnet101/igie/scripts/infer_resnet101_fp16_performance.sh index 6d88a9e2509a8fa15a054602f35568f3c06848fa..94892e1d491f67a8815ebea2e347c2fb472f11b0 100644 --- a/models/cv/classification/resnet101/igie/scripts/infer_resnet101_fp16_performance.sh +++ b/models/cv/classification/resnet101/igie/scripts/infer_resnet101_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="resnet101.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet101_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet101/igie/scripts/infer_resnet101_int8_accuracy.sh b/models/cv/classification/resnet101/igie/scripts/infer_resnet101_int8_accuracy.sh index 23fff27a8a99198cb4d278c08df713fd7ae4c20b..42df575233b0062c69e4da9f956983bf1c6f45a8 100644 --- a/models/cv/classification/resnet101/igie/scripts/infer_resnet101_int8_accuracy.sh +++ b/models/cv/classification/resnet101/igie/scripts/infer_resnet101_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path resnet101_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet101_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet101/igie/scripts/infer_resnet101_int8_performance.sh b/models/cv/classification/resnet101/igie/scripts/infer_resnet101_int8_performance.sh index c8b30b9cec0f40aa837c3c472acdf4a8b6bacd63..c60d54685f5a920a15318f6ed8fa097b047f724e 100644 --- a/models/cv/classification/resnet101/igie/scripts/infer_resnet101_int8_performance.sh +++ b/models/cv/classification/resnet101/igie/scripts/infer_resnet101_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path resnet101_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet101_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet152/igie/README.md b/models/cv/classification/resnet152/igie/README.md index 2f2315a1661679dd8d985aa84d60c2659d312e8c..66165bf79322451e7e92fde5658638fbf9902804 100644 --- a/models/cv/classification/resnet152/igie/README.md +++ b/models/cv/classification/resnet152/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight resnet152-394f9c45.pth --output resnet152.onnx +python3 ../../igie_common/export.py --model-name resnet152 --weight resnet152-394f9c45.pth --output resnet152.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/resnet152/igie/build_engine.py b/models/cv/classification/resnet152/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet152/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet152/igie/ci/prepare.sh b/models/cv/classification/resnet152/igie/ci/prepare.sh index cc147befa6c0cb0f1ea0ce61bea0ec9e53d9f542..cc79f89dc2446c257b587ac3f58b7f6350af8a41 100644 --- a/models/cv/classification/resnet152/igie/ci/prepare.sh +++ b/models/cv/classification/resnet152/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight resnet152-394f9c45.pth --output resnet152.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name resnet152 --weight resnet152-394f9c45.pth --output resnet152.onnx \ No newline at end of file diff --git a/models/cv/classification/resnet152/igie/export.py b/models/cv/classification/resnet152/igie/export.py deleted file mode 100644 index 1386e31d6539244969f1e4b2ffc7e82e962d6760..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet152/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.resnet152() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/resnet152/igie/inference.py b/models/cv/classification/resnet152/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet152/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet152/igie/quantize.py b/models/cv/classification/resnet152/igie/quantize.py deleted file mode 100644 index e851dcc16e57c9531c37f577a98a593ad2bbb46f..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet152/igie/quantize.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 onnx -import psutil -import argparse -import numpy as np -from inference import get_dataloader -from onnxruntime.quantization import (CalibrationDataReader, QuantFormat, - quantize_static, QuantType, - CalibrationMethod) - -class CalibrationDataLoader(CalibrationDataReader): - def __init__(self, input_name, dataloader, cnt_limit=100): - self.cnt = 0 - self.input_name = input_name - self.cnt_limit = cnt_limit - self.iter = iter(dataloader) - - # avoid oom - @staticmethod - def _exceed_memory_upper_bound(upper_bound=80): - info = psutil.virtual_memory() - total_percent = info.percent - if total_percent >= upper_bound: - return True - return False - - def get_next(self): - if self._exceed_memory_upper_bound() or self.cnt >= self.cnt_limit: - return None - self.cnt += 1 - print(f"onnx calibration data count: {self.cnt}") - input_info = next(self.iter) - - ort_input = {k: np.array(v) for k, v in zip(self.input_name, input_info)} - return ort_input - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--out_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="calibration datasets path.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - model = onnx.load(args.model_path) - input_names = [input.name for input in model.graph.input] - - dataloader = get_dataloader(args.datasets, batch_size=1, num_workers=args.num_workers) - calibration = CalibrationDataLoader(input_names, dataloader, cnt_limit=20) - - quantize_static(args.model_path, - args.out_path, - calibration_data_reader=calibration, - quant_format=QuantFormat.QOperator, - per_channel=False, - activation_type=QuantType.QInt8, - weight_type=QuantType.QInt8, - use_external_data_format=False, - calibrate_method=CalibrationMethod.Percentile, - nodes_to_exclude=['/Softmax'], - extra_options = { - 'ActivationSymmetric': True, - 'WeightSymmetric': True - } - ) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet152/igie/requirements.txt b/models/cv/classification/resnet152/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet152/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/resnet152/igie/scripts/infer_resnet152_fp16_accuracy.sh b/models/cv/classification/resnet152/igie/scripts/infer_resnet152_fp16_accuracy.sh index 625abd36e1659f4ff6c427ac37f1a0f2ad84a30c..56f9bdf4e58d02b646e53c4e5ef476cf33d7201b 100644 --- a/models/cv/classification/resnet152/igie/scripts/infer_resnet152_fp16_accuracy.sh +++ b/models/cv/classification/resnet152/igie/scripts/infer_resnet152_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="resnet152.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet152_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet152/igie/scripts/infer_resnet152_fp16_performance.sh b/models/cv/classification/resnet152/igie/scripts/infer_resnet152_fp16_performance.sh index 541d3b203eed8edbab44a378bfa333b8f7c402cf..a5802aeee4996e155e602483f12ba879bb621733 100644 --- a/models/cv/classification/resnet152/igie/scripts/infer_resnet152_fp16_performance.sh +++ b/models/cv/classification/resnet152/igie/scripts/infer_resnet152_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="resnet152.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet152_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet152/igie/scripts/infer_resnet152_int8_accuracy.sh b/models/cv/classification/resnet152/igie/scripts/infer_resnet152_int8_accuracy.sh index f19ffad3ca49822031396e410873df25bf7ff2e9..18aa65742973dcf2c84001e889a4ef6dfa3ce21b 100644 --- a/models/cv/classification/resnet152/igie/scripts/infer_resnet152_int8_accuracy.sh +++ b/models/cv/classification/resnet152/igie/scripts/infer_resnet152_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path resnet152_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet152_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet152/igie/scripts/infer_resnet152_int8_performance.sh b/models/cv/classification/resnet152/igie/scripts/infer_resnet152_int8_performance.sh index 9a8fe01664f7428cb34d18ca591addd333735877..587eb0dffc897ac4233de1d29c74670a64a5fd09 100644 --- a/models/cv/classification/resnet152/igie/scripts/infer_resnet152_int8_performance.sh +++ b/models/cv/classification/resnet152/igie/scripts/infer_resnet152_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path resnet152_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet152_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet18/igie/README.md b/models/cv/classification/resnet18/igie/README.md index 95ef89edaa081bbca4b42c6ce56fc6d21a27dba3..a9d3492c5a3f6c4b6eae15a366030297efeba5cc 100644 --- a/models/cv/classification/resnet18/igie/README.md +++ b/models/cv/classification/resnet18/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight resnet18-f37072fd.pth --output resnet18.onnx +python3 ../../igie_common/export.py --model-name resnet18 --weight resnet18-f37072fd.pth --output resnet18.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/resnet18/igie/build_engine.py b/models/cv/classification/resnet18/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet18/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet18/igie/ci/prepare.sh b/models/cv/classification/resnet18/igie/ci/prepare.sh index 9e663ad12807337fc49d33a9df65772d78938c00..87fac53b5db1ba0d5b408b48139163862d33041c 100644 --- a/models/cv/classification/resnet18/igie/ci/prepare.sh +++ b/models/cv/classification/resnet18/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight resnet18-f37072fd.pth --output resnet18.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name resnet18 --weight resnet18-f37072fd.pth --output resnet18.onnx \ No newline at end of file diff --git a/models/cv/classification/resnet18/igie/export.py b/models/cv/classification/resnet18/igie/export.py deleted file mode 100644 index b1f571b7411acfd6a34c315bc7085fbc7c017042..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet18/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.resnet18() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/resnet18/igie/inference.py b/models/cv/classification/resnet18/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet18/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet18/igie/quantize.py b/models/cv/classification/resnet18/igie/quantize.py deleted file mode 100644 index e851dcc16e57c9531c37f577a98a593ad2bbb46f..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet18/igie/quantize.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 onnx -import psutil -import argparse -import numpy as np -from inference import get_dataloader -from onnxruntime.quantization import (CalibrationDataReader, QuantFormat, - quantize_static, QuantType, - CalibrationMethod) - -class CalibrationDataLoader(CalibrationDataReader): - def __init__(self, input_name, dataloader, cnt_limit=100): - self.cnt = 0 - self.input_name = input_name - self.cnt_limit = cnt_limit - self.iter = iter(dataloader) - - # avoid oom - @staticmethod - def _exceed_memory_upper_bound(upper_bound=80): - info = psutil.virtual_memory() - total_percent = info.percent - if total_percent >= upper_bound: - return True - return False - - def get_next(self): - if self._exceed_memory_upper_bound() or self.cnt >= self.cnt_limit: - return None - self.cnt += 1 - print(f"onnx calibration data count: {self.cnt}") - input_info = next(self.iter) - - ort_input = {k: np.array(v) for k, v in zip(self.input_name, input_info)} - return ort_input - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--out_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="calibration datasets path.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - model = onnx.load(args.model_path) - input_names = [input.name for input in model.graph.input] - - dataloader = get_dataloader(args.datasets, batch_size=1, num_workers=args.num_workers) - calibration = CalibrationDataLoader(input_names, dataloader, cnt_limit=20) - - quantize_static(args.model_path, - args.out_path, - calibration_data_reader=calibration, - quant_format=QuantFormat.QOperator, - per_channel=False, - activation_type=QuantType.QInt8, - weight_type=QuantType.QInt8, - use_external_data_format=False, - calibrate_method=CalibrationMethod.Percentile, - nodes_to_exclude=['/Softmax'], - extra_options = { - 'ActivationSymmetric': True, - 'WeightSymmetric': True - } - ) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet18/igie/requirements.txt b/models/cv/classification/resnet18/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet18/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/resnet18/igie/scripts/infer_resnet18_fp16_accuracy.sh b/models/cv/classification/resnet18/igie/scripts/infer_resnet18_fp16_accuracy.sh index 081e75f7e8dd86c1f9466ea1f3cdc6e5a8b92707..83fa1ee760cca796b206e5e3b7ff68b5574edeba 100644 --- a/models/cv/classification/resnet18/igie/scripts/infer_resnet18_fp16_accuracy.sh +++ b/models/cv/classification/resnet18/igie/scripts/infer_resnet18_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="resnet18.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet18_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet18/igie/scripts/infer_resnet18_fp16_performance.sh b/models/cv/classification/resnet18/igie/scripts/infer_resnet18_fp16_performance.sh index afe5dd9abd1b11382fa3674af69b3465811437c4..3bb0d35c8e3067292d2e5cbe248a20711f05fab5 100644 --- a/models/cv/classification/resnet18/igie/scripts/infer_resnet18_fp16_performance.sh +++ b/models/cv/classification/resnet18/igie/scripts/infer_resnet18_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="resnet18.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet18_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet18/igie/scripts/infer_resnet18_int8_accuracy.sh b/models/cv/classification/resnet18/igie/scripts/infer_resnet18_int8_accuracy.sh index 18f3b271716c47800a1b0ff2b87ee8dac848f200..daf59879a3610b2a49100808ab0a17854a5d40d1 100644 --- a/models/cv/classification/resnet18/igie/scripts/infer_resnet18_int8_accuracy.sh +++ b/models/cv/classification/resnet18/igie/scripts/infer_resnet18_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path resnet18_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet18_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet18/igie/scripts/infer_resnet18_int8_performance.sh b/models/cv/classification/resnet18/igie/scripts/infer_resnet18_int8_performance.sh index 2372017f5fb774d31447856fc5958fefb20f68d9..bd40992cd1c6c457cfcd3ed6635c7748904bde86 100644 --- a/models/cv/classification/resnet18/igie/scripts/infer_resnet18_int8_performance.sh +++ b/models/cv/classification/resnet18/igie/scripts/infer_resnet18_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path resnet18_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet18_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet50/igie/README.md b/models/cv/classification/resnet50/igie/README.md index 2bba59c53ed3a2a11fd090aea2d428a802dd51fc..93d4d284566186050ca02f47e8ee2c6e447c54c7 100644 --- a/models/cv/classification/resnet50/igie/README.md +++ b/models/cv/classification/resnet50/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight resnet50-0676ba61.pth --output resnet50.onnx +python3 ../../igie_common/export.py --model-name resnet50 --weight resnet50-0676ba61.pth --output resnet50.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/resnet50/igie/build_engine.py b/models/cv/classification/resnet50/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet50/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet50/igie/ci/prepare.sh b/models/cv/classification/resnet50/igie/ci/prepare.sh index 152f4ebce7df75ad8120f2a4bde4be06b55d8796..fa922538016bfce0045f7c6219611b17c737d090 100644 --- a/models/cv/classification/resnet50/igie/ci/prepare.sh +++ b/models/cv/classification/resnet50/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight resnet50-0676ba61.pth --output resnet50.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name resnet50 --weight resnet50-0676ba61.pth --output resnet50.onnx \ No newline at end of file diff --git a/models/cv/classification/resnet50/igie/export.py b/models/cv/classification/resnet50/igie/export.py deleted file mode 100644 index affc54b55a553bde7740240607f918b9cc3f4272..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet50/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.resnet50() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/resnet50/igie/inference.py b/models/cv/classification/resnet50/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet50/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet50/igie/quantize.py b/models/cv/classification/resnet50/igie/quantize.py deleted file mode 100644 index e851dcc16e57c9531c37f577a98a593ad2bbb46f..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet50/igie/quantize.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 onnx -import psutil -import argparse -import numpy as np -from inference import get_dataloader -from onnxruntime.quantization import (CalibrationDataReader, QuantFormat, - quantize_static, QuantType, - CalibrationMethod) - -class CalibrationDataLoader(CalibrationDataReader): - def __init__(self, input_name, dataloader, cnt_limit=100): - self.cnt = 0 - self.input_name = input_name - self.cnt_limit = cnt_limit - self.iter = iter(dataloader) - - # avoid oom - @staticmethod - def _exceed_memory_upper_bound(upper_bound=80): - info = psutil.virtual_memory() - total_percent = info.percent - if total_percent >= upper_bound: - return True - return False - - def get_next(self): - if self._exceed_memory_upper_bound() or self.cnt >= self.cnt_limit: - return None - self.cnt += 1 - print(f"onnx calibration data count: {self.cnt}") - input_info = next(self.iter) - - ort_input = {k: np.array(v) for k, v in zip(self.input_name, input_info)} - return ort_input - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--out_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="calibration datasets path.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - model = onnx.load(args.model_path) - input_names = [input.name for input in model.graph.input] - - dataloader = get_dataloader(args.datasets, batch_size=1, num_workers=args.num_workers) - calibration = CalibrationDataLoader(input_names, dataloader, cnt_limit=20) - - quantize_static(args.model_path, - args.out_path, - calibration_data_reader=calibration, - quant_format=QuantFormat.QOperator, - per_channel=False, - activation_type=QuantType.QInt8, - weight_type=QuantType.QInt8, - use_external_data_format=False, - calibrate_method=CalibrationMethod.Percentile, - nodes_to_exclude=['/Softmax'], - extra_options = { - 'ActivationSymmetric': True, - 'WeightSymmetric': True - } - ) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnet50/igie/requirements.txt b/models/cv/classification/resnet50/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnet50/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/resnet50/igie/scripts/infer_resnet50_fp16_accuracy.sh b/models/cv/classification/resnet50/igie/scripts/infer_resnet50_fp16_accuracy.sh index 875a8678ba9be0429773cdf029d5c57fdd34ec11..c79516f5281360ace51f7f1db32e1262de19c703 100644 --- a/models/cv/classification/resnet50/igie/scripts/infer_resnet50_fp16_accuracy.sh +++ b/models/cv/classification/resnet50/igie/scripts/infer_resnet50_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="resnet50.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet50/igie/scripts/infer_resnet50_fp16_performance.sh b/models/cv/classification/resnet50/igie/scripts/infer_resnet50_fp16_performance.sh index 37d2e3218521641713f1ba39b1c4d748312f50b7..7de10ae35a044be34effd43bcf454ca89b5f7595 100644 --- a/models/cv/classification/resnet50/igie/scripts/infer_resnet50_fp16_performance.sh +++ b/models/cv/classification/resnet50/igie/scripts/infer_resnet50_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="resnet50.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet50/igie/scripts/infer_resnet50_int8_accuracy.sh b/models/cv/classification/resnet50/igie/scripts/infer_resnet50_int8_accuracy.sh index 8b5edbdb949dee85ca9c4f2722b83e256daa2554..7543912103526c01011470320f5312b6cca102f2 100644 --- a/models/cv/classification/resnet50/igie/scripts/infer_resnet50_int8_accuracy.sh +++ b/models/cv/classification/resnet50/igie/scripts/infer_resnet50_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path resnet50_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet50_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnet50/igie/scripts/infer_resnet50_int8_performance.sh b/models/cv/classification/resnet50/igie/scripts/infer_resnet50_int8_performance.sh index 488bf8509ad3d8f08606c54112487fa12a88836d..99b5fbe8c52e8f1f13c1151c4c9e9a90cf8b7262 100644 --- a/models/cv/classification/resnet50/igie/scripts/infer_resnet50_int8_performance.sh +++ b/models/cv/classification/resnet50/igie/scripts/infer_resnet50_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path resnet50_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnet50_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnetv1d50/igie/README.md b/models/cv/classification/resnetv1d50/igie/README.md index e9cc913cf7c6791a0fc1db25e4f2944d1cfe4eb1..52d32a70ba88d728820016b317698e0e41b12b04 100644 --- a/models/cv/classification/resnetv1d50/igie/README.md +++ b/models/cv/classification/resnetv1d50/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/resnet/resnetv1d50_b32x8_imagenet.py --weight resnetv1d50_b32x8_imagenet_20210531-db14775a.pth --output resnetv1d50.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/resnet/resnetv1d50_b32x8_imagenet.py --weight resnetv1d50_b32x8_imagenet_20210531-db14775a.pth --output resnetv1d50.onnx ``` @@ -45,6 +46,7 @@ python3 export.py --cfg mmpretrain/configs/resnet/resnetv1d50_b32x8_imagenet.py ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/resnetv1d50/igie/build_engine.py b/models/cv/classification/resnetv1d50/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnetv1d50/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnetv1d50/igie/ci/prepare.sh b/models/cv/classification/resnetv1d50/igie/ci/prepare.sh index aa230851db44eac781a9f092cb188c0734aa649b..1a6b8cc5324e8a4463fd573651dbdb6e6bb99c36 100644 --- a/models/cv/classification/resnetv1d50/igie/ci/prepare.sh +++ b/models/cv/classification/resnetv1d50/igie/ci/prepare.sh @@ -24,6 +24,7 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ -python3 export.py --cfg mmpretrain/configs/resnet/resnetv1d50_b32x8_imagenet.py --weight resnetv1d50_b32x8_imagenet_20210531-db14775a.pth --output resnetv1d50.onnx \ No newline at end of file +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/resnet/resnetv1d50_b32x8_imagenet.py --weight resnetv1d50_b32x8_imagenet_20210531-db14775a.pth --output resnetv1d50.onnx \ No newline at end of file diff --git a/models/cv/classification/resnetv1d50/igie/export.py b/models/cv/classification/resnetv1d50/igie/export.py deleted file mode 100644 index 840450997c44ecfab2191cfbd1c545e00ff9de57..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnetv1d50/igie/export.py +++ /dev/null @@ -1,76 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - head = self.model.simple_test(x) - return head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/resnetv1d50/igie/inference.py b/models/cv/classification/resnetv1d50/igie/inference.py deleted file mode 100644 index c42cf871274e316f7175ea3ad77c5468d2c9b936..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnetv1d50/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/resnetv1d50/igie/requirements.txt b/models/cv/classification/resnetv1d50/igie/requirements.txt deleted file mode 100644 index 4d5ea05fc6b67b4fd9274544384d4473c2994a5b..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnetv1d50/igie/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -onnx -tqdm -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/resnetv1d50/igie/scripts/infer_resnetv1d50_fp16_accuracy.sh b/models/cv/classification/resnetv1d50/igie/scripts/infer_resnetv1d50_fp16_accuracy.sh index c3a3d869ea6fee7b4c4d29b34aa8b345dc8cea3e..5964159b60974ca86b76c1a2e4979d255247172d 100644 --- a/models/cv/classification/resnetv1d50/igie/scripts/infer_resnetv1d50_fp16_accuracy.sh +++ b/models/cv/classification/resnetv1d50/igie/scripts/infer_resnetv1d50_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="resnetv1d50.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnetv1d50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnetv1d50/igie/scripts/infer_resnetv1d50_fp16_performance.sh b/models/cv/classification/resnetv1d50/igie/scripts/infer_resnetv1d50_fp16_performance.sh index 194d290069c5c9da11d5483a81ed1d5b665d9a06..93f64ffff7493c28fd4d42cff3255b6a98b74c58 100644 --- a/models/cv/classification/resnetv1d50/igie/scripts/infer_resnetv1d50_fp16_performance.sh +++ b/models/cv/classification/resnetv1d50/igie/scripts/infer_resnetv1d50_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="resnetv1d50.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnetv1d50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnext101_32x8d/igie/README.md b/models/cv/classification/resnext101_32x8d/igie/README.md index d88f9c318aaf4df722c042329c891035ac321edf..d2e6b25dc658dc89c144de8629150d946b259c2a 100644 --- a/models/cv/classification/resnext101_32x8d/igie/README.md +++ b/models/cv/classification/resnext101_32x8d/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight resnext101_32x8d-8ba56ff5.pth --output resnext101_32x8d.onnx +python3 ../../igie_common/export.py --model-name resnext101_32x8d --weight resnext101_32x8d-8ba56ff5.pth --output resnext101_32x8d.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/resnext101_32x8d/igie/build_engine.py b/models/cv/classification/resnext101_32x8d/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext101_32x8d/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnext101_32x8d/igie/ci/prepare.sh b/models/cv/classification/resnext101_32x8d/igie/ci/prepare.sh index 7c7fd897c6b176ecc8d50f9773aa7dd5c690ff80..028e88a24fd965517e68a407b0bd0529b0887564 100644 --- a/models/cv/classification/resnext101_32x8d/igie/ci/prepare.sh +++ b/models/cv/classification/resnext101_32x8d/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight resnext101_32x8d-8ba56ff5.pth --output resnext101_32x8d.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name resnext101_32x8d --weight resnext101_32x8d-8ba56ff5.pth --output resnext101_32x8d.onnx \ No newline at end of file diff --git a/models/cv/classification/resnext101_32x8d/igie/export.py b/models/cv/classification/resnext101_32x8d/igie/export.py deleted file mode 100644 index d06c13da1d41ac2bb3505a8d20375f58eb863de9..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext101_32x8d/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.resnext101_32x8d() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/resnext101_32x8d/igie/inference.py b/models/cv/classification/resnext101_32x8d/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext101_32x8d/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnext101_32x8d/igie/requirements.txt b/models/cv/classification/resnext101_32x8d/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext101_32x8d/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/resnext101_32x8d/igie/scripts/infer_resnext101_32x8d_fp16_accuracy.sh b/models/cv/classification/resnext101_32x8d/igie/scripts/infer_resnext101_32x8d_fp16_accuracy.sh index 713f81cd17674e832f187f0fe3fb329af1c9bb70..c0178bcd1539f8d14003e173628dbcc8717670d0 100644 --- a/models/cv/classification/resnext101_32x8d/igie/scripts/infer_resnext101_32x8d_fp16_accuracy.sh +++ b/models/cv/classification/resnext101_32x8d/igie/scripts/infer_resnext101_32x8d_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="resnext101_32x8d.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnext101_32x8d_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnext101_32x8d/igie/scripts/infer_resnext101_32x8d_fp16_performance.sh b/models/cv/classification/resnext101_32x8d/igie/scripts/infer_resnext101_32x8d_fp16_performance.sh index c48175aa02d78498ab5732fc324dbc0a8ce26c23..4e11b53d4bbdffb63a3f25255434d858bdf94d33 100644 --- a/models/cv/classification/resnext101_32x8d/igie/scripts/infer_resnext101_32x8d_fp16_performance.sh +++ b/models/cv/classification/resnext101_32x8d/igie/scripts/infer_resnext101_32x8d_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="resnext101_32x8d.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnext101_32x8d_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnext101_64x4d/igie/README.md b/models/cv/classification/resnext101_64x4d/igie/README.md index b50b71abb260f77ee2215fbe80b6fd754046142b..22ff3449c5b8ed37c6319c883dae6a722d4d569b 100644 --- a/models/cv/classification/resnext101_64x4d/igie/README.md +++ b/models/cv/classification/resnext101_64x4d/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight resnext101_64x4d-173b62eb.pth --output resnext101_64x4d.onnx +python3 ../../igie_common/export.py --model-name resnext101_64x4d --weight resnext101_64x4d-173b62eb.pth --output resnext101_64x4d.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/resnext101_64x4d/igie/build_engine.py b/models/cv/classification/resnext101_64x4d/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext101_64x4d/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnext101_64x4d/igie/ci/prepare.sh b/models/cv/classification/resnext101_64x4d/igie/ci/prepare.sh index 4641ef9f77b761496b0aa9c25973043df54e9a68..3ed80a331997d132f0aa78c6e0854c93269945a3 100644 --- a/models/cv/classification/resnext101_64x4d/igie/ci/prepare.sh +++ b/models/cv/classification/resnext101_64x4d/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight resnext101_64x4d-173b62eb.pth --output resnext101_64x4d.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name resnext101_64x4d --weight resnext101_64x4d-173b62eb.pth --output resnext101_64x4d.onnx \ No newline at end of file diff --git a/models/cv/classification/resnext101_64x4d/igie/export.py b/models/cv/classification/resnext101_64x4d/igie/export.py deleted file mode 100644 index 43a20fca1eb39c834868a4cb3cb39d0de4c1a465..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext101_64x4d/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.resnext101_64x4d() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/resnext101_64x4d/igie/inference.py b/models/cv/classification/resnext101_64x4d/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext101_64x4d/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnext101_64x4d/igie/requirements.txt b/models/cv/classification/resnext101_64x4d/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext101_64x4d/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/resnext101_64x4d/igie/scripts/infer_resnext101_64x4d_fp16_accuracy.sh b/models/cv/classification/resnext101_64x4d/igie/scripts/infer_resnext101_64x4d_fp16_accuracy.sh index edcb1a0040c0c401fdc0c5f2601232112e25d307..24bae45ed17abcbbb09ff35bb3f75486d7d416d8 100644 --- a/models/cv/classification/resnext101_64x4d/igie/scripts/infer_resnext101_64x4d_fp16_accuracy.sh +++ b/models/cv/classification/resnext101_64x4d/igie/scripts/infer_resnext101_64x4d_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="resnext101_64x4d.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnext101_64x4d_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnext101_64x4d/igie/scripts/infer_resnext101_64x4d_fp16_performance.sh b/models/cv/classification/resnext101_64x4d/igie/scripts/infer_resnext101_64x4d_fp16_performance.sh index 3ccf3bc5c58bf3e672ab4d22a3c4724b2f3df880..ddcbd098fa694fdac13400b339410c8f665b89f6 100644 --- a/models/cv/classification/resnext101_64x4d/igie/scripts/infer_resnext101_64x4d_fp16_performance.sh +++ b/models/cv/classification/resnext101_64x4d/igie/scripts/infer_resnext101_64x4d_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="resnext101_64x4d.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnext101_64x4d_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnext50_32x4d/igie/README.md b/models/cv/classification/resnext50_32x4d/igie/README.md index de8b396c153c990ec5d26ac515307aaa006e8b7f..980c53d9254ac9f6d5d242f05bb2e71ffd65d0e4 100644 --- a/models/cv/classification/resnext50_32x4d/igie/README.md +++ b/models/cv/classification/resnext50_32x4d/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight resnext50_32x4d-7cdf4587.pth --output resnext50_32x4d.onnx +python3 ../../igie_common/export.py --model-name resnext50_32x4d --weight resnext50_32x4d-7cdf4587.pth --output resnext50_32x4d.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/resnext50_32x4d/igie/build_engine.py b/models/cv/classification/resnext50_32x4d/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext50_32x4d/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnext50_32x4d/igie/ci/prepare.sh b/models/cv/classification/resnext50_32x4d/igie/ci/prepare.sh index 82d79b9e35814e8e03d426e0a2c831ee29dc6f9b..b5d7963f48300ec8579dc76e0c4dba75d7066814 100644 --- a/models/cv/classification/resnext50_32x4d/igie/ci/prepare.sh +++ b/models/cv/classification/resnext50_32x4d/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight resnext50_32x4d-7cdf4587.pth --output resnext50_32x4d.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name resnext50_32x4d --weight resnext50_32x4d-7cdf4587.pth --output resnext50_32x4d.onnx \ No newline at end of file diff --git a/models/cv/classification/resnext50_32x4d/igie/export.py b/models/cv/classification/resnext50_32x4d/igie/export.py deleted file mode 100644 index ba2dc1da692f767a1804ba38cf104a0109793cd7..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext50_32x4d/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.resnext50_32x4d() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/resnext50_32x4d/igie/inference.py b/models/cv/classification/resnext50_32x4d/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext50_32x4d/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/resnext50_32x4d/igie/requirements.txt b/models/cv/classification/resnext50_32x4d/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/resnext50_32x4d/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/resnext50_32x4d/igie/scripts/infer_resnext50_32x4d_fp16_accuracy.sh b/models/cv/classification/resnext50_32x4d/igie/scripts/infer_resnext50_32x4d_fp16_accuracy.sh index f92f1188657587ed95335a2b95d1f98e4fee1f7f..5cbec203c654416557368cfad38c07a982fb55a5 100644 --- a/models/cv/classification/resnext50_32x4d/igie/scripts/infer_resnext50_32x4d_fp16_accuracy.sh +++ b/models/cv/classification/resnext50_32x4d/igie/scripts/infer_resnext50_32x4d_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="resnext50_32x4d.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnext50_32x4d_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/resnext50_32x4d/igie/scripts/infer_resnext50_32x4d_fp16_performance.sh b/models/cv/classification/resnext50_32x4d/igie/scripts/infer_resnext50_32x4d_fp16_performance.sh index a916c8950aca43ed4264646a05230b3463441f25..3d0a709bda4e9a9a38239afa88b3ca915564793b 100644 --- a/models/cv/classification/resnext50_32x4d/igie/scripts/infer_resnext50_32x4d_fp16_performance.sh +++ b/models/cv/classification/resnext50_32x4d/igie/scripts/infer_resnext50_32x4d_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="resnext50_32x4d.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine resnext50_32x4d_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/seresnet50/igie/README.md b/models/cv/classification/seresnet50/igie/README.md index 552eb861dd31d28e5d06b214b3e7b952ee54457a..3642b2e58f87d5670d4bd69694fdf0ff1157d7dc 100644 --- a/models/cv/classification/seresnet50/igie/README.md +++ b/models/cv/classification/seresnet50/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,13 +38,14 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/seresnet/seresnet50_8xb32_in1k.py --weight se-resnet50_batch256_imagenet_20200804-ae206104.pth --output seresnet50.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/seresnet/seresnet50_8xb32_in1k.py --weight se-resnet50_batch256_imagenet_20200804-ae206104.pth --output seresnet50.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/seresnet50/igie/build_engine.py b/models/cv/classification/seresnet50/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/seresnet50/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/seresnet50/igie/ci/prepare.sh b/models/cv/classification/seresnet50/igie/ci/prepare.sh index 502110362bae1b1f2374a4a85827e254fc4d923b..ab17cf7ee88fe60e3e81f773981cb8fd7313f105 100644 --- a/models/cv/classification/seresnet50/igie/ci/prepare.sh +++ b/models/cv/classification/seresnet50/igie/ci/prepare.sh @@ -24,6 +24,7 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ -python3 export.py --cfg mmpretrain/configs/seresnet/seresnet50_8xb32_in1k.py --weight se-resnet50_batch256_imagenet_20200804-ae206104.pth --output seresnet50.onnx \ No newline at end of file +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/seresnet/seresnet50_8xb32_in1k.py --weight se-resnet50_batch256_imagenet_20200804-ae206104.pth --output seresnet50.onnx \ No newline at end of file diff --git a/models/cv/classification/seresnet50/igie/export.py b/models/cv/classification/seresnet50/igie/export.py deleted file mode 100644 index 7dc8d9fde531853bb9d25966dc7f706f2d9276dd..0000000000000000000000000000000000000000 --- a/models/cv/classification/seresnet50/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat) - out_head = self.model.head.fc(feat[0]) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/seresnet50/igie/inference.py b/models/cv/classification/seresnet50/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/seresnet50/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/seresnet50/igie/requirements.txt b/models/cv/classification/seresnet50/igie/requirements.txt deleted file mode 100644 index 4d5ea05fc6b67b4fd9274544384d4473c2994a5b..0000000000000000000000000000000000000000 --- a/models/cv/classification/seresnet50/igie/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -onnx -tqdm -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/seresnet50/igie/scripts/infer_seresnet50_fp16_accuracy.sh b/models/cv/classification/seresnet50/igie/scripts/infer_seresnet50_fp16_accuracy.sh index c861e85be2300f51b41c671d09513272480cb5fe..4bc5e4f7351683ed825c069335ab83347fbe7ea7 100644 --- a/models/cv/classification/seresnet50/igie/scripts/infer_seresnet50_fp16_accuracy.sh +++ b/models/cv/classification/seresnet50/igie/scripts/infer_seresnet50_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="seresnet50.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine seresnet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/seresnet50/igie/scripts/infer_seresnet50_fp16_performance.sh b/models/cv/classification/seresnet50/igie/scripts/infer_seresnet50_fp16_performance.sh index caaeaa7c2035f56fe4f62fd23387024e8c85875d..32eaa8e2b5630065d147ec7138f2bd459ad84225 100644 --- a/models/cv/classification/seresnet50/igie/scripts/infer_seresnet50_fp16_performance.sh +++ b/models/cv/classification/seresnet50/igie/scripts/infer_seresnet50_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="seresnet50.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine seresnet_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/shufflenetv2_x0_5/igie/README.md b/models/cv/classification/shufflenetv2_x0_5/igie/README.md index a7bf7f2b9ddf4919808d1ab29109641af9959980..507d16f29f6874f00492cede7df6f61e35d3d439 100644 --- a/models/cv/classification/shufflenetv2_x0_5/igie/README.md +++ b/models/cv/classification/shufflenetv2_x0_5/igie/README.md @@ -23,19 +23,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight shufflenetv2_x0.5-f707e7126e.pth --output shufflenetv2_x0_5.onnx +python3 ../../igie_common/export.py --model-name shufflenet_v2_x0_5 --weight shufflenetv2_x0.5-f707e7126e.pth --output shufflenetv2_x0_5.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/shufflenetv2_x0_5/igie/build_engine.py b/models/cv/classification/shufflenetv2_x0_5/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x0_5/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x0_5/igie/ci/prepare.sh b/models/cv/classification/shufflenetv2_x0_5/igie/ci/prepare.sh index cfc6d3b2c80c41f6ad35844fb1f2342a80a3839b..a40629a496d4239f0b9e14980e66e90c0553622c 100644 --- a/models/cv/classification/shufflenetv2_x0_5/igie/ci/prepare.sh +++ b/models/cv/classification/shufflenetv2_x0_5/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight shufflenetv2_x0.5-f707e7126e.pth --output shufflenetv2_x0_5.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name shufflenet_v2_x0_5 --weight shufflenetv2_x0.5-f707e7126e.pth --output shufflenetv2_x0_5.onnx \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x0_5/igie/export.py b/models/cv/classification/shufflenetv2_x0_5/igie/export.py deleted file mode 100644 index da1a97d665a4aa67e332bcdd2465e2d6c091bffd..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x0_5/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.shufflenet_v2_x0_5() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/shufflenetv2_x0_5/igie/inference.py b/models/cv/classification/shufflenetv2_x0_5/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x0_5/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x0_5/igie/requirements.txt b/models/cv/classification/shufflenetv2_x0_5/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x0_5/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/shufflenetv2_x0_5/igie/scripts/infer_shufflenetv2_x0_5_fp16_accuracy.sh b/models/cv/classification/shufflenetv2_x0_5/igie/scripts/infer_shufflenetv2_x0_5_fp16_accuracy.sh index 4be39d2fdefb6255204ce266717b683aa62cae36..1bc3e019f9e3c454d9d0921fab1be2f1be03b1af 100644 --- a/models/cv/classification/shufflenetv2_x0_5/igie/scripts/infer_shufflenetv2_x0_5_fp16_accuracy.sh +++ b/models/cv/classification/shufflenetv2_x0_5/igie/scripts/infer_shufflenetv2_x0_5_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="shufflenetv2_x0_5.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine shufflenetv2_x0_5_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/shufflenetv2_x0_5/igie/scripts/infer_shufflenetv2_x0_5_fp16_performance.sh b/models/cv/classification/shufflenetv2_x0_5/igie/scripts/infer_shufflenetv2_x0_5_fp16_performance.sh index 26f920112de67dd0b29d57e4f2c9be00f201bbe2..153f1499f0e6b3c8222c8bebbef94c052531831d 100644 --- a/models/cv/classification/shufflenetv2_x0_5/igie/scripts/infer_shufflenetv2_x0_5_fp16_performance.sh +++ b/models/cv/classification/shufflenetv2_x0_5/igie/scripts/infer_shufflenetv2_x0_5_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="shufflenetv2_x0_5.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine shufflenetv2_x0_5_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/shufflenetv2_x1_0/igie/README.md b/models/cv/classification/shufflenetv2_x1_0/igie/README.md index e426ce304112bbcb8e633262bf7f1aa9573b6d93..3e43cf66014fa30549666fcf24a9fc1f73034771 100644 --- a/models/cv/classification/shufflenetv2_x1_0/igie/README.md +++ b/models/cv/classification/shufflenetv2_x1_0/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight shufflenetv2_x1-5666bf0f80.pth --output shufflenetv2_x1_0.onnx +python3 ../../igie_common/export.py --model-name shufflenet_v2_x1_0 --weight shufflenetv2_x1-5666bf0f80.pth --output shufflenetv2_x1_0.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/shufflenetv2_x1_0/igie/build_engine.py b/models/cv/classification/shufflenetv2_x1_0/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x1_0/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x1_0/igie/ci/prepare.sh b/models/cv/classification/shufflenetv2_x1_0/igie/ci/prepare.sh index 464a7dfc30db2438f3946dacb936a3b5a02263ac..6bb64983b0ad9532c836b5b8d6b9281737325751 100644 --- a/models/cv/classification/shufflenetv2_x1_0/igie/ci/prepare.sh +++ b/models/cv/classification/shufflenetv2_x1_0/igie/ci/prepare.sh @@ -17,4 +17,4 @@ set -x pip install -r requirements.txt -python3 export.py --weight shufflenetv2_x1-5666bf0f80.pth --output shufflenetv2_x1_0.onnx \ No newline at end of file +python3 ../../igie_common/export.py --model-name shufflenet_v2_x1_0 --weight shufflenetv2_x1-5666bf0f80.pth --output shufflenetv2_x1_0.onnx \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x1_0/igie/export.py b/models/cv/classification/shufflenetv2_x1_0/igie/export.py deleted file mode 100644 index 3a4ff9ba8168c1e33a9a5677facc9b9d03afb911..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x1_0/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.shufflenet_v2_x1_0() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/shufflenetv2_x1_0/igie/inference.py b/models/cv/classification/shufflenetv2_x1_0/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x1_0/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x1_0/igie/requirements.txt b/models/cv/classification/shufflenetv2_x1_0/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x1_0/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/shufflenetv2_x1_0/igie/scripts/infer_shufflenetv2_x1_0_fp16_accuracy.sh b/models/cv/classification/shufflenetv2_x1_0/igie/scripts/infer_shufflenetv2_x1_0_fp16_accuracy.sh index 91802347326ce32c17d7f0a3309328a3976314db..0a1ff1e7ef161ac712ed3aba3f9592c7789576ab 100644 --- a/models/cv/classification/shufflenetv2_x1_0/igie/scripts/infer_shufflenetv2_x1_0_fp16_accuracy.sh +++ b/models/cv/classification/shufflenetv2_x1_0/igie/scripts/infer_shufflenetv2_x1_0_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="shufflenetv2_x1_0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine shufflenetv2_x1_0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/shufflenetv2_x1_0/igie/scripts/infer_shufflenetv2_x1_0_fp16_performance.sh b/models/cv/classification/shufflenetv2_x1_0/igie/scripts/infer_shufflenetv2_x1_0_fp16_performance.sh index 21353dc71844e052a925a493f58d6860a20323da..78eaab44520a28af28a7202b297d75976c7a3edf 100644 --- a/models/cv/classification/shufflenetv2_x1_0/igie/scripts/infer_shufflenetv2_x1_0_fp16_performance.sh +++ b/models/cv/classification/shufflenetv2_x1_0/igie/scripts/infer_shufflenetv2_x1_0_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="shufflenetv2_x1_0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine shufflenetv2_x1_0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/shufflenetv2_x1_5/igie/README.md b/models/cv/classification/shufflenetv2_x1_5/igie/README.md index fee173070bbe3e31d5cba466980865f68762e172..6eb085a6cb6b3417c04e3c86dfdb761729c1c8fc 100644 --- a/models/cv/classification/shufflenetv2_x1_5/igie/README.md +++ b/models/cv/classification/shufflenetv2_x1_5/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight shufflenetv2_x1_5-3c479a10.pth --output shufflenetv2_x1_5.onnx +python3 ../../igie_common/export.py --model-name shufflenet_v2_x1_5 --weight shufflenetv2_x1_5-3c479a10.pth --output shufflenetv2_x1_5.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/shufflenetv2_x1_5/igie/build_engine.py b/models/cv/classification/shufflenetv2_x1_5/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x1_5/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x1_5/igie/ci/prepare.sh b/models/cv/classification/shufflenetv2_x1_5/igie/ci/prepare.sh index 7d72032ccdfc9f044835bcc8cc4c13210f3c438b..b8ac05919b78a6914af8439811e88695be38778e 100644 --- a/models/cv/classification/shufflenetv2_x1_5/igie/ci/prepare.sh +++ b/models/cv/classification/shufflenetv2_x1_5/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight shufflenetv2_x1_5-3c479a10.pth --output shufflenetv2_x1_5.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name shufflenet_v2_x1_5 --weight shufflenetv2_x1_5-3c479a10.pth --output shufflenetv2_x1_5.onnx \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x1_5/igie/export.py b/models/cv/classification/shufflenetv2_x1_5/igie/export.py deleted file mode 100644 index 0f89ba7cb2d837243d465e78df5102abbbbbdf99..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x1_5/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.shufflenet_v2_x1_5() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/shufflenetv2_x1_5/igie/inference.py b/models/cv/classification/shufflenetv2_x1_5/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x1_5/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x1_5/igie/requirements.txt b/models/cv/classification/shufflenetv2_x1_5/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x1_5/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/shufflenetv2_x1_5/igie/scripts/infer_shufflenetv2_x1_5_fp16_accuracy.sh b/models/cv/classification/shufflenetv2_x1_5/igie/scripts/infer_shufflenetv2_x1_5_fp16_accuracy.sh index 9be9264cb25b816606f414611f611eaf2ffa5b27..c7e19a53032533da76d7d0cffc3d65fdcf1a55b2 100644 --- a/models/cv/classification/shufflenetv2_x1_5/igie/scripts/infer_shufflenetv2_x1_5_fp16_accuracy.sh +++ b/models/cv/classification/shufflenetv2_x1_5/igie/scripts/infer_shufflenetv2_x1_5_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="shufflenetv2_x1_5.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine shufflenetv2_x1_5_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/shufflenetv2_x1_5/igie/scripts/infer_shufflenetv2_x1_5_fp16_performance.sh b/models/cv/classification/shufflenetv2_x1_5/igie/scripts/infer_shufflenetv2_x1_5_fp16_performance.sh index cc5a424d73a3467a0d5be0e79e2f309fc7083a84..ed2b7dea68913b0e6379ee71872f719d4dfc0edd 100644 --- a/models/cv/classification/shufflenetv2_x1_5/igie/scripts/infer_shufflenetv2_x1_5_fp16_performance.sh +++ b/models/cv/classification/shufflenetv2_x1_5/igie/scripts/infer_shufflenetv2_x1_5_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="shufflenetv2_x1_5.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine shufflenetv2_x1_5_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/shufflenetv2_x2_0/igie/README.md b/models/cv/classification/shufflenetv2_x2_0/igie/README.md index c002d0b06825f02770cb8b4b78f0c12e43aeb64a..bfca029149aa6dd15858a582a4bb5334e6b20eaf 100644 --- a/models/cv/classification/shufflenetv2_x2_0/igie/README.md +++ b/models/cv/classification/shufflenetv2_x2_0/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight shufflenetv2_x2_0-8be3c8ee.pth --output shufflenetv2_x2_0.onnx +python3 ../../igie_common/export.py --model-name shufflenet_v2_x2_0 --weight shufflenetv2_x2_0-8be3c8ee.pth --output shufflenetv2_x2_0.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/shufflenetv2_x2_0/igie/build_engine.py b/models/cv/classification/shufflenetv2_x2_0/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x2_0/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x2_0/igie/ci/prepare.sh b/models/cv/classification/shufflenetv2_x2_0/igie/ci/prepare.sh index 4eccbb5a048a06e493d0b30ae26f30d1975a2a79..2099ae52079849ce8f3bee5f222af221d36b124f 100644 --- a/models/cv/classification/shufflenetv2_x2_0/igie/ci/prepare.sh +++ b/models/cv/classification/shufflenetv2_x2_0/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight shufflenetv2_x2_0-8be3c8ee.pth --output shufflenetv2_x2_0.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name shufflenet_v2_x2_0 --weight shufflenetv2_x2_0-8be3c8ee.pth --output shufflenetv2_x2_0.onnx \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x2_0/igie/export.py b/models/cv/classification/shufflenetv2_x2_0/igie/export.py deleted file mode 100644 index f4727e438f83e6fc66e599ff68af17d2867c63e7..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x2_0/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.shufflenet_v2_x2_0() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/shufflenetv2_x2_0/igie/inference.py b/models/cv/classification/shufflenetv2_x2_0/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x2_0/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/shufflenetv2_x2_0/igie/requirements.txt b/models/cv/classification/shufflenetv2_x2_0/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/shufflenetv2_x2_0/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/shufflenetv2_x2_0/igie/scripts/infer_shufflenetv2_x2_0_fp16_accuracy.sh b/models/cv/classification/shufflenetv2_x2_0/igie/scripts/infer_shufflenetv2_x2_0_fp16_accuracy.sh index be582ff6b6ed4ae140a9e0f08b4636bbd80ad929..b0e0cd243f204ffe179f0356ba3fcb0ad88841dc 100644 --- a/models/cv/classification/shufflenetv2_x2_0/igie/scripts/infer_shufflenetv2_x2_0_fp16_accuracy.sh +++ b/models/cv/classification/shufflenetv2_x2_0/igie/scripts/infer_shufflenetv2_x2_0_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="shufflenetv2_x2_0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine shufflenetv2_x2_0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/shufflenetv2_x2_0/igie/scripts/infer_shufflenetv2_x2_0_fp16_performance.sh b/models/cv/classification/shufflenetv2_x2_0/igie/scripts/infer_shufflenetv2_x2_0_fp16_performance.sh index f94cc83b47e6fc39e1db43cdadd30363e823b963..c9a4f634735570ca7bf600897c19e38b6dfddb47 100644 --- a/models/cv/classification/shufflenetv2_x2_0/igie/scripts/infer_shufflenetv2_x2_0_fp16_performance.sh +++ b/models/cv/classification/shufflenetv2_x2_0/igie/scripts/infer_shufflenetv2_x2_0_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="shufflenetv2_x2_0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine shufflenetv2_x2_0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/squeezenet_v1_0/igie/README.md b/models/cv/classification/squeezenet_v1_0/igie/README.md index eb6262598e4ae76ea1eeae2c0f528a3bad02fe04..965a2b14bbe02f21a03f250e8d984a69f5c18717 100644 --- a/models/cv/classification/squeezenet_v1_0/igie/README.md +++ b/models/cv/classification/squeezenet_v1_0/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight squeezenet1_0-b66bff10.pth --output squeezenet1_0.onnx +python3 ../../igie_common/export.py --model-name squeezenet1_0 --weight squeezenet1_0-b66bff10.pth --output squeezenet1_0.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/squeezenet_v1_0/igie/build_engine.py b/models/cv/classification/squeezenet_v1_0/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/squeezenet_v1_0/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/squeezenet_v1_0/igie/ci/prepare.sh b/models/cv/classification/squeezenet_v1_0/igie/ci/prepare.sh index e01e8a8a3dcf8999fa2d25604ce5ee11b6a31ce3..697849b37640107457f05f286cd0285d9dbe217b 100644 --- a/models/cv/classification/squeezenet_v1_0/igie/ci/prepare.sh +++ b/models/cv/classification/squeezenet_v1_0/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight squeezenet1_0-b66bff10.pth --output squeezenet1_0.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name squeezenet1_0 --weight squeezenet1_0-b66bff10.pth --output squeezenet1_0.onnx \ No newline at end of file diff --git a/models/cv/classification/squeezenet_v1_0/igie/export.py b/models/cv/classification/squeezenet_v1_0/igie/export.py deleted file mode 100644 index 940674f5b4a51f4c8d101cbce6c785171e1214a4..0000000000000000000000000000000000000000 --- a/models/cv/classification/squeezenet_v1_0/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.squeezenet1_0() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/squeezenet_v1_0/igie/inference.py b/models/cv/classification/squeezenet_v1_0/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/squeezenet_v1_0/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/squeezenet_v1_0/igie/requirements.txt b/models/cv/classification/squeezenet_v1_0/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/squeezenet_v1_0/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/squeezenet_v1_0/igie/scripts/infer_squeezenet_v1_0_fp16_accuracy.sh b/models/cv/classification/squeezenet_v1_0/igie/scripts/infer_squeezenet_v1_0_fp16_accuracy.sh index 21d4784176d041dcdfa073242aa1f1e1b835fad5..f637704f6e98516e4b5909deb48eacd7ba739fea 100644 --- a/models/cv/classification/squeezenet_v1_0/igie/scripts/infer_squeezenet_v1_0_fp16_accuracy.sh +++ b/models/cv/classification/squeezenet_v1_0/igie/scripts/infer_squeezenet_v1_0_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="squeezenet1_0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine squeezenet1_0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/squeezenet_v1_0/igie/scripts/infer_squeezenet_v1_0_fp16_performance.sh b/models/cv/classification/squeezenet_v1_0/igie/scripts/infer_squeezenet_v1_0_fp16_performance.sh index 634654a09c4cb87ec7b6a82e7d68340ae84e08e6..46ee505ac991f192fb0cb4086d298d07d00d09c2 100644 --- a/models/cv/classification/squeezenet_v1_0/igie/scripts/infer_squeezenet_v1_0_fp16_performance.sh +++ b/models/cv/classification/squeezenet_v1_0/igie/scripts/infer_squeezenet_v1_0_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="squeezenet1_0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine squeezenet1_0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/squeezenet_v1_1/igie/README.md b/models/cv/classification/squeezenet_v1_1/igie/README.md index 02cfcd64fc4cf23a71ed86a89fce491de4d3b609..5b9a92143c02e2e2c9cdf482af3bc7e7aeb16f8f 100644 --- a/models/cv/classification/squeezenet_v1_1/igie/README.md +++ b/models/cv/classification/squeezenet_v1_1/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight squeezenet1_1-b8a52dc0.pth --output squeezenet1_1.onnx +python3 ../../igie_common/export.py --model-name squeezenet1_1 --weight squeezenet1_1-b8a52dc0.pth --output squeezenet1_1.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/squeezenet_v1_1/igie/build_engine.py b/models/cv/classification/squeezenet_v1_1/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/squeezenet_v1_1/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/squeezenet_v1_1/igie/ci/prepare.sh b/models/cv/classification/squeezenet_v1_1/igie/ci/prepare.sh index 19ca729f3d71a0039f7e71aca0442dd52b3d2e09..d2ff32ec6731e8c3ef3a550848acd71524233b67 100644 --- a/models/cv/classification/squeezenet_v1_1/igie/ci/prepare.sh +++ b/models/cv/classification/squeezenet_v1_1/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight squeezenet1_1-b8a52dc0.pth --output squeezenet1_1.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name squeezenet1_1 --weight squeezenet1_1-b8a52dc0.pth --output squeezenet1_1.onnx \ No newline at end of file diff --git a/models/cv/classification/squeezenet_v1_1/igie/export.py b/models/cv/classification/squeezenet_v1_1/igie/export.py deleted file mode 100644 index 3dde82ae3b64bdaf32b8843fc18de91e56d14a5d..0000000000000000000000000000000000000000 --- a/models/cv/classification/squeezenet_v1_1/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.squeezenet1_1() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/squeezenet_v1_1/igie/inference.py b/models/cv/classification/squeezenet_v1_1/igie/inference.py deleted file mode 100644 index 0563f72e11c44bceb5acf127981e225746aa9155..0000000000000000000000000000000000000000 --- a/models/cv/classification/squeezenet_v1_1/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/squeezenet_v1_1/igie/requirements.txt b/models/cv/classification/squeezenet_v1_1/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/squeezenet_v1_1/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/squeezenet_v1_1/igie/scripts/infer_squeezenet_v1_1_fp16_accuracy.sh b/models/cv/classification/squeezenet_v1_1/igie/scripts/infer_squeezenet_v1_1_fp16_accuracy.sh index c4af3d1acb8c3e5ad95b39461969caf7941de693..74847cc4faa5f2b75238c7c34e82ec91b69872e4 100644 --- a/models/cv/classification/squeezenet_v1_1/igie/scripts/infer_squeezenet_v1_1_fp16_accuracy.sh +++ b/models/cv/classification/squeezenet_v1_1/igie/scripts/infer_squeezenet_v1_1_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="squeezenet1_1.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine squeezenet1_1_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/squeezenet_v1_1/igie/scripts/infer_squeezenet_v1_1_fp16_performance.sh b/models/cv/classification/squeezenet_v1_1/igie/scripts/infer_squeezenet_v1_1_fp16_performance.sh index fa542f1d2dbb2d8f23e736ffea1410ad9e96eb9f..5a76e9aaccf550d228cc96a4dc131981150b833f 100644 --- a/models/cv/classification/squeezenet_v1_1/igie/scripts/infer_squeezenet_v1_1_fp16_performance.sh +++ b/models/cv/classification/squeezenet_v1_1/igie/scripts/infer_squeezenet_v1_1_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="squeezenet1_1.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine squeezenet1_1_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/svt_base/igie/README.md b/models/cv/classification/svt_base/igie/README.md index 2d4ca27cc38d4e300f76a88fca5533dd12fdc825..3076aa46566b3af9d36941eaf2b8995266f2594a 100644 --- a/models/cv/classification/svt_base/igie/README.md +++ b/models/cv/classification/svt_base/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/twins/twins-svt-base_8xb128_in1k.py --weight twins-svt-base_3rdparty_8xb128_in1k_20220126-e31cc8e9.pth --output svt_base.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/twins/twins-svt-base_8xb128_in1k.py --weight twins-svt-base_3rdparty_8xb128_in1k_20220126-e31cc8e9.pth --output svt_base.onnx # Use onnxsim optimize onnx model onnxsim svt_base.onnx svt_base_opt.onnx @@ -47,6 +48,7 @@ onnxsim svt_base.onnx svt_base_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/svt_base/igie/build_engine.py b/models/cv/classification/svt_base/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/svt_base/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/svt_base/igie/ci/prepare.sh b/models/cv/classification/svt_base/igie/ci/prepare.sh index b11820c134e36afb9ca2984a473741be9e7a97bf..29ab6743ac6e52a176d2def57384181cc9bbb0ab 100644 --- a/models/cv/classification/svt_base/igie/ci/prepare.sh +++ b/models/cv/classification/svt_base/igie/ci/prepare.sh @@ -25,10 +25,11 @@ else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ # export onnx model -python3 export.py --cfg mmpretrain/configs/twins/twins-svt-base_8xb128_in1k.py --weight twins-svt-base_3rdparty_8xb128_in1k_20220126-e31cc8e9.pth --output svt_base.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/twins/twins-svt-base_8xb128_in1k.py --weight twins-svt-base_3rdparty_8xb128_in1k_20220126-e31cc8e9.pth --output svt_base.onnx # Use onnxsim optimize onnx model onnxsim svt_base.onnx svt_base_opt.onnx \ No newline at end of file diff --git a/models/cv/classification/svt_base/igie/export.py b/models/cv/classification/svt_base/igie/export.py deleted file mode 100644 index 6eafb7b164d52da54d010aeb1d92b838215c7702..0000000000000000000000000000000000000000 --- a/models/cv/classification/svt_base/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat[0]) - out_head = self.model.head.fc(feat) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/svt_base/igie/inference.py b/models/cv/classification/svt_base/igie/inference.py deleted file mode 100644 index 1b0c602a8f5c899e34f621851c10a5d00c47583c..0000000000000000000000000000000000000000 --- a/models/cv/classification/svt_base/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/svt_base/igie/requirements.txt b/models/cv/classification/svt_base/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/svt_base/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/svt_base/igie/scripts/infer_svt_base_fp16_accuracy.sh b/models/cv/classification/svt_base/igie/scripts/infer_svt_base_fp16_accuracy.sh index 331017947f2f3ca41408a3e39dd1333dfd4b5c90..f1d71a766680ca756fd45c12da577c099823a124 100644 --- a/models/cv/classification/svt_base/igie/scripts/infer_svt_base_fp16_accuracy.sh +++ b/models/cv/classification/svt_base/igie/scripts/infer_svt_base_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="svt_base_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine svt_base_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/svt_base/igie/scripts/infer_svt_base_fp16_performance.sh b/models/cv/classification/svt_base/igie/scripts/infer_svt_base_fp16_performance.sh index 34f0a080b7ef5d23dd8fbd045c2630be4a820219..d74a631152f667942f93f0138e8a64298b7ce29b 100644 --- a/models/cv/classification/svt_base/igie/scripts/infer_svt_base_fp16_performance.sh +++ b/models/cv/classification/svt_base/igie/scripts/infer_svt_base_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="svt_base_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine svt_base_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/twins_pcpvt/igie/README.md b/models/cv/classification/twins_pcpvt/igie/README.md index 7ea3d08209d60dabe727fe8e8a1a30c6cd68778a..2173542534a4ba4d6dd6b3b5bcfdf1f652a7b46b 100644 --- a/models/cv/classification/twins_pcpvt/igie/README.md +++ b/models/cv/classification/twins_pcpvt/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/twins/twins-pcpvt-small_8xb128_in1k.py --weight twins-pcpvt-small_3rdparty_8xb128_in1k_20220126-ef23c132.pth --output twins_pcpvt_small.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/twins/twins-pcpvt-small_8xb128_in1k.py --weight twins-pcpvt-small_3rdparty_8xb128_in1k_20220126-ef23c132.pth --output twins_pcpvt_small.onnx # Use onnxsim optimize onnx model onnxsim twins_pcpvt_small.onnx twins_pcpvt_small_opt.onnx @@ -48,6 +49,7 @@ onnxsim twins_pcpvt_small.onnx twins_pcpvt_small_opt.onnx ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/twins_pcpvt/igie/build_engine.py b/models/cv/classification/twins_pcpvt/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/twins_pcpvt/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/twins_pcpvt/igie/ci/prepare.sh b/models/cv/classification/twins_pcpvt/igie/ci/prepare.sh index b377a631237ac835b5781f05f75c0cc5608c1256..7687074bf5328f18832271562a7a4fe878f456ef 100644 --- a/models/cv/classification/twins_pcpvt/igie/ci/prepare.sh +++ b/models/cv/classification/twins_pcpvt/igie/ci/prepare.sh @@ -24,10 +24,11 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ # export onnx model -python3 export.py --cfg mmpretrain/configs/twins/twins-pcpvt-small_8xb128_in1k.py --weight twins-pcpvt-small_3rdparty_8xb128_in1k_20220126-ef23c132.pth --output twins_pcpvt_small.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/twins/twins-pcpvt-small_8xb128_in1k.py --weight twins-pcpvt-small_3rdparty_8xb128_in1k_20220126-ef23c132.pth --output twins_pcpvt_small.onnx # Use onnxsim optimize onnx model onnxsim twins_pcpvt_small.onnx twins_pcpvt_small_opt.onnx diff --git a/models/cv/classification/twins_pcpvt/igie/export.py b/models/cv/classification/twins_pcpvt/igie/export.py deleted file mode 100644 index c7681b6883fffd4e126cc01d946e7abfb69df315..0000000000000000000000000000000000000000 --- a/models/cv/classification/twins_pcpvt/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat[0]) - out_head = self.model.head.fc(feat) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/twins_pcpvt/igie/inference.py b/models/cv/classification/twins_pcpvt/igie/inference.py deleted file mode 100644 index b0a8ed039512863abde9bcb6ade316900a9fdaac..0000000000000000000000000000000000000000 --- a/models/cv/classification/twins_pcpvt/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/twins_pcpvt/igie/requirements.txt b/models/cv/classification/twins_pcpvt/igie/requirements.txt deleted file mode 100644 index 41c3166395b56ce698ec11c8f3aef19624cae2bb..0000000000000000000000000000000000000000 --- a/models/cv/classification/twins_pcpvt/igie/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -onnx -tqdm -onnxsim -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/twins_pcpvt/igie/scripts/infer_twins_pcpvt_fp16_accuracy.sh b/models/cv/classification/twins_pcpvt/igie/scripts/infer_twins_pcpvt_fp16_accuracy.sh index 3d372907bb9a7e862641d7b5e0394a801d29f98d..6937726bfb08bec6728b780f9f7c1f78c51bd8e0 100644 --- a/models/cv/classification/twins_pcpvt/igie/scripts/infer_twins_pcpvt_fp16_accuracy.sh +++ b/models/cv/classification/twins_pcpvt/igie/scripts/infer_twins_pcpvt_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="twins_pcpvt_small_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine twins_pcpvt_small_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/twins_pcpvt/igie/scripts/infer_twins_pcpvt_fp16_performance.sh b/models/cv/classification/twins_pcpvt/igie/scripts/infer_twins_pcpvt_fp16_performance.sh index f7e32f1a08ea1372a2666e2a6a44bbee5ea6911d..22c723b42c2de4f8fbaf61c5162eb97152e62907 100644 --- a/models/cv/classification/twins_pcpvt/igie/scripts/infer_twins_pcpvt_fp16_performance.sh +++ b/models/cv/classification/twins_pcpvt/igie/scripts/infer_twins_pcpvt_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="twins_pcpvt_small_opt.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine twins_pcpvt_small_opt_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/van_b0/igie/README.md b/models/cv/classification/van_b0/igie/README.md index 09bfcafe1725422a27daf5728b6bce03aa9cff68..421add0edef0067c18f6f0112cabaccce9eb8039 100644 --- a/models/cv/classification/van_b0/igie/README.md +++ b/models/cv/classification/van_b0/igie/README.md @@ -27,7 +27,8 @@ yum install -y mesa-libGL ## Ubuntu apt install -y libgl1-mesa-glx -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 ``` ### Model Conversion @@ -37,7 +38,7 @@ pip3 install -r requirements.txt git clone -b v0.24.0 https://github.com/open-mmlab/mmpretrain.git # export onnx model -python3 export.py --cfg mmpretrain/configs/van/van-b0_8xb128_in1k.py --weight van-tiny_8xb128_in1k_20220501-385941af.pth --output van_b0.onnx +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/van/van-b0_8xb128_in1k.py --weight van-tiny_8xb128_in1k_20220501-385941af.pth --output van_b0.onnx ``` @@ -45,6 +46,7 @@ python3 export.py --cfg mmpretrain/configs/van/van-b0_8xb128_in1k.py --weight va ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/van_b0/igie/build_engine.py b/models/cv/classification/van_b0/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/van_b0/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/van_b0/igie/ci/prepare.sh b/models/cv/classification/van_b0/igie/ci/prepare.sh index 076ffcbec8e5a440deac10459f5fa0e87b38e836..852cb1eb769e0e6333822c7a200a9f822a110902 100644 --- a/models/cv/classification/van_b0/igie/ci/prepare.sh +++ b/models/cv/classification/van_b0/igie/ci/prepare.sh @@ -24,7 +24,8 @@ elif [[ ${ID} == "centos" ]]; then else echo "Not Support Os" fi -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt +pip3 install mmcv==1.5.3 mmcls==0.24.0 unzip -q /mnt/deepspark/data/repos/mmpretrain-0.24.0.zip -d ./ # export onnx model -python3 export.py --cfg mmpretrain/configs/van/van-b0_8xb128_in1k.py --weight van-tiny_8xb128_in1k_20220501-385941af.pth --output van_b0.onnx \ No newline at end of file +python3 ../../igie_common/export_mmcls.py --cfg mmpretrain/configs/van/van-b0_8xb128_in1k.py --weight van-tiny_8xb128_in1k_20220501-385941af.pth --output van_b0.onnx \ No newline at end of file diff --git a/models/cv/classification/van_b0/igie/export.py b/models/cv/classification/van_b0/igie/export.py deleted file mode 100644 index c7681b6883fffd4e126cc01d946e7abfb69df315..0000000000000000000000000000000000000000 --- a/models/cv/classification/van_b0/igie/export.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -from mmcls.apis import init_model - -class Model(torch.nn.Module): - def __init__(self, config_file, checkpoint_file): - super().__init__() - self.model = init_model(config_file, checkpoint_file, device="cpu") - - def forward(self, x): - feat = self.model.backbone(x) - feat = self.model.neck(feat[0]) - out_head = self.model.head.fc(feat) - return out_head - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--cfg", - type=str, - required=True, - help="model config file.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - config_file = args.cfg - checkpoint_file = args.weight - model = Model(config_file, checkpoint_file).eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == '__main__': - main() - diff --git a/models/cv/classification/van_b0/igie/inference.py b/models/cv/classification/van_b0/igie/inference.py deleted file mode 100644 index b0a8ed039512863abde9bcb6ade316900a9fdaac..0000000000000000000000000000000000000000 --- a/models/cv/classification/van_b0/igie/inference.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/van_b0/igie/requirements.txt b/models/cv/classification/van_b0/igie/requirements.txt deleted file mode 100644 index 4d5ea05fc6b67b4fd9274544384d4473c2994a5b..0000000000000000000000000000000000000000 --- a/models/cv/classification/van_b0/igie/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -onnx -tqdm -mmcv==1.5.3 -mmcls diff --git a/models/cv/classification/van_b0/igie/scripts/infer_van_b0_fp16_accuracy.sh b/models/cv/classification/van_b0/igie/scripts/infer_van_b0_fp16_accuracy.sh index 9b993277afc491579b73ae2c88ec2c19ffcb1f14..d1f8ac08e1e50cf5b6e2c86f44e97f2b9c5a6282 100644 --- a/models/cv/classification/van_b0/igie/scripts/infer_van_b0_fp16_accuracy.sh +++ b/models/cv/classification/van_b0/igie/scripts/infer_van_b0_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="van_b0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine van_b0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/van_b0/igie/scripts/infer_van_b0_fp16_performance.sh b/models/cv/classification/van_b0/igie/scripts/infer_van_b0_fp16_performance.sh index 8aa8caef65cbf576409473cc04c8dc62a86231bc..1b26acda5b08325dda2fb1ee064610d6a6b1c591 100644 --- a/models/cv/classification/van_b0/igie/scripts/infer_van_b0_fp16_performance.sh +++ b/models/cv/classification/van_b0/igie/scripts/infer_van_b0_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="van_b0.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine van_b0_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg11/igie/README.md b/models/cv/classification/vgg11/igie/README.md index 38966dd250de02235eb37b241eb6fe331cd5fc0c..1c8b85109e44a740a261665c0909afe5dd03a5c6 100644 --- a/models/cv/classification/vgg11/igie/README.md +++ b/models/cv/classification/vgg11/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight vgg11-8a719046.pth --output vgg11.onnx +python3 ../../igie_common/export.py --model-name vgg11 --weight vgg11-8a719046.pth --output vgg11.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/vgg11/igie/build_engine.py b/models/cv/classification/vgg11/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg11/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/vgg11/igie/ci/prepare.sh b/models/cv/classification/vgg11/igie/ci/prepare.sh index 3be89fdd261a27ba1af3400df542dc42ab781111..9b88a075722116870a1648dd628ffd67dcbec2d9 100644 --- a/models/cv/classification/vgg11/igie/ci/prepare.sh +++ b/models/cv/classification/vgg11/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight vgg11-8a719046.pth --output vgg11.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name vgg11 --weight vgg11-8a719046.pth --output vgg11.onnx \ No newline at end of file diff --git a/models/cv/classification/vgg11/igie/export.py b/models/cv/classification/vgg11/igie/export.py deleted file mode 100644 index a09d19b72ce845307a8aca7fcad70cccc9689177..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg11/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.vgg11() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/vgg11/igie/inference.py b/models/cv/classification/vgg11/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg11/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/vgg11/igie/requirements.txt b/models/cv/classification/vgg11/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg11/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/vgg11/igie/scripts/infer_vgg11_fp16_accuracy.sh b/models/cv/classification/vgg11/igie/scripts/infer_vgg11_fp16_accuracy.sh index fde4b177dbc5004a8e8c8dcb1c27129176515b20..001c73dbf75e935108dd5de91fca57246407d259 100644 --- a/models/cv/classification/vgg11/igie/scripts/infer_vgg11_fp16_accuracy.sh +++ b/models/cv/classification/vgg11/igie/scripts/infer_vgg11_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="vgg11.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg11_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg11/igie/scripts/infer_vgg11_fp16_performance.sh b/models/cv/classification/vgg11/igie/scripts/infer_vgg11_fp16_performance.sh index 136961e51cc042c63298329682f775e9372e43d5..3ae0511ffd21ff1c98d3f21352f7445aaddf2f44 100644 --- a/models/cv/classification/vgg11/igie/scripts/infer_vgg11_fp16_performance.sh +++ b/models/cv/classification/vgg11/igie/scripts/infer_vgg11_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="vgg11.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg11_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg16/igie/README.md b/models/cv/classification/vgg16/igie/README.md index caa229ba8fcb1cf7a97bf4e822ed4eaba1723045..840c520edf1d5550b3e56cf40316a971b8c40df4 100644 --- a/models/cv/classification/vgg16/igie/README.md +++ b/models/cv/classification/vgg16/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight vgg16-397923af.pth --output vgg16.onnx +python3 ../../igie_common/export.py --model-name vgg16 --weight vgg16-397923af.pth --output vgg16.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/vgg16/igie/build_engine.py b/models/cv/classification/vgg16/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg16/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/vgg16/igie/ci/prepare.sh b/models/cv/classification/vgg16/igie/ci/prepare.sh index 3a8b0d9d326fbd5634510e1cf2f3ce384317f028..784049f439771413400799260f8138aff102eb2d 100644 --- a/models/cv/classification/vgg16/igie/ci/prepare.sh +++ b/models/cv/classification/vgg16/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight vgg16-397923af.pth --output vgg16.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name vgg16 --weight vgg16-397923af.pth --output vgg16.onnx \ No newline at end of file diff --git a/models/cv/classification/vgg16/igie/export.py b/models/cv/classification/vgg16/igie/export.py deleted file mode 100644 index 817d73195a784ba436ee705bd3a86095205e0022..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg16/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.vgg16() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/vgg16/igie/inference.py b/models/cv/classification/vgg16/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg16/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/vgg16/igie/quantize.py b/models/cv/classification/vgg16/igie/quantize.py deleted file mode 100644 index 1cdeb93fd3d5bd126324524d330be916b709d380..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg16/igie/quantize.py +++ /dev/null @@ -1,103 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 onnx -import psutil -import argparse -import numpy as np -from inference import get_dataloader -from onnxruntime.quantization import (CalibrationDataReader, QuantFormat, - quantize_static, QuantType, - CalibrationMethod) - -class CalibrationDataLoader(CalibrationDataReader): - def __init__(self, input_name, dataloader, cnt_limit=100): - self.cnt = 0 - self.input_name = input_name - self.cnt_limit = cnt_limit - self.iter = iter(dataloader) - - # avoid oom - @staticmethod - def _exceed_memory_upper_bound(upper_bound=80): - info = psutil.virtual_memory() - total_percent = info.percent - if total_percent >= upper_bound: - return True - return False - - def get_next(self): - if self._exceed_memory_upper_bound() or self.cnt >= self.cnt_limit: - return None - self.cnt += 1 - print(f"onnx calibration data count: {self.cnt}") - input_info = next(self.iter) - - ort_input = {k: np.array(v) for k, v in zip(self.input_name, input_info)} - return ort_input - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--out_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="calibration datasets path.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - model = onnx.load(args.model_path) - input_names = [input.name for input in model.graph.input] - - dataloader = get_dataloader(args.datasets, batch_size=1, num_workers=args.num_workers) - calibration = CalibrationDataLoader(input_names, dataloader, cnt_limit=20) - - quantize_static(args.model_path, - args.out_path, - calibration_data_reader=calibration, - quant_format=QuantFormat.QOperator, - per_channel=False, - activation_type=QuantType.QInt8, - weight_type=QuantType.QInt8, - use_external_data_format=False, - calibrate_method=CalibrationMethod.Percentile, - extra_options = { - 'ActivationSymmetric': True, - 'WeightSymmetric': True - } - ) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/vgg16/igie/requirements.txt b/models/cv/classification/vgg16/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg16/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/vgg16/igie/scripts/infer_vgg16_fp16_accuracy.sh b/models/cv/classification/vgg16/igie/scripts/infer_vgg16_fp16_accuracy.sh index f06907525d63615242857b1ea5965dbda4aae531..4c2e15d54f1c4875c2e9d345315fbf66d2eb88b9 100644 --- a/models/cv/classification/vgg16/igie/scripts/infer_vgg16_fp16_accuracy.sh +++ b/models/cv/classification/vgg16/igie/scripts/infer_vgg16_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="vgg16.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg16_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg16/igie/scripts/infer_vgg16_fp16_performance.sh b/models/cv/classification/vgg16/igie/scripts/infer_vgg16_fp16_performance.sh index 86eb5fa932d0cd620a6586516881b7fb71d30870..002c6c2169e637180701d571a0f8b8ae87890fe0 100644 --- a/models/cv/classification/vgg16/igie/scripts/infer_vgg16_fp16_performance.sh +++ b/models/cv/classification/vgg16/igie/scripts/infer_vgg16_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="vgg16.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg16_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg16/igie/scripts/infer_vgg16_int8_accuracy.sh b/models/cv/classification/vgg16/igie/scripts/infer_vgg16_int8_accuracy.sh index fae365ab6dd881576af895c136fa38a4cfdafb75..88e1b026d7e4639dc3c7626df34c7f8a2626bae8 100644 --- a/models/cv/classification/vgg16/igie/scripts/infer_vgg16_int8_accuracy.sh +++ b/models/cv/classification/vgg16/igie/scripts/infer_vgg16_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path vgg16_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg16_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg16/igie/scripts/infer_vgg16_int8_performance.sh b/models/cv/classification/vgg16/igie/scripts/infer_vgg16_int8_performance.sh index 77c2b179037facccbb3ee57bfc8b8d2f72832b26..ff38449194e33c7b605ecff34afb11d3620460fa 100644 --- a/models/cv/classification/vgg16/igie/scripts/infer_vgg16_int8_performance.sh +++ b/models/cv/classification/vgg16/igie/scripts/infer_vgg16_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path vgg16_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg16_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg19/igie/README.md b/models/cv/classification/vgg19/igie/README.md index cbf3e66821f73d496aa08e3cf3f2f3e9d5bcb0d1..4e2500027158870aafb07ddb87f74e7b8e2bdeec 100644 --- a/models/cv/classification/vgg19/igie/README.md +++ b/models/cv/classification/vgg19/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight vgg19-dcbb9e9d.pth --output vgg19.onnx +python3 ../../igie_common/export.py --model-name vgg19 --weight vgg19-dcbb9e9d.pth --output vgg19.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/vgg19/igie/build_engine.py b/models/cv/classification/vgg19/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg19/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/vgg19/igie/ci/prepare.sh b/models/cv/classification/vgg19/igie/ci/prepare.sh index 620ca769b9b69f4e5e57a84d19884c37727b2700..b944ccb7783e6e8c74a06c2459a6659c4b407a02 100644 --- a/models/cv/classification/vgg19/igie/ci/prepare.sh +++ b/models/cv/classification/vgg19/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight vgg19-dcbb9e9d.pth --output vgg19.onnx +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name vgg19 --weight vgg19-dcbb9e9d.pth --output vgg19.onnx diff --git a/models/cv/classification/vgg19/igie/export.py b/models/cv/classification/vgg19/igie/export.py deleted file mode 100644 index 71b0d655feb84bcb071fdb9d62e7112e48798a08..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg19/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.vgg19() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/vgg19/igie/inference.py b/models/cv/classification/vgg19/igie/inference.py deleted file mode 100644 index 0563f72e11c44bceb5acf127981e225746aa9155..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg19/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/vgg19/igie/requirements.txt b/models/cv/classification/vgg19/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg19/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/vgg19/igie/scripts/infer_vgg19_fp16_accuracy.sh b/models/cv/classification/vgg19/igie/scripts/infer_vgg19_fp16_accuracy.sh index acd9fd7de42dd18c5582861e3a3dc585a4ba39a4..723a37e1e0fee8c27c57bca7746f952513a47082 100644 --- a/models/cv/classification/vgg19/igie/scripts/infer_vgg19_fp16_accuracy.sh +++ b/models/cv/classification/vgg19/igie/scripts/infer_vgg19_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="vgg19.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg19_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg19/igie/scripts/infer_vgg19_fp16_performance.sh b/models/cv/classification/vgg19/igie/scripts/infer_vgg19_fp16_performance.sh index d0d050345cb0e379dfdb1db07b88915bd1ddabd6..41832493b82635b9a388885798e9e164c54806da 100644 --- a/models/cv/classification/vgg19/igie/scripts/infer_vgg19_fp16_performance.sh +++ b/models/cv/classification/vgg19/igie/scripts/infer_vgg19_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="vgg19.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg19_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg19_bn/igie/README.md b/models/cv/classification/vgg19_bn/igie/README.md index a46c31eb60dbcbd80b11b58aec1341f69c3f493f..0fbdb01a433b0920cf79117b74081d9e51bc96b7 100644 --- a/models/cv/classification/vgg19_bn/igie/README.md +++ b/models/cv/classification/vgg19_bn/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight vgg19_bn-c79401a0.pth --output vgg19_bn.onnx +python3 ../../igie_common/export.py --model-name vgg19_bn --weight vgg19_bn-c79401a0.pth --output vgg19_bn.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/vgg19_bn/igie/build_engine.py b/models/cv/classification/vgg19_bn/igie/build_engine.py deleted file mode 100644 index 54aa8847e5ceafba3f40d44f0e1e280d4740d870..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg19_bn/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/vgg19_bn/igie/ci/prepare.sh b/models/cv/classification/vgg19_bn/igie/ci/prepare.sh index 386f823fa0e299e7ce61e6087eaf293be752406f..dcd721b01cebe35c33ec9fcedaa351aefd08efc6 100644 --- a/models/cv/classification/vgg19_bn/igie/ci/prepare.sh +++ b/models/cv/classification/vgg19_bn/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight vgg19_bn-c79401a0.pth --output vgg19_bn.onnx +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name vgg19_bn --weight vgg19_bn-c79401a0.pth --output vgg19_bn.onnx diff --git a/models/cv/classification/vgg19_bn/igie/export.py b/models/cv/classification/vgg19_bn/igie/export.py deleted file mode 100644 index 525f6c836fa26a7d9ebf40059eff0ea6272ed1f4..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg19_bn/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.vgg19_bn() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/vgg19_bn/igie/inference.py b/models/cv/classification/vgg19_bn/igie/inference.py deleted file mode 100644 index 0563f72e11c44bceb5acf127981e225746aa9155..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg19_bn/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/vgg19_bn/igie/requirements.txt b/models/cv/classification/vgg19_bn/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/vgg19_bn/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/vgg19_bn/igie/scripts/infer_vgg19_bn_fp16_accuracy.sh b/models/cv/classification/vgg19_bn/igie/scripts/infer_vgg19_bn_fp16_accuracy.sh index 881c4f29b015ed7c711f8a57a7a06f44a5eac3a8..2738bc39a518cebc8c895cb0c7adf0a3c59a7fea 100644 --- a/models/cv/classification/vgg19_bn/igie/scripts/infer_vgg19_bn_fp16_accuracy.sh +++ b/models/cv/classification/vgg19_bn/igie/scripts/infer_vgg19_bn_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="vgg19_bn.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg19_bn_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/vgg19_bn/igie/scripts/infer_vgg19_bn_fp16_performance.sh b/models/cv/classification/vgg19_bn/igie/scripts/infer_vgg19_bn_fp16_performance.sh index 7b875e8a2dc668fbd7cdbaf01fabb93d3ff7f089..4d4255905f4250acdabdc24b0b66c65506553e6d 100644 --- a/models/cv/classification/vgg19_bn/igie/scripts/infer_vgg19_bn_fp16_performance.sh +++ b/models/cv/classification/vgg19_bn/igie/scripts/infer_vgg19_bn_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="vgg19_bn.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine vgg19_bn_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/wide_resnet101/igie/README.md b/models/cv/classification/wide_resnet101/igie/README.md index 9de215e9ef4532d7a26b63040c7e5da92ecd37bb..7abc2c6452965a76aa2ae379a55be12ce96b7372 100644 --- a/models/cv/classification/wide_resnet101/igie/README.md +++ b/models/cv/classification/wide_resnet101/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight wide_resnet101_2-32ee1156.pth --output wide_resnet101.onnx +python3 ../../igie_common/export.py --model-name wide_resnet101_2 --weight wide_resnet101_2-32ee1156.pth --output wide_resnet101.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/wide_resnet101/igie/build_engine.py b/models/cv/classification/wide_resnet101/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/wide_resnet101/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/wide_resnet101/igie/ci/prepare.sh b/models/cv/classification/wide_resnet101/igie/ci/prepare.sh index 1b4b8b183a8a7d7af1b42e5157d9a1b23aa8017b..40b9a26a3423f858665abe05a9af02d59f9d07aa 100644 --- a/models/cv/classification/wide_resnet101/igie/ci/prepare.sh +++ b/models/cv/classification/wide_resnet101/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight wide_resnet101_2-32ee1156.pth --output wide_resnet101.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name wide_resnet101_2 --weight wide_resnet101_2-32ee1156.pth --output wide_resnet101.onnx \ No newline at end of file diff --git a/models/cv/classification/wide_resnet101/igie/export.py b/models/cv/classification/wide_resnet101/igie/export.py deleted file mode 100644 index 4752f0eaf9b9c73c940aaa2d68cf230b8a2ae741..0000000000000000000000000000000000000000 --- a/models/cv/classification/wide_resnet101/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.wide_resnet101_2() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/wide_resnet101/igie/inference.py b/models/cv/classification/wide_resnet101/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/wide_resnet101/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/wide_resnet101/igie/requirements.txt b/models/cv/classification/wide_resnet101/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/wide_resnet101/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/wide_resnet101/igie/scripts/infer_wide_resnet101_fp16_accuracy.sh b/models/cv/classification/wide_resnet101/igie/scripts/infer_wide_resnet101_fp16_accuracy.sh index 42f1ed878249f9e9d7b976d4ddbcd370da65265a..274a0cca98e313d06194e652b70d5ab75dc9dc59 100644 --- a/models/cv/classification/wide_resnet101/igie/scripts/infer_wide_resnet101_fp16_accuracy.sh +++ b/models/cv/classification/wide_resnet101/igie/scripts/infer_wide_resnet101_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="wide_resnet101.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine wide_resnet101_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/wide_resnet101/igie/scripts/infer_wide_resnet101_fp16_performance.sh b/models/cv/classification/wide_resnet101/igie/scripts/infer_wide_resnet101_fp16_performance.sh index 05155037876ab1e575f46b8807db81a4a899adf1..dceedfdf512a6b8c0bbbe9ac83a1db1f9b45c049 100644 --- a/models/cv/classification/wide_resnet101/igie/scripts/infer_wide_resnet101_fp16_performance.sh +++ b/models/cv/classification/wide_resnet101/igie/scripts/infer_wide_resnet101_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="wide_resnet101.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine wide_resnet101_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/wide_resnet50/igie/README.md b/models/cv/classification/wide_resnet50/igie/README.md index 76b31309fe5ee1d82620a5f61ceeca1c5d88103d..2b334596e05fa490d961f8122e791fd1ee85c205 100644 --- a/models/cv/classification/wide_resnet50/igie/README.md +++ b/models/cv/classification/wide_resnet50/igie/README.md @@ -21,19 +21,20 @@ Dataset: to download the validation dat ### Install Dependencies ```bash -pip3 install -r requirements.txt +pip3 install -r ../../igie_common/requirements.txt ``` ### Model Conversion ```bash -python3 export.py --weight wide_resnet50_2-95faca4d.pth --output wide_resnet50.onnx +python3 ../../igie_common/export.py --model-name wide_resnet50_2 --weight wide_resnet50_2-95faca4d.pth --output wide_resnet50.onnx ``` ## Model Inference ```bash export DATASETS_DIR=/Path/to/imagenet_val/ +export RUN_DIR=../../igie_common/ ``` ### FP16 diff --git a/models/cv/classification/wide_resnet50/igie/build_engine.py b/models/cv/classification/wide_resnet50/igie/build_engine.py deleted file mode 100644 index d3626ae76cc9781d9a01ec3d3e2afbdbca409ff5..0000000000000000000000000000000000000000 --- a/models/cv/classification/wide_resnet50/igie/build_engine.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 tvm -import argparse -from tvm import relay -from tvm.relay.import_model import import_model_to_igie - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--engine_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--input", - type=str, - required=True, - help=""" - input info of the model, format should be: - input_name:input_shape - eg: --input input:1,3,224,224. - """) - - parser.add_argument("--precision", - type=str, - choices=["fp32", "fp16", "int8"], - required=True, - help="model inference precision.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - # get input valueinfo - input_name, input_shape = args.input.split(":") - shape = tuple([int(s) for s in input_shape.split(",")]) - input_dict = {input_name: shape} - - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - - mod, params = import_model_to_igie(args.model_path, input_dict, backend="igie") - - # build engine - lib = tvm.relay.build(mod, target=target, params=params, precision=args.precision) - - # export engine - lib.export_library(args.engine_path) - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/wide_resnet50/igie/ci/prepare.sh b/models/cv/classification/wide_resnet50/igie/ci/prepare.sh index 795996eebae4a505e460e5d278aa10a339080ac1..28c1938c2a547d6f3dee8d1d9626b4e8aef7ddeb 100644 --- a/models/cv/classification/wide_resnet50/igie/ci/prepare.sh +++ b/models/cv/classification/wide_resnet50/igie/ci/prepare.sh @@ -16,5 +16,5 @@ set -x -pip3 install -r requirements.txt -python3 export.py --weight wide_resnet50_2-95faca4d.pth --output wide_resnet50.onnx \ No newline at end of file +pip3 install -r ../../igie_common/requirements.txt +python3 ../../igie_common/export.py --model-name wide_resnet50_2 --weight wide_resnet50_2-95faca4d.pth --output wide_resnet50.onnx \ No newline at end of file diff --git a/models/cv/classification/wide_resnet50/igie/export.py b/models/cv/classification/wide_resnet50/igie/export.py deleted file mode 100644 index 1d3c64c825ab3aaf172f0c6ca7ef9b802ea06bf9..0000000000000000000000000000000000000000 --- a/models/cv/classification/wide_resnet50/igie/export.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 torch -import torchvision -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--weight", - type=str, - required=True, - help="pytorch model weight.") - - parser.add_argument("--output", - type=str, - required=True, - help="export onnx model path.") - - args = parser.parse_args() - return args - -def main(): - args = parse_args() - - model = torchvision.models.wide_resnet50_2() - model.load_state_dict(torch.load(args.weight)) - model.eval() - - input_names = ['input'] - output_names = ['output'] - dynamic_axes = {'input': {0: '-1'}, 'output': {0: '-1'}} - dummy_input = torch.randn(1, 3, 224, 224) - - torch.onnx.export( - model, - dummy_input, - args.output, - input_names = input_names, - dynamic_axes = dynamic_axes, - output_names = output_names, - opset_version=13 - ) - - print("Export onnx model successfully! ") - -if __name__ == "__main__": - main() diff --git a/models/cv/classification/wide_resnet50/igie/inference.py b/models/cv/classification/wide_resnet50/igie/inference.py deleted file mode 100644 index 3aef3ec70fa7e88917c54aeb8242fc73a910c696..0000000000000000000000000000000000000000 --- a/models/cv/classification/wide_resnet50/igie/inference.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 argparse -import tvm -import torch -import torchvision -import numpy as np -from tvm import relay -from tqdm import tqdm -from torchvision import transforms -from torchvision.transforms.functional import InterpolationMode - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--engine", - type=str, - required=True, - help="igie engine path.") - - parser.add_argument("--batchsize", - type=int, - required=True, - help="inference batch size.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="datasets path.") - - parser.add_argument("--input_name", - type=str, - required=True, - help="input name of the model.") - - parser.add_argument("--warmup", - type=int, - default=3, - help="number of warmup before test.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - parser.add_argument("--acc_target", - type=float, - default=None, - help="Model inference Accuracy target.") - - parser.add_argument("--fps_target", - type=float, - default=None, - help="Model inference FPS target.") - - parser.add_argument("--perf_only", - type=bool, - default=False, - help="Run performance test only") - - args = parser.parse_args() - - return args - -def get_dataloader(data_path, batch_size, num_workers): - dataset = torchvision.datasets.ImageFolder( - data_path, - transforms.Compose( - [ - transforms.Resize(256, interpolation=InterpolationMode.BILINEAR), - transforms.CenterCrop(224), - transforms.PILToTensor(), - transforms.ConvertImageDtype(torch.float), - transforms.Normalize( - mean=(0.485, 0.456, 0.406), - std=(0.229, 0.224, 0.225) - ) - ] - ) - ) - - dataloader = torch.utils.data.DataLoader(dataset, batch_size, num_workers=num_workers) - - return dataloader - -def get_topk_accuracy(pred, label): - if isinstance(pred, np.ndarray): - pred = torch.from_numpy(pred) - - if isinstance(label, np.ndarray): - label = torch.from_numpy(label) - - top1_acc = 0 - top5_acc = 0 - for idx in range(len(label)): - label_value = label[idx] - if label_value == torch.topk(pred[idx].float(), 1).indices.data: - top1_acc += 1 - top5_acc += 1 - - elif label_value in torch.topk(pred[idx].float(), 5).indices.data: - top5_acc += 1 - - return top1_acc, top5_acc - -def main(): - args = parse_args() - - batch_size = args.batchsize - - # create iluvatar target & device - target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer") - device = tvm.device(target.kind.name, 0) - - # load engine - lib = tvm.runtime.load_module(args.engine) - - # create runtime from engine - module = tvm.contrib.graph_executor.GraphModule(lib["default"](device)) - - # just run perf test - if args.perf_only: - ftimer = module.module.time_evaluator("run", device, number=100, repeat=1) - prof_res = np.array(ftimer().results) * 1000 - fps = batch_size * 1000 / np.mean(prof_res) - print(f"\n* Mean inference time: {np.mean(prof_res):.3f} ms, Mean fps: {fps:.3f}") - else: - # warm up - for _ in range(args.warmup): - module.run() - - # get dataloader - dataloader = get_dataloader(args.datasets, batch_size, args.num_workers) - - top1_acc = 0 - top5_acc = 0 - total_num = 0 - - for image, label in tqdm(dataloader): - - # pad the last batch - pad_batch = len(image) != batch_size - - if pad_batch: - origin_size = len(image) - image = np.resize(image, (batch_size, *image.shape[1:])) - - module.set_input(args.input_name, tvm.nd.array(image, device)) - - # run inference - module.run() - - pred = module.get_output(0).asnumpy() - - if pad_batch: - pred = pred[:origin_size] - - # get batch accuracy - batch_top1_acc, batch_top5_acc = get_topk_accuracy(pred, label) - - top1_acc += batch_top1_acc - top5_acc += batch_top5_acc - total_num += batch_size - - result_stat = {} - result_stat["acc@1"] = round(top1_acc / total_num * 100.0, 3) - result_stat["acc@5"] = round(top5_acc / total_num * 100.0, 3) - - print(f"\n* Top1 acc: {result_stat['acc@1']} %, Top5 acc: {result_stat['acc@5']} %") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/wide_resnet50/igie/quantize.py b/models/cv/classification/wide_resnet50/igie/quantize.py deleted file mode 100644 index e851dcc16e57c9531c37f577a98a593ad2bbb46f..0000000000000000000000000000000000000000 --- a/models/cv/classification/wide_resnet50/igie/quantize.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor 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 onnx -import psutil -import argparse -import numpy as np -from inference import get_dataloader -from onnxruntime.quantization import (CalibrationDataReader, QuantFormat, - quantize_static, QuantType, - CalibrationMethod) - -class CalibrationDataLoader(CalibrationDataReader): - def __init__(self, input_name, dataloader, cnt_limit=100): - self.cnt = 0 - self.input_name = input_name - self.cnt_limit = cnt_limit - self.iter = iter(dataloader) - - # avoid oom - @staticmethod - def _exceed_memory_upper_bound(upper_bound=80): - info = psutil.virtual_memory() - total_percent = info.percent - if total_percent >= upper_bound: - return True - return False - - def get_next(self): - if self._exceed_memory_upper_bound() or self.cnt >= self.cnt_limit: - return None - self.cnt += 1 - print(f"onnx calibration data count: {self.cnt}") - input_info = next(self.iter) - - ort_input = {k: np.array(v) for k, v in zip(self.input_name, input_info)} - return ort_input - -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument("--model_path", - type=str, - required=True, - help="original model path.") - - parser.add_argument("--out_path", - type=str, - required=True, - help="igie export engine path.") - - parser.add_argument("--datasets", - type=str, - required=True, - help="calibration datasets path.") - - parser.add_argument("--num_workers", - type=int, - default=16, - help="number of workers used in pytorch dataloader.") - - args = parser.parse_args() - - return args - -def main(): - args = parse_args() - - model = onnx.load(args.model_path) - input_names = [input.name for input in model.graph.input] - - dataloader = get_dataloader(args.datasets, batch_size=1, num_workers=args.num_workers) - calibration = CalibrationDataLoader(input_names, dataloader, cnt_limit=20) - - quantize_static(args.model_path, - args.out_path, - calibration_data_reader=calibration, - quant_format=QuantFormat.QOperator, - per_channel=False, - activation_type=QuantType.QInt8, - weight_type=QuantType.QInt8, - use_external_data_format=False, - calibrate_method=CalibrationMethod.Percentile, - nodes_to_exclude=['/Softmax'], - extra_options = { - 'ActivationSymmetric': True, - 'WeightSymmetric': True - } - ) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/models/cv/classification/wide_resnet50/igie/requirements.txt b/models/cv/classification/wide_resnet50/igie/requirements.txt deleted file mode 100644 index 9e8111264d4bb2c985cdd10c1de3b894d4e50bef..0000000000000000000000000000000000000000 --- a/models/cv/classification/wide_resnet50/igie/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -onnx -tqdm diff --git a/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_fp16_accuracy.sh b/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_fp16_accuracy.sh index 54098ac48297369e22b602b7ac6bbcb0f8e75633..ec5db555b235c1e6220c3e19c138b5ec0fca782b 100644 --- a/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_fp16_accuracy.sh +++ b/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_fp16_accuracy.sh @@ -20,7 +20,7 @@ model_path="wide_resnet50.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine wide_resnet50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_fp16_performance.sh b/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_fp16_performance.sh index 7fa6ba8cb354152d46c8b0c834a78073bff3670a..e8eac48171e372a9fd54df3a08a6e183e8c6a2bf 100644 --- a/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_fp16_performance.sh +++ b/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_fp16_performance.sh @@ -20,7 +20,7 @@ model_path="wide_resnet50.onnx" datasets_path=${DATASETS_DIR} # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${model_path} \ --input input:${batchsize},3,224,224 \ --precision fp16 \ @@ -28,7 +28,7 @@ python3 build_engine.py \ # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine wide_resnet50_bs_${batchsize}_fp16.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_int8_accuracy.sh b/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_int8_accuracy.sh index 047a5ecb6e7626ed13e3aeabf7684bac2a9d8880..ec0543072fdb8eb651bb3f68c00f5d926aee9971 100644 --- a/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_int8_accuracy.sh +++ b/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_int8_accuracy.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path wide_resnet50_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine wide_resnet50_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_int8_performance.sh b/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_int8_performance.sh index e36dfc1ae963bbcc9e58da54ba06189ca98842a9..91f89c467b9023cf7c76330d43940f06b5b0d36d 100644 --- a/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_int8_performance.sh +++ b/models/cv/classification/wide_resnet50/igie/scripts/infer_wide_resnet50_int8_performance.sh @@ -22,21 +22,21 @@ datasets_path=${DATASETS_DIR} if [ ! -e $quantized_model_path ]; then # quantize model to int8 - python3 quantize.py \ + python3 ${RUN_DIR}quantize.py \ --model_path ${model_path} \ --out_path ${quantized_model_path} \ --datasets ${datasets_path} fi # build engine -python3 build_engine.py \ +python3 ${RUN_DIR}build_engine.py \ --model_path ${quantized_model_path} \ --input input:${batchsize},3,224,224 \ --precision int8 \ --engine_path wide_resnet50_bs_${batchsize}_int8.so # inference -python3 inference.py \ +python3 ${RUN_DIR}inference.py \ --engine wide_resnet50_bs_${batchsize}_int8.so \ --batchsize ${batchsize} \ --input_name input \ diff --git a/tests/model_info.json b/tests/model_info.json index 7972ff1a01b9045386fa99cd676e56ba2704bf41..cacad86d858ab47bddd890a79871a7a72f854d99 100644 --- a/tests/model_info.json +++ b/tests/model_info.json @@ -184,8 +184,8 @@ "github_repo": "", "github_branch": "", "github_path": "", - "datasets": "", - "download_url": "", + "datasets": "https://www.image-net.org/download.php", + "download_url": "https://huggingface.co/openai/clip-vit-base-patch32", "need_third_part": "", "precisions": "", "type": "inference", @@ -2969,6 +2969,70 @@ "hasDemo": false, "demoType": "" }, + { + "model_name": "twins_pcpvt", + "framework": "igie", + "release_version": "25.06", + "release_sdk": "4.2.0", + "release_gpgpu": "BI-V150", + "latest_sdk": "4.2.0", + "latest_gpgpu": "BI-V150", + "category": "cv/classification", + "toolbox": "", + "mdims": "", + "dataset": "", + "license": "", + "model_path": "models/cv/classification/twins_pcpvt/igie", + "readme_file": "models/cv/classification/twins_pcpvt/igie/README.md", + "bitbucket_repo": "", + "bitbucket_branch": "", + "bitbucket_path": "", + "develop_owner": "", + "github_repo": "", + "github_branch": "", + "github_path": "", + "datasets": "https://www.image-net.org/download.php", + "download_url": "https://download.openmmlab.com/mmclassification/v0/twins/twins-pcpvt-small_3rdparty_8xb128_in1k_20220126-ef23c132.pth", + "need_third_part": false, + "precisions": [ + "fp16" + ], + "type": "inference", + "hasDemo": false, + "demoType": "" + }, + { + "model_name": "van_b0", + "framework": "igie", + "release_version": "25.06", + "release_sdk": "4.2.0", + "release_gpgpu": "BI-V150", + "latest_sdk": "4.2.0", + "latest_gpgpu": "BI-V150", + "category": "cv/classification", + "toolbox": "", + "mdims": "", + "dataset": "", + "license": "", + "model_path": "models/cv/classification/van_b0/igie", + "readme_file": "models/cv/classification/van_b0/igie/README.md", + "bitbucket_repo": "", + "bitbucket_branch": "", + "bitbucket_path": "", + "develop_owner": "", + "github_repo": "", + "github_branch": "", + "github_path": "", + "datasets": "https://www.image-net.org/download.php", + "download_url": "https://download.openmmlab.com/mmclassification/v0/van/van-tiny_8xb128_in1k_20220501-385941af.pth", + "need_third_part": false, + "precisions": [ + "fp16" + ], + "type": "inference", + "hasDemo": false, + "demoType": "" + }, { "model_name": "vgg11", "framework": "igie", diff --git a/tests/run_igie.py b/tests/run_igie.py index cc8683b993030c1679d1c11af7386216ba92f188..46e5636b98effaef49606ed74a94596f13013d45 100644 --- a/tests/run_igie.py +++ b/tests/run_igie.py @@ -160,7 +160,8 @@ def run_clf_testcase(model): for prec in model["precisions"]: logging.info(f"Start running {model_name} {prec} test case") script = f""" - export DATASETS_DIR=/mnt/deepspark/data/datasets/{dataset_n} + export DATASETS_DIR=/mnt/deepspark/data/datasets/imagenet-val + export RUN_DIR=../../igie_common/ cd ../{model['model_path']} bash scripts/infer_{model_name}_{prec}_accuracy.sh bash scripts/infer_{model_name}_{prec}_performance.sh