Skip to content

Commit 36a75a8

Browse files
1. Add download_unpack_cnn_models.py. 2. Modify model_list.txt. Put zip names explicitly
1 parent 0422dd3 commit 36a75a8

File tree

2 files changed

+266
-51
lines changed

2 files changed

+266
-51
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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)))

caffe_models_zipped/model_list.txt

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,55 @@
66
# python download_unpack_cnn_models.py --model_list model_list.txt --model_path <absolute_path_to_model_folder>
77
# You can edit the list of models below
88

9-
alexnet
10-
DAN
11-
deeplab
12-
denoiser
13-
densenet
14-
face_net
15-
facedetect_v1
16-
facedetect_v2
17-
faster_rcnn_resnet101
18-
fcn
19-
googlenet
20-
icnet
21-
imagenet_mean
22-
images
23-
inception_resnet_v1
24-
inception_resnet_v2
25-
inception_v1
26-
inception_v2
27-
inception_v3
28-
inception_v4
29-
lenet
30-
mobilenet
31-
mobilenet_ssd
32-
mtcnn_v1
33-
openpose
34-
pspnet
35-
pvanet
36-
resnet50_ssd
37-
resnet_101
38-
resnet_152
39-
resnet_50
40-
ResNeXt_101
41-
ResNeXt_152
42-
ResNeXt_50
43-
retinanet
44-
segnet
45-
shufflenet_v1
46-
shufflenet_v2
47-
squeeze
48-
srcnn
49-
srgan
50-
ssd
51-
unet
52-
vdsr
53-
vgg16
54-
yolo_tiny
55-
yolo_v1
56-
yolo_v2_coco
57-
yolo_v2_tiny
58-
yolo_v2_voc
59-
yolo_v3
9+
alexnet.zip
10+
DAN.zip
11+
deeplab.zip
12+
denoiser.zip
13+
densenet.zip
14+
face_net.zip
15+
facedetect_v1.zip
16+
facedetect_v2.zip
17+
faster_rcnn_resnet101.zip
18+
fcn1.zip
19+
fcn2.zip
20+
googlenet.zip
21+
icnet.zip
22+
imagenet_mean.zip
23+
images.zip
24+
inception_resnet_v1.zip
25+
inception_resnet_v2.zip
26+
inception_v1.zip
27+
inception_v2.zip
28+
inception_v3.zip
29+
inception_v4.zip
30+
lenet.zip
31+
mobilenet.zip
32+
mobilenet_ssd.zip
33+
mtcnn_v1.zip
34+
openpose.zip
35+
pspnet.zip
36+
pvanet.zip
37+
resnet50_ssd.zip
38+
resnet_101.zip
39+
resnet_152.zip
40+
resnet_50.zip
41+
ResNeXt_101.zip
42+
ResNeXt_152.zip
43+
ResNeXt_50.zip
44+
retinanet.zip
45+
segnet.zip
46+
shufflenet_v1.zip
47+
shufflenet_v2.zip
48+
squeeze.zip
49+
srcnn.zip
50+
srgan.zip
51+
ssd.zip
52+
unet.zip
53+
vdsr.zip
54+
vgg16.zip
55+
yolo_tiny.zip
56+
yolo_v1.zip
57+
yolo_v2_coco.zip
58+
yolo_v2_tiny.zip
59+
yolo_v2_voc.zip
60+
yolo_v3.zip

0 commit comments

Comments
 (0)