forked from jason9693/MusicTransformer-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
199 lines (159 loc) · 6.02 KB
/
utils.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
import os
import numpy as np
from deprecated.sequence import EventSeq, ControlSeq
import torch
import torch.nn.functional as F
import torchvision
# from custom.config import config
def find_files_by_extensions(root, exts=[]):
def _has_ext(name):
if not exts:
return True
name = name.lower()
for ext in exts:
if name.endswith(ext):
return True
return False
for path, _, files in os.walk(root):
for name in files:
if _has_ext(name):
yield os.path.join(path, name)
def event_indeces_to_midi_file(event_indeces, midi_file_name, velocity_scale=0.8):
event_seq = EventSeq.from_array(event_indeces)
note_seq = event_seq.to_note_seq()
for note in note_seq.notes:
note.velocity = int((note.velocity - 64) * velocity_scale + 64)
note_seq.to_midi_file(midi_file_name)
return len(note_seq.notes)
def dict2params(d, f=','):
return f.join(f'{k}={v}' for k, v in d.items())
def params2dict(p, f=',', e='='):
d = {}
for item in p.split(f):
item = item.split(e)
if len(item) < 2:
continue
k, *v = item
d[k] = eval('='.join(v))
return d
def compute_gradient_norm(parameters, norm_type=2):
total_norm = 0
for p in parameters:
param_norm = p.grad.data.norm(norm_type)
total_norm += param_norm ** norm_type
total_norm = total_norm ** (1. / norm_type)
return total_norm
def get_masked_with_pad_tensor(size, src, trg, pad_token):
"""
:param size: the size of target input
:param src: source tensor
:param trg: target tensor
:param pad_token: pad token
:return:
"""
src = src[:, None, None, :]
trg = trg[:, None, None, :]
src_pad_tensor = torch.ones_like(src).to(src.device.type) * pad_token
src_mask = torch.equal(src, src_pad_tensor)
trg_mask = torch.equal(src, src_pad_tensor)
if trg is not None:
trg_pad_tensor = torch.ones_like(trg).to(trg.device.type) * pad_token
dec_trg_mask = trg == trg_pad_tensor
# boolean reversing i.e) True * -1 + 1 = False
seq_mask = ~sequence_mask(torch.arange(1, size+1).to(trg.device), size)
# look_ahead_mask = torch.max(dec_trg_mask, seq_mask)
look_ahead_mask = dec_trg_mask | seq_mask
else:
trg_mask = None
look_ahead_mask = None
return src_mask, trg_mask, look_ahead_mask
def get_mask_tensor(size):
"""
:param size: max length of token
:return:
"""
# boolean reversing i.e) True * -1 + 1 = False
seq_mask = ~sequence_mask(torch.arange(1, size + 1), size)
return seq_mask
def fill_with_placeholder(prev_data: list, max_len: int, fill_val: float):
placeholder = [fill_val for _ in range(max_len - len(prev_data))]
return prev_data + placeholder
def pad_with_length(max_length: int, seq: list, pad_val: float):
"""
:param max_length: max length of token
:param seq: token list with shape:(length, dim)
:param pad_val: padding value
:return:
"""
pad_length = max(max_length - len(seq), 0)
pad = [pad_val] * pad_length
return seq + pad
def append_token(data: torch.Tensor, eos_token):
start_token = torch.ones((data.size(0), 1), dtype=data.dtype) * eos_token
end_token = torch.ones((data.size(0), 1), dtype=data.dtype) * eos_token
return torch.cat([start_token, data, end_token], -1)
def shape_list(x):
"""Shape list"""
x_shape = x.size()
x_get_shape = list(x.size())
res = []
for i, d in enumerate(x_get_shape):
if d is not None:
res.append(d)
else:
res.append(x_shape[i])
return res
def attention_image_summary(name, attn, step=0, writer=None):
"""Compute color image summary.
Args:
attn: a Tensor with shape [batch, num_heads, query_length, memory_length]
image_shapes: optional tuple of integer scalars.
If the query positions and memory positions represent the
pixels of flattened images, then pass in their dimensions:
(query_rows, query_cols, memory_rows, memory_cols).
If the query positions and memory positions represent the
pixels x channels of flattened images, then pass in their dimensions:
(query_rows, query_cols, query_channels,
memory_rows, memory_cols, memory_channels).
"""
num_heads = attn.size(1)
# [batch, query_length, memory_length, num_heads]
image = attn.permute(0, 2, 3, 1)
image = torch.pow(image, 0.2) # for high-dynamic-range
# Each head will correspond to one of RGB.
# pad the heads to be a multiple of 3
image = F.pad(image, [0, -num_heads % 3, 0, 0, 0, 0, 0, 0,])
image = split_last_dimension(image, 3)
image = image.max(dim=4).values
grid_image = torchvision.utils.make_grid(image.permute(0, 3, 1, 2))
writer.add_image(name, grid_image, global_step=step, dataformats='CHW')
def split_last_dimension(x, n):
"""Reshape x so that the last dimension becomes two dimensions.
The first of these two dimensions is n.
Args:
x: a Tensor with shape [..., m]
n: an integer.
Returns:
a Tensor with shape [..., n, m/n]
"""
x_shape = x.size()
m = x_shape[-1]
if isinstance(m, int) and isinstance(n, int):
assert m % n == 0
return torch.reshape(x, x_shape[:-1] + (n, m // n))
def subsequent_mask(size):
"Mask out subsequent positions."
attn_shape = (1, size, size)
subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
return torch.from_numpy(subsequent_mask) == 0
def sequence_mask(length, max_length=None):
"""Tensorflow의 sequence_mask를 구현"""
if max_length is None:
max_length = length.max()
x = torch.arange(max_length, dtype=length.dtype, device=length.device)
return x.unsqueeze(0) < length.unsqueeze(1)
if __name__ == '__main__':
s = np.array([np.array([1, 2]*50),np.array([1, 2, 3, 4]*25)])
t = np.array([np.array([2, 3, 4, 5, 6]*20), np.array([1, 2, 3, 4, 5]*20)])
print(t.shape)
print(get_masked_with_pad_tensor(100, s, t))