From 7981f1673c93dbbb962a74951fab0a6902b13533 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 28 May 2021 11:13:23 +0300 Subject: [PATCH 1/7] Skip model configs having unknown platform --- examples/rknn_convert/rknn_convert.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/rknn_convert/rknn_convert.py b/examples/rknn_convert/rknn_convert.py index 2b66200..9ad97fc 100644 --- a/examples/rknn_convert/rknn_convert.py +++ b/examples/rknn_convert/rknn_convert.py @@ -60,7 +60,8 @@ def convert_model(model_path, out_path, pre_compile): model_file_path = os.path.join(model_path, model['model_file_path']) rknn.load_onnx(model=model_file_path) else: - print("platform %s not support!" % (model['platform'])) + print("Platform {:} is not supported! Moving on.".format(model['platform'])) + continue print('done') if model['quantize']: From 39d26aef89ff85f7e6598e3b5f30dc8cc55df452 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 28 May 2021 12:56:39 +0300 Subject: [PATCH 2/7] Proper relative paths handling --- examples/rknn_convert/rknn_convert.py | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/rknn_convert/rknn_convert.py b/examples/rknn_convert/rknn_convert.py index 9ad97fc..cc3b44e 100644 --- a/examples/rknn_convert/rknn_convert.py +++ b/examples/rknn_convert/rknn_convert.py @@ -16,17 +16,17 @@ def parse_model_config(yaml_config_file): return model_configs -def convert_model(model_path, out_path, pre_compile): - if os.path.isfile(model_path): - yaml_config_file = model_path - model_path = os.path.dirname(yaml_config_file) +def convert_model(config_path, out_path, pre_compile): + if os.path.isfile(config_path): + config_file = os.path.abspath(config_path) + config_path = os.path.dirname(config_file) else: - yaml_config_file = os.path.join(model_path, 'model_config.yml') - if not os.path.exists(yaml_config_file): - print('model config {} not exist!'.format(yaml_config_file)) + config_file = os.path.join(config_path, 'model_config.yml') + if not os.path.exists(config_file): + print('model config {} not exist!'.format(config_file)) exit(-1) - model_configs = parse_model_config(yaml_config_file) + model_configs = parse_model_config(config_file) exported_rknn_model_path_list = [] @@ -39,7 +39,7 @@ def convert_model(model_path, out_path, pre_compile): print('--> Loading model...') if model['platform'] == 'tensorflow': - model_file_path = os.path.join(model_path, model['model_file_path']) + model_file_path = os.path.join(config_path, model['model_file_path']) input_size_list = [] for input_size_str in model['subgraphs']['input-size-list']: input_size = list(map(int, input_size_str.split(','))) @@ -50,14 +50,14 @@ def convert_model(model_path, out_path, pre_compile): outputs=model['subgraphs']['outputs'], input_size_list=input_size_list) elif model['platform'] == 'tflite': - model_file_path = os.path.join(model_path, model['model_file_path']) + model_file_path = os.path.join(config_path, model['model_file_path']) rknn.load_tflite(model=model_file_path) elif model['platform'] == 'caffe': - prototxt_file_path = os.path.join(model_path,model['prototxt_file_path']) - caffemodel_file_path = os.path.join(model_path,model['caffemodel_file_path']) + prototxt_file_path = os.path.join(config_path,model['prototxt_file_path']) + caffemodel_file_path = os.path.join(config_path,model['caffemodel_file_path']) rknn.load_caffe(model=prototxt_file_path, proto='caffe', blobs=caffemodel_file_path) elif model['platform'] == 'onnx': - model_file_path = os.path.join(model_path, model['model_file_path']) + model_file_path = os.path.join(config_path, model['model_file_path']) rknn.load_onnx(model=model_file_path) else: print("Platform {:} is not supported! Moving on.".format(model['platform'])) @@ -65,7 +65,7 @@ def convert_model(model_path, out_path, pre_compile): print('done') if model['quantize']: - dataset_path = os.path.join(model_path, model['dataset']) + dataset_path = os.path.join(config_path, model['dataset']) else: dataset_path = './dataset' @@ -83,8 +83,8 @@ def convert_model(model_path, out_path, pre_compile): if __name__ == '__main__': - model_path = sys.argv[1] + config_path = sys.argv[1] out_path = sys.argv[2] pre_compile = sys.argv[3] in ['true', '1', 'True'] - convert_model(model_path, out_path, pre_compile) + convert_model(config_path, out_path, pre_compile) From 45ffba3658d281e35305c9e2c6b09c8a3f45fd04 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 28 May 2021 13:15:55 +0300 Subject: [PATCH 3/7] Small refactoring --- examples/rknn_convert/rknn_convert.py | 47 ++++++++++++++------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/examples/rknn_convert/rknn_convert.py b/examples/rknn_convert/rknn_convert.py index cc3b44e..6d6f84a 100644 --- a/examples/rknn_convert/rknn_convert.py +++ b/examples/rknn_convert/rknn_convert.py @@ -1,19 +1,18 @@ #!/usr/bin/env python3 -import os import sys - -#import yaml +import os import ruamel.yaml from rknn.api import RKNN -yaml = ruamel.yaml.YAML(typ='rt') -def parse_model_config(yaml_config_file): - with open(yaml_config_file) as f: - yaml_config = f.read() - model_configs = yaml.load(yaml_config) - return model_configs +def parse_model_config(config_file): + config_text = "" + with open(config_file) as f: + config_text = f.read() + if config_text: + yaml = ruamel.yaml.YAML(typ='rt') + return yaml.load(config_text) def convert_model(config_path, out_path, pre_compile): @@ -23,21 +22,23 @@ def convert_model(config_path, out_path, pre_compile): else: config_file = os.path.join(config_path, 'model_config.yml') if not os.path.exists(config_file): - print('model config {} not exist!'.format(config_file)) + print('Model config {:} not exist!'.format(config_file)) exit(-1) - model_configs = parse_model_config(config_file) - - exported_rknn_model_path_list = [] + exported_rknn_model_paths = [] + config = parse_model_config(config_file) + if config is None: + print('Invalid configuration.') + return exported_rknn_model_paths - for model_name in model_configs['models']: - model = model_configs['models'][model_name] + for model_name in config['models']: + model = config['models'][model_name] rknn = RKNN() rknn.config(**model['configs']) - print('--> Loading model...') + print('--> Load model...') if model['platform'] == 'tensorflow': model_file_path = os.path.join(config_path, model['model_file_path']) input_size_list = [] @@ -62,7 +63,7 @@ def convert_model(config_path, out_path, pre_compile): else: print("Platform {:} is not supported! Moving on.".format(model['platform'])) continue - print('done') + print('Done') if model['quantize']: dataset_path = os.path.join(config_path, model['dataset']) @@ -71,15 +72,15 @@ def convert_model(config_path, out_path, pre_compile): print('--> Build RKNN model...') rknn.build(do_quantization=model['quantize'], dataset=dataset_path, pre_compile=pre_compile) - print('done') + print('Done') - export_rknn_model_path = "%s.rknn" % (os.path.join(out_path, model_name)) - print('--> Export RKNN model to: {}'.format(export_rknn_model_path)) + export_rknn_model_path = "{:}.rknn".format(os.path.join(out_path, model_name)) + print('--> Export RKNN model to: {:}'.format(export_rknn_model_path)) rknn.export_rknn(export_path=export_rknn_model_path) - exported_rknn_model_path_list.append(export_rknn_model_path) - print('done') + exported_rknn_model_paths.append(export_rknn_model_path) + print('Done') - return exported_rknn_model_path_list + return exported_rknn_model_paths if __name__ == '__main__': From 3c8c1cf6b7f7631f646b309d1c633618cb45ea57 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 28 May 2021 13:21:27 +0300 Subject: [PATCH 4/7] More small refactoring --- examples/rknn_convert/rknn_convert.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/examples/rknn_convert/rknn_convert.py b/examples/rknn_convert/rknn_convert.py index 6d6f84a..a3ac5c9 100644 --- a/examples/rknn_convert/rknn_convert.py +++ b/examples/rknn_convert/rknn_convert.py @@ -16,6 +16,8 @@ def parse_model_config(config_file): def convert_model(config_path, out_path, pre_compile): + exported_rknn_model_paths = [] + if os.path.isfile(config_path): config_file = os.path.abspath(config_path) config_path = os.path.dirname(config_file) @@ -23,9 +25,8 @@ def convert_model(config_path, out_path, pre_compile): config_file = os.path.join(config_path, 'model_config.yml') if not os.path.exists(config_file): print('Model config {:} not exist!'.format(config_file)) - exit(-1) + return exported_rknn_model_paths - exported_rknn_model_paths = [] config = parse_model_config(config_file) if config is None: print('Invalid configuration.') @@ -39,8 +40,8 @@ def convert_model(config_path, out_path, pre_compile): rknn.config(**model['configs']) print('--> Load model...') + model_file_path = os.path.join(config_path, model['model_file_path']) if model['platform'] == 'tensorflow': - model_file_path = os.path.join(config_path, model['model_file_path']) input_size_list = [] for input_size_str in model['subgraphs']['input-size-list']: input_size = list(map(int, input_size_str.split(','))) @@ -51,15 +52,13 @@ def convert_model(config_path, out_path, pre_compile): outputs=model['subgraphs']['outputs'], input_size_list=input_size_list) elif model['platform'] == 'tflite': - model_file_path = os.path.join(config_path, model['model_file_path']) rknn.load_tflite(model=model_file_path) - elif model['platform'] == 'caffe': - prototxt_file_path = os.path.join(config_path,model['prototxt_file_path']) - caffemodel_file_path = os.path.join(config_path,model['caffemodel_file_path']) - rknn.load_caffe(model=prototxt_file_path, proto='caffe', blobs=caffemodel_file_path) elif model['platform'] == 'onnx': - model_file_path = os.path.join(config_path, model['model_file_path']) rknn.load_onnx(model=model_file_path) + elif model['platform'] == 'caffe': + prototxt_file_path = os.path.join(config_path, model['prototxt_file_path']) + caffemodel_file_path = os.path.join(config_path, model['caffemodel_file_path']) + rknn.load_caffe(model=prototxt_file_path, proto='caffe', blobs=caffemodel_file_path) else: print("Platform {:} is not supported! Moving on.".format(model['platform'])) continue From d929ceaa1934773566953a9cead373d926b62c2b Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 28 May 2021 13:47:50 +0300 Subject: [PATCH 5/7] Fixes around input-size-list --- .../models/tensorflow/mobilenet-ssd/model_config.yml | 4 ++-- examples/rknn_convert/rknn_convert.py | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/examples/rknn_convert/models/tensorflow/mobilenet-ssd/model_config.yml b/examples/rknn_convert/models/tensorflow/mobilenet-ssd/model_config.yml index 57d0465..162dbcc 100644 --- a/examples/rknn_convert/models/tensorflow/mobilenet-ssd/model_config.yml +++ b/examples/rknn_convert/models/tensorflow/mobilenet-ssd/model_config.yml @@ -6,8 +6,8 @@ models: subgraphs: inputs: - FeatureExtractor/MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/mul_1 - input-size-list: - - 300,300,3 + input_tensor_shapes: + - [300, 300, 3] outputs: - concat - concat_1 diff --git a/examples/rknn_convert/rknn_convert.py b/examples/rknn_convert/rknn_convert.py index a3ac5c9..75f992a 100644 --- a/examples/rknn_convert/rknn_convert.py +++ b/examples/rknn_convert/rknn_convert.py @@ -42,15 +42,11 @@ def convert_model(config_path, out_path, pre_compile): print('--> Load model...') model_file_path = os.path.join(config_path, model['model_file_path']) if model['platform'] == 'tensorflow': - input_size_list = [] - for input_size_str in model['subgraphs']['input-size-list']: - input_size = list(map(int, input_size_str.split(','))) - input_size_list.append(input_size) - pass + subgraphs = model['subgraphs'] rknn.load_tensorflow(tf_pb=model_file_path, - inputs=model['subgraphs']['inputs'], - outputs=model['subgraphs']['outputs'], - input_size_list=input_size_list) + inputs=subgraphs['inputs'], + outputs=subgraphs['outputs'], + input_size_list=subgraphs['input_tensor_shapes']) elif model['platform'] == 'tflite': rknn.load_tflite(model=model_file_path) elif model['platform'] == 'onnx': From 3aa5419522301626f8bc42fbf03591238b8431b8 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 28 May 2021 14:11:07 +0300 Subject: [PATCH 6/7] Proper argument handling --- examples/rknn_convert/rknn_convert.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/examples/rknn_convert/rknn_convert.py b/examples/rknn_convert/rknn_convert.py index 75f992a..0976696 100644 --- a/examples/rknn_convert/rknn_convert.py +++ b/examples/rknn_convert/rknn_convert.py @@ -2,6 +2,7 @@ import sys import os +import argparse import ruamel.yaml from rknn.api import RKNN @@ -78,9 +79,24 @@ def convert_model(config_path, out_path, pre_compile): return exported_rknn_model_paths +def parse_args(*argv): + parser = argparse.ArgumentParser(description="Build RKNN models") + parser.add_argument("-c", "--config", required=True) + parser.add_argument("-o", "--out_dir", required=True) + parser.add_argument("-p", "--precompile", action="store_true") + args = parser.parse_args(argv) + + if not os.path.isfile(args.config): + print("Enter an existing config file.") + sys.exit(-1) + return args.config, args.out_dir, args.precompile + + if __name__ == '__main__': - config_path = sys.argv[1] - out_path = sys.argv[2] - pre_compile = sys.argv[3] in ['true', '1', 'True'] + config_path, out_path, pre_compile = parse_args(*sys.argv[1:]) + #print(config_path, out_path, pre_compile) + + if out_path: + os.makedirs(out_path, exist_ok=True) convert_model(config_path, out_path, pre_compile) From a345d99be30c1ca84f56dd68a213c740450eeaa5 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 28 May 2021 14:17:46 +0300 Subject: [PATCH 7/7] Remove redundant checks (belongs to previous commit) --- examples/rknn_convert/rknn_convert.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/examples/rknn_convert/rknn_convert.py b/examples/rknn_convert/rknn_convert.py index 0976696..5a1ac61 100644 --- a/examples/rknn_convert/rknn_convert.py +++ b/examples/rknn_convert/rknn_convert.py @@ -16,18 +16,11 @@ def parse_model_config(config_file): return yaml.load(config_text) -def convert_model(config_path, out_path, pre_compile): - exported_rknn_model_paths = [] +def convert_model(config_file, out_path, pre_compile): - if os.path.isfile(config_path): - config_file = os.path.abspath(config_path) - config_path = os.path.dirname(config_file) - else: - config_file = os.path.join(config_path, 'model_config.yml') - if not os.path.exists(config_file): - print('Model config {:} not exist!'.format(config_file)) - return exported_rknn_model_paths + config_path = os.path.dirname(config_file) + exported_rknn_model_paths = [] config = parse_model_config(config_file) if config is None: print('Invalid configuration.') @@ -93,10 +86,10 @@ def parse_args(*argv): if __name__ == '__main__': - config_path, out_path, pre_compile = parse_args(*sys.argv[1:]) - #print(config_path, out_path, pre_compile) + config_file, out_path, pre_compile = parse_args(*sys.argv[1:]) + #print(config_file, out_path, pre_compile) if out_path: os.makedirs(out_path, exist_ok=True) - convert_model(config_path, out_path, pre_compile) + convert_model(config_file, out_path, pre_compile)