Example #1
0
def test_meanvar_incremental():
    np.random.seed(1234)
    N = 32
    X = np.random.randn(N, 100, 24)
    lengths = [len(x) for x in X]
    X_mean = np.mean(X, axis=(0, 1))
    X_var = np.var(X, axis=(0, 1))
    X_std = np.sqrt(X_var)

    # Check consistency with numpy
    X_mean_inc, X_var_inc = P.meanvar(X)
    assert np.allclose(X_mean, X_mean_inc)
    assert np.allclose(X_var, X_var_inc)

    # Split dataset and compute meanvar incrementaly
    X_a = X[:N // 2]
    X_b = X[N // 2:]
    X_mean_a, X_var_a, last_sample_count = P.meanvar(
        X_a, return_last_sample_count=True)
    assert last_sample_count == np.sum(lengths[:N // 2])
    X_mean_b, X_var_b = P.meanvar(
        X_b, mean_=X_mean_a, var_=X_var_a,
        last_sample_count=last_sample_count)
    assert np.allclose(X_mean, X_mean_b)
    assert np.allclose(X_var, X_var_b)

    # meanstd
    X_mean_a, X_std_a, last_sample_count = P.meanstd(
        X_a, return_last_sample_count=True)
    assert last_sample_count == np.sum(lengths[:N // 2])
    X_mean_b, X_std_b = P.meanstd(
        X_b, mean_=X_mean_a, var_=X_std_a**2,
        last_sample_count=last_sample_count)
    assert np.allclose(X_mean, X_mean_b)
    assert np.allclose(X_std, X_std_b)
    def __init__(self,input_file_source,output_file_source, mfcc_only, num_frames=128, norm_calc=True):
        self.input_file_source = input_file_source
        self.output_file_source = output_file_source
        self.num_frames = num_frames
        self.mfcc_only = mfcc_only
        self.input_meanstd = None
        self.output_meanstd = None

        if norm_calc:
            print("Performing speaker 1 normalization...")
            SF1_lengths = [len(y) for y in self.input_file_source]
            self.input_meanstd = meanstd(self.input_file_source, SF1_lengths)
            print("Performing speaker 2 normalization...")
            TF2_lengths = [len(y) for y in self.output_file_source]
            self.output_meanstd = meanstd(self.output_file_source, TF2_lengths)
Example #3
0
def test_meanvar():
    # Pick acoustic features for testing
    _, X = example_file_data_sources_for_acoustic_model()
    X = FileSourceDataset(X)
    lengths = [len(x) for x in X]
    D = X[0].shape[-1]
    X_mean, X_var = P.meanvar(X)
    X_std = np.sqrt(X_var)
    assert np.isfinite(X_mean).all()
    assert np.isfinite(X_var).all()
    assert X_mean.shape[-1] == D
    assert X_var.shape[-1] == D

    _, X_std_hat = P.meanstd(X)
    assert np.allclose(X_std, X_std_hat)

    x = X[0]
    x_scaled = P.scale(x, X_mean, X_std)
    assert np.isfinite(x_scaled).all()

    # For padded dataset
    _, X = example_file_data_sources_for_acoustic_model()
    X = PaddedFileSourceDataset(X, 1000)
    # Should get same results with padded features
    X_mean_hat, X_var_hat = P.meanvar(X, lengths)
    assert np.allclose(X_mean, X_mean_hat)
    assert np.allclose(X_var, X_var_hat)

    # Inverse transform
    x = X[0]
    x_hat = P.inv_scale(P.scale(x, X_mean, X_std), X_mean, X_std)
    assert np.allclose(x, x_hat, atol=1e-5)