|
| 1 | +# ============================================================================== |
| 2 | +# Copyright 2021 Synopsys, Inc. |
| 3 | +# |
| 4 | +# This file and the associated documentation are proprietary to Synopsys, |
| 5 | +# Inc., and may only be used in accordance with the terms and conditions of |
| 6 | +# a written license agreement with Synopsys, Inc. |
| 7 | +# Notwithstanding contrary terms in the DFPUC, Licensee may provide the |
| 8 | +# binaries of the EV CNN SDK to its end-customer that purchase Licensee ICs |
| 9 | +# that incorporate the Synopsys EV processor core with the CNN option, |
| 10 | +# subject to confidentiality terms no less restrictive than those contained in |
| 11 | +# the DFPUC. All other use, reproduction, or distribution of this file |
| 12 | +# is strictly prohibited. |
| 13 | +# ============================================================================== |
| 14 | + |
| 15 | +""" |
| 16 | +.. module:: download_unpack_cnn_modeles |
| 17 | + :platform: Unix, Windows |
| 18 | + :synopsis: Download zipped models from GitHub and unpack them |
| 19 | +
|
| 20 | +.. moduleauthors:: Dmitry Golovkin <[email protected] |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | +import os |
| 25 | +from pathlib import Path |
| 26 | +import argparse |
| 27 | +import re |
| 28 | +import shutil |
| 29 | +import zipfile |
| 30 | +import time |
| 31 | +import multiprocessing |
| 32 | +import requests |
| 33 | + |
| 34 | +# Multi-process shared objects |
| 35 | +download_cnt = multiprocessing.Value('d', 0) # Count downloaded models |
| 36 | +issue_cnt = multiprocessing.Value('d', 0) # Count issues |
| 37 | +model_list_num = multiprocessing.Value('d', 0) |
| 38 | + |
| 39 | + |
| 40 | +def download_unpack_zip(zip_name): |
| 41 | + """ |
| 42 | + Download and unpack one zip file |
| 43 | + """ |
| 44 | + |
| 45 | + digit_match = re.search(r'\d.zip$', zip_name) # if we have multi archive |
| 46 | + if digit_match: |
| 47 | + name = zip_name[:-5] # cut last character with number |
| 48 | + else: |
| 49 | + name = zip_name[:-4] |
| 50 | + |
| 51 | + model_out_folder = output_path/name |
| 52 | + |
| 53 | + if model_out_folder.is_dir(): |
| 54 | + if FORCE_DOWNLOAD: |
| 55 | + print("[INFO] --force option is set. {} will be rewritten". |
| 56 | + format(model_out_folder)) |
| 57 | + shutil.rmtree(model_out_folder) |
| 58 | + else: |
| 59 | + print("[INFO] Skip downloading {}. Model already exists here {}". |
| 60 | + format(zip_name, model_out_folder)) |
| 61 | + return |
| 62 | + |
| 63 | + file_name_url = git_hub_repo_url + '/' + zip_name |
| 64 | + file_name_zip_out = zip_path / zip_name |
| 65 | + |
| 66 | + print("[INFO] Start downloading {}".format(zip_name)) |
| 67 | + request_check = requests.get(file_name_url) |
| 68 | + if request_check.status_code != 200: |
| 69 | + issue_cnt.value += 1 |
| 70 | + print(" [ERROR]. Can not access to Zip file by address {}". |
| 71 | + format(file_name_url)) |
| 72 | + print("[INFO] File {} is missed. {} from {} " |
| 73 | + .format(zip_name, int(issue_cnt.value), |
| 74 | + int(model_list_num.value))) |
| 75 | + return |
| 76 | + |
| 77 | + with requests.get(file_name_url, stream=True) as r_zip_file: |
| 78 | + with open(file_name_zip_out, 'wb') as f_download: |
| 79 | + shutil.copyfileobj(r_zip_file.raw, f_download) |
| 80 | + |
| 81 | + with zipfile.ZipFile(file_name_zip_out, 'r') as zip_ref: |
| 82 | + zip_ref.extractall(output_path) |
| 83 | + |
| 84 | + download_cnt.value += 1 |
| 85 | + print("[INFO] {} is downloaded and unpacked. {}th from {} zip files" |
| 86 | + .format(zip_name, int(download_cnt.value), |
| 87 | + int(model_list_num.value))) |
| 88 | + |
| 89 | + |
| 90 | +def arg2bool(bool_arg): |
| 91 | + """ |
| 92 | + argparse does not allow bools by default, very annoying |
| 93 | + """ |
| 94 | + # In case options come from files they may have weird characters such as \r |
| 95 | + bool_arg = bool_arg.replace('\n', "").replace('\r', "") |
| 96 | + if bool_arg.lower() in ('yes', 'true', 't', 'y', '1', 'on'): |
| 97 | + return True |
| 98 | + |
| 99 | + if bool_arg.lower() in ('no', 'false', 'f', 'n', '0', 'off'): |
| 100 | + return False |
| 101 | + |
| 102 | + raise argparse.ArgumentTypeError('Boolean value expected.') |
| 103 | + |
| 104 | + |
| 105 | +parser = argparse.ArgumentParser() |
| 106 | + |
| 107 | +parser.add_argument( |
| 108 | + "--model_path", type=str, required=False, |
| 109 | + help="Path to NN Models home") |
| 110 | + |
| 111 | +parser.add_argument( |
| 112 | + "--model_list", type=str, required=True, |
| 113 | + help="File with list of models") |
| 114 | + |
| 115 | +parser.add_argument( |
| 116 | + "--force", type=arg2bool, nargs='?', const=True, required=False, |
| 117 | + help="Force to download/unpack models. Overwrite existed") |
| 118 | + |
| 119 | +args = parser.parse_args() |
| 120 | + |
| 121 | +list_file = Path(args.model_list) |
| 122 | + |
| 123 | +FORCE_DOWNLOAD = False # Rewrite existed models |
| 124 | +if args.force is not None: |
| 125 | + if args.force: |
| 126 | + FORCE_DOWNLOAD = True |
| 127 | + |
| 128 | +if not list_file.is_file(): |
| 129 | + print("[ERROR]. List file {} is not found".format(list_file)) |
| 130 | + exit(1) |
| 131 | + |
| 132 | +# Get a file with models |
| 133 | +with open(list_file, "r") as f: |
| 134 | + zips_files_lines = f.readlines() |
| 135 | + |
| 136 | +# Create zip lists from list_file |
| 137 | +model_list = [] |
| 138 | +for line in zips_files_lines: |
| 139 | + model_name = line.strip() |
| 140 | + # Avoid comments and empty strings |
| 141 | + if model_name.isspace() or model_name == "" or model_name[0] == '#': |
| 142 | + # print("[DEBUG] string is empty or comment {}".format(model_name)) |
| 143 | + continue |
| 144 | + model_list.extend([model_name]) |
| 145 | + |
| 146 | +model_list_num.value = len(model_list) |
| 147 | + |
| 148 | +if model_list_num == 0: |
| 149 | + print("[ERROR]. There is no model names in model list file {}". |
| 150 | + format(list_file)) |
| 151 | + exit(1) |
| 152 | + |
| 153 | +print("[INFO] Number of models in the model list is {}" |
| 154 | + .format(model_list_num.value)) |
| 155 | + |
| 156 | +# Get and check output path |
| 157 | +if args.model_path: |
| 158 | + output_path = Path(args.model_path)/"caffe_models" |
| 159 | +elif os.environ['EV_CNNMODELS_HOME']: |
| 160 | + output_path = Path(os.environ['EV_CNNMODELS_HOME']) / "caffe_models" |
| 161 | +else: |
| 162 | + print("[ERROR]. Output path is not defined. " |
| 163 | + "Use --model_path or setup EV_CNNMODELS_HOME environment variable") |
| 164 | + exit(1) |
| 165 | +try: |
| 166 | + output_path.mkdir(parents=True, exist_ok=True) |
| 167 | +except: |
| 168 | + print("[ERROR]. Problem of creating CNN Models folder {}". |
| 169 | + format(output_path)) |
| 170 | + exit(1) |
| 171 | + |
| 172 | +zip_path = output_path/"download" # folder for downloaded zip files |
| 173 | +try: |
| 174 | + zip_path.mkdir(parents=True, exist_ok=True) |
| 175 | +except: |
| 176 | + print( |
| 177 | + "[ERROR]. Problem of creating folder to download zipped CNN Models: {}" |
| 178 | + .format(zip_path)) |
| 179 | + exit(1) |
| 180 | + |
| 181 | +print("[INFO] NN Models path is {}".format(output_path)) |
| 182 | +print("[INFO] Zipped NN Models path is {}".format(zip_path)) |
| 183 | + |
| 184 | +git_hub_repo_url = 'https://github.com/foss-for-synopsys-dwc-arc-processors/synopsys-caffe-models/raw/master/caffe_models_zipped' |
| 185 | + |
| 186 | +# check GitHub site access |
| 187 | +request = requests.get(git_hub_repo_url) |
| 188 | +if request.status_code != 200: |
| 189 | + print("[ERROR]. Can not access to GitHub by address {}" |
| 190 | + .format(git_hub_repo_url)) |
| 191 | + exit(1) |
| 192 | + |
| 193 | +# Download and unpack zip files in parallel mode |
| 194 | +download_cnt.value = 0 |
| 195 | +issue_cnt.value = 0 |
| 196 | +MAX_NUM_CORE = 8 |
| 197 | +num_cores = multiprocessing.cpu_count() |
| 198 | +parallel_task = num_cores if num_cores < MAX_NUM_CORE else MAX_NUM_CORE |
| 199 | + |
| 200 | +start_time = time.time() |
| 201 | +with multiprocessing.Pool(parallel_task) as p: |
| 202 | + p.map(download_unpack_zip, model_list) |
| 203 | + |
| 204 | +end_time = int(time.time() - start_time) |
| 205 | +hours, rem = divmod(end_time, 3600) |
| 206 | +minutes, seconds = divmod(rem, 60) |
| 207 | + |
| 208 | +print("[INFO] Finish. \n" |
| 209 | + " Total models: {}\n" |
| 210 | + " Successful: {}\n" |
| 211 | + " Failed : {}\n" |
| 212 | + " Time: {:0>2}:{:0>2}:{:05.2f}". |
| 213 | + format(int(model_list_num.value), int(download_cnt.value), |
| 214 | + int(issue_cnt.value), int(hours), int(minutes), int(seconds))) |
0 commit comments