From 986a39b0b7df07cfe53c2d35f9aa2a58711de005 Mon Sep 17 00:00:00 2001 From: "hongliang.yuan" Date: Wed, 14 May 2025 14:56:39 +0800 Subject: [PATCH] add igie common for cls --- .../cv/classification/alexnet/igie/README.md | 5 +- .../classification/alexnet/igie/ci/prepare.sh | 4 +- .../cv/classification/alexnet/igie/export.py | 61 ------ .../scripts/infer_alexnet_fp16_accuracy.sh | 4 +- .../scripts/infer_alexnet_fp16_performance.sh | 4 +- .../scripts/infer_alexnet_int8_accuracy.sh | 6 +- .../scripts/infer_alexnet_int8_performance.sh | 6 +- models/cv/classification/clip/igie/README.md | 6 +- .../convnext_base/igie/README.md | 5 +- .../convnext_base/igie/build_engine.py | 73 ------- .../convnext_base/igie/ci/prepare.sh | 4 +- .../convnext_base/igie/export.py | 61 ------ .../convnext_base/igie/inference.py | 186 ------------------ .../convnext_base/igie/requirements.txt | 2 - .../infer_convnext_base_fp16_accuracy.sh | 4 +- .../infer_convnext_base_fp16_performance.sh | 4 +- .../classification/convnext_s/igie/README.md | 6 +- .../convnext_s/igie/build_engine.py | 73 ------- .../convnext_s/igie/ci/prepare.sh | 5 +- .../classification/convnext_s/igie/export.py | 77 -------- .../convnext_s/igie/inference.py | 185 ----------------- .../convnext_s/igie/requirements.txt | 5 - .../scripts/infer_convnext_s_fp16_accuracy.sh | 4 +- .../infer_convnext_s_fp16_performance.sh | 4 +- .../convnext_small/igie/README.md | 5 +- .../convnext_small/igie/build_engine.py | 73 ------- .../convnext_small/igie/ci/prepare.sh | 4 +- .../convnext_small/igie/export.py | 61 ------ .../convnext_small/igie/inference.py | 186 ------------------ .../convnext_small/igie/requirements.txt | 2 - .../infer_convnext_small_fp16_accuracy.sh | 4 +- .../infer_convnext_small_fp16_performance.sh | 4 +- .../convnext_tiny/igie/README.md | 5 +- .../convnext_tiny/igie/build_engine.py | 73 ------- .../convnext_tiny/igie/ci/prepare.sh | 4 +- .../convnext_tiny/igie/export.py | 61 ------ .../convnext_tiny/igie/inference.py | 186 ------------------ .../convnext_tiny/igie/requirements.txt | 2 - .../infer_convnext_tiny_fp16_accuracy.sh | 4 +- .../infer_convnext_tiny_fp16_performance.sh | 4 +- .../cspdarknet53/igie/README.md | 6 +- .../cspdarknet53/igie/build_engine.py | 73 ------- .../cspdarknet53/igie/ci/prepare.sh | 5 +- .../cspdarknet53/igie/inference.py | 185 ----------------- .../cspdarknet53/igie/requirements.txt | 5 - .../infer_cspdarknet53_fp16_accuracy.sh | 4 +- .../infer_cspdarknet53_fp16_performance.sh | 4 +- .../classification/cspresnet50/igie/README.md | 6 +- .../cspresnet50/igie/build_engine.py | 73 ------- .../cspresnet50/igie/ci/prepare.sh | 5 +- .../classification/cspresnet50/igie/export.py | 78 -------- .../cspresnet50/igie/inference.py | 185 ----------------- .../cspresnet50/igie/requirements.txt | 5 - .../infer_cspresnet50_fp16_accuracy.sh | 4 +- .../infer_cspresnet50_fp16_performance.sh | 4 +- .../cspresnext50/igie/README.md | 6 +- .../cspresnext50/igie/build_engine.py | 73 ------- .../cspresnext50/igie/ci/prepare.sh | 5 +- .../cspresnext50/igie/export.py | 78 -------- .../cspresnext50/igie/inference.py | 185 ----------------- .../cspresnext50/igie/requirements.txt | 5 - .../infer_cspresnext50_fp16_accuracy.sh | 4 +- .../infer_cspresnext50_fp16_performance.sh | 4 +- .../classification/deit_tiny/igie/README.md | 6 +- .../deit_tiny/igie/build_engine.py | 73 ------- .../deit_tiny/igie/ci/prepare.sh | 5 +- .../classification/deit_tiny/igie/export.py | 78 -------- .../deit_tiny/igie/inference.py | 185 ----------------- .../deit_tiny/igie/requirements.txt | 5 - .../scripts/infer_deit_tiny_fp16_accuracy.sh | 4 +- .../infer_deit_tiny_fp16_performance.sh | 4 +- .../classification/densenet121/igie/README.md | 5 +- .../densenet121/igie/build_engine.py | 73 ------- .../densenet121/igie/ci/prepare.sh | 4 +- .../classification/densenet121/igie/export.py | 74 ------- .../densenet121/igie/inference.py | 186 ------------------ .../densenet121/igie/requirements.txt | 2 - .../infer_densenet121_fp16_accuracy.sh | 4 +- .../infer_densenet121_fp16_performance.sh | 4 +- .../classification/densenet161/igie/README.md | 5 +- .../densenet161/igie/build_engine.py | 73 ------- .../densenet161/igie/ci/prepare.sh | 4 +- .../densenet161/igie/inference.py | 186 ------------------ .../densenet161/igie/requirements.txt | 2 - .../infer_densenet161_fp16_accuracy.sh | 4 +- .../infer_densenet161_fp16_performance.sh | 4 +- .../classification/densenet169/igie/README.md | 5 +- .../densenet169/igie/build_engine.py | 73 ------- .../densenet169/igie/ci/prepare.sh | 4 +- .../classification/densenet169/igie/export.py | 74 ------- .../densenet169/igie/inference.py | 186 ------------------ .../densenet169/igie/requirements.txt | 2 - .../infer_densenet169_fp16_accuracy.sh | 4 +- .../infer_densenet169_fp16_performance.sh | 4 +- .../classification/densenet201/igie/README.md | 5 +- .../densenet201/igie/build_engine.py | 73 ------- .../densenet201/igie/ci/prepare.sh | 4 +- .../classification/densenet201/igie/export.py | 74 ------- .../densenet201/igie/inference.py | 186 ------------------ .../densenet201/igie/requirements.txt | 2 - .../infer_densenet201_fp16_accuracy.sh | 4 +- .../infer_densenet201_fp16_performance.sh | 4 +- .../efficientnet_b0/igie/README.md | 5 +- .../efficientnet_b0/igie/build_engine.py | 73 ------- .../efficientnet_b0/igie/ci/prepare.sh | 4 +- .../efficientnet_b0/igie/export.py | 61 ------ .../efficientnet_b0/igie/inference.py | 186 ------------------ .../efficientnet_b0/igie/requirements.txt | 2 - .../infer_efficientnet_b0_fp16_accuracy.sh | 4 +- .../infer_efficientnet_b0_fp16_performance.sh | 4 +- .../efficientnet_b1/igie/README.md | 5 +- .../efficientnet_b1/igie/build_engine.py | 73 ------- .../efficientnet_b1/igie/ci/prepare.sh | 4 +- .../efficientnet_b1/igie/export.py | 61 ------ .../efficientnet_b1/igie/inference.py | 186 ------------------ .../efficientnet_b1/igie/requirements.txt | 2 - .../infer_efficientnet_b1_fp16_accuracy.sh | 4 +- .../infer_efficientnet_b1_fp16_performance.sh | 4 +- .../efficientnet_b2/igie/README.md | 5 +- .../efficientnet_b2/igie/build_engine.py | 73 ------- .../efficientnet_b2/igie/ci/prepare.sh | 4 +- .../efficientnet_b2/igie/export.py | 61 ------ .../efficientnet_b2/igie/inference.py | 186 ------------------ .../efficientnet_b2/igie/requirements.txt | 2 - .../infer_efficientnet_b2_fp16_accuracy.sh | 4 +- .../infer_efficientnet_b2_fp16_performance.sh | 4 +- .../efficientnet_b3/igie/README.md | 5 +- .../efficientnet_b3/igie/build_engine.py | 73 ------- .../efficientnet_b3/igie/ci/prepare.sh | 4 +- .../efficientnet_b3/igie/export.py | 61 ------ .../efficientnet_b3/igie/inference.py | 186 ------------------ .../efficientnet_b3/igie/requirements.txt | 2 - .../infer_efficientnet_b3_fp16_accuracy.sh | 4 +- .../infer_efficientnet_b3_fp16_performance.sh | 4 +- .../efficientnet_b4/igie/README.md | 5 +- .../efficientnet_b4/igie/build_engine.py | 73 ------- .../efficientnet_b4/igie/ci/prepare.sh | 4 +- .../efficientnet_b4/igie/export.py | 61 ------ .../efficientnet_b4/igie/inference.py | 186 ------------------ .../efficientnet_b4/igie/requirements.txt | 2 - .../infer_efficientnet_b4_fp16_accuracy.sh | 4 +- .../infer_efficientnet_b4_fp16_performance.sh | 4 +- .../efficientnet_b5/igie/README.md | 5 +- .../efficientnet_b5/igie/build_engine.py | 73 ------- .../efficientnet_b5/igie/ci/prepare.sh | 4 +- .../efficientnet_b5/igie/export.py | 61 ------ .../efficientnet_b5/igie/inference.py | 186 ------------------ .../efficientnet_b5/igie/requirements.txt | 2 - .../infer_efficientnet_b5_fp16_accuracy.sh | 4 +- .../infer_efficientnet_b5_fp16_performance.sh | 4 +- .../efficientnet_v2/igie/README.md | 5 +- .../efficientnet_v2/igie/build_engine.py | 73 ------- .../efficientnet_v2/igie/ci/prepare.sh | 4 +- .../efficientnet_v2/igie/export.py | 61 ------ .../efficientnet_v2/igie/inference.py | 186 ------------------ .../efficientnet_v2/igie/requirements.txt | 2 - .../infer_efficientnet_v2_fp16_accuracy.sh | 4 +- .../infer_efficientnet_v2_fp16_performance.sh | 4 +- .../efficientnet_v2_s/igie/README.md | 5 +- .../efficientnet_v2_s/igie/build_engine.py | 73 ------- .../efficientnet_v2_s/igie/ci/prepare.sh | 4 +- .../efficientnet_v2_s/igie/export.py | 61 ------ .../efficientnet_v2_s/igie/inference.py | 186 ------------------ .../efficientnet_v2_s/igie/requirements.txt | 2 - .../infer_efficientnet_v2_s_fp16_accuracy.sh | 4 +- ...nfer_efficientnet_v2_s_fp16_performance.sh | 4 +- .../efficientnetv2_rw_t/igie/README.md | 6 +- .../efficientnetv2_rw_t/igie/build_engine.py | 73 ------- .../efficientnetv2_rw_t/igie/ci/prepare.sh | 5 +- .../efficientnetv2_rw_t/igie/inference.py | 183 ----------------- ...infer_efficientnetv2_rw_t_fp16_accuracy.sh | 4 +- ...er_efficientnetv2_rw_t_fp16_performance.sh | 4 +- .../classification/googlenet/igie/README.md | 5 +- .../googlenet/igie/build_engine.py | 73 ------- .../googlenet/igie/ci/prepare.sh | 4 +- .../classification/googlenet/igie/export.py | 61 ------ .../googlenet/igie/inference.py | 186 ------------------ .../classification/googlenet/igie/quantize.py | 103 ---------- .../googlenet/igie/requirements.txt | 2 - .../scripts/infer_googlenet_fp16_accuracy.sh | 4 +- .../infer_googlenet_fp16_performance.sh | 4 +- .../scripts/infer_googlenet_int8_accuracy.sh | 6 +- .../infer_googlenet_int8_performance.sh | 6 +- .../classification/hrnet_w18/igie/README.md | 6 +- .../hrnet_w18/igie/build_engine.py | 73 ------- .../hrnet_w18/igie/ci/prepare.sh | 5 +- .../classification/hrnet_w18/igie/export.py | 78 -------- .../hrnet_w18/igie/inference.py | 185 ----------------- .../hrnet_w18/igie/requirements.txt | 5 - .../scripts/infer_hrnet_w18_fp16_accuracy.sh | 4 +- .../infer_hrnet_w18_fp16_performance.sh | 4 +- .../igie => igie_common}/build_engine.py | 0 .../igie => igie_common}/export.py | 34 +++- .../export.py => igie_common/export_mmcls.py} | 22 ++- .../export.py => igie_common/export_timm.py} | 8 +- .../igie => igie_common}/inference.py | 0 .../{alexnet/igie => igie_common}/quantize.py | 0 .../igie => igie_common}/requirements.txt | 0 .../inception_v3/igie/README.md | 5 +- .../inception_v3/igie/build_engine.py | 73 ------- .../inception_v3/igie/ci/prepare.sh | 4 +- .../inception_v3/igie/export.py | 61 ------ .../inception_v3/igie/inference.py | 186 ------------------ .../inception_v3/igie/quantize.py | 103 ---------- .../inception_v3/igie/requirements.txt | 2 - .../infer_inception_v3_fp16_accuracy.sh | 4 +- .../infer_inception_v3_fp16_performance.sh | 4 +- .../infer_inception_v3_int8_accuracy.sh | 6 +- .../infer_inception_v3_int8_performance.sh | 6 +- .../mlp_mixer_base/igie/README.md | 6 +- .../mlp_mixer_base/igie/build_engine.py | 73 ------- .../mlp_mixer_base/igie/ci/prepare.sh | 5 +- .../mlp_mixer_base/igie/export.py | 78 -------- .../mlp_mixer_base/igie/inference.py | 185 ----------------- .../mlp_mixer_base/igie/requirements.txt | 5 - .../infer_mlp_mixer_base_fp16_accuracy.sh | 4 +- .../infer_mlp_mixer_base_fp16_performance.sh | 4 +- .../classification/mnasnet0_5/igie/README.md | 5 +- .../mnasnet0_5/igie/build_engine.py | 73 ------- .../mnasnet0_5/igie/ci/prepare.sh | 4 +- .../classification/mnasnet0_5/igie/export.py | 61 ------ .../mnasnet0_5/igie/inference.py | 186 ------------------ .../mnasnet0_5/igie/requirements.txt | 2 - .../scripts/infer_mnasnet0_5_fp16_accuracy.sh | 4 +- .../infer_mnasnet0_5_fp16_performance.sh | 4 +- .../classification/mnasnet0_75/igie/README.md | 5 +- .../mnasnet0_75/igie/build_engine.py | 73 ------- .../mnasnet0_75/igie/ci/prepare.sh | 4 +- .../classification/mnasnet0_75/igie/export.py | 61 ------ .../mnasnet0_75/igie/inference.py | 186 ------------------ .../mnasnet0_75/igie/requirements.txt | 2 - .../infer_mnasnet0_75_fp16_accuracy.sh | 4 +- .../infer_mnasnet0_75_fp16_performance.sh | 4 +- .../classification/mnasnet1_0/igie/README.md | 5 +- .../mnasnet1_0/igie/build_engine.py | 73 ------- .../mnasnet1_0/igie/ci/prepare.sh | 4 +- .../classification/mnasnet1_0/igie/export.py | 61 ------ .../mnasnet1_0/igie/inference.py | 186 ------------------ .../mnasnet1_0/igie/requirements.txt | 2 - .../scripts/infer_mnasnet1_0_fp16_accuracy.sh | 4 +- .../infer_mnasnet1_0_fp16_performance.sh | 4 +- .../mobilenet_v2/igie/README.md | 6 +- .../mobilenet_v2/igie/build_engine.py | 73 ------- .../mobilenet_v2/igie/ci/prepare.sh | 5 +- .../mobilenet_v2/igie/export.py | 61 ------ .../mobilenet_v2/igie/inference.py | 186 ------------------ .../mobilenet_v2/igie/quantize.py | 104 ---------- .../mobilenet_v2/igie/requirements.txt | 3 - .../infer_mobilenet_v2_fp16_accuracy.sh | 4 +- .../infer_mobilenet_v2_fp16_performance.sh | 4 +- .../infer_mobilenet_v2_int8_accuracy.sh | 6 +- .../infer_mobilenet_v2_int8_performance.sh | 6 +- .../mobilenet_v3/igie/README.md | 5 +- .../mobilenet_v3/igie/build_engine.py | 73 ------- .../mobilenet_v3/igie/ci/prepare.sh | 4 +- .../mobilenet_v3/igie/export.py | 61 ------ .../mobilenet_v3/igie/inference.py | 186 ------------------ .../mobilenet_v3/igie/requirements.txt | 2 - .../infer_mobilenet_v3_fp16_accuracy.sh | 4 +- .../infer_mobilenet_v3_fp16_performance.sh | 4 +- .../mobilenet_v3_large/igie/README.md | 5 +- .../mobilenet_v3_large/igie/build_engine.py | 73 ------- .../mobilenet_v3_large/igie/ci/prepare.sh | 4 +- .../mobilenet_v3_large/igie/export.py | 61 ------ .../mobilenet_v3_large/igie/inference.py | 186 ------------------ .../mobilenet_v3_large/igie/requirements.txt | 2 - .../infer_mobilenet_v3_large_fp16_accuracy.sh | 4 +- ...fer_mobilenet_v3_large_fp16_performance.sh | 4 +- .../classification/mvitv2_base/igie/README.md | 6 +- .../mvitv2_base/igie/build_engine.py | 73 ------- .../mvitv2_base/igie/ci/prepare.sh | 5 +- .../classification/mvitv2_base/igie/export.py | 74 ------- .../mvitv2_base/igie/inference.py | 185 ----------------- .../mvitv2_base/igie/requirements.txt | 5 - .../infer_mvitv2_base_fp16_accuracy.sh | 4 +- .../infer_mvitv2_base_fp16_performance.sh | 4 +- .../regnet_x_16gf/igie/README.md | 5 +- .../regnet_x_16gf/igie/build_engine.py | 73 ------- .../regnet_x_16gf/igie/ci/prepare.sh | 4 +- .../regnet_x_16gf/igie/export.py | 61 ------ .../regnet_x_16gf/igie/inference.py | 186 ------------------ .../regnet_x_16gf/igie/requirements.txt | 2 - .../infer_regnet_x_16gf_fp16_accuracy.sh | 4 +- .../infer_regnet_x_16gf_fp16_performance.sh | 4 +- .../regnet_x_1_6gf/igie/README.md | 5 +- .../regnet_x_1_6gf/igie/build_engine.py | 73 ------- .../regnet_x_1_6gf/igie/ci/prepare.sh | 4 +- .../regnet_x_1_6gf/igie/export.py | 61 ------ .../regnet_x_1_6gf/igie/inference.py | 186 ------------------ .../regnet_x_1_6gf/igie/requirements.txt | 2 - .../infer_regnet_x_1_6gf_fp16_accuracy.sh | 4 +- .../infer_regnet_x_1_6gf_fp16_performance.sh | 4 +- .../regnet_x_3_2gf/igie/README.md | 5 +- .../regnet_x_3_2gf/igie/build_engine.py | 73 ------- .../regnet_x_3_2gf/igie/ci/prepare.sh | 4 +- .../regnet_x_3_2gf/igie/export.py | 61 ------ .../regnet_x_3_2gf/igie/inference.py | 186 ------------------ .../regnet_x_3_2gf/igie/requirements.txt | 2 - .../infer_regnet_x_3_2gf_fp16_accuracy.sh | 4 +- .../infer_regnet_x_3_2gf_fp16_performance.sh | 4 +- .../regnet_y_16gf/igie/README.md | 5 +- .../regnet_y_16gf/igie/build_engine.py | 73 ------- .../regnet_y_16gf/igie/ci/prepare.sh | 4 +- .../regnet_y_16gf/igie/export.py | 61 ------ .../regnet_y_16gf/igie/inference.py | 186 ------------------ .../regnet_y_16gf/igie/requirements.txt | 2 - .../infer_regnet_y_16gf_fp16_accuracy.sh | 4 +- .../infer_regnet_y_16gf_fp16_performance.sh | 4 +- .../regnet_y_1_6gf/igie/README.md | 5 +- .../regnet_y_1_6gf/igie/build_engine.py | 73 ------- .../regnet_y_1_6gf/igie/ci/prepare.sh | 4 +- .../regnet_y_1_6gf/igie/export.py | 61 ------ .../regnet_y_1_6gf/igie/inference.py | 186 ------------------ .../regnet_y_1_6gf/igie/requirements.txt | 2 - .../infer_regnet_y_1_6gf_fp16_accuracy.sh | 4 +- .../infer_regnet_y_1_6gf_fp16_performance.sh | 4 +- .../cv/classification/repvgg/igie/README.md | 6 +- .../repvgg/igie/build_engine.py | 73 ------- .../classification/repvgg/igie/ci/prepare.sh | 5 +- .../cv/classification/repvgg/igie/export.py | 78 -------- .../classification/repvgg/igie/inference.py | 185 ----------------- .../repvgg/igie/requirements.txt | 5 - .../scripts/infer_repvgg_fp16_accuracy.sh | 4 +- .../scripts/infer_repvgg_fp16_performance.sh | 4 +- .../classification/res2net50/igie/README.md | 6 +- .../res2net50/igie/build_engine.py | 73 ------- .../res2net50/igie/ci/prepare.sh | 5 +- .../classification/res2net50/igie/export.py | 78 -------- .../res2net50/igie/inference.py | 185 ----------------- .../res2net50/igie/requirements.txt | 5 - .../scripts/infer_res2net50_fp16_accuracy.sh | 4 +- .../infer_res2net50_fp16_performance.sh | 4 +- .../classification/resnest50/igie/README.md | 6 +- .../resnest50/igie/build_engine.py | 73 ------- .../resnest50/igie/ci/prepare.sh | 5 +- .../classification/resnest50/igie/export.py | 61 ------ .../resnest50/igie/inference.py | 185 ----------------- .../resnest50/igie/requirements.txt | 4 - .../scripts/infer_resnest50_fp16_accuracy.sh | 4 +- .../infer_resnest50_fp16_performance.sh | 4 +- .../classification/resnet101/igie/README.md | 5 +- .../resnet101/igie/build_engine.py | 73 ------- .../resnet101/igie/ci/prepare.sh | 4 +- .../classification/resnet101/igie/export.py | 61 ------ .../resnet101/igie/inference.py | 186 ------------------ .../classification/resnet101/igie/quantize.py | 104 ---------- .../resnet101/igie/requirements.txt | 2 - .../scripts/infer_resnet101_fp16_accuracy.sh | 4 +- .../infer_resnet101_fp16_performance.sh | 4 +- .../scripts/infer_resnet101_int8_accuracy.sh | 6 +- .../infer_resnet101_int8_performance.sh | 6 +- .../classification/resnet152/igie/README.md | 5 +- .../resnet152/igie/build_engine.py | 73 ------- .../resnet152/igie/ci/prepare.sh | 4 +- .../classification/resnet152/igie/export.py | 61 ------ .../resnet152/igie/inference.py | 186 ------------------ .../classification/resnet152/igie/quantize.py | 104 ---------- .../resnet152/igie/requirements.txt | 2 - .../scripts/infer_resnet152_fp16_accuracy.sh | 4 +- .../infer_resnet152_fp16_performance.sh | 4 +- .../scripts/infer_resnet152_int8_accuracy.sh | 6 +- .../infer_resnet152_int8_performance.sh | 6 +- .../cv/classification/resnet18/igie/README.md | 5 +- .../resnet18/igie/build_engine.py | 73 ------- .../resnet18/igie/ci/prepare.sh | 4 +- .../cv/classification/resnet18/igie/export.py | 61 ------ .../classification/resnet18/igie/inference.py | 186 ------------------ .../classification/resnet18/igie/quantize.py | 104 ---------- .../resnet18/igie/requirements.txt | 2 - .../scripts/infer_resnet18_fp16_accuracy.sh | 4 +- .../infer_resnet18_fp16_performance.sh | 4 +- .../scripts/infer_resnet18_int8_accuracy.sh | 6 +- .../infer_resnet18_int8_performance.sh | 6 +- .../cv/classification/resnet50/igie/README.md | 5 +- .../resnet50/igie/build_engine.py | 73 ------- .../resnet50/igie/ci/prepare.sh | 4 +- .../cv/classification/resnet50/igie/export.py | 61 ------ .../classification/resnet50/igie/inference.py | 186 ------------------ .../classification/resnet50/igie/quantize.py | 104 ---------- .../resnet50/igie/requirements.txt | 2 - .../scripts/infer_resnet50_fp16_accuracy.sh | 4 +- .../infer_resnet50_fp16_performance.sh | 4 +- .../scripts/infer_resnet50_int8_accuracy.sh | 6 +- .../infer_resnet50_int8_performance.sh | 6 +- .../classification/resnetv1d50/igie/README.md | 6 +- .../resnetv1d50/igie/build_engine.py | 73 ------- .../resnetv1d50/igie/ci/prepare.sh | 5 +- .../classification/resnetv1d50/igie/export.py | 76 ------- .../resnetv1d50/igie/inference.py | 185 ----------------- .../resnetv1d50/igie/requirements.txt | 4 - .../infer_resnetv1d50_fp16_accuracy.sh | 4 +- .../infer_resnetv1d50_fp16_performance.sh | 4 +- .../resnext101_32x8d/igie/README.md | 5 +- .../resnext101_32x8d/igie/build_engine.py | 73 ------- .../resnext101_32x8d/igie/ci/prepare.sh | 4 +- .../resnext101_32x8d/igie/export.py | 61 ------ .../resnext101_32x8d/igie/inference.py | 186 ------------------ .../resnext101_32x8d/igie/requirements.txt | 2 - .../infer_resnext101_32x8d_fp16_accuracy.sh | 4 +- ...infer_resnext101_32x8d_fp16_performance.sh | 4 +- .../resnext101_64x4d/igie/README.md | 5 +- .../resnext101_64x4d/igie/build_engine.py | 73 ------- .../resnext101_64x4d/igie/ci/prepare.sh | 4 +- .../resnext101_64x4d/igie/export.py | 61 ------ .../resnext101_64x4d/igie/inference.py | 186 ------------------ .../resnext101_64x4d/igie/requirements.txt | 2 - .../infer_resnext101_64x4d_fp16_accuracy.sh | 4 +- ...infer_resnext101_64x4d_fp16_performance.sh | 4 +- .../resnext50_32x4d/igie/README.md | 5 +- .../resnext50_32x4d/igie/build_engine.py | 73 ------- .../resnext50_32x4d/igie/ci/prepare.sh | 4 +- .../resnext50_32x4d/igie/export.py | 61 ------ .../resnext50_32x4d/igie/inference.py | 186 ------------------ .../resnext50_32x4d/igie/requirements.txt | 2 - .../infer_resnext50_32x4d_fp16_accuracy.sh | 4 +- .../infer_resnext50_32x4d_fp16_performance.sh | 4 +- .../classification/seresnet50/igie/README.md | 6 +- .../seresnet50/igie/build_engine.py | 73 ------- .../seresnet50/igie/ci/prepare.sh | 5 +- .../classification/seresnet50/igie/export.py | 78 -------- .../seresnet50/igie/inference.py | 185 ----------------- .../seresnet50/igie/requirements.txt | 4 - .../scripts/infer_seresnet50_fp16_accuracy.sh | 4 +- .../infer_seresnet50_fp16_performance.sh | 4 +- .../shufflenetv2_x0_5/igie/README.md | 5 +- .../shufflenetv2_x0_5/igie/build_engine.py | 73 ------- .../shufflenetv2_x0_5/igie/ci/prepare.sh | 4 +- .../shufflenetv2_x0_5/igie/export.py | 61 ------ .../shufflenetv2_x0_5/igie/inference.py | 186 ------------------ .../shufflenetv2_x0_5/igie/requirements.txt | 2 - .../infer_shufflenetv2_x0_5_fp16_accuracy.sh | 4 +- ...nfer_shufflenetv2_x0_5_fp16_performance.sh | 4 +- .../shufflenetv2_x1_0/igie/README.md | 5 +- .../shufflenetv2_x1_0/igie/build_engine.py | 73 ------- .../shufflenetv2_x1_0/igie/ci/prepare.sh | 2 +- .../shufflenetv2_x1_0/igie/export.py | 61 ------ .../shufflenetv2_x1_0/igie/inference.py | 186 ------------------ .../shufflenetv2_x1_0/igie/requirements.txt | 2 - .../infer_shufflenetv2_x1_0_fp16_accuracy.sh | 4 +- ...nfer_shufflenetv2_x1_0_fp16_performance.sh | 4 +- .../shufflenetv2_x1_5/igie/README.md | 5 +- .../shufflenetv2_x1_5/igie/build_engine.py | 73 ------- .../shufflenetv2_x1_5/igie/ci/prepare.sh | 4 +- .../shufflenetv2_x1_5/igie/export.py | 61 ------ .../shufflenetv2_x1_5/igie/inference.py | 186 ------------------ .../shufflenetv2_x1_5/igie/requirements.txt | 2 - .../infer_shufflenetv2_x1_5_fp16_accuracy.sh | 4 +- ...nfer_shufflenetv2_x1_5_fp16_performance.sh | 4 +- .../shufflenetv2_x2_0/igie/README.md | 5 +- .../shufflenetv2_x2_0/igie/build_engine.py | 73 ------- .../shufflenetv2_x2_0/igie/ci/prepare.sh | 4 +- .../shufflenetv2_x2_0/igie/export.py | 61 ------ .../shufflenetv2_x2_0/igie/inference.py | 186 ------------------ .../shufflenetv2_x2_0/igie/requirements.txt | 2 - .../infer_shufflenetv2_x2_0_fp16_accuracy.sh | 4 +- ...nfer_shufflenetv2_x2_0_fp16_performance.sh | 4 +- .../squeezenet_v1_0/igie/README.md | 5 +- .../squeezenet_v1_0/igie/build_engine.py | 73 ------- .../squeezenet_v1_0/igie/ci/prepare.sh | 4 +- .../squeezenet_v1_0/igie/export.py | 61 ------ .../squeezenet_v1_0/igie/inference.py | 186 ------------------ .../squeezenet_v1_0/igie/requirements.txt | 2 - .../infer_squeezenet_v1_0_fp16_accuracy.sh | 4 +- .../infer_squeezenet_v1_0_fp16_performance.sh | 4 +- .../squeezenet_v1_1/igie/README.md | 5 +- .../squeezenet_v1_1/igie/build_engine.py | 73 ------- .../squeezenet_v1_1/igie/ci/prepare.sh | 4 +- .../squeezenet_v1_1/igie/export.py | 61 ------ .../squeezenet_v1_1/igie/inference.py | 186 ------------------ .../squeezenet_v1_1/igie/requirements.txt | 2 - .../infer_squeezenet_v1_1_fp16_accuracy.sh | 4 +- .../infer_squeezenet_v1_1_fp16_performance.sh | 4 +- .../cv/classification/svt_base/igie/README.md | 6 +- .../svt_base/igie/build_engine.py | 73 ------- .../svt_base/igie/ci/prepare.sh | 5 +- .../cv/classification/svt_base/igie/export.py | 78 -------- .../classification/svt_base/igie/inference.py | 185 ----------------- .../svt_base/igie/requirements.txt | 5 - .../scripts/infer_svt_base_fp16_accuracy.sh | 4 +- .../infer_svt_base_fp16_performance.sh | 4 +- .../classification/twins_pcpvt/igie/README.md | 6 +- .../twins_pcpvt/igie/build_engine.py | 73 ------- .../twins_pcpvt/igie/ci/prepare.sh | 5 +- .../classification/twins_pcpvt/igie/export.py | 78 -------- .../twins_pcpvt/igie/inference.py | 185 ----------------- .../twins_pcpvt/igie/requirements.txt | 5 - .../infer_twins_pcpvt_fp16_accuracy.sh | 4 +- .../infer_twins_pcpvt_fp16_performance.sh | 4 +- .../cv/classification/van_b0/igie/README.md | 6 +- .../van_b0/igie/build_engine.py | 73 ------- .../classification/van_b0/igie/ci/prepare.sh | 5 +- .../cv/classification/van_b0/igie/export.py | 78 -------- .../classification/van_b0/igie/inference.py | 185 ----------------- .../van_b0/igie/requirements.txt | 4 - .../scripts/infer_van_b0_fp16_accuracy.sh | 4 +- .../scripts/infer_van_b0_fp16_performance.sh | 4 +- models/cv/classification/vgg11/igie/README.md | 5 +- .../classification/vgg11/igie/build_engine.py | 73 ------- .../classification/vgg11/igie/ci/prepare.sh | 4 +- models/cv/classification/vgg11/igie/export.py | 61 ------ .../cv/classification/vgg11/igie/inference.py | 186 ------------------ .../vgg11/igie/requirements.txt | 2 - .../igie/scripts/infer_vgg11_fp16_accuracy.sh | 4 +- .../scripts/infer_vgg11_fp16_performance.sh | 4 +- models/cv/classification/vgg16/igie/README.md | 5 +- .../classification/vgg16/igie/build_engine.py | 73 ------- .../classification/vgg16/igie/ci/prepare.sh | 4 +- models/cv/classification/vgg16/igie/export.py | 61 ------ .../cv/classification/vgg16/igie/inference.py | 186 ------------------ .../cv/classification/vgg16/igie/quantize.py | 103 ---------- .../vgg16/igie/requirements.txt | 2 - .../igie/scripts/infer_vgg16_fp16_accuracy.sh | 4 +- .../scripts/infer_vgg16_fp16_performance.sh | 4 +- .../igie/scripts/infer_vgg16_int8_accuracy.sh | 6 +- .../scripts/infer_vgg16_int8_performance.sh | 6 +- models/cv/classification/vgg19/igie/README.md | 5 +- .../classification/vgg19/igie/build_engine.py | 73 ------- .../classification/vgg19/igie/ci/prepare.sh | 4 +- models/cv/classification/vgg19/igie/export.py | 61 ------ .../cv/classification/vgg19/igie/inference.py | 186 ------------------ .../vgg19/igie/requirements.txt | 2 - .../igie/scripts/infer_vgg19_fp16_accuracy.sh | 4 +- .../scripts/infer_vgg19_fp16_performance.sh | 4 +- .../cv/classification/vgg19_bn/igie/README.md | 5 +- .../vgg19_bn/igie/build_engine.py | 73 ------- .../vgg19_bn/igie/ci/prepare.sh | 4 +- .../cv/classification/vgg19_bn/igie/export.py | 61 ------ .../classification/vgg19_bn/igie/inference.py | 186 ------------------ .../vgg19_bn/igie/requirements.txt | 2 - .../scripts/infer_vgg19_bn_fp16_accuracy.sh | 4 +- .../infer_vgg19_bn_fp16_performance.sh | 4 +- .../wide_resnet101/igie/README.md | 5 +- .../wide_resnet101/igie/build_engine.py | 73 ------- .../wide_resnet101/igie/ci/prepare.sh | 4 +- .../wide_resnet101/igie/export.py | 61 ------ .../wide_resnet101/igie/inference.py | 186 ------------------ .../wide_resnet101/igie/requirements.txt | 2 - .../infer_wide_resnet101_fp16_accuracy.sh | 4 +- .../infer_wide_resnet101_fp16_performance.sh | 4 +- .../wide_resnet50/igie/README.md | 5 +- .../wide_resnet50/igie/build_engine.py | 73 ------- .../wide_resnet50/igie/ci/prepare.sh | 4 +- .../wide_resnet50/igie/export.py | 61 ------ .../wide_resnet50/igie/inference.py | 186 ------------------ .../wide_resnet50/igie/quantize.py | 104 ---------- .../wide_resnet50/igie/requirements.txt | 2 - .../infer_wide_resnet50_fp16_accuracy.sh | 4 +- .../infer_wide_resnet50_fp16_performance.sh | 4 +- .../infer_wide_resnet50_int8_accuracy.sh | 6 +- .../infer_wide_resnet50_int8_performance.sh | 6 +- tests/model_info.json | 68 ++++++- tests/run_igie.py | 3 +- 552 files changed, 802 insertions(+), 22311 deletions(-) delete mode 100644 models/cv/classification/alexnet/igie/export.py delete mode 100644 models/cv/classification/convnext_base/igie/build_engine.py delete mode 100644 models/cv/classification/convnext_base/igie/export.py delete mode 100644 models/cv/classification/convnext_base/igie/inference.py delete mode 100644 models/cv/classification/convnext_base/igie/requirements.txt delete mode 100644 models/cv/classification/convnext_s/igie/build_engine.py delete mode 100644 models/cv/classification/convnext_s/igie/export.py delete mode 100644 models/cv/classification/convnext_s/igie/inference.py delete mode 100644 models/cv/classification/convnext_s/igie/requirements.txt delete mode 100644 models/cv/classification/convnext_small/igie/build_engine.py delete mode 100644 models/cv/classification/convnext_small/igie/export.py delete mode 100644 models/cv/classification/convnext_small/igie/inference.py delete mode 100644 models/cv/classification/convnext_small/igie/requirements.txt delete mode 100644 models/cv/classification/convnext_tiny/igie/build_engine.py delete mode 100644 models/cv/classification/convnext_tiny/igie/export.py delete mode 100644 models/cv/classification/convnext_tiny/igie/inference.py delete mode 100644 models/cv/classification/convnext_tiny/igie/requirements.txt delete mode 100644 models/cv/classification/cspdarknet53/igie/build_engine.py delete mode 100644 models/cv/classification/cspdarknet53/igie/inference.py delete mode 100644 models/cv/classification/cspdarknet53/igie/requirements.txt delete mode 100644 models/cv/classification/cspresnet50/igie/build_engine.py delete mode 100644 models/cv/classification/cspresnet50/igie/export.py delete mode 100644 models/cv/classification/cspresnet50/igie/inference.py delete mode 100644 models/cv/classification/cspresnet50/igie/requirements.txt delete mode 100644 models/cv/classification/cspresnext50/igie/build_engine.py delete mode 100644 models/cv/classification/cspresnext50/igie/export.py delete mode 100644 models/cv/classification/cspresnext50/igie/inference.py delete mode 100644 models/cv/classification/cspresnext50/igie/requirements.txt delete mode 100644 models/cv/classification/deit_tiny/igie/build_engine.py delete mode 100644 models/cv/classification/deit_tiny/igie/export.py delete mode 100644 models/cv/classification/deit_tiny/igie/inference.py delete mode 100644 models/cv/classification/deit_tiny/igie/requirements.txt delete mode 100644 models/cv/classification/densenet121/igie/build_engine.py delete mode 100644 models/cv/classification/densenet121/igie/export.py delete mode 100644 models/cv/classification/densenet121/igie/inference.py delete mode 100644 models/cv/classification/densenet121/igie/requirements.txt delete mode 100644 models/cv/classification/densenet161/igie/build_engine.py delete mode 100644 models/cv/classification/densenet161/igie/inference.py delete mode 100644 models/cv/classification/densenet161/igie/requirements.txt delete mode 100644 models/cv/classification/densenet169/igie/build_engine.py delete mode 100644 models/cv/classification/densenet169/igie/export.py delete mode 100644 models/cv/classification/densenet169/igie/inference.py delete mode 100644 models/cv/classification/densenet169/igie/requirements.txt delete mode 100644 models/cv/classification/densenet201/igie/build_engine.py delete mode 100644 models/cv/classification/densenet201/igie/export.py delete mode 100644 models/cv/classification/densenet201/igie/inference.py delete mode 100644 models/cv/classification/densenet201/igie/requirements.txt delete mode 100644 models/cv/classification/efficientnet_b0/igie/build_engine.py delete mode 100644 models/cv/classification/efficientnet_b0/igie/export.py delete mode 100644 models/cv/classification/efficientnet_b0/igie/inference.py delete mode 100644 models/cv/classification/efficientnet_b0/igie/requirements.txt delete mode 100644 models/cv/classification/efficientnet_b1/igie/build_engine.py delete mode 100644 models/cv/classification/efficientnet_b1/igie/export.py delete mode 100644 models/cv/classification/efficientnet_b1/igie/inference.py delete mode 100644 models/cv/classification/efficientnet_b1/igie/requirements.txt delete mode 100644 models/cv/classification/efficientnet_b2/igie/build_engine.py delete mode 100644 models/cv/classification/efficientnet_b2/igie/export.py delete mode 100644 models/cv/classification/efficientnet_b2/igie/inference.py delete mode 100644 models/cv/classification/efficientnet_b2/igie/requirements.txt delete mode 100644 models/cv/classification/efficientnet_b3/igie/build_engine.py delete mode 100644 models/cv/classification/efficientnet_b3/igie/export.py delete mode 100644 models/cv/classification/efficientnet_b3/igie/inference.py delete mode 100644 models/cv/classification/efficientnet_b3/igie/requirements.txt delete mode 100644 models/cv/classification/efficientnet_b4/igie/build_engine.py delete mode 100644 models/cv/classification/efficientnet_b4/igie/export.py delete mode 100644 models/cv/classification/efficientnet_b4/igie/inference.py delete mode 100644 models/cv/classification/efficientnet_b4/igie/requirements.txt delete mode 100644 models/cv/classification/efficientnet_b5/igie/build_engine.py delete mode 100644 models/cv/classification/efficientnet_b5/igie/export.py delete mode 100644 models/cv/classification/efficientnet_b5/igie/inference.py delete mode 100644 models/cv/classification/efficientnet_b5/igie/requirements.txt delete mode 100644 models/cv/classification/efficientnet_v2/igie/build_engine.py delete mode 100644 models/cv/classification/efficientnet_v2/igie/export.py delete mode 100644 models/cv/classification/efficientnet_v2/igie/inference.py delete mode 100644 models/cv/classification/efficientnet_v2/igie/requirements.txt delete mode 100644 models/cv/classification/efficientnet_v2_s/igie/build_engine.py delete mode 100644 models/cv/classification/efficientnet_v2_s/igie/export.py delete mode 100644 models/cv/classification/efficientnet_v2_s/igie/inference.py delete mode 100644 models/cv/classification/efficientnet_v2_s/igie/requirements.txt delete mode 100644 models/cv/classification/efficientnetv2_rw_t/igie/build_engine.py delete mode 100644 models/cv/classification/efficientnetv2_rw_t/igie/inference.py delete mode 100644 models/cv/classification/googlenet/igie/build_engine.py delete mode 100644 models/cv/classification/googlenet/igie/export.py delete mode 100644 models/cv/classification/googlenet/igie/inference.py delete mode 100644 models/cv/classification/googlenet/igie/quantize.py delete mode 100644 models/cv/classification/googlenet/igie/requirements.txt delete mode 100644 models/cv/classification/hrnet_w18/igie/build_engine.py delete mode 100644 models/cv/classification/hrnet_w18/igie/export.py delete mode 100644 models/cv/classification/hrnet_w18/igie/inference.py delete mode 100644 models/cv/classification/hrnet_w18/igie/requirements.txt rename models/cv/classification/{alexnet/igie => igie_common}/build_engine.py (100%) rename models/cv/classification/{densenet161/igie => igie_common}/export.py (63%) rename models/cv/classification/{cspdarknet53/igie/export.py => igie_common/export_mmcls.py} (72%) rename models/cv/classification/{efficientnetv2_rw_t/igie/export.py => igie_common/export_timm.py} (85%) rename models/cv/classification/{alexnet/igie => igie_common}/inference.py (100%) rename models/cv/classification/{alexnet/igie => igie_common}/quantize.py (100%) rename models/cv/classification/{alexnet/igie => igie_common}/requirements.txt (100%) delete mode 100644 models/cv/classification/inception_v3/igie/build_engine.py delete mode 100644 models/cv/classification/inception_v3/igie/export.py delete mode 100644 models/cv/classification/inception_v3/igie/inference.py delete mode 100644 models/cv/classification/inception_v3/igie/quantize.py delete mode 100644 models/cv/classification/inception_v3/igie/requirements.txt delete mode 100644 models/cv/classification/mlp_mixer_base/igie/build_engine.py delete mode 100644 models/cv/classification/mlp_mixer_base/igie/export.py delete mode 100644 models/cv/classification/mlp_mixer_base/igie/inference.py delete mode 100644 models/cv/classification/mlp_mixer_base/igie/requirements.txt delete mode 100644 models/cv/classification/mnasnet0_5/igie/build_engine.py delete mode 100644 models/cv/classification/mnasnet0_5/igie/export.py delete mode 100644 models/cv/classification/mnasnet0_5/igie/inference.py delete mode 100644 models/cv/classification/mnasnet0_5/igie/requirements.txt delete mode 100644 models/cv/classification/mnasnet0_75/igie/build_engine.py delete mode 100644 models/cv/classification/mnasnet0_75/igie/export.py delete mode 100644 models/cv/classification/mnasnet0_75/igie/inference.py delete mode 100644 models/cv/classification/mnasnet0_75/igie/requirements.txt delete mode 100644 models/cv/classification/mnasnet1_0/igie/build_engine.py delete mode 100644 models/cv/classification/mnasnet1_0/igie/export.py delete mode 100644 models/cv/classification/mnasnet1_0/igie/inference.py delete mode 100644 models/cv/classification/mnasnet1_0/igie/requirements.txt delete mode 100644 models/cv/classification/mobilenet_v2/igie/build_engine.py delete mode 100644 models/cv/classification/mobilenet_v2/igie/export.py delete mode 100644 models/cv/classification/mobilenet_v2/igie/inference.py delete mode 100644 models/cv/classification/mobilenet_v2/igie/quantize.py delete mode 100644 models/cv/classification/mobilenet_v2/igie/requirements.txt delete mode 100644 models/cv/classification/mobilenet_v3/igie/build_engine.py delete mode 100644 models/cv/classification/mobilenet_v3/igie/export.py delete mode 100644 models/cv/classification/mobilenet_v3/igie/inference.py delete mode 100644 models/cv/classification/mobilenet_v3/igie/requirements.txt delete mode 100644 models/cv/classification/mobilenet_v3_large/igie/build_engine.py delete mode 100644 models/cv/classification/mobilenet_v3_large/igie/export.py delete mode 100644 models/cv/classification/mobilenet_v3_large/igie/inference.py delete mode 100644 models/cv/classification/mobilenet_v3_large/igie/requirements.txt delete mode 100644 models/cv/classification/mvitv2_base/igie/build_engine.py delete mode 100644 models/cv/classification/mvitv2_base/igie/export.py delete mode 100644 models/cv/classification/mvitv2_base/igie/inference.py delete mode 100644 models/cv/classification/mvitv2_base/igie/requirements.txt delete mode 100644 models/cv/classification/regnet_x_16gf/igie/build_engine.py delete mode 100644 models/cv/classification/regnet_x_16gf/igie/export.py delete mode 100644 models/cv/classification/regnet_x_16gf/igie/inference.py delete mode 100644 models/cv/classification/regnet_x_16gf/igie/requirements.txt delete mode 100644 models/cv/classification/regnet_x_1_6gf/igie/build_engine.py delete mode 100644 models/cv/classification/regnet_x_1_6gf/igie/export.py delete mode 100644 models/cv/classification/regnet_x_1_6gf/igie/inference.py delete mode 100644 models/cv/classification/regnet_x_1_6gf/igie/requirements.txt delete mode 100644 models/cv/classification/regnet_x_3_2gf/igie/build_engine.py delete mode 100644 models/cv/classification/regnet_x_3_2gf/igie/export.py delete mode 100644 models/cv/classification/regnet_x_3_2gf/igie/inference.py delete mode 100644 models/cv/classification/regnet_x_3_2gf/igie/requirements.txt delete mode 100644 models/cv/classification/regnet_y_16gf/igie/build_engine.py delete mode 100644 models/cv/classification/regnet_y_16gf/igie/export.py delete mode 100644 models/cv/classification/regnet_y_16gf/igie/inference.py delete mode 100644 models/cv/classification/regnet_y_16gf/igie/requirements.txt delete mode 100644 models/cv/classification/regnet_y_1_6gf/igie/build_engine.py delete mode 100644 models/cv/classification/regnet_y_1_6gf/igie/export.py delete mode 100644 models/cv/classification/regnet_y_1_6gf/igie/inference.py delete mode 100644 models/cv/classification/regnet_y_1_6gf/igie/requirements.txt delete mode 100644 models/cv/classification/repvgg/igie/build_engine.py delete mode 100644 models/cv/classification/repvgg/igie/export.py delete mode 100644 models/cv/classification/repvgg/igie/inference.py delete mode 100644 models/cv/classification/repvgg/igie/requirements.txt delete mode 100644 models/cv/classification/res2net50/igie/build_engine.py delete mode 100644 models/cv/classification/res2net50/igie/export.py delete mode 100644 models/cv/classification/res2net50/igie/inference.py delete mode 100644 models/cv/classification/res2net50/igie/requirements.txt delete mode 100644 models/cv/classification/resnest50/igie/build_engine.py delete mode 100644 models/cv/classification/resnest50/igie/export.py delete mode 100644 models/cv/classification/resnest50/igie/inference.py delete mode 100644 models/cv/classification/resnest50/igie/requirements.txt delete mode 100644 models/cv/classification/resnet101/igie/build_engine.py delete mode 100644 models/cv/classification/resnet101/igie/export.py delete mode 100644 models/cv/classification/resnet101/igie/inference.py delete mode 100644 models/cv/classification/resnet101/igie/quantize.py delete mode 100644 models/cv/classification/resnet101/igie/requirements.txt delete mode 100644 models/cv/classification/resnet152/igie/build_engine.py delete mode 100644 models/cv/classification/resnet152/igie/export.py delete mode 100644 models/cv/classification/resnet152/igie/inference.py delete mode 100644 models/cv/classification/resnet152/igie/quantize.py delete mode 100644 models/cv/classification/resnet152/igie/requirements.txt delete mode 100644 models/cv/classification/resnet18/igie/build_engine.py delete mode 100644 models/cv/classification/resnet18/igie/export.py delete mode 100644 models/cv/classification/resnet18/igie/inference.py delete mode 100644 models/cv/classification/resnet18/igie/quantize.py delete mode 100644 models/cv/classification/resnet18/igie/requirements.txt delete mode 100644 models/cv/classification/resnet50/igie/build_engine.py delete mode 100644 models/cv/classification/resnet50/igie/export.py delete mode 100644 models/cv/classification/resnet50/igie/inference.py delete mode 100644 models/cv/classification/resnet50/igie/quantize.py delete mode 100644 models/cv/classification/resnet50/igie/requirements.txt delete mode 100644 models/cv/classification/resnetv1d50/igie/build_engine.py delete mode 100644 models/cv/classification/resnetv1d50/igie/export.py delete mode 100644 models/cv/classification/resnetv1d50/igie/inference.py delete mode 100644 models/cv/classification/resnetv1d50/igie/requirements.txt delete mode 100644 models/cv/classification/resnext101_32x8d/igie/build_engine.py delete mode 100644 models/cv/classification/resnext101_32x8d/igie/export.py delete mode 100644 models/cv/classification/resnext101_32x8d/igie/inference.py delete mode 100644 models/cv/classification/resnext101_32x8d/igie/requirements.txt delete mode 100644 models/cv/classification/resnext101_64x4d/igie/build_engine.py delete mode 100644 models/cv/classification/resnext101_64x4d/igie/export.py delete mode 100644 models/cv/classification/resnext101_64x4d/igie/inference.py delete mode 100644 models/cv/classification/resnext101_64x4d/igie/requirements.txt delete mode 100644 models/cv/classification/resnext50_32x4d/igie/build_engine.py delete mode 100644 models/cv/classification/resnext50_32x4d/igie/export.py delete mode 100644 models/cv/classification/resnext50_32x4d/igie/inference.py delete mode 100644 models/cv/classification/resnext50_32x4d/igie/requirements.txt delete mode 100644 models/cv/classification/seresnet50/igie/build_engine.py delete mode 100644 models/cv/classification/seresnet50/igie/export.py delete mode 100644 models/cv/classification/seresnet50/igie/inference.py delete mode 100644 models/cv/classification/seresnet50/igie/requirements.txt delete mode 100644 models/cv/classification/shufflenetv2_x0_5/igie/build_engine.py delete mode 100644 models/cv/classification/shufflenetv2_x0_5/igie/export.py delete mode 100644 models/cv/classification/shufflenetv2_x0_5/igie/inference.py delete mode 100644 models/cv/classification/shufflenetv2_x0_5/igie/requirements.txt delete mode 100644 models/cv/classification/shufflenetv2_x1_0/igie/build_engine.py delete mode 100644 models/cv/classification/shufflenetv2_x1_0/igie/export.py delete mode 100644 models/cv/classification/shufflenetv2_x1_0/igie/inference.py delete mode 100644 models/cv/classification/shufflenetv2_x1_0/igie/requirements.txt delete mode 100644 models/cv/classification/shufflenetv2_x1_5/igie/build_engine.py delete mode 100644 models/cv/classification/shufflenetv2_x1_5/igie/export.py delete mode 100644 models/cv/classification/shufflenetv2_x1_5/igie/inference.py delete mode 100644 models/cv/classification/shufflenetv2_x1_5/igie/requirements.txt delete mode 100644 models/cv/classification/shufflenetv2_x2_0/igie/build_engine.py delete mode 100644 models/cv/classification/shufflenetv2_x2_0/igie/export.py delete mode 100644 models/cv/classification/shufflenetv2_x2_0/igie/inference.py delete mode 100644 models/cv/classification/shufflenetv2_x2_0/igie/requirements.txt delete mode 100644 models/cv/classification/squeezenet_v1_0/igie/build_engine.py delete mode 100644 models/cv/classification/squeezenet_v1_0/igie/export.py delete mode 100644 models/cv/classification/squeezenet_v1_0/igie/inference.py delete mode 100644 models/cv/classification/squeezenet_v1_0/igie/requirements.txt delete mode 100644 models/cv/classification/squeezenet_v1_1/igie/build_engine.py delete mode 100644 models/cv/classification/squeezenet_v1_1/igie/export.py delete mode 100644 models/cv/classification/squeezenet_v1_1/igie/inference.py delete mode 100644 models/cv/classification/squeezenet_v1_1/igie/requirements.txt delete mode 100644 models/cv/classification/svt_base/igie/build_engine.py delete mode 100644 models/cv/classification/svt_base/igie/export.py delete mode 100644 models/cv/classification/svt_base/igie/inference.py delete mode 100644 models/cv/classification/svt_base/igie/requirements.txt delete mode 100644 models/cv/classification/twins_pcpvt/igie/build_engine.py delete mode 100644 models/cv/classification/twins_pcpvt/igie/export.py delete mode 100644 models/cv/classification/twins_pcpvt/igie/inference.py delete mode 100644 models/cv/classification/twins_pcpvt/igie/requirements.txt delete mode 100644 models/cv/classification/van_b0/igie/build_engine.py delete mode 100644 models/cv/classification/van_b0/igie/export.py delete mode 100644 models/cv/classification/van_b0/igie/inference.py delete mode 100644 models/cv/classification/van_b0/igie/requirements.txt delete mode 100644 models/cv/classification/vgg11/igie/build_engine.py delete mode 100644 models/cv/classification/vgg11/igie/export.py delete mode 100644 models/cv/classification/vgg11/igie/inference.py delete mode 100644 models/cv/classification/vgg11/igie/requirements.txt delete mode 100644 models/cv/classification/vgg16/igie/build_engine.py delete mode 100644 models/cv/classification/vgg16/igie/export.py delete mode 100644 models/cv/classification/vgg16/igie/inference.py delete mode 100644 models/cv/classification/vgg16/igie/quantize.py delete mode 100644 models/cv/classification/vgg16/igie/requirements.txt delete mode 100644 models/cv/classification/vgg19/igie/build_engine.py delete mode 100644 models/cv/classification/vgg19/igie/export.py delete mode 100644 models/cv/classification/vgg19/igie/inference.py delete mode 100644 models/cv/classification/vgg19/igie/requirements.txt delete mode 100644 models/cv/classification/vgg19_bn/igie/build_engine.py delete mode 100644 models/cv/classification/vgg19_bn/igie/export.py delete mode 100644 models/cv/classification/vgg19_bn/igie/inference.py delete mode 100644 models/cv/classification/vgg19_bn/igie/requirements.txt delete mode 100644 models/cv/classification/wide_resnet101/igie/build_engine.py delete mode 100644 models/cv/classification/wide_resnet101/igie/export.py delete mode 100644 models/cv/classification/wide_resnet101/igie/inference.py delete mode 100644 models/cv/classification/wide_resnet101/igie/requirements.txt delete mode 100644 models/cv/classification/wide_resnet50/igie/build_engine.py delete mode 100644 models/cv/classification/wide_resnet50/igie/export.py delete mode 100644 models/cv/classification/wide_resnet50/igie/inference.py delete mode 100644 models/cv/classification/wide_resnet50/igie/quantize.py delete mode 100644 models/cv/classification/wide_resnet50/igie/requirements.txt diff --git a/models/cv/classification/alexnet/igie/README.md b/models/cv/classification/alexnet/igie/README.md index 59be111f..a5985877 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 f79ad70d..56267d4a 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 543676ee..00000000 --- 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 eee72a15..5e7525f0 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 5ea9adb9..ab1b19ce 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 e3464357..2bd275af 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 0fab8e84..d85fff44 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 1fd0c789..380d95ba 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 c59e8933..2a60872f 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 d3626ae7..00000000 --- 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 17d287d5..f820b393 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 d9a2fe01..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 42575772..1e0b7e3b 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 d5ae0649..87c3d1ee 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 b7ed3c62..9222a959 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 d3626ae7..00000000 --- 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 52f2253a..29082e3d 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 662c6a01..00000000 --- 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 1b0c602a..00000000 --- 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 41c31663..00000000 --- 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 48d67214..4cbdc4a1 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 f09fcc9c..3a9b08ea 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 8a62ba22..c665dcb6 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 d3626ae7..00000000 --- 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 6c67be93..2303d1ec 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 1ec39771..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 c7837d79..37d7b59c 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 cde8da5a..12956b0e 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 5608999f..3d083135 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 54aa8847..00000000 --- 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 9710375e..1ec6f531 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 3364e7be..00000000 --- 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 0563f72e..00000000 --- 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 9e811126..00000000 --- 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 c0957757..9c6cfe46 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 36a26f5a..5571d5e9 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 c7c1624b..07da984c 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 d3626ae7..00000000 --- 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 6b99b129..782aa45f 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 1b0c602a..00000000 --- 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 41c31663..00000000 --- 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 3838127c..c0c7ea34 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 0c1a847b..0a794977 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 b16f992c..5c01bbd1 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 d3626ae7..00000000 --- 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 f35c6c7b..f1ae8f10 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 6eafb7b1..00000000 --- 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 1b0c602a..00000000 --- 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 41c31663..00000000 --- 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 47a37e73..398a1cee 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 a1ea0cc5..4fcab323 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 4f56b0f0..7a8ddc1e 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 54aa8847..00000000 --- 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 5f18b379..5f3a564f 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 c7681b68..00000000 --- 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 b0a8ed03..00000000 --- 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 41c31663..00000000 --- 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 7086d1e1..b711f2ee 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 1716d61c..f1810a25 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 1f703580..439a3cc7 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 d3626ae7..00000000 --- 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 d310ee26..223bd4ed 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 0078670f..00000000 --- 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 1b0c602a..00000000 --- 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 41c31663..00000000 --- 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 f6f9a8fb..80080453 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 a21f13b2..91fc101c 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 3f8d0c2e..e3ab597d 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 d3626ae7..00000000 --- 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 d97d1a7f..fbd8c730 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 456fe29e..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 72aff406..e499ceef 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 50db35bb..8cd54621 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 26e7f1d4..c2f5a294 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 d3626ae7..00000000 --- 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 575a4e06..09c727cd 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 7ad5d57e..407c1c51 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 96548717..c192f2a9 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 507fa070..5e961ca4 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 d3626ae7..00000000 --- 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 0f877836..b6bb73a7 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 44c72690..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 6475ddcd..a2ab73db 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 8a36fc2f..ca965a5f 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 9707e577..fc54b25b 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 d3626ae7..00000000 --- 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 7a4eb046..1f9515b4 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 66019547..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 470b285a..521eb073 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 e1ad69b7..99834cc8 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 cd111f43..9bf513dc 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 d3626ae7..00000000 --- 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 9fa9a304..b305db60 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 f01809d7..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 5785456a..77106c12 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 b26ffa3d..d6303c59 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 315c6486..7825dc7d 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 d3626ae7..00000000 --- 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 4ba1c5d4..7e0c8231 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 ec2d6de9..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 5d57cdf1..0e64b382 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 69c2ad2f..0ceaa988 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 d0a0069a..2da29aff 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 d3626ae7..00000000 --- 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 7903f623..8aac9635 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 7761ffac..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 d4ed3ef0..e1e4f855 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 4b960221..c988d5c8 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 072ab493..44c0fd3e 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 d3626ae7..00000000 --- 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 c6da04a2..8cdfec1b 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 f66e2cb0..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 36e9a874..2f75ca4a 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 27b13c90..5df9e54e 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 bd04f34a..68a12a6a 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 d3626ae7..00000000 --- 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 4303d2aa..96834932 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 bffe9c47..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 018dfb22..72b966df 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 6ad001c7..6ac6a3c2 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 f5ed4d10..e2a626bf 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 54aa8847..00000000 --- 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 13c3aed5..ce45e7c1 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 7c4e8523..00000000 --- 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 0563f72e..00000000 --- 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 9e811126..00000000 --- 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 e7c1330b..2db3257a 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 60d83a37..3c42938c 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 eab2fd14..752160a5 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 d3626ae7..00000000 --- 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 75133e4b..ccda00ca 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 cfb9f76e..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 d7aa78a2..57bf599d 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 21073983..18714a42 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 66f446c3..8a8fa2fa 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 d3626ae7..00000000 --- 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 ac0106d5..93bcc6a0 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 63b1b4c8..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 2e7d1133..22f2084e 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 4c67dd99..c6ef87e8 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 e0df0c12..cfdd1b9a 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 d3626ae7..00000000 --- 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 1eee601b..dc9e1c67 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 e33c91fa..00000000 --- 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 e1e98414..9d96fcfb 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 797791fe..a3f9d585 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 7c6cb329..2165ac5f 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 d3626ae7..00000000 --- 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 e6fedc20..df6b7922 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 ffd662e1..00000000 --- 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 3aef3ec7..00000000 --- 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 1cdeb93f..00000000 --- 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 9e811126..00000000 --- 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 e546516f..945acc1b 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 54077315..0730255f 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 f9be8585..2f102e96 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 064ab656..5df60743 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 7ae6a59f..b27ad9d6 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 d3626ae7..00000000 --- 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 65bfe18c..55010bdc 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 7dc8d9fd..00000000 --- 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 1b0c602a..00000000 --- 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 41c31663..00000000 --- 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 8d6849da..2bd1d5ab 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 e6158cc7..1bf39bfb 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 cb5466db..7c4ff989 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 7dc8d9fd..4b9e5bf7 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 3c3f579c..25e995a8 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 c9e9ce27..a1c0fe40 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 d3626ae7..00000000 --- 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 6214ee35..52aa92cc 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 77ced0fd..00000000 --- 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 3aef3ec7..00000000 --- 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 1cdeb93f..00000000 --- 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 9e811126..00000000 --- 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 9598ad90..d20faeb3 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 abf94b1d..9154fd25 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 444768fa..cb7ff468 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 36502299..736b18bb 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 618116ba..de3fadf4 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 d3626ae7..00000000 --- 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 79746d8e..1e50e388 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 6eafb7b1..00000000 --- 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 1b0c602a..00000000 --- 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 41c31663..00000000 --- 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 448fe268..14b17e6e 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 c2c62600..9bc0267a 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 da619b03..4847f2ce 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 d3626ae7..00000000 --- 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 b242e80a..020c8735 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 bd48e206..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 a1c3b37a..29da5778 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 89271d33..68ba2957 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 29cf14fb..12bf5601 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 d3626ae7..00000000 --- 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 96e6e152..ffdaed97 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 e1b69e4d..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 9c5fb76d..2814192e 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 014ac67a..c8b6a2b4 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 f4d0e339..cde6a033 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 54aa8847..00000000 --- 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 a78b6fed..8c5519aa 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 9dbcdd4e..00000000 --- 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 0563f72e..00000000 --- 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 9e811126..00000000 --- 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 33e04e8f..7fc5a71b 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 5fbdcde9..e8800ae0 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 4ef43425..be32dd57 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 d3626ae7..00000000 --- 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 736be770..74d130ec 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 8cf121cc..00000000 --- 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 3aef3ec7..00000000 --- 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 c3311ad0..00000000 --- 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 08a0a972..00000000 --- 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 24547ddf..3b52593e 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 30b0b9ad..d788d740 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 e7f81cfd..4ff0d535 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 5c58794a..c6900b1d 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 81a2c05f..9e7ebb87 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 d3626ae7..00000000 --- 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 8e30886c..c597a33d 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 e02485e4..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 95a26480..55146d89 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 05b9e1ec..7ba4d963 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 1d71e1ff..365d5297 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 d3626ae7..00000000 --- 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 76d67fd8..3584dfe1 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 1e6c82ea..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 dff18e9b..5a20e7dc 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 49380c0a..503c71b8 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 433d0765..75d16212 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 d3626ae7..00000000 --- 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 747bee51..e073e9c7 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 d78b898b..00000000 --- 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 c42cf871..00000000 --- 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 41c31663..00000000 --- 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 85f66d50..b32b237c 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 d54dac50..a37b1285 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 9eb57795..f36c4241 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 d3626ae7..00000000 --- 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 385a3ae8..adefc57b 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 7807f0c6..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 5df1457c..badd0039 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 f6b4f481..f9e681cb 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 f2ff1a2c..1c2bbf49 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 d3626ae7..00000000 --- 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 3e54150d..b5cfc4ff 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 3e60b43a..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 4dda13dc..427665f9 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 2fa72b64..9668fe1d 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 991b23f1..89433371 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 54aa8847..00000000 --- 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 78f28f45..13623e82 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 c6e2019f..00000000 --- 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 0563f72e..00000000 --- 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 9e811126..00000000 --- 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 c68d181c..1f88a012 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 0e1fc7c4..b2ca03fe 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 2ec70dd1..b8486e55 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 54aa8847..00000000 --- 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 8fc7ac11..fbdbe04c 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 33f35b3d..00000000 --- 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 0563f72e..00000000 --- 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 9e811126..00000000 --- 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 d7f0adb2..f7f91dda 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 30dc217f..7ede053b 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 e4972c22..2151fea7 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 d3626ae7..00000000 --- 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 e6346b74..541c682c 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 f2a9f0a2..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 62d9cc11..4f335412 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 afe3a450..c542815b 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 970e8de0..4a3c87d5 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 d3626ae7..00000000 --- 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 4433e485..2daf189b 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 7dc8d9fd..00000000 --- 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 1b0c602a..00000000 --- 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 2bd089c8..00000000 --- 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 37f6fcb2..ea8323e5 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 ad29f598..ebb2456c 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 59d0446a..fc8fec18 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 d3626ae7..00000000 --- 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 ddc583f2..e2372d86 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 7dc8d9fd..00000000 --- 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 1b0c602a..00000000 --- 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 41c31663..00000000 --- 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 155ec563..ca78878b 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 c24472ba..85ce8455 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 d3ed08b4..b39cc5ba 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 d3626ae7..00000000 --- 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 21f3114a..1a67b423 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 9514777b..00000000 --- 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 1b0c602a..00000000 --- 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 b6cbf88f..00000000 --- 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 c5a33efe..c59ea3d7 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 82c32e89..8146397a 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 0a986582..2aaa1594 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 d3626ae7..00000000 --- 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 2e4e4579..a1e32ba0 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 a9cd5b05..00000000 --- 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 3aef3ec7..00000000 --- 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 e851dcc1..00000000 --- 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 9e811126..00000000 --- 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 3175d05b..2445a04e 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 6d88a9e2..94892e1d 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 23fff27a..42df5752 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 c8b30b9c..c60d5468 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 2f2315a1..66165bf7 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 d3626ae7..00000000 --- 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 cc147bef..cc79f89d 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 1386e31d..00000000 --- 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 3aef3ec7..00000000 --- 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 e851dcc1..00000000 --- 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 9e811126..00000000 --- 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 625abd36..56f9bdf4 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 541d3b20..a5802aee 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 f19ffad3..18aa6574 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 9a8fe016..587eb0df 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 95ef89ed..a9d3492c 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 d3626ae7..00000000 --- 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 9e663ad1..87fac53b 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 b1f571b7..00000000 --- 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 3aef3ec7..00000000 --- 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 e851dcc1..00000000 --- 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 9e811126..00000000 --- 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 081e75f7..83fa1ee7 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 afe5dd9a..3bb0d35c 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 18f3b271..daf59879 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 2372017f..bd40992c 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 2bba59c5..93d4d284 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 d3626ae7..00000000 --- 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 152f4ebc..fa922538 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 affc54b5..00000000 --- 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 3aef3ec7..00000000 --- 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 e851dcc1..00000000 --- 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 9e811126..00000000 --- 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 875a8678..c79516f5 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 37d2e321..7de10ae3 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 8b5edbdb..75439121 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 488bf850..99b5fbe8 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 e9cc913c..52d32a70 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 d3626ae7..00000000 --- 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 aa230851..1a6b8cc5 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 84045099..00000000 --- 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 c42cf871..00000000 --- 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 4d5ea05f..00000000 --- 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 c3a3d869..5964159b 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 194d2900..93f64fff 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 d88f9c31..d2e6b25d 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 d3626ae7..00000000 --- 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 7c7fd897..028e88a2 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 d06c13da..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 713f81cd..c0178bcd 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 c48175aa..4e11b53d 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 b50b71ab..22ff3449 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 d3626ae7..00000000 --- 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 4641ef9f..3ed80a33 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 43a20fca..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 edcb1a00..24bae45e 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 3ccf3bc5..ddcbd098 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 de8b396c..980c53d9 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 d3626ae7..00000000 --- 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 82d79b9e..b5d7963f 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 ba2dc1da..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 f92f1188..5cbec203 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 a916c895..3d0a709b 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 552eb861..3642b2e5 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 d3626ae7..00000000 --- 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 50211036..ab17cf7e 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 7dc8d9fd..00000000 --- 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 1b0c602a..00000000 --- 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 4d5ea05f..00000000 --- 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 c861e85b..4bc5e4f7 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 caaeaa7c..32eaa8e2 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 a7bf7f2b..507d16f2 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 d3626ae7..00000000 --- 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 cfc6d3b2..a40629a4 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 da1a97d6..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 4be39d2f..1bc3e019 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 26f92011..153f1499 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 e426ce30..3e43cf66 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 d3626ae7..00000000 --- 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 464a7dfc..6bb64983 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 3a4ff9ba..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 91802347..0a1ff1e7 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 21353dc7..78eaab44 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 fee17307..6eb085a6 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 d3626ae7..00000000 --- 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 7d72032c..b8ac0591 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 0f89ba7c..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 9be9264c..c7e19a53 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 cc5a424d..ed2b7dea 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 c002d0b0..bfca0291 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 d3626ae7..00000000 --- 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 4eccbb5a..2099ae52 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 f4727e43..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 be582ff6..b0e0cd24 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 f94cc83b..c9a4f634 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 eb626259..965a2b14 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 d3626ae7..00000000 --- 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 e01e8a8a..697849b3 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 940674f5..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 21d47841..f637704f 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 634654a0..46ee505a 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 02cfcd64..5b9a9214 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 54aa8847..00000000 --- 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 19ca729f..d2ff32ec 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 3dde82ae..00000000 --- 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 0563f72e..00000000 --- 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 9e811126..00000000 --- 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 c4af3d1a..74847cc4 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 fa542f1d..5a76e9aa 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 2d4ca27c..3076aa46 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 d3626ae7..00000000 --- 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 b11820c1..29ab6743 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 6eafb7b1..00000000 --- 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 1b0c602a..00000000 --- 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 41c31663..00000000 --- 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 33101794..f1d71a76 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 34f0a080..d74a6311 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 7ea3d082..21735425 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 54aa8847..00000000 --- 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 b377a631..7687074b 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 c7681b68..00000000 --- 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 b0a8ed03..00000000 --- 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 41c31663..00000000 --- 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 3d372907..6937726b 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 f7e32f1a..22c723b4 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 09bfcafe..421add0e 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 54aa8847..00000000 --- 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 076ffcbe..852cb1eb 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 c7681b68..00000000 --- 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 b0a8ed03..00000000 --- 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 4d5ea05f..00000000 --- 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 9b993277..d1f8ac08 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 8aa8caef..1b26acda 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 38966dd2..1c8b8510 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 d3626ae7..00000000 --- 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 3be89fdd..9b88a075 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 a09d19b7..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 fde4b177..001c73db 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 136961e5..3ae0511f 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 caa229ba..840c520e 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 d3626ae7..00000000 --- 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 3a8b0d9d..784049f4 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 817d7319..00000000 --- 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 3aef3ec7..00000000 --- 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 1cdeb93f..00000000 --- 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 9e811126..00000000 --- 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 f0690752..4c2e15d5 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 86eb5fa9..002c6c21 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 fae365ab..88e1b026 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 77c2b179..ff384491 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 cbf3e668..4e250002 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 54aa8847..00000000 --- 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 620ca769..b944ccb7 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 71b0d655..00000000 --- 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 0563f72e..00000000 --- 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 9e811126..00000000 --- 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 acd9fd7d..723a37e1 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 d0d05034..41832493 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 a46c31eb..0fbdb01a 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 54aa8847..00000000 --- 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 386f823f..dcd721b0 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 525f6c83..00000000 --- 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 0563f72e..00000000 --- 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 9e811126..00000000 --- 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 881c4f29..2738bc39 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 7b875e8a..4d425590 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 9de215e9..7abc2c64 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 d3626ae7..00000000 --- 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 1b4b8b18..40b9a26a 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 4752f0ea..00000000 --- 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 3aef3ec7..00000000 --- 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 9e811126..00000000 --- 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 42f1ed87..274a0cca 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 05155037..dceedfdf 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 76b31309..2b334596 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 d3626ae7..00000000 --- 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 795996ee..28c1938c 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 1d3c64c8..00000000 --- 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 3aef3ec7..00000000 --- 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 e851dcc1..00000000 --- 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 9e811126..00000000 --- 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 54098ac4..ec5db555 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 7fa6ba8c..e8eac481 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 047a5ecb..ec054307 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 e36dfc1a..91f89c46 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 7972ff1a..cacad86d 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 cc8683b9..46e5636b 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 -- Gitee