Skip to content

Instantly share code, notes, and snippets.

@keunwoochoi
Created November 18, 2016 04:19
Show Gist options
  • Save keunwoochoi/1ca999ddabb42c57e3e143ba0f359683 to your computer and use it in GitHub Desktop.
Save keunwoochoi/1ca999ddabb42c57e3e143ba0f359683 to your computer and use it in GitHub Desktop.
class ParametricMel(Layer):
def __init__(self, n_mels, n_freqs, sr, scale=24., init='mel', **kwargs):
self.supports_masking = True
self.scale = scale # scaling
self.n_mels = n_mels
if init == 'mel':
self.means_init = np.array(_mel_frequencies(n_mels, fmin=0.0, fmax=sr/2), dtype='float32')
stds = self.means_init[1:] - self.means_init[:-1]
self.stds_init = 0.3 * np.hstack((stds[0:1], stds[:])) # 0.3: kinda make sense by the resulting images..
self.center_freqs_init = [float(i)*sr/2/(n_freqs-1) for i in range(n_freqs)] # dft frequencies
super(ParametricMel, self).__init__(**kwargs)
def build(self, input_shape):
self.means = K.variable(self.means_init,
name='{}_means'.format(self.name))
self.stds = K.variable(self.stds_init,
name='{}_stds'.format(self.name))
self.center_freqs_init = np.array(self.center_freqs_init)[np.newaxis, :] # (1, n_freq)
self.center_freqs_init = np.tile(self.center_freqs_init, (self.n_mels, 1)) # (n_mels, n_freq)
self.center_freqs = K.variable(self.center_freqs_init,
name='{}_center_freqs'.format(self.name))
self.trainable_weights = [self.means, self.stds] # [self.means, self.stds]
self.n_freq = input_shape[1]
self.n_time = input_shape[2]
print '--build--'
def get_output_shape_for(self, input_shape):
return (input_shape[0], self.n_mels, input_shape[2])
def call(self, x, mask=None):
means = K.expand_dims(self.means, dim=1)
stds = K.expand_dims(self.stds, dim=1)
freq_to_mel = (self.scale * K.exp(-1. * K.square(self.center_freqs - means) \
/ (2. * K.square(stds)))) \
/ (np.sqrt(2. * np.pi).astype('float32') * stds) # (n_mel, n_freq)
out = K.dot(freq_to_mel, x) # (n_mel, None, n_time)
return K.permute_dimensions(out, (1, 0, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment