alexander | f4e2c47 | 2021-05-14 13:14:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 2 | |
| 3 | # Copyright (c) 2021 Arm Limited. All rights reserved. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | import os, errno |
| 19 | import urllib.request |
| 20 | import subprocess |
| 21 | import fnmatch |
| 22 | import logging |
| 23 | import sys |
| 24 | |
| 25 | from argparse import ArgumentParser |
| 26 | from urllib.error import URLError |
| 27 | |
| 28 | json_uc_res = [{ |
| 29 | "use_case_name": "ad", |
| 30 | "resources": [{"name": "ad_medium_int8.tflite", |
| 31 | "url": "https://github.com/ARM-software/ML-zoo/raw/7c32b097f7d94aae2cd0b98a8ed5a3ba81e66b18/models/anomaly_detection/micronet_medium/tflite_int8/ad_medium_int8.tflite"}, |
| 32 | {"name": "ifm0.npy", |
| 33 | "url": "https://github.com/ARM-software/ML-zoo/raw/7c32b097f7d94aae2cd0b98a8ed5a3ba81e66b18/models/anomaly_detection/micronet_medium/tflite_int8/testing_input/input/0.npy"}, |
| 34 | {"name": "ofm0.npy", |
| 35 | "url": "https://github.com/ARM-software/ML-zoo/raw/7c32b097f7d94aae2cd0b98a8ed5a3ba81e66b18/models/anomaly_detection/micronet_medium/tflite_int8/testing_output/Identity/0.npy"}] |
| 36 | }, |
| 37 | { |
| 38 | "use_case_name": "asr", |
| 39 | "resources": [{"name": "wav2letter_int8.tflite", |
| 40 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/speech_recognition/wav2letter/tflite_int8/wav2letter_int8.tflite"}, |
| 41 | {"name": "ifm0.npy", |
| 42 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/speech_recognition/wav2letter/tflite_int8/testing_input/input_2_int8/0.npy"}, |
| 43 | {"name": "ofm0.npy", |
| 44 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/speech_recognition/wav2letter/tflite_int8/testing_output/Identity_int8/0.npy"}] |
| 45 | }, |
| 46 | { |
| 47 | "use_case_name": "img_class", |
| 48 | "resources": [{"name": "mobilenet_v2_1.0_224_quantized_1_default_1.tflite", |
| 49 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/image_classification/mobilenet_v2_1.0_224/tflite_uint8/mobilenet_v2_1.0_224_quantized_1_default_1.tflite"}, |
| 50 | {"name": "ifm0.npy", |
| 51 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/image_classification/mobilenet_v2_1.0_224/tflite_uint8/testing_input/input/0.npy"}, |
| 52 | {"name": "ofm0.npy", |
| 53 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/image_classification/mobilenet_v2_1.0_224/tflite_uint8/testing_output/output/0.npy"}] |
| 54 | }, |
| 55 | { |
| 56 | "use_case_name": "kws", |
| 57 | "resources": [{"name": "ds_cnn_clustered_int8.tflite", |
| 58 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/ds_cnn_clustered_int8.tflite"}, |
| 59 | {"name": "ifm0.npy", |
| 60 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/testing_input/input_2/0.npy"}, |
| 61 | {"name": "ofm0.npy", |
| 62 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/testing_output/Identity/0.npy"}] |
| 63 | }, |
| 64 | { |
| 65 | "use_case_name": "kws_asr", |
| 66 | "resources": [{"name": "wav2letter_int8.tflite", |
| 67 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/speech_recognition/wav2letter/tflite_int8/wav2letter_int8.tflite"}, |
| 68 | {"sub_folder": "asr", "name": "ifm0.npy", |
| 69 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/speech_recognition/wav2letter/tflite_int8/testing_input/input_2_int8/0.npy"}, |
| 70 | {"sub_folder": "asr", "name": "ofm0.npy", |
| 71 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/speech_recognition/wav2letter/tflite_int8/testing_output/Identity_int8/0.npy"}, |
| 72 | {"name": "ds_cnn_clustered_int8.tflite", |
| 73 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/ds_cnn_clustered_int8.tflite"}, |
| 74 | {"sub_folder": "kws", "name": "ifm0.npy", |
| 75 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/testing_input/input_2/0.npy"}, |
| 76 | {"sub_folder": "kws", "name": "ofm0.npy", |
| 77 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/testing_output/Identity/0.npy"}] |
| 78 | }, |
| 79 | { |
| 80 | "use_case_name": "inference_runner", |
| 81 | "resources": [{"name": "dnn_s_quantized.tflite", |
| 82 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/dnn_small/tflite_int8/dnn_s_quantized.tflite"} |
| 83 | ] |
| 84 | },] |
| 85 | |
| 86 | |
| 87 | def call_command(command: str) -> str: |
| 88 | """ |
| 89 | Helpers function that call subprocess and return the output. |
| 90 | |
| 91 | Parameters: |
| 92 | ---------- |
| 93 | command (string): Specifies the command to run. |
| 94 | """ |
| 95 | logging.info(command) |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 96 | proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) |
| 97 | log = proc.stdout.decode("utf-8") |
| 98 | if proc.returncode == 0: |
| 99 | logging.info(log) |
| 100 | else: |
| 101 | logging.error(log) |
| 102 | proc.check_returncode() |
| 103 | return log |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 104 | |
| 105 | |
| 106 | def set_up_resources(run_vela_on_models=False): |
| 107 | """ |
| 108 | Helpers function that retrieve the output from a command. |
| 109 | |
| 110 | Parameters: |
| 111 | ---------- |
| 112 | run_vela_on_models (bool): Specifies if run vela on downloaded models. |
| 113 | """ |
| 114 | current_file_dir = os.path.dirname(os.path.abspath(__file__)) |
| 115 | download_dir = os.path.abspath(os.path.join(current_file_dir, "resources_downloaded")) |
| 116 | logging.basicConfig(filename='log_build_default.log', level=logging.DEBUG) |
| 117 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 118 | |
| 119 | try: |
| 120 | # 1.1 Does the download dir exist? |
| 121 | os.mkdir(download_dir) |
| 122 | except OSError as e: |
| 123 | if e.errno == errno.EEXIST: |
| 124 | logging.info("'resources_downloaded' directory exists.") |
| 125 | else: |
| 126 | raise |
| 127 | |
| 128 | # 1.2 Does the virtual environment exist? |
| 129 | env_python = str(os.path.abspath(os.path.join(download_dir, "env", "bin", "python3"))) |
| 130 | env_activate = str(os.path.abspath(os.path.join(download_dir, "env", "bin", "activate"))) |
| 131 | if not os.path.isdir(os.path.join(download_dir, "env")): |
| 132 | os.chdir(download_dir) |
| 133 | # Create the virtual environment |
| 134 | command = "python3 -m venv env" |
| 135 | call_command(command) |
| 136 | commands = ["pip install --upgrade pip", "pip install --upgrade setuptools"] |
| 137 | for c in commands: |
| 138 | command = f"{env_python} -m {c}" |
| 139 | call_command(command) |
| 140 | os.chdir(current_file_dir) |
| 141 | # 1.3 Make sure to have all the requirement |
| 142 | requirements = ["ethos-u-vela==2.1.1"] |
| 143 | command = f"{env_python} -m pip freeze" |
| 144 | packages = call_command(command) |
| 145 | for req in requirements: |
| 146 | if req not in packages: |
| 147 | command = f"{env_python} -m pip install {req}" |
| 148 | call_command(command) |
| 149 | |
| 150 | # 2. Download models |
| 151 | for uc in json_uc_res: |
| 152 | try: |
| 153 | # Does the usecase_name download dir exist? |
| 154 | os.mkdir(os.path.join(download_dir, uc["use_case_name"])) |
| 155 | except OSError as e: |
| 156 | if e.errno != errno.EEXIST: |
| 157 | logging.error(f"Error creating {uc['use_case_name']} directory.") |
| 158 | raise |
| 159 | |
| 160 | for res in uc["resources"]: |
| 161 | res_name = res["name"] |
| 162 | res_url = res["url"] |
| 163 | if "sub_folder" in res: |
| 164 | try: |
| 165 | # Does the usecase_name/sub_folder download dir exist? |
| 166 | os.mkdir(os.path.join(download_dir, uc["use_case_name"], res["sub_folder"])) |
| 167 | except OSError as e: |
| 168 | if e.errno != errno.EEXIST: |
| 169 | logging.error(f"Error creating {uc['use_case_name']} / {res['sub_folder']} directory.") |
| 170 | raise |
| 171 | res_dst = os.path.join(download_dir, |
| 172 | uc["use_case_name"], |
| 173 | res["sub_folder"], |
| 174 | res_name) |
| 175 | else: |
| 176 | res_dst = os.path.join(download_dir, |
| 177 | uc["use_case_name"], |
| 178 | res_name) |
| 179 | try: |
| 180 | g = urllib.request.urlopen(res_url) |
| 181 | with open(res_dst, 'b+w') as f: |
| 182 | f.write(g.read()) |
| 183 | logging.info(f"- Downloaded {res_url} to {res_dst}.") |
| 184 | except URLError: |
| 185 | logging.error(f"URLError while downloading {res_url}.") |
| 186 | raise |
| 187 | |
| 188 | # 3. Run vela on models in resources_downloaded |
| 189 | # New models will have same name with '_vela' appended. |
| 190 | # For example: |
| 191 | # original model: ds_cnn_clustered_int8.tflite |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 192 | # after vela model: ds_cnn_clustered_int8_vela_H128.tflite |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 193 | # |
| 194 | # Note: To avoid to run vela twice on the same model, it's supposed that |
| 195 | # downloaded model names don't contain the 'vela' word. |
| 196 | if run_vela_on_models is True: |
| 197 | config_file = os.path.join(current_file_dir, "scripts", "vela", "default_vela.ini") |
| 198 | models = [os.path.join(dirpath, f) |
| 199 | for dirpath, dirnames, files in os.walk(download_dir) |
| 200 | for f in fnmatch.filter(files, '*.tflite') if "vela" not in f] |
| 201 | |
| 202 | for model in models: |
| 203 | output_dir = os.path.dirname(model) |
| 204 | command = (f". {env_activate} && vela {model} " + |
| 205 | "--accelerator-config=ethos-u55-128 " + |
| 206 | "--block-config-limit=0 " + |
| 207 | f"--config {config_file} " + |
| 208 | "--memory-mode=Shared_Sram " + |
| 209 | "--system-config=Ethos_U55_High_End_Embedded " + |
| 210 | f"--output-dir={output_dir}") |
| 211 | call_command(command) |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 212 | # model name after compiling with vela is an initial model name + _vela suffix |
| 213 | vela_optimised_model_path = str(model).replace(".tflite", "_vela.tflite") |
| 214 | # we want it to be initial model name + _vela_H128 suffix which indicates selected MAC config. |
| 215 | new_vela_optimised_model_path = vela_optimised_model_path.replace("_vela.tflite", "_vela_H128.tflite") |
| 216 | # rename default vela model |
| 217 | os.rename(vela_optimised_model_path, new_vela_optimised_model_path) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 218 | |
| 219 | |
| 220 | if __name__ == '__main__': |
| 221 | parser = ArgumentParser() |
| 222 | parser.add_argument("--skip-vela", |
| 223 | help="Do not run Vela optimizer on downloaded models.", |
| 224 | action="store_true") |
| 225 | args = parser.parse_args() |
| 226 | set_up_resources(not args.skip_vela) |