Repository: DemisEom/SpecAugment
Branch: master
Commit: 7f1435963b37
Files: 13
Total size: 55.4 KB
Directory structure:
gitextract_qwqfy5oq/
├── .gitignore
├── LICENSE
├── README.md
├── SpecAugment/
│ ├── __init__.py
│ ├── sparse_image_warp_np.py
│ ├── sparse_image_warp_pytorch.py
│ ├── spec_augment_pytorch.py
│ └── spec_augment_tensorflow.py
├── requirements.txt
├── setup.cfg
├── setup.py
└── tests/
├── spec_augment_test_TF.py
└── spec_augment_test_pytorch.py
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
venv
.DS_Store
.idea
================================================
FILE: LICENSE
================================================
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
================================================
FILE: README.md
================================================
# SpecAugment [](https://opensource.org/licenses/Apache-2.0)
This is a implementation of SpecAugment that speech data augmentation method which directly process the spectrogram with Tensorflow & Pytorch, introduced by Google Brain[1]. This is currently under the Apache 2.0, Please feel free to use for your project. Enjoy!
## How to use
First, you need to have python 3 installed along with [Tensorflow](https://www.tensorflow.org/install/).
Next, you need to install some audio libraries work properly. To install the requirement packages. Run the following command:
```bash
pip3 install SpecAugment
```
And then, run the specAugment.py program. It modifies the spectrogram by warping it in the time direction, masking blocks of consecutive frequency channels, and masking blocks of utterances in time.
#### *Try your audio file SpecAugment*
```shell
$ python3
```
```python
>>> import librosa
>>> from specAugment import spec_augment_tensorflow
# If you are Pytorch, then import spec_augment_pytorch instead of spec_augment_tensorflow
>>> audio, sampling_rate = librosa.load(audio_path)
>>> mel_spectrogram = librosa.feature.melspectrogram(y=audio,
sr=sampling_rate,
n_mels=256,
hop_length=128,
fmax=8000)
>>> warped_masked_spectrogram = spec_augment_tensorflow.spec_augment(mel_spectrogram=mel_spectrogram)
>>> print(warped_masked_spectrogram)
'
[[1.54055389e-01 7.51822486e-01 7.29588015e-01 ... 1.03616300e-01
1.04682689e-01 1.05411769e-01]
[2.21608739e-01 1.38559084e-01 1.01564167e-01 ... 4.19907116e-02
4.86430404e-02 5.27331798e-02]
[3.62784019e-01 2.09934399e-01 1.79158230e-01 ... 2.42307431e-01
3.18662338e-01 3.67405599e-01]
...
[6.36117335e-07 8.06897948e-07 8.55346431e-07 ... 2.84445018e-07
4.02975952e-07 5.57131738e-07]
[6.27753429e-07 7.53681318e-07 8.13035033e-07 ... 1.35111146e-07
2.74058225e-07 4.56901031e-07]
[0.00000000e+00 7.48416680e-07 5.51771037e-07 ... 1.13901361e-07
2.56365068e-07 4.43868592e-07]]
'
```
Learn more examples about how to do specific tasks in SpecAugment at the test code.
```bash
python spec_augment_test.py
```
In test code, we using one of the [LibriSpeech dataset](http://www.openslr.org/12/).
# Reference
1. https://arxiv.org/pdf/1904.08779.pdf
================================================
FILE: SpecAugment/__init__.py
================================================
================================================
FILE: SpecAugment/sparse_image_warp_np.py
================================================
"""Image warping using sparse flow defined at control points."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import scipy as sp
import skimage
from scipy.interpolate import interp2d
from skimage.transform import warp
def _get_grid_locations(image_height, image_width):
"""Wrapper for np.meshgrid."""
y_range = np.linspace(0, image_height - 1, image_height)
x_range = np.linspace(0, image_width - 1, image_width)
y_grid, x_grid = np.meshgrid(y_range, x_range, indexing='ij')
return np.stack((y_grid, x_grid), -1)
def _expand_to_minibatch(np_array, batch_size):
"""Tile arbitrarily-sized np_array to include new batch dimension."""
tiles = [batch_size] + [1] * np_array.ndim
return np.tile(np.expand_dims(np_array, 0), tiles)
def _get_boundary_locations(image_height, image_width, num_points_per_edge):
"""Compute evenly-spaced indices along edge of image."""
y_range = np.linspace(0, image_height - 1, num_points_per_edge + 2)
x_range = np.linspace(0, image_width - 1, num_points_per_edge + 2)
ys, xs = np.meshgrid(y_range, x_range, indexing='ij')
is_boundary = np.logical_or(
np.logical_or(xs == 0, xs == image_width - 1),
np.logical_or(ys == 0, ys == image_height - 1))
return np.stack([ys[is_boundary], xs[is_boundary]], axis=-1)
def _add_zero_flow_controls_at_boundary(control_point_locations,
control_point_flows, image_height,
image_width, boundary_points_per_edge):
# batch_size = tensor_shape.dimension_value(control_point_locations.shape[0])
batch_size = control_point_locations.shape[0]
boundary_point_locations = _get_boundary_locations(image_height, image_width,
boundary_points_per_edge)
boundary_point_flows = np.zeros([boundary_point_locations.shape[0], 2])
type_to_use = control_point_locations.dtype
# boundary_point_locations = constant_op.constant(
# _expand_to_minibatch(boundary_point_locations, batch_size),
# dtype=type_to_use)
boundary_point_locations = _expand_to_minibatch(boundary_point_locations, batch_size)
# boundary_point_flows = constant_op.constant(
# _expand_to_minibatch(boundary_point_flows, batch_size), dtype=type_to_use)
boundary_point_flows = _expand_to_minibatch(boundary_point_flows, batch_size)
# merged_control_point_locations = array_ops.concat(
# [control_point_locations, boundary_point_locations], 1)
merged_control_point_locations = np.concatenate(
[control_point_locations, boundary_point_locations], 1)
# merged_control_point_flows = array_ops.concat(
# [control_point_flows, boundary_point_flows], 1)
merged_control_point_flows = np.concatenate(
[control_point_flows, boundary_point_flows], 1)
return merged_control_point_locations, merged_control_point_flows
def sparse_image_warp_np(image,
source_control_point_locations,
dest_control_point_locations,
interpolation_order=2,
regularization_weight=0.0,
num_boundary_points=0):
# image = ops.convert_to_tensor(image)
# source_control_point_locations = ops.convert_to_tensor(
# source_control_point_locations)
# dest_control_point_locations = ops.convert_to_tensor(
# dest_control_point_locations)
control_point_flows = (
dest_control_point_locations - source_control_point_locations)
clamp_boundaries = num_boundary_points > 0
boundary_points_per_edge = num_boundary_points - 1
# batch_size, image_height, image_width, _ = image.get_shape().as_list()
batch_size, image_height, image_width, _ = list(image.shape)
# This generates the dense locations where the interpolant
# will be evaluated.
grid_locations = _get_grid_locations(image_height, image_width)
flattened_grid_locations = np.reshape(grid_locations,
[image_height * image_width, 2])
# flattened_grid_locations = constant_op.constant(
# _expand_to_minibatch(flattened_grid_locations, batch_size), image.dtype)
flattened_grid_locations = _expand_to_minibatch(flattened_grid_locations, batch_size)
if clamp_boundaries:
(dest_control_point_locations,
control_point_flows) = _add_zero_flow_controls_at_boundary(
dest_control_point_locations, control_point_flows, image_height,
image_width, boundary_points_per_edge)
# flattened_flows = interpolate_spline.interpolate_spline(
# dest_control_point_locations, control_point_flows,
# flattened_grid_locations, interpolation_order, regularization_weight)
flattened_flows = sp.interpolate.spline(
dest_control_point_locations, control_point_flows,
flattened_grid_locations, interpolation_order, regularization_weight)
# dense_flows = array_ops.reshape(flattened_flows,
# [batch_size, image_height, image_width, 2])
dense_flows = np.reshape(flattened_flows,
[batch_size, image_height, image_width, 2])
# warped_image = dense_image_warp.dense_image_warp(image, dense_flows)
warped_image = warp(image, dense_flows)
return warped_image, dense_flows
def dense_image_warp(image, flow):
# batch_size, height, width, channels = (array_ops.shape(image)[0],
# array_ops.shape(image)[1],
# array_ops.shape(image)[2],
# array_ops.shape(image)[3])
batch_size, height, width, channels = (np.shape(image)[0],
np.shape(image)[1],
np.shape(image)[2],
np.shape(image)[3])
# The flow is defined on the image grid. Turn the flow into a list of query
# points in the grid space.
# grid_x, grid_y = array_ops.meshgrid(
# math_ops.range(width), math_ops.range(height))
# stacked_grid = math_ops.cast(
# array_ops.stack([grid_y, grid_x], axis=2), flow.dtype)
# batched_grid = array_ops.expand_dims(stacked_grid, axis=0)
# query_points_on_grid = batched_grid - flow
# query_points_flattened = array_ops.reshape(query_points_on_grid,
# [batch_size, height * width, 2])
grid_x, grid_y = np.meshgrid(
np.range(width), np.range(height))
stacked_grid = np.cast(
np.stack([grid_y, grid_x], axis=2), flow.dtype)
batched_grid = np.expand_dims(stacked_grid, axis=0)
query_points_on_grid = batched_grid - flow
query_points_flattened = np.reshape(query_points_on_grid,
[batch_size, height * width, 2])
# Compute values at the query points, then reshape the result back to the
# image grid.
interpolated = interp2d(image, query_points_flattened)
interpolated = np.reshape(interpolated,
[batch_size, height, width, channels])
return interpolated
================================================
FILE: SpecAugment/sparse_image_warp_pytorch.py
================================================
# Copyright 2019 RnD at Spoon Radio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# import torch
# import numpy as np
# from torch.autograd import Variable
# import librosa
import random
import numpy as np
# import scipy.signal
import torch
# import torchaudio
# from torchaudio import transforms
# import math
# from torch.utils.data import DataLoader
# from torch.utils.data import Dataset
def time_warp(spec, W=5):
spec = spec.view(1, spec.shape[0], spec.shape[1])
num_rows = spec.shape[1]
spec_len = spec.shape[2]
y = num_rows // 2
horizontal_line_at_ctr = spec[0][y]
assert len(horizontal_line_at_ctr) == spec_len
point_to_warp = horizontal_line_at_ctr[random.randrange(W, spec_len - W)]
assert isinstance(point_to_warp, torch.Tensor)
# Uniform distribution from (0,W) with chance to be up to W negative
dist_to_warp = random.randrange(-W, W)
src_pts, dest_pts = torch.tensor([[[y, point_to_warp]]]), torch.tensor([[[y, point_to_warp + dist_to_warp]]])
warped_spectro, dense_flows = SparseImageWarp.sparse_image_warp(spec, src_pts, dest_pts)
return warped_spectro.squeeze(3)
def freq_mask(spec, F=15, num_masks=1, replace_with_zero=False):
cloned = spec.clone()
num_mel_channels = cloned.shape[1]
for i in range(0, num_masks):
f = random.randrange(0, F)
f_zero = random.randrange(0, num_mel_channels - f)
# avoids randrange error if values are equal and range is empty
if (f_zero == f_zero + f): return cloned
mask_end = random.randrange(f_zero, f_zero + f)
if (replace_with_zero):
cloned[0][f_zero:mask_end] = 0
else:
cloned[0][f_zero:mask_end] = cloned.mean()
return cloned
def time_mask(spec, T=15, num_masks=1, replace_with_zero=False):
cloned = spec.clone()
len_spectro = cloned.shape[2]
for i in range(0, num_masks):
t = random.randrange(0, T)
t_zero = random.randrange(0, len_spectro - t)
# avoids randrange error if values are equal and range is empty
if (t_zero == t_zero + t): return cloned
mask_end = random.randrange(t_zero, t_zero + t)
if (replace_with_zero):
cloned[0][:, t_zero:mask_end] = 0
else:
cloned[0][:, t_zero:mask_end] = cloned.mean()
return cloned
def sparse_image_warp(img_tensor,
source_control_point_locations,
dest_control_point_locations,
interpolation_order=2,
regularization_weight=0.0,
num_boundaries_points=0):
control_point_flows = (dest_control_point_locations - source_control_point_locations)
batch_size, image_height, image_width = img_tensor.shape
grid_locations = get_grid_locations(image_height, image_width)
flattened_grid_locations = torch.tensor(flatten_grid_locations(grid_locations, image_height, image_width))
flattened_flows = interpolate_spline(
dest_control_point_locations,
control_point_flows,
flattened_grid_locations,
interpolation_order,
regularization_weight)
dense_flows = create_dense_flows(flattened_flows, batch_size, image_height, image_width)
warped_image = dense_image_warp(img_tensor, dense_flows)
return warped_image, dense_flows
def get_grid_locations(image_height, image_width):
"""Wrapper for np.meshgrid."""
y_range = np.linspace(0, image_height - 1, image_height)
x_range = np.linspace(0, image_width - 1, image_width)
y_grid, x_grid = np.meshgrid(y_range, x_range, indexing='ij')
return np.stack((y_grid, x_grid), -1)
def flatten_grid_locations(grid_locations, image_height, image_width):
return np.reshape(grid_locations, [image_height * image_width, 2])
def create_dense_flows(flattened_flows, batch_size, image_height, image_width):
# possibly .view
return torch.reshape(flattened_flows, [batch_size, image_height, image_width, 2])
def interpolate_spline(train_points, train_values, query_points, order, regularization_weight=0.0, ):
# First, fit the spline to the observed data.
w, v = solve_interpolation(train_points, train_values, order, regularization_weight)
# Then, evaluate the spline at the query locations.
query_values = apply_interpolation(query_points, train_points, w, v, order)
return query_values
def solve_interpolation(train_points, train_values, order, regularization_weight):
b, n, d = train_points.shape
k = train_values.shape[-1]
# First, rename variables so that the notation (c, f, w, v, A, B, etc.)
# follows https://en.wikipedia.org/wiki/Polyharmonic_spline.
# To account for python style guidelines we use
# matrix_a for A and matrix_b for B.
c = train_points
f = train_values.float()
matrix_a = phi(cross_squared_distance_matrix(c, c), order).unsqueeze(0) # [b, n, n]
# if regularization_weight > 0:
# batch_identity_matrix = array_ops.expand_dims(
# linalg_ops.eye(n, dtype=c.dtype), 0)
# matrix_a += regularization_weight * batch_identity_matrix
# Append ones to the feature values for the bias term in the linear model.
ones = torch.ones(1, dtype=train_points.dtype).view([-1, 1, 1])
matrix_b = torch.cat((c, ones), 2).float() # [b, n, d + 1]
# [b, n + d + 1, n]
left_block = torch.cat((matrix_a, torch.transpose(matrix_b, 2, 1)), 1)
num_b_cols = matrix_b.shape[2] # d + 1
# In Tensorflow, zeros are used here. Pytorch gesv fails with zeros for some reason we don't understand.
# So instead we use very tiny randn values (variance of one, zero mean) on one side of our multiplication.
lhs_zeros = torch.randn((b, num_b_cols, num_b_cols)) / 1e10
right_block = torch.cat((matrix_b, lhs_zeros),
1) # [b, n + d + 1, d + 1]
lhs = torch.cat((left_block, right_block),
2) # [b, n + d + 1, n + d + 1]
rhs_zeros = torch.zeros((b, d + 1, k), dtype=train_points.dtype).float()
rhs = torch.cat((f, rhs_zeros), 1) # [b, n + d + 1, k]
# Then, solve the linear system and unpack the results.
X, LU = torch.solve(rhs, lhs)
w = X[:, :n, :]
v = X[:, n:, :]
return w, v
def cross_squared_distance_matrix(x, y):
"""Pairwise squared distance between two (batch) matrices' rows (2nd dim).
Computes the pairwise distances between rows of x and rows of y
Args:
x: [batch_size, n, d] float `Tensor`
y: [batch_size, m, d] float `Tensor`
Returns:
squared_dists: [batch_size, n, m] float `Tensor`, where
squared_dists[b,i,j] = ||x[b,i,:] - y[b,j,:]||^2
"""
x_norm_squared = torch.sum(torch.mul(x, x))
y_norm_squared = torch.sum(torch.mul(y, y))
x_y_transpose = torch.matmul(x.squeeze(0), y.squeeze(0).transpose(0, 1))
# squared_dists[b,i,j] = ||x_bi - y_bj||^2 = x_bi'x_bi- 2x_bi'x_bj + x_bj'x_bj
squared_dists = x_norm_squared - 2 * x_y_transpose + y_norm_squared
return squared_dists.float()
def phi(r, order):
"""Coordinate-wise nonlinearity used to define the order of the interpolation.
See https://en.wikipedia.org/wiki/Polyharmonic_spline for the definition.
Args:
r: input op
order: interpolation order
Returns:
phi_k evaluated coordinate-wise on r, for k = r
"""
EPSILON = torch.tensor(1e-10)
# using EPSILON prevents log(0), sqrt0), etc.
# sqrt(0) is well-defined, but its gradient is not
if order == 1:
r = torch.max(r, EPSILON)
r = torch.sqrt(r)
return r
elif order == 2:
return 0.5 * r * torch.log(torch.max(r, EPSILON))
elif order == 4:
return 0.5 * torch.square(r) * torch.log(torch.max(r, EPSILON))
elif order % 2 == 0:
r = torch.max(r, EPSILON)
return 0.5 * torch.pow(r, 0.5 * order) * torch.log(r)
else:
r = torch.max(r, EPSILON)
return torch.pow(r, 0.5 * order)
def apply_interpolation(query_points, train_points, w, v, order):
"""Apply polyharmonic interpolation model to data.
Given coefficients w and v for the interpolation model, we evaluate
interpolated function values at query_points.
Args:
query_points: `[b, m, d]` x values to evaluate the interpolation at
train_points: `[b, n, d]` x values that act as the interpolation centers
( the c variables in the wikipedia article)
w: `[b, n, k]` weights on each interpolation center
v: `[b, d, k]` weights on each input dimension
order: order of the interpolation
Returns:
Polyharmonic interpolation evaluated at points defined in query_points.
"""
query_points = query_points.unsqueeze(0)
# First, compute the contribution from the rbf term.
pairwise_dists = cross_squared_distance_matrix(query_points.float(), train_points.float())
phi_pairwise_dists = phi(pairwise_dists, order)
rbf_term = torch.matmul(phi_pairwise_dists, w)
# Then, compute the contribution from the linear term.
# Pad query_points with ones, for the bias term in the linear model.
ones = torch.ones_like(query_points[..., :1])
query_points_pad = torch.cat((
query_points,
ones
), 2).float()
linear_term = torch.matmul(query_points_pad, v)
return rbf_term + linear_term
def dense_image_warp(image, flow):
"""Image warping using per-pixel flow vectors.
Apply a non-linear warp to the image, where the warp is specified by a dense
flow field of offset vectors that define the correspondences of pixel values
in the output image back to locations in the source image. Specifically, the
pixel value at output[b, j, i, c] is
images[b, j - flow[b, j, i, 0], i - flow[b, j, i, 1], c].
The locations specified by this formula do not necessarily map to an int
index. Therefore, the pixel value is obtained by bilinear
interpolation of the 4 nearest pixels around
(b, j - flow[b, j, i, 0], i - flow[b, j, i, 1]). For locations outside
of the image, we use the nearest pixel values at the image boundary.
Args:
image: 4-D float `Tensor` with shape `[batch, height, width, channels]`.
flow: A 4-D float `Tensor` with shape `[batch, height, width, 2]`.
name: A name for the operation (optional).
Note that image and flow can be of type tf.half, tf.float32, or tf.float64,
and do not necessarily have to be the same type.
Returns:
A 4-D float `Tensor` with shape`[batch, height, width, channels]`
and same type as input image.
Raises:
ValueError: if height < 2 or width < 2 or the inputs have the wrong number
of dimensions.
"""
image = image.unsqueeze(3) # add a single channel dimension to image tensor
batch_size, height, width, channels = image.shape
# The flow is defined on the image grid. Turn the flow into a list of query
# points in the grid space.
grid_x, grid_y = torch.meshgrid(
torch.arange(width), torch.arange(height))
stacked_grid = torch.stack((grid_y, grid_x), dim=2).float()
batched_grid = stacked_grid.unsqueeze(-1).permute(3, 1, 0, 2)
query_points_on_grid = batched_grid - flow
query_points_flattened = torch.reshape(query_points_on_grid,
[batch_size, height * width, 2])
# Compute values at the query points, then reshape the result back to the
# image grid.
interpolated = interpolate_bilinear(image, query_points_flattened)
interpolated = torch.reshape(interpolated,
[batch_size, height, width, channels])
return interpolated
def interpolate_bilinear(grid,
query_points,
name='interpolate_bilinear',
indexing='ij'):
"""Similar to Matlab's interp2 function.
Finds values for query points on a grid using bilinear interpolation.
Args:
grid: a 4-D float `Tensor` of shape `[batch, height, width, channels]`.
query_points: a 3-D float `Tensor` of N points with shape `[batch, N, 2]`.
name: a name for the operation (optional).
indexing: whether the query points are specified as row and column (ij),
or Cartesian coordinates (xy).
Returns:
values: a 3-D `Tensor` with shape `[batch, N, channels]`
Raises:
ValueError: if the indexing mode is invalid, or if the shape of the inputs
invalid.
"""
if indexing != 'ij' and indexing != 'xy':
raise ValueError('Indexing mode must be \'ij\' or \'xy\'')
shape = grid.shape
if len(shape) != 4:
msg = 'Grid must be 4 dimensional. Received size: '
raise ValueError(msg + str(grid.shape))
batch_size, height, width, channels = grid.shape
shape = [batch_size, height, width, channels]
query_type = query_points.dtype
grid_type = grid.dtype
num_queries = query_points.shape[1]
alphas = []
floors = []
ceils = []
index_order = [0, 1] if indexing == 'ij' else [1, 0]
unstacked_query_points = query_points.unbind(2)
for dim in index_order:
queries = unstacked_query_points[dim]
size_in_indexing_dimension = shape[dim + 1]
# max_floor is size_in_indexing_dimension - 2 so that max_floor + 1
# is still a valid index into the grid.
max_floor = torch.tensor(size_in_indexing_dimension - 2, dtype=query_type)
min_floor = torch.tensor(0.0, dtype=query_type)
maxx = torch.max(min_floor, torch.floor(queries))
floor = torch.min(maxx, max_floor)
int_floor = floor.long()
floors.append(int_floor)
ceil = int_floor + 1
ceils.append(ceil)
# alpha has the same type as the grid, as we will directly use alpha
# when taking linear combinations of pixel values from the image.
alpha = torch.tensor(queries - floor, dtype=grid_type)
min_alpha = torch.tensor(0.0, dtype=grid_type)
max_alpha = torch.tensor(1.0, dtype=grid_type)
alpha = torch.min(torch.max(min_alpha, alpha), max_alpha)
# Expand alpha to [b, n, 1] so we can use broadcasting
# (since the alpha values don't depend on the channel).
alpha = torch.unsqueeze(alpha, 2)
alphas.append(alpha)
flattened_grid = torch.reshape(
grid, [batch_size * height * width, channels])
batch_offsets = torch.reshape(
torch.arange(batch_size) * height * width, [batch_size, 1])
# This wraps array_ops.gather. We reshape the image data such that the
# batch, y, and x coordinates are pulled into the first dimension.
# Then we gather. Finally, we reshape the output back. It's possible this
# code would be made simpler by using array_ops.gather_nd.
def gather(y_coords, x_coords, name):
linear_coordinates = batch_offsets + y_coords * width + x_coords
gathered_values = torch.gather(flattened_grid.t(), 1, linear_coordinates)
return torch.reshape(gathered_values,
[batch_size, num_queries, channels])
# grab the pixel values in the 4 corners around each query point
top_left = gather(floors[0], floors[1], 'top_left')
top_right = gather(floors[0], ceils[1], 'top_right')
bottom_left = gather(ceils[0], floors[1], 'bottom_left')
bottom_right = gather(ceils[0], ceils[1], 'bottom_right')
interp_top = alphas[1] * (top_right - top_left) + top_left
interp_bottom = alphas[1] * (bottom_right - bottom_left) + bottom_left
interp = alphas[0] * (interp_bottom - interp_top) + interp_top
return interp
================================================
FILE: SpecAugment/spec_augment_pytorch.py
================================================
# Copyright 2019 RnD at Spoon Radio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""SpecAugment Implementation for Tensorflow.
Related paper : https://arxiv.org/pdf/1904.08779.pdf
In this paper, show summarized parameters by each open datasets in Tabel 1.
-----------------------------------------
Policy | W | F | m_F | T | p | m_T
-----------------------------------------
None | 0 | 0 | - | 0 | - | -
-----------------------------------------
LB | 80 | 27 | 1 | 100 | 1.0 | 1
-----------------------------------------
LD | 80 | 27 | 2 | 100 | 1.0 | 2
-----------------------------------------
SM | 40 | 15 | 2 | 70 | 0.2 | 2
-----------------------------------------
SS | 40 | 27 | 2 | 70 | 0.2 | 2
-----------------------------------------
LB : LibriSpeech basic
LD : LibriSpeech double
SM : Switchboard mild
SS : Switchboard strong
"""
import librosa
import librosa.display
import numpy as np
import random
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from SpecAugment.sparse_image_warp_pytorch import sparse_image_warp
import torch
def time_warp(spec, W=5):
num_rows = spec.shape[1]
spec_len = spec.shape[2]
y = num_rows // 2
horizontal_line_at_ctr = spec[0][y]
# assert len(horizontal_line_at_ctr) == spec_len
point_to_warp = horizontal_line_at_ctr[random.randrange(W, spec_len-W)]
# assert isinstance(point_to_warp, torch.Tensor)
# Uniform distribution from (0,W) with chance to be up to W negative
dist_to_warp = random.randrange(-W, W)
src_pts = torch.tensor([[[y, point_to_warp]]])
dest_pts = torch.tensor([[[y, point_to_warp + dist_to_warp]]])
warped_spectro, dense_flows = sparse_image_warp(spec, src_pts, dest_pts)
return warped_spectro.squeeze(3)
def spec_augment(mel_spectrogram, time_warping_para=80, frequency_masking_para=27,
time_masking_para=100, frequency_mask_num=1, time_mask_num=1):
"""Spec augmentation Calculation Function.
'SpecAugment' have 3 steps for audio data augmentation.
first step is time warping using Tensorflow's image_sparse_warp function.
Second step is frequency masking, last step is time masking.
# Arguments:
mel_spectrogram(numpy array): audio file path of you want to warping and masking.
time_warping_para(float): Augmentation parameter, "time warp parameter W".
If none, default = 80 for LibriSpeech.
frequency_masking_para(float): Augmentation parameter, "frequency mask parameter F"
If none, default = 100 for LibriSpeech.
time_masking_para(float): Augmentation parameter, "time mask parameter T"
If none, default = 27 for LibriSpeech.
frequency_mask_num(float): number of frequency masking lines, "m_F".
If none, default = 1 for LibriSpeech.
time_mask_num(float): number of time masking lines, "m_T".
If none, default = 1 for LibriSpeech.
# Returns
mel_spectrogram(numpy array): warped and masked mel spectrogram.
"""
v = mel_spectrogram.shape[1]
tau = mel_spectrogram.shape[2]
# Step 1 : Time warping
warped_mel_spectrogram = time_warp(mel_spectrogram, W=time_warping_para)
# Step 2 : Frequency masking
for i in range(frequency_mask_num):
f = np.random.uniform(low=0.0, high=frequency_masking_para)
f = int(f)
f0 = random.randint(0, v-f)
warped_mel_spectrogram[:, f0:f0+f, :] = 0
# Step 3 : Time masking
for i in range(time_mask_num):
t = np.random.uniform(low=0.0, high=time_masking_para)
t = int(t)
t0 = random.randint(0, tau-t)
warped_mel_spectrogram[:, :, t0:t0+t] = 0
return warped_mel_spectrogram
def visualization_spectrogram(mel_spectrogram, title):
"""visualizing result of SpecAugment
# Arguments:
mel_spectrogram(ndarray): mel_spectrogram to visualize.
title(String): plot figure's title
"""
# Show mel-spectrogram using librosa's specshow.
plt.figure(figsize=(10, 4))
librosa.display.specshow(librosa.power_to_db(mel_spectrogram[0, :, :], ref=np.max), y_axis='mel', fmax=8000, x_axis='time')
# plt.colorbar(format='%+2.0f dB')
plt.title(title)
plt.tight_layout()
plt.show()
================================================
FILE: SpecAugment/spec_augment_tensorflow.py
================================================
# Copyright 2019 RnD at Spoon Radio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""SpecAugment Implementation for Tensorflow.
Related paper : https://arxiv.org/pdf/1904.08779.pdf
In this paper, show summarized parameters by each open datasets in Tabel 1.
-----------------------------------------
Policy | W | F | m_F | T | p | m_T
-----------------------------------------
None | 0 | 0 | - | 0 | - | -
-----------------------------------------
LB | 80 | 27 | 1 | 100 | 1.0 | 1
-----------------------------------------
LD | 80 | 27 | 2 | 100 | 1.0 | 2
-----------------------------------------
SM | 40 | 15 | 2 | 70 | 0.2 | 2
-----------------------------------------
SS | 40 | 27 | 2 | 70 | 0.2 | 2
-----------------------------------------
LB : LibriSpeech basic
LD : LibriSpeech double
SM : Switchboard mild
SS : Switchboard strong
"""
import librosa
import librosa.display
import tensorflow as tf
from tensorflow_addons.image import sparse_image_warp
import numpy as np
import matplotlib.pyplot as plt
def sparse_warp(mel_spectrogram, time_warping_para=80):
"""Spec augmentation Calculation Function.
'SpecAugment' have 3 steps for audio data augmentation.
first step is time warping using Tensorflow's image_sparse_warp function.
Second step is frequency masking, last step is time masking.
# Arguments:
mel_spectrogram(numpy array): audio file path of you want to warping and masking.
time_warping_para(float): Augmentation parameter, "time warp parameter W".
If none, default = 80 for LibriSpeech.
# Returns
mel_spectrogram(numpy array): warped and masked mel spectrogram.
"""
fbank_size = tf.shape(mel_spectrogram)
n, v = fbank_size[1], fbank_size[2]
# Step 1 : Time warping
# Image warping control point setting.
# Source
pt = tf.random.uniform([], time_warping_para, n-time_warping_para, tf.int32) # radnom point along the time axis
src_ctr_pt_freq = tf.range(v // 2) # control points on freq-axis
src_ctr_pt_time = tf.ones_like(src_ctr_pt_freq) * pt # control points on time-axis
src_ctr_pts = tf.stack((src_ctr_pt_time, src_ctr_pt_freq), -1)
src_ctr_pts = tf.cast(src_ctr_pts, dtype=tf.float32)
# Destination
w = tf.random.uniform([], -time_warping_para, time_warping_para, tf.int32) # distance
dest_ctr_pt_freq = src_ctr_pt_freq
dest_ctr_pt_time = src_ctr_pt_time + w
dest_ctr_pts = tf.stack((dest_ctr_pt_time, dest_ctr_pt_freq), -1)
dest_ctr_pts = tf.cast(dest_ctr_pts, dtype=tf.float32)
# warp
source_control_point_locations = tf.expand_dims(src_ctr_pts, 0) # (1, v//2, 2)
dest_control_point_locations = tf.expand_dims(dest_ctr_pts, 0) # (1, v//2, 2)
warped_image, _ = sparse_image_warp(mel_spectrogram,
source_control_point_locations,
dest_control_point_locations)
return warped_image
def frequency_masking(mel_spectrogram, v, frequency_masking_para=27, frequency_mask_num=2):
"""Spec augmentation Calculation Function.
'SpecAugment' have 3 steps for audio data augmentation.
first step is time warping using Tensorflow's image_sparse_warp function.
Second step is frequency masking, last step is time masking.
# Arguments:
mel_spectrogram(numpy array): audio file path of you want to warping and masking.
frequency_masking_para(float): Augmentation parameter, "frequency mask parameter F"
If none, default = 100 for LibriSpeech.
frequency_mask_num(float): number of frequency masking lines, "m_F".
If none, default = 1 for LibriSpeech.
# Returns
mel_spectrogram(numpy array): warped and masked mel spectrogram.
"""
# Step 2 : Frequency masking
fbank_size = tf.shape(mel_spectrogram)
n, v = fbank_size[1], fbank_size[2]
for i in range(frequency_mask_num):
f = tf.random.uniform([], minval=0, maxval=frequency_masking_para, dtype=tf.int32)
v = tf.cast(v, dtype=tf.int32)
f0 = tf.random.uniform([], minval=0, maxval=v-f, dtype=tf.int32)
# warped_mel_spectrogram[f0:f0 + f, :] = 0
mask = tf.concat((tf.ones(shape=(1, n, v - f0 - f, 1)),
tf.zeros(shape=(1, n, f, 1)),
tf.ones(shape=(1, n, f0, 1)),
), 2)
mel_spectrogram = mel_spectrogram * mask
return tf.cast(mel_spectrogram, dtype=tf.float32)
def time_masking(mel_spectrogram, tau, time_masking_para=100, time_mask_num=2):
"""Spec augmentation Calculation Function.
'SpecAugment' have 3 steps for audio data augmentation.
first step is time warping using Tensorflow's image_sparse_warp function.
Second step is frequency masking, last step is time masking.
# Arguments:
mel_spectrogram(numpy array): audio file path of you want to warping and masking.
time_masking_para(float): Augmentation parameter, "time mask parameter T"
If none, default = 27 for LibriSpeech.
time_mask_num(float): number of time masking lines, "m_T".
If none, default = 1 for LibriSpeech.
# Returns
mel_spectrogram(numpy array): warped and masked mel spectrogram.
"""
fbank_size = tf.shape(mel_spectrogram)
n, v = fbank_size[1], fbank_size[2]
# Step 3 : Time masking
for i in range(time_mask_num):
t = tf.random.uniform([], minval=0, maxval=time_masking_para, dtype=tf.int32)
t0 = tf.random.uniform([], minval=0, maxval=tau-t, dtype=tf.int32)
# mel_spectrogram[:, t0:t0 + t] = 0
mask = tf.concat((tf.ones(shape=(1, n-t0-t, v, 1)),
tf.zeros(shape=(1, t, v, 1)),
tf.ones(shape=(1, t0, v, 1)),
), 1)
mel_spectrogram = mel_spectrogram * mask
return tf.cast(mel_spectrogram, dtype=tf.float32)
def spec_augment(mel_spectrogram):
v = mel_spectrogram.shape[0]
tau = mel_spectrogram.shape[1]
warped_mel_spectrogram = sparse_warp(mel_spectrogram)
warped_frequency_spectrogram = frequency_masking(warped_mel_spectrogram, v=v)
warped_frequency_time_sepctrogram = time_masking(warped_frequency_spectrogram, tau=tau)
return warped_frequency_time_sepctrogram
def visualization_spectrogram(mel_spectrogram, title):
"""visualizing first one result of SpecAugment
# Arguments:
mel_spectrogram(ndarray): mel_spectrogram to visualize.
title(String): plot figure's title
"""
# Show mel-spectrogram using librosa's specshow.
plt.figure(figsize=(10, 4))
librosa.display.specshow(librosa.power_to_db(mel_spectrogram[0, :, :, 0], ref=np.max), y_axis='mel', fmax=8000, x_axis='time')
plt.title(title)
plt.tight_layout()
plt.show()
def visualization_tensor_spectrogram(mel_spectrogram, title):
"""visualizing first one result of SpecAugment
# Arguments:
mel_spectrogram(ndarray): mel_spectrogram to visualize.
title(String): plot figure's title
"""
# Show mel-spectrogram using librosa's specshow.
plt.figure(figsize=(10, 4))
librosa.display.specshow(librosa.power_to_db(mel_spectrogram[0, :, :, 0], ref=np.max), y_axis='mel', fmax=8000, x_axis='time')
# plt.colorbar(format='%+2.0f dB')
plt.title(title)
plt.tight_layout()
plt.show()
================================================
FILE: requirements.txt
================================================
librosa
matplotlib
numpy
================================================
FILE: setup.cfg
================================================
[metadata]
description-file = README.md
================================================
FILE: setup.py
================================================
from setuptools import setup, find_packages
setup(
name='SpecAugment',
version='1.2.3',
description='A implementation of "SpecAugment"',
url = 'https://github.com/shelling203/SpecAugment',
packages = find_packages(exclude = ['docs', 'tests*']),
install_requires=['tensorflow', 'librosa', 'matplotlib', 'torch'], #external packages as dependencies
)
================================================
FILE: tests/spec_augment_test_TF.py
================================================
# Copyright 2019 RnD at Spoon Radio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""SpecAugment test"""
import argparse
import librosa
from SpecAugment import spec_augment_tensorflow
import os, sys
import numpy as np
# sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
parser = argparse.ArgumentParser(description='Spec Augment')
parser.add_argument('--audio-path', default='../data/61-70968-0002.wav',
help='The audio file.')
parser.add_argument('--time-warp-para', default=80,
help='time warp parameter W')
parser.add_argument('--frequency-mask-para', default=100,
help='frequency mask parameter F')
parser.add_argument('--time-mask-para', default=27,
help='time mask parameter T')
parser.add_argument('--masking-line-number', default=1,
help='masking line number')
args = parser.parse_args()
audio_path = args.audio_path
time_warping_para = args.time_warp_para
time_masking_para = args.frequency_mask_para
frequency_masking_para = args.time_mask_para
masking_line_number = args.masking_line_number
if __name__ == "__main__":
# Step 0 : load audio file, extract mel spectrogram
audio, sampling_rate = librosa.load(audio_path)
mel_spectrogram = librosa.feature.melspectrogram(y=audio,
sr=sampling_rate,
n_mels=256,
hop_length=128,
fmax=8000)
# reshape spectrogram shape to [batch_size, time, frequency, 1]
shape = mel_spectrogram.shape
mel_spectrogram = np.reshape(mel_spectrogram, (-1, shape[0], shape[1], 1))
# Show Raw mel-spectrogram
spec_augment_tensorflow.visualization_spectrogram(mel_spectrogram=mel_spectrogram,
title="Raw Mel Spectrogram")
# Show time warped & masked spectrogram
spec_augment_tensorflow.visualization_tensor_spectrogram(mel_spectrogram=spec_augment_tensorflow.spec_augment(mel_spectrogram),
title="tensorflow Warped & Masked Mel Spectrogram")
================================================
FILE: tests/spec_augment_test_pytorch.py
================================================
# Copyright 2019 RnD at Spoon Radio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""SpecAugment test"""
import argparse
import librosa
import numpy as np
import torch
from SpecAugment import spec_augment_pytorch
import os, sys
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
parser = argparse.ArgumentParser(description='Spec Augment')
parser.add_argument('--audio-path', default='../data/61-70968-0002.wav',
help='The audio file.')
parser.add_argument('--time-warp-para', default=80,
help='time warp parameter W')
parser.add_argument('--frequency-mask-para', default=100,
help='frequency mask parameter F')
parser.add_argument('--time-mask-para', default=27,
help='time mask parameter T')
parser.add_argument('--masking-line-number', default=1,
help='masking line number')
args = parser.parse_args()
audio_path = args.audio_path
time_warping_para = args.time_warp_para
time_masking_para = args.frequency_mask_para
frequency_masking_para = args.time_mask_para
masking_line_number = args.masking_line_number
if __name__ == "__main__":
# Step 0 : load audio file, extract mel spectrogram
audio, sampling_rate = librosa.load(audio_path)
mel_spectrogram = librosa.feature.melspectrogram(y=audio,
sr=sampling_rate,
n_mels=256,
hop_length=128,
fmax=8000)
# reshape spectrogram shape to [batch_size, time, frequency]
shape = mel_spectrogram.shape
mel_spectrogram = np.reshape(mel_spectrogram, (-1, shape[0], shape[1]))
mel_spectrogram = torch.from_numpy(mel_spectrogram)
# Show Raw mel-spectrogram
spec_augment_pytorch.visualization_spectrogram(mel_spectrogram=mel_spectrogram,
title="Raw Mel Spectrogram")
# Calculate SpecAugment pytorch
warped_masked_spectrogram = spec_augment_pytorch.spec_augment(mel_spectrogram=mel_spectrogram)
# Show time warped & masked spectrogram
spec_augment_pytorch.visualization_spectrogram(mel_spectrogram=warped_masked_spectrogram,
title="pytorch Warped & Masked Mel Spectrogram")