|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | + |
| 7 | +# list of tuple: config, pretrained model, onnx filename |
| 8 | +CONFIGS = [ |
| 9 | + ( |
| 10 | + 'mmclassification/configs/vision_transformer/vit-base-p32_ft-64xb64_in1k-384.py', # noqa: E501 |
| 11 | + 'https://download.openmmlab.com/mmclassification/v0/vit/finetune/vit-base-p32_in21k-pre-3rdparty_ft-64xb64_in1k-384_20210928-9cea8599.pth', # noqa: E501 |
| 12 | + 'vit.onnx'), |
| 13 | + ( |
| 14 | + 'mmclassification/configs/resnet/resnet50_8xb32_in1k.py', |
| 15 | + 'https://download.openmmlab.com/mmclassification/v0/resnet/resnet50_8xb32_in1k_20210831-ea4938fc.pth', # noqa: E501 |
| 16 | + 'resnet50.onnx', |
| 17 | + ), |
| 18 | + ( |
| 19 | + 'mmclassification/configs/resnet/resnet18_8xb32_in1k.py', |
| 20 | + 'https://download.openmmlab.com/mmclassification/v0/resnet/resnet18_8xb32_in1k_20210831-fbbb1da6.pth', # noqa: E501 |
| 21 | + 'resnet18.onnx', |
| 22 | + 'https://media.githubusercontent.com/media/tpoisonooo/mmdeploy-onnx2ncnn-testdata/main/resnet18.onnx', # noqa: E501 |
| 23 | + ), |
| 24 | + ( |
| 25 | + 'mmclassification/configs/mobilenet_v2/mobilenet-v2_8xb32_in1k.py', |
| 26 | + 'https://download.openmmlab.com/mmclassification/v0/mobilenet_v2/mobilenet_v2_batch256_imagenet_20200708-3b2dc3af.pth', # noqa: E501 |
| 27 | + 'mobilenet-v2.onnx', |
| 28 | + 'https://media.githubusercontent.com/media/tpoisonooo/mmdeploy-onnx2ncnn-testdata/main/mobilenet-v2.onnx', # noqa: E501 |
| 29 | + ) |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +def parse_args(): |
| 34 | + parser = argparse.ArgumentParser( |
| 35 | + description='MMDeploy onnx2ncnn test tool.') |
| 36 | + parser.add_argument('--run', type=bool, help='Execute onnx2ncnn bin.') |
| 37 | + parser.add_argument( |
| 38 | + '--repo-dir', type=str, default='~/', help='mmcls directory.') |
| 39 | + parser.add_argument( |
| 40 | + '--out', |
| 41 | + type=str, |
| 42 | + default='onnx_output', |
| 43 | + help='onnx model output directory.') |
| 44 | + parser.add_argument( |
| 45 | + '--generate-onnx', type=bool, help='Generate onnx model.') |
| 46 | + args = parser.parse_args() |
| 47 | + return args |
| 48 | + |
| 49 | + |
| 50 | +def generate_onnx(args): |
| 51 | + import mmcv |
| 52 | + mmcv.mkdir_or_exist(args.out) |
| 53 | + for conf in CONFIGS: |
| 54 | + config = os.path.join(args.repo_dir, conf[0]) |
| 55 | + model = conf[1] |
| 56 | + convert_cmd = [ |
| 57 | + 'python3', 'tools/deploy.py', |
| 58 | + 'configs/mmcls/classification_ncnn_static.py', config, model, |
| 59 | + 'cat-dog.png', '--work-dir', 'work_dir', '--device', 'cpu' |
| 60 | + ] |
| 61 | + print(subprocess.call(convert_cmd)) |
| 62 | + |
| 63 | + move_cmd = [ |
| 64 | + 'mv', 'work_dir/end2end.onnx', |
| 65 | + os.path.join(args.out, conf[2]) |
| 66 | + ] |
| 67 | + print(subprocess.call(move_cmd)) |
| 68 | + |
| 69 | + |
| 70 | +def run(args): |
| 71 | + for conf in CONFIGS: |
| 72 | + if len(conf) < 4: |
| 73 | + continue |
| 74 | + download_url = conf[3] |
| 75 | + filename = conf[2] |
| 76 | + download_cmd = ['wget', download_url] |
| 77 | + # show processbar |
| 78 | + os.system(' '.join(download_cmd)) |
| 79 | + |
| 80 | + convert_cmd = ['./onnx2ncnn', filename, 'onnx.param', 'onnx.bin'] |
| 81 | + subprocess.run(convert_cmd, capture_output=True, check=True) |
| 82 | + |
| 83 | + |
| 84 | +def main(): |
| 85 | + """test `onnx2ncnn.cpp` |
| 86 | +
|
| 87 | + First generate onnx model then convert it with `onnx2ncnn`. |
| 88 | + """ |
| 89 | + args = parse_args() |
| 90 | + if args.generate_onnx: |
| 91 | + generate_onnx(args) |
| 92 | + if args.run: |
| 93 | + run(args) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + main() |
0 commit comments