layers.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # BSD 3-Clause License
  2. # Copyright (c) 2018-2020, NVIDIA Corporation
  3. # All rights reserved.
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright notice, this
  7. # list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright notice,
  9. # this list of conditions and the following disclaimer in the documentation
  10. # and/or other materials provided with the distribution.
  11. # * Neither the name of the copyright holder nor the names of its
  12. # contributors may be used to endorse or promote products derived from
  13. # this software without specific prior written permission.
  14. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. """https://github.com/NVIDIA/tacotron2"""
  25. import torch
  26. from librosa.filters import mel as librosa_mel_fn
  27. class LinearNorm(torch.nn.Module):
  28. def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'):
  29. super(LinearNorm, self).__init__()
  30. self.linear_layer = torch.nn.Linear(in_dim, out_dim, bias=bias)
  31. torch.nn.init.xavier_uniform_(
  32. self.linear_layer.weight,
  33. gain=torch.nn.init.calculate_gain(w_init_gain))
  34. def forward(self, x):
  35. return self.linear_layer(x)
  36. class ConvNorm(torch.nn.Module):
  37. def __init__(self, in_channels, out_channels, kernel_size=1, stride=1,
  38. padding=None, dilation=1, bias=True, w_init_gain='linear'):
  39. super(ConvNorm, self).__init__()
  40. if padding is None:
  41. assert(kernel_size % 2 == 1)
  42. padding = int(dilation * (kernel_size - 1) / 2)
  43. self.conv = torch.nn.Conv1d(in_channels, out_channels,
  44. kernel_size=kernel_size, stride=stride,
  45. padding=padding, dilation=dilation,
  46. bias=bias)
  47. torch.nn.init.xavier_uniform_(
  48. self.conv.weight, gain=torch.nn.init.calculate_gain(w_init_gain))
  49. def forward(self, signal):
  50. conv_signal = self.conv(signal)
  51. return conv_signal