Exemplo n.º 1
0
def test_transform():
    # minmax
    X1 = np.random.randn(100, 32, 128)

    scaler = Scaler(normalizer="minmax")
    scaler.fit(X1)
    X1_scaled = scaler.transform(X1)
    assert np.amin(X1_scaled) == -1.0
    assert np.amax(X1_scaled) == 1.0

    # standard
    X2 = np.random.randn(100, 32, 128)

    scaler = Scaler(normalizer="standard")
    scaler.fit(X2)
    X2_scaled = scaler.transform(X2)
    X2_scaled_flat = np.reshape(X2_scaled, (-1, X2.shape[-1]))
    assert X2_scaled_flat.shape[1] == X2.shape[-1]

    mean = np.mean(X2_scaled_flat, axis=0)
    std = np.std(X2_scaled_flat, axis=0)

    assert np.allclose(mean, np.zeros(128), rtol=0.001, atol=0.001)
    assert np.allclose(std, np.ones(128), rtol=0.001, atol=0.001)

    # list of scalers
    scaler = Scaler(normalizer=["minmax", "standard"])
    X_list = [X1, X2]
    scaler.fit(X_list)
    X_list_scaled = scaler.transform(X_list)

    assert type(X_list_scaled) is list
    assert len(X_list_scaled) == 2
    assert np.allclose(X_list_scaled[0], X1_scaled, rtol=0.001, atol=0.001)
    assert np.allclose(X_list_scaled[1], X2_scaled, rtol=0.001, atol=0.001)

    # DataGenerator
    feature_extractor = MelSpectrogram()
    feature_extractor.extract(dataset)
    data_generator = DataGenerator(dataset, feature_extractor, folds=["all"])
    scaler = Scaler(normalizer="minmax")
    scaler.fit(data_generator)
    data_generator.set_scaler(scaler)
    X, _ = data_generator.get_data()
    assert np.amin(X) == -1.0
    assert np.amax(X) == 1.0
Exemplo n.º 2
0
def test_mel_spectrogram():
    feature_extractor = MelSpectrogram(pad_mode="constant")
    shape = feature_extractor.get_shape(2.0)
    assert shape == (3, 32, 64)

    feature_extractor = MelSpectrogram(sequence_time=-1, pad_mode="constant")
    shape = feature_extractor.get_shape(2.0)
    assert shape == (1, 64, 64)

    feature_extractor = MelSpectrogram(sequence_time=1.0,
                                       sequence_hop_time=-1,
                                       pad_mode="constant")
    shape = feature_extractor.get_shape(2.0)
    assert shape == (1, 32, 64)
Exemplo n.º 3
0
def test_fit():
    # Array, expect same results than partial_fit
    X = np.random.randn(100, 32, 128)
    scaler = Scaler(normalizer="minmax")
    scaler.fit(X)
    assert type(scaler.scaler) is list
    assert len(scaler.scaler) == 1
    assert scaler.scaler[0][0] == np.amin(X)
    assert scaler.scaler[0][1] == np.amax(X)

    # DataGenerator
    feature_extractor = MelSpectrogram()
    feature_extractor.extract(dataset)
    data_generator = DataGenerator(dataset, feature_extractor, folds=["all"])
    scaler = Scaler(normalizer="minmax")
    scaler.fit(data_generator)
    X, _ = data_generator.get_data()
    assert type(scaler.scaler) is list
    assert len(scaler.scaler) == 1
    assert scaler.scaler[0][0] == np.amin(X)
    assert scaler.scaler[0][1] == np.amax(X)
Exemplo n.º 4
0
        """
        self.file_lists['all'] = sorted(
            glob.glob(os.path.join(self.audio_path, '*.wav')))
        print(self.file_lists['all'])

    def get_annotations(self, file_path, features, time_resolution):
        y = np.zeros((len(features), len(self.label_list)))
        class_ix = int(os.path.basename(file_path).split('-')[1])
        y[:, class_ix] = 1
        return y


dataset_path = './tests/data'
dataset = TestDataset(dataset_path)
feature_extractor = MelSpectrogram()
audio_files = ['147764-4-7-0.wav', '176787-5-0-0.wav', '40722-8-0-7.wav']
features_files = ['147764-4-7-0.npy', '176787-5-0-0.npy', '40722-8-0-7.npy']
classes = [4, 5, 8]

feature_extractor.extract(dataset)
features_path = feature_extractor.get_features_path(dataset)

X_gt = []
Y_gt = []
for j, ff in enumerate(features_files):
    feat = np.load(os.path.join(features_path, 'original', ff))
    X_gt.append(feat)
    ann = np.zeros((len(feat), 10))
    ann[:, classes[j]] = 1
    Y_gt.append(ann)