Repository navigation
Expand file tree
/
Copy pathmodel_config.py
More file actions
74 lines (57 loc) · 2.34 KB
/
Copy pathmodel_config.py
File metadata and controls
74 lines (57 loc) · 2.34 KB
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
import os
from typing import Tuple
import myconf
from absl import flags
from katago.analysis_engine import KataModels
FLAGS = flags.FLAGS
class ModelConfig:
""" to encapsulate model_id + #readouts
model_config should include #readouts, e.g.
/path/to/model1_epoch16.h5#200
"""
def __init__(self, config_str: str):
self._model_path, self.num_readouts = self._split_model_config(config_str)
def model_id(self) -> str:
""" no dir, no .h5 --> model_id#200 """
if self.is_kata_model():
return f'%s#{self.num_readouts}' % KataModels.model_id(self._model_path)
return self._get_model_id(self._model_path, self.num_readouts)
def model_path(self) -> str:
""" abs path """
return self._model_path
def __str__(self):
return self.model_id()
def is_kata_model(self):
return self._is_kata_model(self._model_path)
@staticmethod
def _get_model_id(model_path: str, num_readouts: int) -> str:
basename = os.path.basename(model_path)
model_id, _ = os.path.splitext(basename)
return f'{model_id}#{num_readouts}'
@staticmethod
def _is_kata_model(model_path: str):
return 'kata' in model_path or 'g170' in model_path
@staticmethod
def _split_model_config(model_config: str) -> Tuple[str, int]:
""" figure out abs path, readouts
model_config should include #readouts, e.g.
/path/to/model1_epoch16.h5#200
"""
model_split = model_config.split('#')
if len(model_split) == 2:
model_path, num_readouts = model_split[0], int(model_split[1])
else:
assert len(model_split) == 1
model_path, num_readouts = model_config, FLAGS.num_readouts
if ModelConfig._is_kata_model(model_path):
if not model_path.startswith('/'):
model_path = KataModels.full_path(model_path)
elif not model_path.startswith('/'):
default_model_dir = f'{myconf.MODELS_DIR}'
model_path = f'{default_model_dir}/{model_path}'
if not model_path.endswith('.mlpackage'):
model_path = f'{model_path}.mlpackage'
# backoff to .h5? probably not
# check file exists
assert os.path.exists(model_path), f'{model_path} doesnt exist'
return model_path, num_readouts