-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
174 lines (147 loc) · 5.17 KB
/
train.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
import datetime
import json
import os
import joblib
from sklearn import preprocessing
from stepcovnet import config, data, executor, inputs, training, model, dataset
def load_training_data(
input_path: str,
) -> tuple[str, type[dataset.ModelDataset], list[preprocessing.StandardScaler], dict]:
metadata = json.load(open(os.path.join(input_path, "metadata.json"), "r"))
dataset_name = metadata["dataset_name"]
dataset_type = data.ModelDatasetTypes[metadata["dataset_type"]].value
dataset_path = os.path.join(input_path, dataset_name + "_dataset")
scalers = joblib.load(
open(os.path.join(input_path, dataset_name + "_scaler.pkl"), "rb")
)
dataset_config = metadata["config"]
return dataset_path, dataset_type, scalers, dataset_config
def run_training(
input_path: str,
output_path: str,
model_name: str,
limit: int,
lookback: int,
difficulty: str,
log_path: str,
):
dataset_path, dataset_type, scalers, dataset_config = load_training_data(input_path)
hyperparameters = training.TrainingHyperparameters(log_path=log_path)
training_config = config.TrainingConfig(
dataset_path=dataset_path,
dataset_type=dataset_type,
dataset_config=dataset_config,
hyperparameters=hyperparameters,
all_scalers=scalers,
limit=limit,
lookback=lookback,
difficulty=difficulty,
tokenizer_name=data.Tokenizers.GPT2.name,
)
training_input = inputs.TrainingInput(training_config)
arrow_model = model.GPT2ArrowModel(training_input.config)
audio_model = model.VggishAudioModel(training_input.config)
classifier_model = model.ClassifierModel(
training_input.config, arrow_model, audio_model
)
stepcovnet_model = model.StepCOVNetModel(
model_root_path=str(output_path),
model_name=model_name,
model=classifier_model.model,
)
executor.TrainingExecutor(stepcovnet_model=stepcovnet_model).execute(
input_data=training_input
)
def train(
input_path: str,
output_path: str,
difficulty_int: int,
lookback: int,
limit: int,
name: str,
log_path: str,
):
if not os.path.isdir(input_path):
raise NotADirectoryError(
"Input path %s not found" % os.path.abspath(input_path)
)
if not os.path.isdir(output_path):
print("Model output path not found. Creating directory...")
os.makedirs(output_path, exist_ok=True)
if lookback <= 1:
raise ValueError("Lookback needs to be > 1")
if limit == 0:
raise ValueError("Limit cannot be = 0")
if name is not None and not name:
raise ValueError("Model name cannot be empty")
if log_path is not None and not os.path.isdir(log_path):
print("Log output path not found. Creating directory...")
os.makedirs(log_path, exist_ok=True)
if log_path is not None:
log_path = os.path.join(
log_path, "tensorboard", datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
)
difficulty = ["challenge", "hard", "medium", "easy", "beginner"][difficulty_int]
built_model_name = "time%s_" % lookback if lookback > 1 else ""
built_model_name += "%s_" % difficulty
# finally, specify training timing model
built_model_name += "timing_model"
model_name = name if name is not None else built_model_name
log_path = None if log_path is None else log_path + "_" + model_name
output_path = os.path.join(output_path, model_name)
os.makedirs(output_path, exist_ok=True)
run_training(
input_path=input_path,
output_path=output_path,
model_name=model_name,
limit=limit,
lookback=lookback,
difficulty=difficulty,
log_path=log_path,
)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Train a note timings model")
parser.add_argument(
"-i", "--input", type=str, help="Input training data path", required=True
)
parser.add_argument(
"-o", "--output", type=str, help="Output stored model path", required=True
)
parser.add_argument(
"-d",
"--difficulty",
type=int,
default=0,
choices=[0, 1, 2, 3, 4],
help="Game difficulty to use when training: 0 - challenge, 1 - hard, 2 - medium, 3 - easy, 4, "
"- beginner",
)
parser.add_argument(
"--lookback",
type=int,
default=2,
help="Number of frames to lookback when training: 1 - non timeseries, > 1 timeseries",
)
parser.add_argument(
"--limit",
type=int,
default=-1,
help="Maximum number of frames to use when training: -1 unlimited, > 0 frame limit",
)
parser.add_argument(
"--name", type=str, help="Name to give finished model", required=True
)
parser.add_argument(
"--log", type=str, default=None, help="Output log data path for tensorboard"
)
args = parser.parse_args()
train(
input_path=args.input,
output_path=args.output,
difficulty_int=args.difficulty,
lookback=args.lookback,
limit=args.limit,
name=args.name,
log_path=args.log,
)