Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahn Heymann committed Jan 18, 2016
1 parent 14e917b commit 2830fa8
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 148 deletions.
11 changes: 6 additions & 5 deletions beamform.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import argparse
import os

import numpy as np
from chainer import Variable
from chainer import cuda
from chainer import serializers
from tqdm import tqdm
import os
from chainer import Variable
from fgnt.utils import mkdir_p

from chime_data import gen_flist_simu, \
gen_flist_real, get_audio_data, get_audio_data_with_context
from nn_models import BLSTMMaskEstimator, SimpleFWMaskEstimator
from fgnt.signal_processing import audiowrite, stft, istft
from fgnt.beamforming import gev_wrapper_on_masks
from fgnt.signal_processing import audiowrite, stft, istft
from fgnt.utils import Timer
from fgnt.utils import mkdir_p
from nn_models import BLSTMMaskEstimator, SimpleFWMaskEstimator

parser = argparse.ArgumentParser(description='NN GEV beamforming')
parser.add_argument('flist',
Expand Down
17 changes: 9 additions & 8 deletions chime_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ def gen_flist_simu(chime_data_dir, stage, ext=False):
else:
isolated_dir = 'isolated'
flist = [os.path.join(
chime_data_dir, 'audio', '16kHz', isolated_dir,
'{}05_{}_{}'.format(stage, a['environment'].lower(), 'simu'),
'{}_{}_{}'.format(a['speaker'], a['wsj_name'], a['environment']))
chime_data_dir, 'audio', '16kHz', isolated_dir,
'{}05_{}_{}'.format(stage, a['environment'].lower(), 'simu'),
'{}_{}_{}'.format(a['speaker'], a['wsj_name'], a['environment']))
for a in annotations]
return flist


def gen_flist_real(chime_data_dir, stage):
with open(os.path.join(
chime_data_dir, 'annotations',
'{}05_{}.json'.format(stage, 'real'))) as fid:
annotations = json.load(fid)
flist_tuples = [(os.path.join(
chime_data_dir, 'audio', '16kHz', 'embedded', a['wavfile']),
chime_data_dir, 'audio', '16kHz', 'embedded', a['wavfile']),
a['start'], a['end'], a['wsj_name']) for a in annotations]
return flist_tuples

Expand All @@ -42,21 +43,21 @@ def get_audio_data(file_template, postfix='', ch_range=range(1, 7)):
audio_data = list()
for ch in ch_range:
audio_data.append(audioread(
file_template + '.CH{}{}.wav'.format(ch, postfix))[None, :])
file_template + '.CH{}{}.wav'.format(ch, postfix))[None, :])
audio_data = np.concatenate(audio_data, axis=0)
audio_data = audio_data.astype(np.float32)
return audio_data


def get_audio_data_with_context(embedded_template, t_start, t_end,
ch_range=range(1, 7)):
start_context = max((t_start-5), 0)
start_context = max((t_start - 5), 0)
context_samples = (t_start - start_context) * 16000
audio_data = list()
for ch in ch_range:
audio_data.append(audioread(
embedded_template + '.CH{}.wav'.format(ch),
offset=start_context, duration=t_end-start_context)[None, :])
embedded_template + '.CH{}.wav'.format(ch),
offset=start_context, duration=t_end - start_context)[None, :])
audio_data = np.concatenate(audio_data, axis=0)
audio_data = audio_data.astype(np.float32)
return audio_data, context_samples
Expand Down
50 changes: 0 additions & 50 deletions fgnt/chainer_addons.py

This file was deleted.

7 changes: 3 additions & 4 deletions fgnt/chainer_extensions/binary_cross_entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class BinaryCrossEntropy(function.Function):

"""Binary cross entropy loss."""

def check_type_forward(self, in_types):
Expand All @@ -15,15 +14,15 @@ def check_type_forward(self, in_types):
def forward(self, inputs):
x, t = inputs
xp = cuda.get_array_module(x)
loss = -xp.mean(t*xp.log2(x+1e-6)
+ (1-t)*xp.log2((1-x)+1e-6))
loss = -xp.mean(t * xp.log2(x + 1e-6)
+ (1 - t) * xp.log2((1 - x) + 1e-6))
return force_array(loss),

def backward(self, inputs, grad_outputs):
x, t = inputs
gloss = grad_outputs[0]
xp = cuda.get_array_module(x)
gx = t / (x + 1e-6) - (1-t)/(1-x+1e-6)
gx = t / (x + 1e-6) - (1 - t) / (1 - x + 1e-6)
gx *= -gloss / (x.size * xp.log(2).astype(numpy.float32))
return gx, None

Expand Down
7 changes: 3 additions & 4 deletions fgnt/chainer_extensions/links/sequence_batch_norm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import numpy

from chainer import link
from fgnt.chainer_extensions.sequenze_batch_normalization import\

from fgnt.chainer_extensions.sequenze_batch_normalization import \
sequence_batch_normalization_function


class SequenceBatchNorm(link.Link):

def __init__(self, size):
super().__init__()
self.add_param('gamma', (size,))
Expand All @@ -25,4 +24,4 @@ def __call__(self, x, **kwargs):
"""

return sequence_batch_normalization_function(x, self.gamma, self.beta)
return sequence_batch_normalization_function(x, self.gamma, self.beta)
9 changes: 4 additions & 5 deletions fgnt/chainer_extensions/links/sequence_linear.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import numpy

from fgnt.chainer_extensions.sequence_linear import sequence_linear_function
from chainer import link
from chainer.functions.noise.dropout import dropout

from fgnt.chainer_extensions import weight_init
from fgnt.chainer_extensions.sequenze_batch_normalization import\
from fgnt.chainer_extensions.sequence_linear import sequence_linear_function
from fgnt.chainer_extensions.sequenze_batch_normalization import \
sequence_batch_normalization_function
from chainer.functions.noise.dropout import dropout


class SequenceLinear(link.Link):

"""Sequence linear layer (fully-connected layer/affine transformation).
This link holds a weight matrix ``W`` and optional a bias vector ``b``.
Expand Down
22 changes: 11 additions & 11 deletions fgnt/chainer_extensions/links/sequence_lstms.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import numpy

from chainer import link
from chainer.functions.array.concat import concat
from chainer.functions.noise.dropout import dropout
from chainer.utils import weight_init

from fgnt.chainer_extensions.sequence_linear \
import sequence_linear_function
from fgnt.chainer_extensions.sequence_lstm import sequence_lstm_function
from fgnt.chainer_extensions.sequenze_batch_normalization import \
sequence_batch_normalization_function
from chainer.utils import weight_init


class SequenceLSTM(link.Link):
Expand Down Expand Up @@ -83,15 +83,15 @@ def __init__(self, in_size, out_size, bias=0, nobias=False,
initial_bias=None, normalized=False, concat=False,
stateful=False):
super(SequenceBLSTM, self).__init__(
lstm_fw=SequenceLSTM(in_size, out_size, bias, nobias,
W_x_fw, W_h_fw, initial_bias,
normalized=normalized,
stateful=stateful),
lstm_bw=SequenceLSTM(in_size, out_size, bias, nobias,
W_x_bw, W_h_bw, initial_bias,
reverse=True,
normalized=normalized,
stateful=stateful)
lstm_fw=SequenceLSTM(in_size, out_size, bias, nobias,
W_x_fw, W_h_fw, initial_bias,
normalized=normalized,
stateful=stateful),
lstm_bw=SequenceLSTM(in_size, out_size, bias, nobias,
W_x_bw, W_h_bw, initial_bias,
reverse=True,
normalized=normalized,
stateful=stateful)
)
self.add_persistent('concat', concat)
self.add_persistent('stateful', stateful)
Expand Down
18 changes: 9 additions & 9 deletions fgnt/chainer_extensions/sequence_linear.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy

from chainer import cuda

if cuda.available:
import cupy
from cupy.cuda.cublas import sgemm
Expand Down Expand Up @@ -52,9 +53,9 @@ def check_type_forward(self, in_types):
x_type, W, *_ = in_types

type_check.expect(
x_type.dtype == numpy.float32,
x_type.ndim == 3,
x_type.shape[2] == W.shape[0],
x_type.dtype == numpy.float32,
x_type.ndim == 3,
x_type.shape[2] == W.shape[0],
)

def check_type_backward(self, in_types, out_types):
Expand All @@ -73,7 +74,7 @@ def forward_cpu(self, inputs):
def forward_gpu(self, inputs):
x = inputs[0]
W = inputs[1]
# Prepare BLAS call for hidden-hidden activation
# Prepare BLAS call
handle = cuda.Device().cublas_handle
k, m = W.shape
n, l = x.shape[0] * x.shape[1], x.shape[2]
Expand All @@ -83,7 +84,7 @@ def forward_gpu(self, inputs):
Wx = cupy.empty((x.shape[0], x.shape[1], W.shape[1]),
dtype=numpy.float32)
sgemm(handle, False, False, m, n, k, 1, W.data.ptr, ldb,
x.data.ptr, lda, 0, Wx.data.ptr, ldc)
x.data.ptr, lda, 0, Wx.data.ptr, ldc)
if len(inputs) > 2:
b = inputs[2]
Wx += b
Expand All @@ -102,7 +103,6 @@ def backward(self, inputs, gy):
else:
return gy_2d.dot(W.T).reshape(x.shape), gW


def backward_gpu(self, inputs, gy):
x = inputs[0]
W = inputs[1]
Expand All @@ -115,7 +115,7 @@ def backward_gpu(self, inputs, gy):
ldb = max(1, gy[0].shape[-1])
ldc = max(1, m)
sgemm(handle, False, True, m, n, k, 1, gy[0].data.ptr, ldb,
x.data.ptr, lda, 1, gW.data.ptr, ldc)
x.data.ptr, lda, 1, gW.data.ptr, ldc)
# Backprop input
m, k = W.shape
n, l = x.shape[0] * x.shape[1], gy[0].shape[2]
Expand All @@ -124,7 +124,7 @@ def backward_gpu(self, inputs, gy):
ldc = max(1, m)
gx = cuda.cupy.empty_like(x)
sgemm(handle, True, False, m, n, k, 1, W.data.ptr, ldb,
gy[0].data.ptr, lda, 0, gx.data.ptr, ldc)
gy[0].data.ptr, lda, 0, gx.data.ptr, ldc)
# Backprop bias
if len(inputs) > 2:
gy_2d = _as_mat(gy[0])
Expand All @@ -138,4 +138,4 @@ def sequence_linear_function(x, W, b=None):
if b is None:
return SequenceLinearFunction()(x, W)
else:
return SequenceLinearFunction()(x, W, b)
return SequenceLinearFunction()(x, W, b)
4 changes: 2 additions & 2 deletions fgnt/chainer_extensions/sequence_lstm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import chainer
import numpy
import scipy.special
import six
from chainer import cuda
from chainer import function
import chainer
import scipy.special

F32 = numpy.float32

Expand Down
31 changes: 15 additions & 16 deletions fgnt/chainer_extensions/sequenze_batch_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from chainer import cuda
from chainer import function
from chainer.utils import type_check

if cuda.available:
import cupy
pass


class SequenceBatchNormalizationFunction(function.Function):

"""Batch normalization on sequential output.
This batch normalization is suited for use cases where the dimension of the
Expand Down Expand Up @@ -44,23 +44,22 @@ class SequenceBatchNormalizationFunction(function.Function):
>>> y, c, h = model.lstm(act_norm)
"""
parameter_names = ('gamma', 'beta')
parameter_names = ('gamma', 'beta')
gradient_names = ('ggamma', 'gbeta')


def __init__(self, eps=1e-8):
self.eps = eps

def check_type_forward(self, in_types):
type_check.expect(in_types.size() == 3)
x_type, gamma_type, beta_type= in_types
x_type, gamma_type, beta_type = in_types

self_ = type_check.Variable(self, 'self')
type_check.expect(
x_type.dtype == numpy.float32,
x_type.ndim == 3,
x_type.shape[2] == gamma_type.shape[0],
x_type.shape[2] == beta_type.shape[0],
x_type.dtype == numpy.float32,
x_type.ndim == 3,
x_type.shape[2] == gamma_type.shape[0],
x_type.shape[2] == beta_type.shape[0],
)

def check_type_backward(self, in_types, out_types):
Expand Down Expand Up @@ -94,7 +93,7 @@ def backward(self, inputs, gy):

coeff = gamma / self.std

gx = coeff * (gy - self.x_hat * ggamma/m - gbeta/m)
gx = coeff * (gy - self.x_hat * ggamma / m - gbeta / m)
return gx, ggamma, gbeta

def forward_gpu(self, inputs):
Expand All @@ -104,12 +103,12 @@ def forward_gpu(self, inputs):
var = x.var(axis=(0, 1), keepdims=True) + self.eps

normalize = cuda.elementwise(
'T x, T var, T mean, T gamma, T beta',
'T std, T x_hat, T y',
'std = sqrtf(var);'
'x_hat = (x - mean) / std;'
'y = gamma * x_hat + beta;',
'normalize')
'T x, T var, T mean, T gamma, T beta',
'T std, T x_hat, T y',
'std = sqrtf(var);'
'x_hat = (x - mean) / std;'
'y = gamma * x_hat + beta;',
'normalize')

self.std, self.x_hat, y = normalize(x, var, mean, gamma, beta)

Expand Down
Loading

0 comments on commit 2830fa8

Please sign in to comment.