1""" 2/* Copyright (c) 2023 Amazon 3 Written by Jan Buethe */ 4/* 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions 7 are met: 8 9 - Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 12 - Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*/ 28""" 29 30import torch 31from torch import nn 32import torch.nn.functional as F 33 34from utils.complexity import _conv1d_flop_count 35 36class NoiseShaper(nn.Module): 37 38 def __init__(self, 39 feature_dim, 40 frame_size=160 41 ): 42 """ 43 44 Parameters: 45 ----------- 46 47 feature_dim : int 48 dimension of input features 49 50 frame_size : int 51 frame size 52 53 """ 54 55 super().__init__() 56 57 self.feature_dim = feature_dim 58 self.frame_size = frame_size 59 60 # feature transform 61 self.feature_alpha1 = nn.Conv1d(self.feature_dim, frame_size, 2) 62 self.feature_alpha2 = nn.Conv1d(frame_size, frame_size, 2) 63 64 65 def flop_count(self, rate): 66 67 frame_rate = rate / self.frame_size 68 69 shape_flops = sum([_conv1d_flop_count(x, frame_rate) for x in (self.feature_alpha1, self.feature_alpha2)]) + 11 * frame_rate * self.frame_size 70 71 return shape_flops 72 73 74 def forward(self, features): 75 """ creates temporally shaped noise 76 77 78 Parameters: 79 ----------- 80 features : torch.tensor 81 frame-wise features of shape (batch_size, num_frames, feature_dim) 82 83 """ 84 85 batch_size = features.size(0) 86 num_frames = features.size(1) 87 frame_size = self.frame_size 88 num_samples = num_frames * frame_size 89 90 # feature path 91 f = F.pad(features.permute(0, 2, 1), [1, 0]) 92 alpha = F.leaky_relu(self.feature_alpha1(f), 0.2) 93 alpha = torch.exp(self.feature_alpha2(F.pad(alpha, [1, 0]))) 94 alpha = alpha.permute(0, 2, 1) 95 96 # signal generation 97 y = torch.randn((batch_size, num_frames, frame_size), dtype=features.dtype, device=features.device) 98 y = alpha * y 99 100 return y.reshape(batch_size, 1, num_samples) 101