Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
Torchaudio
Commits
60fd113c
You need to sign in or sign up before continuing.
Unverified
Commit
60fd113c
authored
Jan 17, 2020
by
Vincent QB
Committed by
GitHub
Jan 17, 2020
Browse files
replace reshape by view. (#409)
parent
b32606d6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
26 deletions
+25
-26
torchaudio/functional.py
torchaudio/functional.py
+21
-22
torchaudio/transforms.py
torchaudio/transforms.py
+4
-4
No files found.
torchaudio/functional.py
View file @
60fd113c
...
@@ -129,7 +129,7 @@ def istft(
...
@@ -129,7 +129,7 @@ def istft(
# pack batch
# pack batch
shape
=
stft_matrix
.
size
()
shape
=
stft_matrix
.
size
()
stft_matrix
=
stft_matrix
.
reshape
(
-
1
,
shape
[
-
3
],
shape
[
-
2
],
shape
[
-
1
])
stft_matrix
=
stft_matrix
.
view
(
-
1
,
shape
[
-
3
],
shape
[
-
2
],
shape
[
-
1
])
dtype
=
stft_matrix
.
dtype
dtype
=
stft_matrix
.
dtype
device
=
stft_matrix
.
device
device
=
stft_matrix
.
device
...
@@ -214,7 +214,7 @@ def istft(
...
@@ -214,7 +214,7 @@ def istft(
y
=
(
y
/
window_envelop
).
squeeze
(
1
)
# size (channel, expected_signal_len)
y
=
(
y
/
window_envelop
).
squeeze
(
1
)
# size (channel, expected_signal_len)
# unpack batch
# unpack batch
y
=
y
.
reshape
(
shape
[:
-
3
]
+
y
.
shape
[
-
1
:])
y
=
y
.
view
(
shape
[:
-
3
]
+
y
.
shape
[
-
1
:])
if
stft_matrix_dim
==
3
:
# remove the channel dimension
if
stft_matrix_dim
==
3
:
# remove the channel dimension
y
=
y
.
squeeze
(
0
)
y
=
y
.
squeeze
(
0
)
...
@@ -253,7 +253,7 @@ def spectrogram(
...
@@ -253,7 +253,7 @@ def spectrogram(
# pack batch
# pack batch
shape
=
waveform
.
size
()
shape
=
waveform
.
size
()
waveform
=
waveform
.
reshape
(
-
1
,
shape
[
-
1
])
waveform
=
waveform
.
view
(
-
1
,
shape
[
-
1
])
# default values are consistent with librosa.core.spectrum._spectrogram
# default values are consistent with librosa.core.spectrum._spectrogram
spec_f
=
_stft
(
spec_f
=
_stft
(
...
@@ -261,7 +261,7 @@ def spectrogram(
...
@@ -261,7 +261,7 @@ def spectrogram(
)
)
# unpack batch
# unpack batch
spec_f
=
spec_f
.
reshape
(
shape
[:
-
1
]
+
spec_f
.
shape
[
-
3
:])
spec_f
=
spec_f
.
view
(
shape
[:
-
1
]
+
spec_f
.
shape
[
-
3
:])
if
normalized
:
if
normalized
:
spec_f
/=
window
.
pow
(
2.
).
sum
().
sqrt
()
spec_f
/=
window
.
pow
(
2.
).
sum
().
sqrt
()
...
@@ -317,7 +317,7 @@ def griffinlim(
...
@@ -317,7 +317,7 @@ def griffinlim(
# pack batch
# pack batch
shape
=
specgram
.
size
()
shape
=
specgram
.
size
()
specgram
=
specgram
.
reshape
([
-
1
]
+
list
(
shape
[
-
2
:]))
specgram
=
specgram
.
view
([
-
1
]
+
list
(
shape
[
-
2
:]))
specgram
=
specgram
.
pow
(
1
/
power
)
specgram
=
specgram
.
pow
(
1
/
power
)
...
@@ -363,7 +363,7 @@ def griffinlim(
...
@@ -363,7 +363,7 @@ def griffinlim(
length
=
length
)
length
=
length
)
# unpack batch
# unpack batch
waveform
=
waveform
.
reshape
(
shape
[:
-
2
]
+
waveform
.
shape
[
-
1
:])
waveform
=
waveform
.
view
(
shape
[:
-
2
]
+
waveform
.
shape
[
-
1
:])
return
waveform
return
waveform
...
@@ -587,7 +587,7 @@ def phase_vocoder(complex_specgrams, rate, phase_advance):
...
@@ -587,7 +587,7 @@ def phase_vocoder(complex_specgrams, rate, phase_advance):
# pack batch
# pack batch
shape
=
complex_specgrams
.
size
()
shape
=
complex_specgrams
.
size
()
complex_specgrams
=
complex_specgrams
.
reshape
([
-
1
]
+
list
(
shape
[
-
3
:]))
complex_specgrams
=
complex_specgrams
.
view
([
-
1
]
+
list
(
shape
[
-
3
:]))
time_steps
=
torch
.
arange
(
0
,
time_steps
=
torch
.
arange
(
0
,
complex_specgrams
.
size
(
-
2
),
complex_specgrams
.
size
(
-
2
),
...
@@ -627,7 +627,7 @@ def phase_vocoder(complex_specgrams, rate, phase_advance):
...
@@ -627,7 +627,7 @@ def phase_vocoder(complex_specgrams, rate, phase_advance):
complex_specgrams_stretch
=
torch
.
stack
([
real_stretch
,
imag_stretch
],
dim
=-
1
)
complex_specgrams_stretch
=
torch
.
stack
([
real_stretch
,
imag_stretch
],
dim
=-
1
)
# unpack batch
# unpack batch
complex_specgrams_stretch
=
complex_specgrams_stretch
.
reshape
(
shape
[:
-
3
]
+
complex_specgrams_stretch
.
shape
[
1
:])
complex_specgrams_stretch
=
complex_specgrams_stretch
.
view
(
shape
[:
-
3
]
+
complex_specgrams_stretch
.
shape
[
1
:])
return
complex_specgrams_stretch
return
complex_specgrams_stretch
...
@@ -654,7 +654,7 @@ def lfilter(waveform, a_coeffs, b_coeffs):
...
@@ -654,7 +654,7 @@ def lfilter(waveform, a_coeffs, b_coeffs):
# pack batch
# pack batch
shape
=
waveform
.
size
()
shape
=
waveform
.
size
()
waveform
=
waveform
.
reshape
(
-
1
,
shape
[
-
1
])
waveform
=
waveform
.
view
(
-
1
,
shape
[
-
1
])
assert
(
a_coeffs
.
size
(
0
)
==
b_coeffs
.
size
(
0
))
assert
(
a_coeffs
.
size
(
0
)
==
b_coeffs
.
size
(
0
))
assert
(
len
(
waveform
.
size
())
==
2
)
assert
(
len
(
waveform
.
size
())
==
2
)
...
@@ -697,7 +697,7 @@ def lfilter(waveform, a_coeffs, b_coeffs):
...
@@ -697,7 +697,7 @@ def lfilter(waveform, a_coeffs, b_coeffs):
output
=
torch
.
clamp
(
padded_output_waveform
[:,
(
n_order
-
1
):],
min
=-
1.
,
max
=
1.
)
output
=
torch
.
clamp
(
padded_output_waveform
[:,
(
n_order
-
1
):],
min
=-
1.
,
max
=
1.
)
# unpack batch
# unpack batch
output
=
output
.
reshape
(
shape
[:
-
1
]
+
output
.
shape
[
-
1
:])
output
=
output
.
view
(
shape
[:
-
1
]
+
output
.
shape
[
-
1
:])
return
output
return
output
...
@@ -876,7 +876,7 @@ def mask_along_axis(specgram, mask_param, mask_value, axis):
...
@@ -876,7 +876,7 @@ def mask_along_axis(specgram, mask_param, mask_value, axis):
# pack batch
# pack batch
shape
=
specgram
.
size
()
shape
=
specgram
.
size
()
specgram
=
specgram
.
reshape
([
-
1
]
+
list
(
shape
[
-
2
:]))
specgram
=
specgram
.
view
([
-
1
]
+
list
(
shape
[
-
2
:]))
value
=
torch
.
rand
(
1
)
*
mask_param
value
=
torch
.
rand
(
1
)
*
mask_param
min_value
=
torch
.
rand
(
1
)
*
(
specgram
.
size
(
axis
)
-
value
)
min_value
=
torch
.
rand
(
1
)
*
(
specgram
.
size
(
axis
)
-
value
)
...
@@ -893,7 +893,7 @@ def mask_along_axis(specgram, mask_param, mask_value, axis):
...
@@ -893,7 +893,7 @@ def mask_along_axis(specgram, mask_param, mask_value, axis):
raise
ValueError
(
'Only Frequency and Time masking are supported'
)
raise
ValueError
(
'Only Frequency and Time masking are supported'
)
# unpack batch
# unpack batch
specgram
=
specgram
.
reshape
(
shape
[:
-
2
]
+
specgram
.
shape
[
-
2
:])
specgram
=
specgram
.
view
(
shape
[:
-
2
]
+
specgram
.
shape
[
-
2
:])
return
specgram
return
specgram
...
@@ -925,7 +925,7 @@ def compute_deltas(specgram, win_length=5, mode="replicate"):
...
@@ -925,7 +925,7 @@ def compute_deltas(specgram, win_length=5, mode="replicate"):
# pack batch
# pack batch
shape
=
specgram
.
size
()
shape
=
specgram
.
size
()
specgram
=
specgram
.
reshape
(
1
,
-
1
,
shape
[
-
1
])
specgram
=
specgram
.
view
(
1
,
-
1
,
shape
[
-
1
])
assert
win_length
>=
3
assert
win_length
>=
3
...
@@ -945,7 +945,7 @@ def compute_deltas(specgram, win_length=5, mode="replicate"):
...
@@ -945,7 +945,7 @@ def compute_deltas(specgram, win_length=5, mode="replicate"):
output
=
torch
.
nn
.
functional
.
conv1d
(
specgram
,
kernel
,
groups
=
specgram
.
shape
[
1
])
/
denom
output
=
torch
.
nn
.
functional
.
conv1d
(
specgram
,
kernel
,
groups
=
specgram
.
shape
[
1
])
/
denom
# unpack batch
# unpack batch
output
=
output
.
reshape
(
shape
)
output
=
output
.
view
(
shape
)
return
output
return
output
...
@@ -974,11 +974,10 @@ def _add_noise_shaping(dithered_waveform, waveform):
...
@@ -974,11 +974,10 @@ def _add_noise_shaping(dithered_waveform, waveform):
error[n] = dithered[n] - original[n]
error[n] = dithered[n] - original[n]
noise_shaped_waveform[n] = dithered[n] + error[n-1]
noise_shaped_waveform[n] = dithered[n] + error[n-1]
"""
"""
wf_shape
=
waveform
.
size
()
waveform
=
waveform
.
view
(
-
1
,
waveform
.
size
()[
-
1
])
waveform
=
waveform
.
reshape
(
-
1
,
wf_shape
[
-
1
])
dithered_shape
=
dithered_waveform
.
size
()
dithered_shape
=
dithered_waveform
.
size
()
dithered_waveform
=
dithered_waveform
.
reshape
(
-
1
,
dithered_shape
[
-
1
])
dithered_waveform
=
dithered_waveform
.
view
(
-
1
,
dithered_shape
[
-
1
])
error
=
dithered_waveform
-
waveform
error
=
dithered_waveform
-
waveform
...
@@ -989,7 +988,7 @@ def _add_noise_shaping(dithered_waveform, waveform):
...
@@ -989,7 +988,7 @@ def _add_noise_shaping(dithered_waveform, waveform):
error
[
index
]
=
error_offset
[:
waveform
.
size
()[
1
]]
error
[
index
]
=
error_offset
[:
waveform
.
size
()[
1
]]
noise_shaped
=
dithered_waveform
+
error
noise_shaped
=
dithered_waveform
+
error
return
noise_shaped
.
reshape
(
dithered_shape
[:
-
1
]
+
noise_shaped
.
shape
[
-
1
:])
return
noise_shaped
.
view
(
dithered_shape
[:
-
1
]
+
noise_shaped
.
shape
[
-
1
:])
def
_apply_probability_distribution
(
waveform
,
density_function
=
"TPDF"
):
def
_apply_probability_distribution
(
waveform
,
density_function
=
"TPDF"
):
...
@@ -1020,7 +1019,7 @@ def _apply_probability_distribution(waveform, density_function="TPDF"):
...
@@ -1020,7 +1019,7 @@ def _apply_probability_distribution(waveform, density_function="TPDF"):
# pack batch
# pack batch
shape
=
waveform
.
size
()
shape
=
waveform
.
size
()
waveform
=
waveform
.
reshape
(
-
1
,
shape
[
-
1
])
waveform
=
waveform
.
view
(
-
1
,
shape
[
-
1
])
channel_size
=
waveform
.
size
()[
0
]
-
1
channel_size
=
waveform
.
size
()[
0
]
-
1
time_size
=
waveform
.
size
()[
-
1
]
-
1
time_size
=
waveform
.
size
()[
-
1
]
-
1
...
@@ -1060,7 +1059,7 @@ def _apply_probability_distribution(waveform, density_function="TPDF"):
...
@@ -1060,7 +1059,7 @@ def _apply_probability_distribution(waveform, density_function="TPDF"):
quantised_signal
=
quantised_signal_scaled
/
down_scaling
quantised_signal
=
quantised_signal_scaled
/
down_scaling
# unpack batch
# unpack batch
return
quantised_signal
.
reshape
(
shape
[:
-
1
]
+
quantised_signal
.
shape
[
-
1
:])
return
quantised_signal
.
view
(
shape
[:
-
1
]
+
quantised_signal
.
shape
[
-
1
:])
def
dither
(
waveform
,
density_function
=
"TPDF"
,
noise_shaping
=
False
):
def
dither
(
waveform
,
density_function
=
"TPDF"
,
noise_shaping
=
False
):
...
@@ -1231,7 +1230,7 @@ def detect_pitch_frequency(
...
@@ -1231,7 +1230,7 @@ def detect_pitch_frequency(
# pack batch
# pack batch
shape
=
list
(
waveform
.
size
())
shape
=
list
(
waveform
.
size
())
waveform
=
waveform
.
reshape
([
-
1
]
+
shape
[
-
1
:])
waveform
=
waveform
.
view
([
-
1
]
+
shape
[
-
1
:])
nccf
=
_compute_nccf
(
waveform
,
sample_rate
,
frame_time
,
freq_low
)
nccf
=
_compute_nccf
(
waveform
,
sample_rate
,
frame_time
,
freq_low
)
indices
=
_find_max_per_frame
(
nccf
,
sample_rate
,
freq_high
)
indices
=
_find_max_per_frame
(
nccf
,
sample_rate
,
freq_high
)
...
@@ -1242,6 +1241,6 @@ def detect_pitch_frequency(
...
@@ -1242,6 +1241,6 @@ def detect_pitch_frequency(
freq
=
sample_rate
/
(
EPSILON
+
indices
.
to
(
torch
.
float
))
freq
=
sample_rate
/
(
EPSILON
+
indices
.
to
(
torch
.
float
))
# unpack batch
# unpack batch
freq
=
freq
.
reshape
(
shape
[:
-
1
]
+
list
(
freq
.
shape
[
-
1
:]))
freq
=
freq
.
view
(
shape
[:
-
1
]
+
list
(
freq
.
shape
[
-
1
:]))
return
freq
return
freq
torchaudio/transforms.py
View file @
60fd113c
...
@@ -215,7 +215,7 @@ class MelScale(torch.nn.Module):
...
@@ -215,7 +215,7 @@ class MelScale(torch.nn.Module):
# pack batch
# pack batch
shape
=
specgram
.
size
()
shape
=
specgram
.
size
()
specgram
=
specgram
.
reshape
(
-
1
,
shape
[
-
2
],
shape
[
-
1
])
specgram
=
specgram
.
view
(
-
1
,
shape
[
-
2
],
shape
[
-
1
])
if
self
.
fb
.
numel
()
==
0
:
if
self
.
fb
.
numel
()
==
0
:
tmp_fb
=
F
.
create_fb_matrix
(
specgram
.
size
(
1
),
self
.
f_min
,
self
.
f_max
,
self
.
n_mels
,
self
.
sample_rate
)
tmp_fb
=
F
.
create_fb_matrix
(
specgram
.
size
(
1
),
self
.
f_min
,
self
.
f_max
,
self
.
n_mels
,
self
.
sample_rate
)
...
@@ -228,7 +228,7 @@ class MelScale(torch.nn.Module):
...
@@ -228,7 +228,7 @@ class MelScale(torch.nn.Module):
mel_specgram
=
torch
.
matmul
(
specgram
.
transpose
(
1
,
2
),
self
.
fb
).
transpose
(
1
,
2
)
mel_specgram
=
torch
.
matmul
(
specgram
.
transpose
(
1
,
2
),
self
.
fb
).
transpose
(
1
,
2
)
# unpack batch
# unpack batch
mel_specgram
=
mel_specgram
.
reshape
(
shape
[:
-
2
]
+
mel_specgram
.
shape
[
-
2
:])
mel_specgram
=
mel_specgram
.
view
(
shape
[:
-
2
]
+
mel_specgram
.
shape
[
-
2
:])
return
mel_specgram
return
mel_specgram
...
@@ -349,7 +349,7 @@ class MFCC(torch.nn.Module):
...
@@ -349,7 +349,7 @@ class MFCC(torch.nn.Module):
# pack batch
# pack batch
shape
=
waveform
.
size
()
shape
=
waveform
.
size
()
waveform
=
waveform
.
reshape
(
-
1
,
shape
[
-
1
])
waveform
=
waveform
.
view
(
-
1
,
shape
[
-
1
])
mel_specgram
=
self
.
MelSpectrogram
(
waveform
)
mel_specgram
=
self
.
MelSpectrogram
(
waveform
)
if
self
.
log_mels
:
if
self
.
log_mels
:
...
@@ -362,7 +362,7 @@ class MFCC(torch.nn.Module):
...
@@ -362,7 +362,7 @@ class MFCC(torch.nn.Module):
mfcc
=
torch
.
matmul
(
mel_specgram
.
transpose
(
1
,
2
),
self
.
dct_mat
).
transpose
(
1
,
2
)
mfcc
=
torch
.
matmul
(
mel_specgram
.
transpose
(
1
,
2
),
self
.
dct_mat
).
transpose
(
1
,
2
)
# unpack batch
# unpack batch
mfcc
=
mfcc
.
reshape
(
shape
[:
-
1
]
+
mfcc
.
shape
[
-
2
:])
mfcc
=
mfcc
.
view
(
shape
[:
-
1
]
+
mfcc
.
shape
[
-
2
:])
return
mfcc
return
mfcc
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment