-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
220 lines (190 loc) · 6.95 KB
/
run.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import argparse
import pickle
import shutil
import subprocess
from logging import error
from pathlib import Path
import mlflow
from mpc2c import build, create_template, data_management
from mpc2c import settings as s
from mpc2c import training, evaluate
from mpc2c.asmd_resynth import get_contexts, split_resynth
build.build()
def parse_args():
parser = argparse.ArgumentParser(description="CLI for running experiments")
parser.add_argument(
"-cm",
"--clean-mlflow",
action="store_true",
help=
"If used, mlflow experiment is cleaned before of running experiments")
parser.add_argument(
"-sc",
"--scale",
action="store_true",
help=
"Create the midi file containing the scales for the template; syntehsize it and make the template."
)
parser.add_argument(
"-d",
"--datasets",
action="store_true",
help=
"Prepare the datasets by splitting the various contexts and resynthesizing them"
)
parser.add_argument(
"-p",
"--pedaling",
action="store_true",
help="Perform actions for pedaling estimation (window-wise prediction)."
)
parser.add_argument(
"-v",
"--velocity",
action="store_true",
help="Perform actions for velocity estimation (note-wise prediction).")
parser.add_argument("-t",
"--train",
action="store_true",
help="Train a model.")
parser.add_argument(
"-cs",
"--contextspecific",
action="store_true",
help=
"Train a specializer against context specificity on the same latent space used for performance regression"
)
parser.add_argument(
"-sk",
"--skopt",
action="store_true",
help=
"Perform various little training cycles to look for hyper-parameters using skopt."
)
parser.add_argument("-r",
"--redump",
action="store_true",
help="Pre-process the full dataset and dumps it")
parser.add_argument(
"-pc",
"--printcontexts",
action="store_true",
help="Print contexts in the order with the labels shown in mlflow log and exit")
parser.add_argument("-e",
"--evaluate",
action="store_true",
help="Evaluate configurations and exit")
parser.add_argument(
"-m",
"--metric",
action="store",
help=
"Evaluate configurations against the provided mlflow metric (default: `metrics.perfm_test_avg`).",
default="metrics.perfm_test_avg")
return parser.parse_args()
def load_nmf_params():
nmf_params = pickle.load(open(s.TEMPLATE_PATH, 'rb'))
print("using minpitch: ", nmf_params[1])
print("using maxpitch: ", nmf_params[2])
return nmf_params
def main():
args = parse_args()
if args.scale:
create_template.main()
if args.datasets:
split_resynth(s.DATASETS,
Path(s.CARLA_PROJ), Path(s.RESYNTH_DATA_PATH),
Path(s.METADATASET_PATH), s.CONTEXT_SPLITS,
s.RESYNTH_FINAL_DECAY)
contexts = list(get_contexts(s.CARLA_PROJ).keys())
if args.evaluate:
evaluate.main(args.metric)
return
if args.printcontexts:
for i, c in enumerate(contexts):
print(f"{i}: {c}")
return
if args.pedaling:
mode = 'pedaling'
hpar = s.PED_HYPERPARAMS
elif args.velocity:
mode = 'velocity'
hpar = s.VEL_HYPERPARAMS
else:
error("Please specify -p or -v")
return
nmf_params = load_nmf_params()
if args.redump:
contexts = list(get_contexts(s.CARLA_PROJ).keys())
data_management.get_loader(groups=None,
redump=True,
contexts=contexts,
one_context_per_batch=False,
mode=mode,
nmf_params=nmf_params)
if args.skopt:
def objective(x):
l1, model = training.train(x, mode, False, False, test=True)
# note: deepcopy causes some weakref errors...
# saving a copy to disk instead
try:
pickle.dump(model, open("_model.pkl", "wb"))
except Exception as e:
raise RuntimeError("Error pickling model:" + str(e))
model = pickle.load(open("_model.pkl", "rb"))
l3, _ = training.train(x,
mode,
True,
True,
test=True,
start_from_model=model)
model = pickle.load(open("_model.pkl", "rb"))
l2, _ = training.train(x,
mode,
True,
False,
test=True,
start_from_model=model)
model = pickle.load(open("_model.pkl", "rb"))
l4, _ = training.train(x,
mode,
False,
True,
test=True,
start_from_model=model)
return (l1 + l2 + l3 + l4) / 4
if args.pedaling:
# test_sample = torch.rand(1, s.BINS, 100)
checkpoint_path = "ped_grid.pt"
elif args.velocity:
# test_sample = torch.rand(1, s.BINS, s.MINI_SPEC_SIZE)
checkpoint_path = "vel_grid.pt"
else:
return # not reachable, here to shutup the pyright
# space_constraint = training.model_test(
# lambda x: training.build_model(x, contexts), test_sample)
exp = mlflow.get_experiment_by_name(mode)
if exp and args.clean_mlflow:
if exp.lifecycle_stage == 'deleted':
exp_path = Path(
mlflow.get_registry_uri()) / '.trash' / exp.experiment_id
else:
exp_path = exp.artifact_location
shutil.rmtree(exp_path)
training.grid_search(s.GRIDSPACE, objective, checkpoint_path)
exp = mlflow.get_experiment_by_name(mode)
subprocess.run([
'mlflow', 'experiments', 'csv', '-x', exp.experiment_id, '-o',
f'{mode}_results.csv'
])
if args.train:
print("----------------")
training.train(hpar,
mode,
args.contextspecific,
True,
copy_checkpoint=Path("models") /
f"{mode}_{args.contextspecific}.pt",
test=True)
if __name__ == "__main__":
main()