-
Notifications
You must be signed in to change notification settings - Fork 10
/
scan.py
83 lines (61 loc) · 2.78 KB
/
scan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import json
def scan_data_config(model_name):
"""
Take a model name and return a dictionary of whether the associated data and configuration files
are available for that model or not.
"""
available_data_config = {
'example_data': False,
'example_config': False}
#Check data availability
for filename in os.listdir("./data/"):
if filename.startswith(model_name):
available_data_config['example_data'] = True
#Check config file availability
for filename in os.listdir("./config-files/"):
if filename.startswith(model_name):
available_data_config['example_config'] = True
return available_data_config
def scan_subdir(model_type):
"""Scan a directory and return a list of example models, and a dictionary indicating
whether each model has associated example data and example config file or not."""
#Create the path to the sub directory
dir_path = model_type + "/"
#List of available models
available_models = []
#Dictionary to save data's and config's availability
data_config_avai = {}
for filename in os.listdir(dir_path):
if model_type == 'keras':
if filename.endswith('.json'):
available_models.append(filename)
data_config_avai[filename] = scan_data_config(filename[:-5])
elif model_type == 'pytorch':
if filename.endswith('.pt'):
available_models.append(filename)
data_config_avai[filename] = scan_data_config(filename[:-3])
elif model_type == 'tensorflow':
if filename.endswith('.pb'):
available_models.append(filename)
data_config_avai[filename] = scan_data_config(filename[:-3])
elif model_type == 'onnx':
if filename.endswith('.onnx'):
available_models.append(filename)
data_config_avai[filename] = scan_data_config(filename[:-5])
return available_models, data_config_avai
def scan():
"""Scan all directories that contain example models and write to a file."""
available_types = ['keras', 'pytorch', 'onnx', 'tensorflow']
#Dictionary to save available model list
model_dict = {}
#Dictionary to check for data and configuration availability
data_config_avai = {}
for model_type in available_types:
model_dict[model_type], temp_data_config_avai = scan_subdir(model_type)
#Upate the availability of data and configuration for new scanned model
data_config_avai.update(temp_data_config_avai)
# Serialize data into file:
json.dump(model_dict, open("available_models.json", 'w' ))
json.dump(data_config_avai, open("available_data_config.json", 'w' ))
scan()