-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_dataloader.py
97 lines (75 loc) · 2.92 KB
/
music_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
import itertools
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
class ClassDictionary:
def __init__(self, data_fp):
self.index_to_class = None
self.class_to_index = None
self.make_lookup(data_fp)
self.len = len(self.index_to_class)
def __len__(self):
return self.len
def __getitem__(self, id):
if type(id) is torch.Tensor:
id = int(id.detach().cpu().numpy())
if type(id) is str:
if id not in self.index_to_class:
return self.class_to_index['<unk>']
return self.class_to_index[id]
elif type(id) is int:
return self.index_to_class[id]
elif type(id) is float:
if not id % 1:
return self.index_to_class[int(id)]
else:
print('ERROR: NON INTEGER FLOAT USED AS LOOKUP ID')
return None
print('ERROR: INVALID DATA TYPE USED AS LOOKUP ID')
return None
def make_lookup(self, fp):
with open(fp, 'r') as f:
data = list(itertools.chain(*[l for l in f.readlines() if l != '<end>' and l != '<start>']))
char_set = set(data)
char_set.add('<start>')
char_set.add('<end>')
char_set.add('<unk>')
self.index_to_class = sorted(list(char_set))
self.class_to_index = {v:i for i, v in enumerate(self.index_to_class)}
class MusicDataset(Dataset):
def __init__(self, dictionary, fp):
self.tunes = None
self.dictionary = dictionary
self.len = None
self.load_tunes(fp)
def __len__(self):
return self.len
def __getitem__(self, ind):
x = torch.tensor(self.make_onehot(ind))
y = torch.tensor(self.tunes[ind + 1]).long()
return (x, y)
def load_tunes(self, fp):
with open(fp, 'r') as f:
tokenized = [self.get_ids(l) for l in f.readlines()]
self.tunes = list(itertools.chain(*tokenized))
self.len = len(self.tunes) - 1
def get_ids(self, string):
if string == '<start>\n' or string == '<end>\n':
string = [string[:-1], string[-1]]
return [self.dictionary[c] for c in string]
def make_onehot(self, ind):
onehot = np.float32(np.zeros(len(self.dictionary)))
onehot[self.tunes[ind]] = 1.
return onehot
def create_split_loaders(seq_length):
fp_train = 'data/train.txt'
fp_val = 'data/val.txt'
fp_test = 'data/test.txt'
dictionary = ClassDictionary(fp_train)
train_dataset = MusicDataset(dictionary, fp_train)
val_dataset = MusicDataset(dictionary, fp_val)
test_dataset = MusicDataset(dictionary, fp_test)
train_loader = DataLoader(train_dataset, batch_size=seq_length)
val_loader = DataLoader(val_dataset, batch_size=seq_length)
test_loader = DataLoader(test_dataset, batch_size=seq_length)
return train_loader, val_loader, test_loader, dictionary