-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
292 lines (250 loc) · 10.4 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import os
from tqdm import *
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import pandas as pd
import tensorflow as tf
import struct
import base64
import json
import pickle
import argparse
from music21 import converter, note, chord, stream
# Melody-RNN Format is a sequence of 8-bit integers indicating the following:
# MELODY_NOTE_ON = [0, 127] # (note on at that MIDI pitch)
MELODY_NOTE_OFF = 128 # (stop playing all previous notes)
MELODY_NO_EVENT = 129 # (no change from previous event)
# Each element in the sequence lasts for one sixteenth note.
# This can encode monophonic music only.
MELODY_SIZE = 130
def parse_args():
parser = argparse.ArgumentParser("Entry script to launch training")
parser.add_argument("--data-dir", type=str, default = "./data", help="Path to the data directory")
parser.add_argument("--output-path", type=str, default = './model.json', help="Path to the output file")
parser.add_argument("--config-path", type=str, required = True, help="Path to the output file")
parser.add_argument("--checkpoint-path", type = str, default = None, help="Path to the checkpoint file")
return parser.parse_args()
def streamToNoteArray(stream):
"""
Convert a Music21 sequence to a numpy array of int8s into Melody-RNN format:
0-127 - note on at specified pitch
128 - note off
129 - no event
"""
# Part one, extract from stream
total_length = int(np.round(stream.flatten().highestTime / 0.25)) # in semiquavers
stream_list = []
for element in stream.flatten():
if isinstance(element, note.Note):
stream_list.append([np.round(element.offset / 0.25), np.round(element.quarterLength / 0.25), element.pitch.midi])
elif isinstance(element, chord.Chord):
stream_list.append([np.round(element.offset / 0.25), np.round(element.quarterLength / 0.25), element.sortAscending().pitches[-1].midi])
np_stream_list = np.array(stream_list, dtype=np.int32)
if len(np_stream_list.T) > 0:
df = pd.DataFrame({'pos': np_stream_list.T[0], 'dur': np_stream_list.T[1], 'pitch': np_stream_list.T[2]})
df = df.sort_values(['pos','pitch'], ascending=[True, False]) # sort the dataframe properly
df = df.drop_duplicates(subset=['pos']) # drop duplicate values
# part 2, convert into a sequence of note events
output = np.zeros(total_length+1, dtype=np.int32) + np.int32(MELODY_NO_EVENT) # set array full of no events by default.
# Fill in the output list
for i in range(total_length):
if not df[df.pos==i].empty:
n = df[df.pos==i].iloc[0] # pick the highest pitch at each semiquaver
output[i] = n.pitch # set note on
output[i+n.dur] = MELODY_NOTE_OFF
return output
else:
return []
def noteArrayToDataFrame(note_array):
"""
Convert a numpy array containing a Melody-RNN sequence into a dataframe.
"""
df = pd.DataFrame({"code": note_array})
df['offset'] = df.index
df['duration'] = df.index
df = df[df.code != MELODY_NO_EVENT]
df.duration = df.duration.diff(-1) * -1 * 0.25 # calculate durations and change to quarter note fractions
df = df.fillna(0.25)
return df[['code','duration']]
def noteArrayToStream(note_array):
"""
Convert a numpy array containing a Melody-RNN sequence into a music21 stream.
"""
df = noteArrayToDataFrame(note_array)
melody_stream = stream.Stream()
for index, row in df.iterrows():
if row.code == MELODY_NO_EVENT:
new_note = note.Rest() # bit of an oversimplification, doesn't produce long notes.
elif row.code == MELODY_NOTE_OFF:
new_note = note.Rest()
else:
new_note = note.Note(row.code)
new_note.quarterLength = row.duration
melody_stream.append(new_note)
return melody_stream
# making data to train
def make_training_data(data_dir, config):
notes = []
sequence_length = config["seq_length"]
file_paths = []
def list_files_recursive(directory):
for entry in os.listdir(directory):
full_path = os.path.join(directory, entry)
if os.path.isdir(full_path):
list_files_recursive(full_path)
elif os.path.isfile(full_path):
file_paths.append(full_path)
list_files_recursive(data_dir)
for file_path in tqdm(file_paths):
if file_path.endswith('.mid'):
s = converter.parse(file_path)
arr = streamToNoteArray(s.parts[0])
for item in arr:
notes.append(item)
elif file_path.endswith('.pickle'):
with open(file_path, 'rb') as f:
arr = pickle.load(f)
for item in arr:
notes.append(item)
pitchnames = sorted(set(item for item in notes))
# create a dictionary to map pitches to integers
note_to_index = dict((note, number) for number, note in enumerate(pitchnames))
inputs = []
targets = []
# create input sequences and the corresponding outputs
for i in range(0, len(notes) - sequence_length):
sequence_in = notes[i:i + sequence_length]
sequence_out = notes[i + sequence_length]
inputs.append(sequence_in)
# inputs.append([note_to_index[char] for char in sequence_in])
targets.append([note_to_index[sequence_out]])
# reshape the input into a format compatible with LSTM layers
inputs = np.reshape(inputs, (len(inputs), sequence_length, 1))/MELODY_SIZE
targets = np.array(targets)
return inputs, targets, note_to_index
def create_model(config, model_path = None):
rnn_units = config["rnn_units"]
n_vocab = config["n_vocab"]
sequence_length = config["seq_length"]
if model_path is not None:
model = tf.keras.models.load_model(model_path)
return model
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(sequence_length, 1)),
tf.keras.layers.LSTM(units = rnn_units),
tf.keras.layers.Dense(n_vocab)
])
model.compile(loss= tf.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer='adam', metrics=['accuracy'])
return model
def get_weights(model):
weights = []
for layer in model.layers:
w = layer.get_weights()
weights.append(w)
return weights
def compressConfig(data):
layers = []
for layer in data["config"]["layers"]:
cfg = layer["config"]
if layer["class_name"] == "InputLayer":
layer_config = {
"batch_input_shape": cfg["batch_input_shape"]
}
elif layer["class_name"] == "Rescaling":
layer_config = {
"scale": cfg["scale"],
"offset": cfg["offset"]
}
elif layer["class_name"] == "Dense":
layer_config = {
"units": cfg["units"],
"activation": cfg["activation"]
}
elif layer["class_name"] == "Conv2D":
layer_config = {
"filters": cfg["filters"],
"kernel_size": cfg["kernel_size"],
"strides": cfg["strides"],
"activation": cfg["activation"],
"padding": cfg["padding"]
}
elif layer["class_name"] == "MaxPooling2D":
layer_config = {
"pool_size": cfg["pool_size"],
"strides": cfg["strides"],
"padding": cfg["padding"]
}
elif layer["class_name"] == "Embedding":
layer_config = {
"input_dim": cfg["input_dim"],
"output_dim": cfg["output_dim"]
}
elif layer["class_name"] == "SimpleRNN":
layer_config = {
"units": cfg["units"],
"activation": cfg["activation"]
}
elif layer["class_name"] == "LSTM":
layer_config = {
"units": cfg["units"],
"activation": cfg["activation"],
"recurrent_activation": cfg["recurrent_activation"],
}
else:
layer_config = None
res_layer = {
"class_name": layer["class_name"],
}
if layer_config is not None:
res_layer["config"] = layer_config
layers.append(res_layer)
return {
"config": {
"layers": layers
}
}
def get_model_for_export(output_path, model, vocabulary):
weight_np = get_weights(model)
weight_bytes = bytearray()
for idx, layer in enumerate(weight_np):
# print(layer.shape)
# write_to_file(f"model_weight_{idx:02}.txt", str(layer))
for weight_group in layer:
flatten = weight_group.reshape(-1).tolist()
# print("flattened length: ", len(flatten))
for i in flatten:
weight_bytes.extend(struct.pack("@f", float(i)))
weight_base64 = base64.b64encode(weight_bytes).decode()
config = json.loads(model.to_json())
# print("full config: ", config)
compressed_config = compressConfig(config)
# write to file
with open(output_path, "w") as f:
json.dump({
"model_name": "musicnetgen",
"layers_config": compressed_config,
"weight_b64": weight_base64,
"vocabulary": vocabulary
}, f)
return weight_base64, compressed_config
def main():
args = parse_args()
data_dir = args.data_dir
output_path = args.output_path
ckpt = args.checkpoint_path
config_path = args.config_path
with open(config_path, 'r') as f:
config = json.load(f)
X, y, note_to_index = make_training_data(data_dir, config)
X, y, note_to_index = make_training_data(data_dir, config)
vocabulary = []
for key, value in note_to_index.items():
vocabulary.append(int(key))
config["n_vocab"] = len(vocabulary)
model = create_model(config, ckpt)
model.summary()
model.fit(X, y, epochs=config["epoch_num"], batch_size = config["batch_size"])
get_model_for_export(output_path, model, vocabulary)
if __name__ == "__main__":
main()