-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
413 lines (338 loc) · 17.6 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import glob
import os
from tqdm import tqdm
from music21 import converter, instrument, note, chord, stream, duration, pitch
from data import NoteData, MidiDataset, NetworkData
from generation import create_midi_track
from model import *
from torch.cuda.amp import autocast
def get_notes(directory, get_flat=False):
data = NoteData()
for file in glob.glob(f'{directory}*.mid'):
print("Parsing: ", file)
try:
midi = converter.parse(file)
except Exception as e:
print(f"Warning: could not parse {file}. Skipping. Error: {e}")
continue
if not get_flat:
try: # file has instrument parts
instruments = instrument.partitionByInstrument(midi)
notes_to_parse = instruments.parts[0].recurse()
except: # file has notes in a flat structure
notes_to_parse = midi.flat.notes
else:
notes_to_parse = midi.flat.notes
prev_event_end = -(data.get_random_off())
notes = []
for event in notes_to_parse:
if isinstance(event, note.Note) or isinstance(event, chord.Chord):
offset = event.offset - prev_event_end # Calc offset (distance from last note)
if offset < 0:
offset = 0
try:
inst = event.activeSite.getInstrument()
if inst and inst.midiProgram is not None and inst.midiProgram >= 110: # Filter out percussion
continue
except:
continue
note_val = None
durr_val = None
vel_val = None
if isinstance(event, note.Note):
note_val = str(event.pitch.nameWithOctave)
durr_val = float(event.duration.quarterLength)
vel_val = event.volume.velocity if event.volume else None
elif isinstance(event, chord.Chord):
note_val = '.'.join(str(p.nameWithOctave) for p in event.pitches)
durr_val = float(event.duration.quarterLength)
vel_val = event.volume.velocity if event.volume else None
if note_val is not None and durr_val is not None and vel_val is not None:
note_val = data.add_note_if_absent(note_val)
durr_val = data.add_durr_if_absent(durr_val)
offset = data.add_offs_if_absent(offset)
vel_val = data.add_vel_if_absent(vel_val)
notes.append((note_val, offset, durr_val, vel_val))
prev_event_end = event.offset
data.training_notes.append(notes)
data.calc_vocab()
print(f'train:{data.training_notes}')
print(f'Notes{data.note_table}')
print(f'Offsets{data.offset_table}')
print(f'Durations{data.duration_table}')
print(f'Velocities{data.velocity_table}')
return data
def get_notes_single(directory, get_flat=True):
data = NoteData()
for file in glob.glob(f'{directory}*.mid'):
print("Parsing: ", file)
try:
midi = converter.parse(file)
except Exception as e:
print(f"Warning: could not parse {file}. Skipping. Error: {e}")
continue
if not get_flat:
try: # file has instrument parts
instruments = instrument.partitionByInstrument(midi)
notes_to_parse = instruments.parts[0].recurse()
except: # file has notes in a flat structure
notes_to_parse = midi.flat.notes
else:
notes_to_parse = midi.flat.notes
prev_event_offset = -(data.get_random_off())
notes = []
for event in notes_to_parse:
if isinstance(event, note.Note) or isinstance(event, chord.Chord):
offset = event.offset - prev_event_offset # Calc offset (distance from last note)
if offset < 0:
offset = 0
try:
inst = event.activeSite.getInstrument()
if inst and inst.midiProgram is not None and inst.midiProgram >= 110: # Filter out percussion
continue
except:
continue
if isinstance(event, note.Note):
note_val = data.add_note_if_absent(event.pitch.midi)
durr_val = data.add_durr_if_absent(float(event.duration.quarterLength))
vel_val = data.add_vel_if_absent(event.volume.velocity) if event.volume else None
off_val = data.add_offs_if_absent(offset)
tup = (note_val, off_val, durr_val, vel_val)
if not any(item is None for item in tup):
notes.append(tup)
elif isinstance(event, chord.Chord):
p = event.pitches
for i in range(0, len(event.pitches)):
note_val = data.add_note_if_absent(p[i].midi)
durr_val = data.add_durr_if_absent(event.duration.quarterLength)
vel_val = data.add_vel_if_absent(event.volume.velocity if event.volume else None)
off_val = data.add_offs_if_absent(offset if i == 0 else 0)
tup = (note_val, off_val, durr_val, vel_val)
if not any(item is None for item in tup):
notes.append(tup)
prev_event_offset = event.offset
data.training_notes.append(notes)
data.calc_vocab()
return data
def prepare_sequences(note_data, device=torch.device("cpu"), sequence_length=128):
sequence_length = sequence_length
network_input = []
network_output_notes = []
network_output_offsets = []
network_output_durations = []
network_output_velocities = []
# create input sequences and the corresponding outputs
for notes in note_data.training_notes:
for i in range(0, len(notes) - sequence_length, 1):
sequence_in = notes[i:i + sequence_length]
sequence_out = notes[i + sequence_length]
network_input.append([[x[0], x[1], x[2], x[3]] for x in sequence_in])
network_output_notes.append(sequence_out[0])
network_output_offsets.append(sequence_out[1])
network_output_durations.append(sequence_out[2])
network_output_velocities.append(sequence_out[3])
network_input = torch.tensor(network_input, dtype=torch.float16).to(device)
# Shape for cross_entropy: (N) where N is batch size.
network_output_notes = torch.tensor(network_output_notes, torch.long).view(-1).to(device)
network_output_offsets = torch.tensor(network_output_offsets, torch.long).view(-1).to(device)
network_output_durations = torch.tensor(network_output_durations, torch.long).view(-1).to(device)
network_output_velocities = torch.tensor(network_output_velocities, torch.long).view(-1).to(device)
return NetworkData(network_input, network_output_notes, network_output_offsets, network_output_durations,
network_output_velocities)
def generate_seed_from_int(seed_int, seq_length, note_data):
# Create a random number generator with the provided seed
rng = np.random.default_rng(seed_int)
# Generate random indices within the range of each vocabulary
note_indices = rng.integers(note_data.n_vocab, size=seq_length)
offset_indices = rng.integers(note_data.o_vocab, size=seq_length)
duration_indices = rng.integers(note_data.d_vocab, size=seq_length)
velocity_indices = rng.integers(note_data.v_vocab, size=seq_length)
# Stack the indices into a single sequence and reshape it to the required shape
seed_sequence = np.vstack([note_indices, offset_indices, duration_indices, velocity_indices])
seed_sequence = seed_sequence.T.reshape(1, seq_length, 4)
# Convert to a PyTorch tensor
seed_sequence = torch.tensor(seed_sequence, dtype=torch.float16)
return seed_sequence
#
# def train(model, train_loader, criterion, optimizer, device, note_data, scheduler=None, clip_value=None):
# model.train()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(train_loader):
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity = model(inputs)
#
# # Calculate loss
# loss_note = criterion(output_note.view(-1, note_data.n_vocab), targets_note.view(-1).long())
# loss_offset = criterion(output_offset.view(-1, note_data.o_vocab), targets_offset.view(-1).long())
# loss_duration = criterion(output_duration.view(-1, note_data.d_vocab), targets_duration.view(-1).long())
# loss_velocity = criterion(output_velocity.view(-1, note_data.v_vocab), targets_velocity.view(-1).long())
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity
#
# # Backward pass and optimization
# optimizer.zero_grad()
# loss.backward()
#
# # Gradient clip
# if clip_value is not None: # Gradient clipping
# torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
#
# optimizer.step()
#
# if scheduler is not None:
# scheduler.step()
#
# running_loss += loss.item() * inputs.size(0)
#
# # Calculate accuracy
# _, predicted_notes = torch.max(output_note.data, 1)
# _, predicted_offsets = torch.max(output_offset.data, 1)
# _, predicted_durations = torch.max(output_duration.data, 1)
# _, predicted_velocities = torch.max(output_velocity.data, 1)
#
# total_predictions += targets_note.size(0)
# correct_predictions += (predicted_notes == targets_note).sum().item()
# correct_predictions += (predicted_offsets == targets_offset).sum().item()
# correct_predictions += (predicted_durations == targets_duration).sum().item()
# correct_predictions += (predicted_velocities == targets_velocity).sum().item()
#
# accuracy = correct_predictions / (total_predictions * 4)
#
# return (running_loss / len(train_loader.dataset)) / 4, accuracy
#
#
# def validate(model, valid_loader, criterion, device, note_data):
# model.eval()
# running_loss = 0.0
# correct_predictions = 0
# total_predictions = 0
#
# with torch.no_grad():
# for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(valid_loader):
# inputs = inputs.to(device)
# targets_note = targets_note.to(device)
# targets_offset = targets_offset.to(device)
# targets_duration = targets_duration.to(device)
# targets_velocity = targets_velocity.to(device)
#
# # Forward pass
# output_note, output_offset, output_duration, output_velocity = model(inputs)
#
# # Calculate loss
# loss_note = criterion(output_note.view(-1, note_data.n_vocab), targets_note.view(-1).long())
# loss_offset = criterion(output_offset.view(-1, note_data.o_vocab), targets_offset.view(-1).long())
# loss_duration = criterion(output_duration.view(-1, note_data.d_vocab), targets_duration.view(-1).long())
# loss_velocity = criterion(output_velocity.view(-1, note_data.v_vocab), targets_velocity.view(-1).long())
#
# loss = loss_note + loss_offset + loss_duration + loss_velocity
#
# running_loss += loss.item() * inputs.size(0)
#
# # Calculate accuracy
# _, predicted_notes = torch.max(output_note.data, 1)
# _, predicted_offsets = torch.max(output_offset.data, 1)
# _, predicted_durations = torch.max(output_duration.data, 1)
# _, predicted_velocities = torch.max(output_velocity.data, 1)
#
# total_predictions += targets_note.size(0)
# correct_predictions += (predicted_notes == targets_note).sum().item()
# correct_predictions += (predicted_offsets == targets_offset).sum().item()
# correct_predictions += (predicted_durations == targets_duration).sum().item()
# correct_predictions += (predicted_velocities == targets_velocity).sum().item()
#
# accuracy = correct_predictions / (total_predictions * 4)
#
# return (running_loss / len(valid_loader.dataset)) / 4, accuracy
def train(model, train_loader, criterion, optimizer, device, note_data, scaler, batch_size, scheduler=None, clip_value=None):
model.train()
running_loss = 0.0
correct_predictions = 0
total_predictions = 0
hidden = model.init_hidden(device, batch_size=batch_size)
for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(train_loader):
#hidden = model.detach_hidden(hidden)
model.init_hidden(device, batch_size=batch_size)
inputs = inputs.to(device)
targets_note = targets_note.to(device)
targets_offset = targets_offset.to(device)
targets_duration = targets_duration.to(device)
targets_velocity = targets_velocity.to(device)
# print(inputs.shape)
# print(targets_note.shape)
# Forward pass
with autocast():
output_note, output_offset, output_duration, output_velocity, _ = model(inputs, hidden)
# Calculate loss
loss_note = criterion(output_note.view(-1, note_data.n_vocab), targets_note.view(-1).long())
loss_offset = criterion(output_offset.view(-1, note_data.o_vocab), targets_offset.view(-1).long())
loss_duration = criterion(output_duration.view(-1, note_data.d_vocab), targets_duration.view(-1).long())
loss_velocity = criterion(output_velocity.view(-1, note_data.v_vocab), targets_velocity.view(-1).long())
loss = loss_note + loss_offset + loss_duration + loss_velocity
# Backward pass and optimization
optimizer.zero_grad()
scaler.scale(loss).backward()
# Gradient clip
if clip_value is not None: # Gradient clipping
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
scaler.step(optimizer)
scaler.update()
if scheduler is not None:
scheduler.step()
running_loss += loss.item() * inputs.size(0)
# Calculate accuracy
_, predicted_notes = torch.max(output_note.data, 1)
_, predicted_offsets = torch.max(output_offset.data, 1)
_, predicted_durations = torch.max(output_duration.data, 1)
_, predicted_velocities = torch.max(output_velocity.data, 1)
total_predictions += targets_note.size(0)
correct_predictions += (predicted_notes == targets_note).sum().item()
correct_predictions += (predicted_offsets == targets_offset).sum().item()
correct_predictions += (predicted_durations == targets_duration).sum().item()
correct_predictions += (predicted_velocities == targets_velocity).sum().item()
accuracy = correct_predictions / (total_predictions * 4 )
return (running_loss / len(train_loader.dataset)) / 4, accuracy
def validate(model, valid_loader, criterion, device, note_data, batch_size):
model.eval()
running_loss = 0.0
correct_predictions = 0
total_predictions = 0
with torch.no_grad():
hidden = model.init_hidden(device, batch_size=batch_size)
for inputs, (targets_note, targets_offset, targets_duration, targets_velocity) in tqdm(valid_loader):
inputs = inputs.to(device)
targets_note = targets_note.to(device)
targets_offset = targets_offset.to(device)
targets_duration = targets_duration.to(device)
targets_velocity = targets_velocity.to(device)
# Forward pass
with autocast():
output_note, output_offset, output_duration, output_velocity, hidden = model(inputs, hidden)
# Calculate loss
loss_note = criterion(output_note.view(-1, note_data.n_vocab), targets_note.view(-1).long())
loss_offset = criterion(output_offset.view(-1, note_data.o_vocab), targets_offset.view(-1).long())
loss_duration = criterion(output_duration.view(-1, note_data.d_vocab), targets_duration.view(-1).long())
loss_velocity = criterion(output_velocity.view(-1, note_data.v_vocab), targets_velocity.view(-1).long())
loss = loss_note + loss_offset + loss_duration + loss_velocity
running_loss += loss.item() * inputs.size(0)
# Calculate accuracy
_, predicted_notes = torch.max(output_note.data, 1)
_, predicted_offsets = torch.max(output_offset.data, 1)
_, predicted_durations = torch.max(output_duration.data, 1)
_, predicted_velocities = torch.max(output_velocity.data, 1)
total_predictions += targets_note.size(0)
correct_predictions += (predicted_notes == targets_note).sum().item()
correct_predictions += (predicted_offsets == targets_offset).sum().item()
correct_predictions += (predicted_durations == targets_duration).sum().item()
correct_predictions += (predicted_velocities == targets_velocity).sum().item()
accuracy = correct_predictions / (total_predictions * 4)
return (running_loss / len(valid_loader.dataset)) / 4, accuracy