-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdmelodies_torch_dataloader.py
326 lines (298 loc) · 11.7 KB
/
dmelodies_torch_dataloader.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
"""
Module containing the dMelodies torch dataloader
"""
import numpy as np
import torch
from torch.utils.data import TensorDataset, DataLoader
from typing import Tuple
from helpers import *
from dmelodies_dataset import DMelodiesDataset
from constants_latent_factors import *
class DMelodiesTorchDataset:
"""
Class defining a torch dataloader for the dMelodies dataset
"""
def __init__(self, seed: int = 0):
"""
Initializes the DMelodiesTorchDataset class
Args:
seed: int, specifies the random seed to be used for shuffling the data
"""
self.kwargs = {'pin_memory': True} if torch.cuda.is_available() else {}
self.dataset = None
self.seed = seed
self.note2index_dict = None
self.index2note_dict = None
self.latent_dicts = None
self.tick_durations = None
self.beat_subdivisions = None
np.random.seed(seed)
def load_dataset(self):
"""
Loads and shuffles the data
"""
dataset = DMelodiesDataset()
dataset.make_or_load_dataset()
score = dataset.score_array
score = np.expand_dims(score, axis=1)
latent_values = dataset.latent_array
a = np.c_[
score.reshape(len(score), -1),
latent_values.reshape(len(latent_values), -1)
]
score2 = a[:, :score.size // len(score)].reshape(score.shape)
latent_values2 = a[:, score.size // len(score):].reshape(latent_values.shape)
np.random.shuffle(a)
self.dataset = TensorDataset(
torch.from_numpy(score2),
torch.from_numpy(latent_values2)
)
self.note2index_dict = dataset.note2index_dict
self.index2note_dict = dataset.index2note_dict
self.latent_dicts = dataset.latent_dicts
self.beat_subdivisions = dataset.beat_subdivisions
self.tick_durations = dataset.tick_durations
def data_loaders(
self, batch_size: int, split: tuple = (0.70, 0.20)
) -> Tuple[DataLoader, DataLoader, DataLoader]:
"""
Returns three data loaders obtained by splitting the data
Args:
batch_size: int, number of data points in each batch
split: tuple, specify the ratio in which the dataset is to be divided
Returns:
tuple of 3 DataLoader objects corresponding to the train, validation and test sets
"""
assert sum(split) < 1
if self.dataset is None:
self.load_dataset()
num_examples = len(self.dataset)
a, b = split
train_dataset = TensorDataset(
*self.dataset[: int(a * num_examples)]
)
val_dataset = TensorDataset(
*self.dataset[int(a * num_examples):int((a + b) * num_examples)]
)
eval_dataset = TensorDataset(
*self.dataset[int((a + b) * num_examples):]
)
train_dl = DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=True,
**self.kwargs
)
val_dl = DataLoader(
val_dataset,
batch_size=batch_size,
shuffle=True,
)
eval_dl = DataLoader(
eval_dataset,
batch_size=batch_size,
shuffle=False,
)
return train_dl, val_dl, eval_dl
def tensor_to_m21score(self, tensor_score):
"""
Converts lead given as tensor_lead to a true music21 score
:param tensor_score:
:return:
"""
slur_index = self.note2index_dict[SLUR_SYMBOL]
score = music21.stream.Score()
part = music21.stream.Part()
# LEAD
dur = 0
f = music21.note.Rest()
tensor_lead_np = tensor_score.cpu().numpy().flatten()
tensor_lead_np[tensor_lead_np >= 52] = slur_index
a = 1
for tick_index, note_index in enumerate(tensor_lead_np):
# if it is a played note
if not note_index == slur_index:
# add previous note
if dur > 0:
f.duration = music21.duration.Duration(dur)
part.append(f)
dur = self.tick_durations[tick_index % self.beat_subdivisions]
f = standard_note(self.index2note_dict[note_index])
else:
dur += self.tick_durations[tick_index % self.beat_subdivisions]
# add last note
f.duration = music21.duration.Duration(dur)
part.append(f)
score.insert(part)
return score
@staticmethod
def fix_note_str(note_str):
note_map = {
'B': 'A',
'A': 'G',
'E': 'D',
'D': 'C'
}
new_note_str = ''
if note_str[1] == '-':
new_note_str += note_map[note_str[0]]
new_note_str += '#'
new_note_str += note_str[2]
else:
new_note_str = note_str
return new_note_str
def get_root_note(self, tensor_score):
"""
Returns the root note for the given input score
:param tensor_score: pytorch tensor, (16,)
:return: music21.note.Note object
"""
midi_pitch_array, note_list = self.compute_midi_sequence(tensor_score)
if len(note_list) < 3:
return None
min_idx = np.argmin(midi_pitch_array[:3])
root_note = note_list[min_idx]
return root_note
def compute_midi_sequence(self, tensor_score):
"""
Returns a numpy array of midi pitch numbers given an input score
:param tensor_score: tensor_score: pytorch tensor, (16,)
:return: tuple[np.array, (L,), list[music21.note.Note]]
"""
# create MIDI pitch sequence
slur_index = self.note2index_dict[SLUR_SYMBOL]
rest_index = self.note2index_dict['rest']
numpy_score = tensor_score.numpy()
numpy_score[numpy_score >= 52] = slur_index
numpy_score = numpy_score[numpy_score != rest_index]
numpy_score = numpy_score[numpy_score != slur_index]
midi_pitch_array = np.zeros_like(numpy_score)
note_list = []
for i in range(numpy_score.size):
note_str = self.fix_note_str(self.index2note_dict[numpy_score[i]])
n = music21.note.Note(note_str)
note_list.append(n)
midi_pitch_array[i] = n.pitch.midi
return midi_pitch_array, note_list
def compute_tonic_octave(self, tensor_score):
"""
Computes the indices fo the tonic, octave for a given input score
:param tensor_score: pytorch tensor, (16,)
:return: tuple[int, int]
"""
root_note = self.get_root_note(tensor_score)
if root_note is None:
return -1, -1
octave = OCTAVE_REVERSE_DICT[root_note.octave] if root_note.octave in OCTAVE_REVERSE_DICT.keys() else -1
tonic = TONIC_REVERSE_DICT[root_note.name] if root_note.name in TONIC_REVERSE_DICT.keys() else -1
return tonic, octave
def compute_mode(self, tensor_score):
"""
Computes the most likely mode for a given input score
:param tensor_score: pytorch tensor, (16,)
:return: int
"""
# get midi for root note
root_note = self.get_root_note(tensor_score)
if root_note is None:
return -1
root_midi = root_note.pitch.midi
# get midi pitch sequence
midi_pitch_array, _ = self.compute_midi_sequence(tensor_score)
# create diff array
diff_array = (midi_pitch_array - root_midi) % 12
# compare diff array
mode_idx = -1
for mode in SCALE_NOTES_DICT.keys():
scale_note_set = set(SCALE_NOTES_DICT[mode])
if set(diff_array).issubset(scale_note_set):
mode_idx = SCALE_REVERSE_DICT[mode]
break
return mode_idx
def compute_rhythm(self, tensor_score, bar_num):
"""
Computes the index for the rhythm for a given input score and bar number
:param tensor_score: pytorch tensor, (16,)
:param bar_num: int, 1 or 2
:return: int
"""
slur_index = self.note2index_dict[SLUR_SYMBOL]
rest_index = self.note2index_dict['rest']
if bar_num == 1:
bar1_tensor = tensor_score[:8].clone().numpy()
elif bar_num == 2:
bar1_tensor = tensor_score[8:].clone().numpy()
else:
raise ValueError("Invalid bar number")
bar1_tensor[bar1_tensor >= 52] = rest_index
bar1_tensor[bar1_tensor == slur_index] = rest_index
bar1_tensor[bar1_tensor == rest_index] = -1
bar1_tensor[bar1_tensor != -1] = 1
bar1_tensor[bar1_tensor == -1] = 0
for rhy_idx in RHYTHM_DICT.keys():
if list(bar1_tensor) == RHYTHM_DICT[rhy_idx]:
return rhy_idx
return -1
def compute_arp_dir(self, tensor_score):
"""
Computes the arpeggiation direction for a given input score
:param tensor_score: pytorch tensor, (16,)
:return: tuple[int, int, int, int]
"""
midi_pitch_array, _ = self.compute_midi_sequence(tensor_score)
arp_dir = [-1, -1, -1, -1]
if midi_pitch_array.size == 12:
midi_pitch_array = np.reshape(midi_pitch_array, (4, 3))
diff_array = np.sign(np.diff(midi_pitch_array, axis=1))
s_array = np.sum(diff_array, axis=1)
for i in range(s_array.size):
if s_array[i] > 0:
arp_dir[i] = ARP_REVERSE_DICT['up']
elif s_array[i] < 0:
arp_dir[i] = ARP_REVERSE_DICT['down']
return tuple(arp_dir)
def compute_attributes(self, tensor_score):
"""
Computes all attributes for a given input score
:param tensor_score: tensor_score: pytorch tensor, (16,)
:return: tuple[int, int, int, int, int, int, int, int, int]
"""
# get the midi pitch array
midi_pitch_array, note_list = self.compute_midi_sequence(tensor_score)
if len(note_list) < 3:
return -1, -1, -1, -1, -1, -1, -1, -1, -1
# estimate root note
min_idx = np.argmin(midi_pitch_array[:3])
root_note = note_list[min_idx]
# get tonic and octave
octave_idx = OCTAVE_REVERSE_DICT[root_note.octave] if root_note.octave in OCTAVE_REVERSE_DICT.keys() else -1
tonic_idx = TONIC_REVERSE_DICT[root_note.name] if root_note.name in TONIC_REVERSE_DICT.keys() else -1
# estimate mode
# create diff array
diff_array = (midi_pitch_array - root_note.pitch.midi) % 12
# compare diff array
mode_idx = -1
for mode in SCALE_NOTES_DICT.keys():
scale_note_set = set(SCALE_NOTES_DICT[mode])
if set(diff_array).issubset(scale_note_set):
mode_idx = SCALE_REVERSE_DICT[mode]
break
# estimate rhythm factors
rhy1_idx = self.compute_rhythm(tensor_score, bar_num=1)
rhy2_idx = self.compute_rhythm(tensor_score, bar_num=2)
# estimate arpreggiation factors
arp_dir = [-1, -1, -1, -1]
if midi_pitch_array.size == 12:
midi_pitch_array = np.reshape(midi_pitch_array, (4, 3))
diff_array = np.sign(np.diff(midi_pitch_array, axis=1))
s_array = np.sum(diff_array, axis=1)
for i in range(s_array.size):
if s_array[i] > 0:
arp_dir[i] = ARP_REVERSE_DICT['up']
elif s_array[i] < 0:
arp_dir[i] = ARP_REVERSE_DICT['down']
arp1_idx = arp_dir[0]
arp2_idx = arp_dir[1]
arp3_idx = arp_dir[2]
arp4_idx = arp_dir[3]
return tonic_idx, octave_idx, mode_idx, rhy1_idx, rhy2_idx, arp1_idx, arp2_idx, arp3_idx, arp4_idx