alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | # Copyright (c) 2021 Arm Limited. All rights reserved. |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | """ |
| 17 | Utility script to generate model c file that can be included in the |
| 18 | project directly. This should be called as part of cmake framework |
| 19 | should the models need to be generated at configuration stage. |
| 20 | """ |
| 21 | import datetime |
| 22 | import os |
| 23 | from argparse import ArgumentParser |
| 24 | from pathlib import Path |
| 25 | from jinja2 import Environment, FileSystemLoader |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 26 | import binascii |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | |
| 28 | parser = ArgumentParser() |
| 29 | |
| 30 | parser.add_argument("--tflite_path", help="Model (.tflite) path", required=True) |
| 31 | parser.add_argument("--output_dir", help="Output directory", required=True) |
| 32 | parser.add_argument('-e', '--expression', action='append', default=[], dest="expr") |
| 33 | parser.add_argument('--header', action='append', default=[], dest="headers") |
| 34 | parser.add_argument('-ns', '--namespaces', action='append', default=[], dest="namespaces") |
| 35 | parser.add_argument("--license_template", type=str, help="Header template file", |
| 36 | default="header_template.txt") |
| 37 | args = parser.parse_args() |
| 38 | |
| 39 | env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')), |
| 40 | trim_blocks=True, |
| 41 | lstrip_blocks=True) |
| 42 | |
| 43 | |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 44 | def get_tflite_data(tflite_path: str) -> list: |
| 45 | """ |
| 46 | Reads a binary file and returns a C style array as a |
| 47 | list of strings. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 49 | Argument: |
| 50 | tflite_path: path to the tflite model. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 52 | Returns: |
| 53 | list of strings |
| 54 | """ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | with open(tflite_path, 'rb') as tflite_model: |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 56 | data = tflite_model.read() |
| 57 | |
| 58 | bytes_per_line = 32 |
| 59 | hex_digits_per_line = bytes_per_line * 2 |
| 60 | hexstream = binascii.hexlify(data).decode('utf-8') |
| 61 | hexstring = '{' |
| 62 | |
| 63 | for i in range(0, len(hexstream), 2): |
| 64 | if 0 == (i % hex_digits_per_line): |
| 65 | hexstring += "\n" |
| 66 | hexstring += '0x' + hexstream[i:i+2] + ", " |
| 67 | |
| 68 | hexstring += '};\n' |
| 69 | return [hexstring] |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def main(args): |
| 73 | if not os.path.isfile(args.tflite_path): |
| 74 | raise Exception(f"{args.tflite_path} not found") |
| 75 | |
| 76 | # Cpp filename: |
| 77 | cpp_filename = Path(os.path.join(args.output_dir, os.path.basename(args.tflite_path) + ".cc")).absolute() |
| 78 | print(f"++ Converting {os.path.basename(args.tflite_path)} to\ |
| 79 | {os.path.basename(cpp_filename)}") |
| 80 | |
| 81 | os.makedirs(cpp_filename.parent, exist_ok=True) |
| 82 | |
| 83 | header_template = env.get_template(args.license_template) |
| 84 | |
| 85 | hdr = header_template.render(script_name=os.path.basename(__file__), |
| 86 | file_name=os.path.basename(args.tflite_path), |
| 87 | gen_time=datetime.datetime.now(), |
| 88 | year=datetime.datetime.now().year) |
| 89 | |
| 90 | env.get_template('tflite.cc.template').stream(common_template_header=hdr, |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 91 | model_data=get_tflite_data(args.tflite_path), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | expressions=args.expr, |
| 93 | additional_headers=args.headers, |
| 94 | namespaces=args.namespaces).dump(str(cpp_filename)) |
| 95 | |
| 96 | |
| 97 | if __name__ == '__main__': |
| 98 | main(args) |