-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_utils.py
94 lines (79 loc) · 3 KB
/
train_utils.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
84
85
86
87
88
89
90
91
92
import os
import numpy as np
import torch
import torch.nn.functional as F
import torch.nn.modules.loss
def format_metrics(metrics, split):
"""Format metric in metric dict for logging."""
return " ".join(
["{}_{}: {:.4f}".format(split, metric_name, metric_val) for metric_name, metric_val in metrics.items()])
def get_dir_name(models_dir):
"""Gets a directory to save the model.
If the directory already exists, then append a new integer to the end of
it. This method is useful so that we don't overwrite existing models
when launching new jobs.
Args:
models_dir: The directory where all the models are.
Returns:
The name of a new directory to save the training logs and model weights.
"""
if not os.path.exists(models_dir):
save_dir = os.path.join(models_dir, '0')
os.makedirs(save_dir)
else:
existing_dirs = np.array(
[
d
for d in os.listdir(models_dir)
if os.path.isdir(os.path.join(models_dir, d))
]
).astype(np.int)
if len(existing_dirs) > 0:
dir_id = str(existing_dirs.max() + 1)
else:
dir_id = "1"
save_dir = os.path.join(models_dir, dir_id)
os.makedirs(save_dir)
return save_dir
def add_flags_from_config(parser, config_dict):
"""
Adds a flag (and default value) to an ArgumentParser for each parameter in a config
"""
def OrNone(default):
def func(x):
# Convert "none" to proper None object
if x.lower() == "none":
return None
# If default is None (and x is not None), return x without conversion as str
elif default is None:
return str(x)
# Otherwise, default has non-None type; convert x to that type
else:
return type(default)(x)
return func
for param in config_dict:
default, description = config_dict[param]
try:
if isinstance(default, dict):
parser = add_flags_from_config(parser, default)
elif isinstance(default, list):
if len(default) > 0:
# pass a list as argument
parser.add_argument(
f"--{param}",
action="append",
type=type(default[0]),
default=default,
help=description
)
else:
pass
parser.add_argument(f"--{param}", action="append", default=default, help=description)
else:
pass
parser.add_argument(f"--{param}", type=OrNone(default), default=default, help=description)
except argparse.ArgumentError:
print(
f"Could not add flag for param {param} because it was already present."
)
return parser