|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import glob |
|
6
|
|
|
import dill |
|
7
|
|
|
import inspect |
|
8
|
|
|
import hashlib |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def get_func_str(func): |
|
12
|
|
|
return inspect.getsource(func) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def get_hash(object): |
|
16
|
|
|
return hashlib.sha1(object).hexdigest() |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def get_model_id(model): |
|
20
|
|
|
return str(get_hash(get_func_str(model).encode("utf-8"))) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def _get_pkl_hash(hash, model_path): |
|
24
|
|
|
paths = glob.glob(model_path + hash + "*.pkl") |
|
25
|
|
|
|
|
26
|
|
|
return paths |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def _hash2obj(search_space, model_path): |
|
30
|
|
|
hash2obj_dict = {} |
|
31
|
|
|
para_hash_list = _get_para_hash_list(search_space) |
|
32
|
|
|
|
|
33
|
|
|
for para_hash in para_hash_list: |
|
34
|
|
|
obj = _read_dill(para_hash, model_path) |
|
35
|
|
|
hash2obj_dict[para_hash] = obj |
|
36
|
|
|
|
|
37
|
|
|
return hash2obj_dict |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def _read_dill(value, model_path): |
|
41
|
|
|
paths = _get_pkl_hash(value, model_path) |
|
42
|
|
|
for path in paths: |
|
43
|
|
|
with open(path, "rb") as fp: |
|
44
|
|
|
value = dill.load(fp) |
|
45
|
|
|
value = dill.loads(value) |
|
46
|
|
|
break |
|
47
|
|
|
|
|
48
|
|
|
return value |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
def _get_para_hash_list(search_space): |
|
52
|
|
|
para_hash_list = [] |
|
53
|
|
|
for key in search_space.keys(): |
|
54
|
|
|
values = search_space[key] |
|
55
|
|
|
|
|
56
|
|
|
for value in values: |
|
57
|
|
|
if ( |
|
58
|
|
|
not isinstance(value, int) |
|
59
|
|
|
and not isinstance(value, float) |
|
60
|
|
|
and not isinstance(value, str) |
|
61
|
|
|
): |
|
62
|
|
|
|
|
63
|
|
|
para_dill = dill.dumps(value) |
|
64
|
|
|
para_hash = get_hash(para_dill) |
|
65
|
|
|
para_hash_list.append(para_hash) |
|
66
|
|
|
|
|
67
|
|
|
return para_hash_list |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
""" |
|
71
|
|
|
def is_sha1(maybe_sha): |
|
72
|
|
|
if len(maybe_sha) != 40: |
|
73
|
|
|
return False |
|
74
|
|
|
try: |
|
75
|
|
|
sha_int = int(maybe_sha, 16) |
|
76
|
|
|
except ValueError: |
|
77
|
|
|
return False |
|
78
|
|
|
return True |
|
79
|
|
|
""" |
|
80
|
|
|
|