def test_serialize_parameters(self): transform = AddBackgroundNoise(sounds_path=os.path.join( DEMO_DIR, "background_noises"), p=1.0) samples = np.random.normal(0, 1, size=1024).astype(np.float32) transform.randomize_parameters(samples, sample_rate=44100) json.dumps(transform.serialize_parameters())
def test_freeze_and_unfreeze_parameters(self): samples = np.zeros((20,), dtype=np.float32) sample_rate = 16000 augmenter = Compose( [ AddBackgroundNoise( sounds_path=os.path.join(DEMO_DIR, "background_noises"), min_snr_in_db=15, max_snr_in_db=35, p=1.0, ), ClippingDistortion(p=0.5), ] ) perturbed_samples1 = augmenter(samples=samples, sample_rate=sample_rate) augmenter.freeze_parameters() for transform in augmenter.transforms: self.assertTrue(transform.are_parameters_frozen) perturbed_samples2 = augmenter(samples=samples, sample_rate=sample_rate) assert_array_equal(perturbed_samples1, perturbed_samples2) augmenter.unfreeze_parameters() for transform in augmenter.transforms: self.assertFalse(transform.are_parameters_frozen)
def test_picklability(self): transform = AddBackgroundNoise( sounds_path=os.path.join(DEMO_DIR, "background_noises"), p=1.0 ) pickled = pickle.dumps(transform) unpickled = pickle.loads(pickled) self.assertEqual(transform.sound_file_paths, unpickled.sound_file_paths)
def test_add_background_noise_when_noise_sound_is_too_short(self): samples = np.sin(np.linspace(0, 440 * 2 * np.pi, 224000)).astype(np.float32) sample_rate = 16000 augmenter = Compose([ AddBackgroundNoise( sounds_path=os.path.join(DEMO_DIR, "background_noises"), min_snr_in_db=15, max_snr_in_db=35, p=1.0, ) ]) samples_out = augmenter(samples=samples, sample_rate=sample_rate) self.assertEqual(samples_out.dtype, np.float32)
def test_try_add_almost_silent_file(self): samples = np.sin(np.linspace(0, 440 * 2 * np.pi, 30000)).astype(np.float32) sample_rate = 48000 augmenter = Compose([ AddBackgroundNoise( sounds_path=os.path.join(DEMO_DIR, "almost_silent"), min_snr_in_db=15, max_snr_in_db=35, p=1.0, ) ]) samples_out = augmenter(samples=samples, sample_rate=sample_rate) assert not np.allclose(samples, samples_out) self.assertEqual(samples_out.dtype, np.float32)
def test_try_add_digital_silence(self): samples = np.sin(np.linspace(0, 440 * 2 * np.pi, 40000)).astype(np.float32) sample_rate = 48000 augmenter = Compose([ AddBackgroundNoise( sounds_path=os.path.join(DEMO_DIR, "digital_silence"), min_snr_in_db=15, max_snr_in_db=35, p=1.0, ) ]) with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") samples_out = augmenter(samples=samples, sample_rate=sample_rate) assert "is too silent to be added as noise" in str(w[-1].message) assert np.allclose(samples, samples_out) self.assertEqual(samples_out.dtype, np.float32)
def test_randomize_parameters_and_apply(self): samples = 1.0 / np.arange(1, 21, dtype=np.float32) sample_rate = 44100 augmenter = Compose([ AddBackgroundNoise( sounds_path=os.path.join(DEMO_DIR, "background_noises"), min_snr_in_db=15, max_snr_in_db=35, p=1.0, ), ClippingDistortion(p=0.5), FrequencyMask(min_frequency_band=0.3, max_frequency_band=0.5, p=0.5), TimeMask(min_band_part=0.2, max_band_part=0.5, p=0.5), Shift(min_fraction=0.5, max_fraction=0.5, p=0.5), ]) augmenter.freeze_parameters() augmenter.randomize_parameters(samples=samples, sample_rate=sample_rate) parameters = [ transform.parameters for transform in augmenter.transforms ] perturbed_samples1 = augmenter(samples=samples, sample_rate=sample_rate) perturbed_samples2 = augmenter(samples=samples, sample_rate=sample_rate) assert_array_equal(perturbed_samples1, perturbed_samples2) augmenter.unfreeze_parameters() for transform_parameters, transform in zip(parameters, augmenter.transforms): self.assertTrue(transform_parameters == transform.parameters) self.assertFalse(transform.are_parameters_frozen)