forked from jason9693/MusicTransformer-tensorflow2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
191 lines (157 loc) · 5.92 KB
/
data.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
import utils
import random
import pickle
from tensorflow.python import keras
import numpy as np
import params as par
import sequence
class Data:
def __init__(self, dir_path):
self.files = list(utils.find_files_by_extensions(dir_path, ['.pickle']))
self.file_dict = {
'train': self.files[:int(len(self.files) * 0.8)],
'eval': self.files[int(len(self.files) * 0.8): int(len(self.files) * 0.9)],
'test': self.files[int(len(self.files) * 0.9):],
}
self._seq_file_name_idx = 0
self._seq_idx = 0
pass
def __repr__(self):
return '<class Data has "'+str(len(self.files))+'" files>'
def batch(self, batch_size, length, mode='train'):
batch_files = random.sample(self.file_dict[mode], k=batch_size)
batch_data = [
self._get_seq(file, length)
for file in batch_files
]
return np.array(batch_data) # batch_size, seq_len
def seq2seq_batch(self, batch_size, length, mode='train'):
data = self.batch(batch_size, length * 2, mode)
x = data[:, :length]
y = data[:, length:]
return x, y
def slide_seq2seq_batch(self, batch_size, length, mode='train'):
data = self.batch(batch_size, length+1, mode)
x = data[:, :-1]
y = data[:, 1:]
return x, y
def random_sequential_batch(self, batch_size, length):
batch_files = random.sample(self.files, k=batch_size)
batch_data = []
for i in range(batch_size):
data = self._get_seq(batch_files[i])
for j in range(len(data) - length):
batch_data.append(data[j:j+length])
if len(batch_data) == batch_size:
return batch_data
def sequential_batch(self, batch_size, length):
batch_data = []
data = self._get_seq(self.files[self._seq_file_name_idx])
while len(batch_data) < batch_size:
while self._seq_idx < len(data) - length:
batch_data.append(data[self._seq_idx: self._seq_idx + length])
self._seq_idx += 1
if len(batch_data) == batch_size:
return batch_data
self._seq_idx = 0
self._seq_file_name_idx = self._seq_file_name_idx + 1
if self._seq_file_name_idx == len(self.files):
self._seq_file_name_idx = 0
print('iter intialized')
def _get_seq(self, fname, max_length=None):
with open(fname, 'rb') as f:
data = pickle.load(f)
if max_length is not None:
if max_length <= len(data):
start = random.randrange(0,len(data) - max_length)
data = data[start:start + max_length]
else:
data = np.append(data, par.token_eos)
while len(data) < max_length:
data = np.append(data, par.pad_token)
return data
class PositionalY:
def __init__(self, data, idx):
self.data = data
self.idx = idx
def position(self):
return self.idx
def data(self):
return self.data
def __repr__(self):
return '<Label located in {} position.>'.format(self.idx)
class DataSequence(keras.utils.Sequence):
def __init__(self, path, batch_size, seq_len, vocab_size=sequence.EventSeq.dim()+2):
self.data = Data(path)
self.batch_size = batch_size
self.file_idx = 0
self.cache = []
self.vocab_size = vocab_size
self.seq_len = seq_len
pass
def _update_cache(self):
if self.file_idx < len(self.data.files)-1:
self.file_idx += 1
else:
self.file_idx = 0
seq = self.data._get_seq(self.data.files[self.file_idx])
self.cache = self._cut_data(seq)
def __len__(self):
return len(self.data.files)
def __getitem__(self, idx):
data_batch = self.data.batch(self.batch_size,self.seq_len + 1)
data_batch = np.array(data_batch)
try:
x = data_batch[:,:-1]
y = data_batch[:,1:]
except:
print('except')
return self.__getitem__(idx)
return np.array(x), np.eye(self.vocab_size)[np.array(y)]
if __name__ == '__main__':
import pprint
def count_dict(max_length, data):
cnt_arr = [0] * max_length
cnt_dict = {}
# print(cnt_arr)
for batch in data:
for index in batch:
try:
cnt_arr[int(index)] += 1
except:
print(index)
try:
cnt_dict['index-'+str(index)] += 1
except KeyError:
cnt_dict['index-'+str(index)] = 1
return cnt_arr
print(par.vocab_size)
data = Data('dataset/processed')
# ds = DataSequence('dataset/processed', 10, 2048)
sample = data.seq2seq_batch(1000, 100)[0]
pprint.pprint(list(sample))
arr = count_dict(par.vocab_size+3,sample)
pprint.pprint(
arr)
from sequence import EventSeq, Event
event_cnt = {
'note_on': 0,
'note_off': 0,
'velocity': 0,
'time_shift': 0
}
for event_index in range(len(arr)):
for event_type, feat_range in EventSeq.feat_ranges().items():
if feat_range.start <= event_index < feat_range.stop:
print(event_type+':'+str(arr[event_index])+' event cnt: '+str(event_cnt))
event_cnt[event_type] += arr[event_index]
# event_value = event_index - feat_range.start
# events.append(Event(event_type, time, event_value))
# if event_type == 'time_shift':
# time += EventSeq.time_shift_bins[event_value]
# break
print(event_cnt)
# print(np.max(sample), np.min(sample))
# print([data._get_seq(file).shape for file in data.files])
#while True:
# print(ds.__getitem__(10)[1].argmax(-1))