-
Notifications
You must be signed in to change notification settings - Fork 74
/
utils.py
executable file
·148 lines (111 loc) · 3.55 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
import librosa
import numpy as np
import torch
import soundfile
from model import *
from packaging import version
def librosa_write(outfile, x, sr):
if version.parse(librosa.__version__) < version.parse('0.8.0'):
librosa.output.write_wav(outfile, x, sr)
else:
soundfile.write(outfile, x, sr)
def wav2spectrum(filename):
x, sr = librosa.load(filename)
S = librosa.stft(x, n_fft=N_FFT)
p = np.angle(S)
S = np.log1p(np.abs(S))
return S, sr
def spectrum2wav(spectrum, sr, outfile):
# Return the all-zero vector with the same shape of `a_content`
a = np.exp(spectrum) - 1
p = 2 * np.pi * np.random.random_sample(spectrum.shape) - np.pi
for i in range(50):
S = a * np.exp(1j * p)
x = librosa.istft(S)
p = np.angle(librosa.stft(x, n_fft=N_FFT))
librosa_write(outfile, x, sr)
def wav2spectrum_keep_phase(filename):
x, sr = librosa.load(filename)
S = librosa.stft(x, n_fft=N_FFT)
p = np.angle(S)
S = np.log1p(np.abs(S))
return S, p, sr
def spectrum2wav_keep_phase(spectrum, p, sr, outfile):
# Return the all-zero vector with the same shape of `a_content`
a = np.exp(spectrum) - 1
for i in range(50):
S = a * np.exp(1j * p)
x = librosa.istft(S)
p = np.angle(librosa.stft(x, n_fft=N_FFT))
librosa_write(outfile, x, sr)
def compute_content_loss(a_C, a_G):
"""
Compute the content cost
Arguments:
a_C -- tensor of dimension (1, n_C, n_H, n_W)
a_G -- tensor of dimension (1, n_C, n_H, n_W)
Returns:
J_content -- scalar that you compute using equation 1 above
"""
m, n_C, n_H, n_W = a_G.shape
# Reshape a_C and a_G to the (m * n_C, n_H * n_W)
a_C_unrolled = a_C.view(m * n_C, n_H * n_W)
a_G_unrolled = a_G.view(m * n_C, n_H * n_W)
# Compute the cost
J_content = 1.0 / (4 * m * n_C * n_H * n_W) * torch.sum((a_C_unrolled - a_G_unrolled) ** 2)
return J_content
def gram(A):
"""
Argument:
A -- matrix of shape (n_C, n_L)
Returns:
GA -- Gram matrix of shape (n_C, n_C)
"""
GA = torch.matmul(A, A.t())
return GA
def gram_over_time_axis(A):
"""
Argument:
A -- matrix of shape (1, n_C, n_H, n_W)
Returns:
GA -- Gram matrix of A along time axis, of shape (n_C, n_C)
"""
m, n_C, n_H, n_W = A.shape
# Reshape the matrix to the shape of (n_C, n_L)
# Reshape a_C and a_G to the (m * n_C, n_H * n_W)
A_unrolled = A.view(m * n_C * n_H, n_W)
GA = torch.matmul(A_unrolled, A_unrolled.t())
return GA
def compute_layer_style_loss(a_S, a_G):
"""
Arguments:
a_S -- tensor of dimension (1, n_C, n_H, n_W)
a_G -- tensor of dimension (1, n_C, n_H, n_W)
Returns:
J_style_layer -- tensor representing a scalar style cost.
"""
m, n_C, n_H, n_W = a_G.shape
# Reshape the matrix to the shape of (n_C, n_L)
# Reshape a_C and a_G to the (m * n_C, n_H * n_W)
# Calculate the gram
# !!!!!! IMPORTANT !!!!! Here we compute the Gram along n_C,
# not along n_H * n_W. But is the result the same? No.
GS = gram_over_time_axis(a_S)
GG = gram_over_time_axis(a_G)
# Computing the loss
J_style_layer = 1.0 / (4 * (n_C ** 2) * (n_H * n_W)) * torch.sum((GS - GG) ** 2)
return J_style_layer
"""
# Test
test_S = torch.randn(1, 6, 2, 2)
test_G = torch.randn(1, 6, 2, 2)
print(test_S)
print(test_G)
print(compute_layer_style_loss(test_S, test_G))
# Test
test_C = torch.randn(1, 6, 2, 2)
test_G = torch.randn(1, 6, 2, 2)
print(test_C)
print(test_G)
print(compute_content_loss(test_C, test_G))
"""