def test_randomized_smoothing_schedule(): audio = torch.zeros(16, 16000, dtype=torch.float32) tfnm = RandomizedSmoothing(sigma=[(0, 0.01), (100, 0.5)], p=0.8) audio_aug = tfnm(audio) # Shapes are the same assert audio.shape == audio_aug.shape # All samples are different than the input audio assert (audio != audio_aug).any() # Different batch samples receive different augmentation: # we sum along the time axis and compare the summed values; # if all examples got the same augmentation, # there would have been just one unique value. assert len(set(audio_aug.sum(dim=1).tolist())) > 1 tfnm.step = 1000 audio_aug2 = tfnm(audio) # The schedule kicked in and the abs magnitudes should be larger. assert audio_aug2.abs().sum() > audio_aug.abs().sum()
def test_randomized_smoothing_p1(): audio = torch.zeros(64, 4000, dtype=torch.float32) tfnm = RandomizedSmoothing(sigma=0.1, p=1.0) audio_aug = tfnm(audio) # Shapes are the same assert audio.shape == audio_aug.shape # Some (most) samples are different than the input audio assert (audio != audio_aug).any() # Different batch samples receive different augmentation assert (audio_aug[0] != audio_aug[1]).any()
def test_randomized_smoothing_p0(): audio = torch.zeros(64, 4000, dtype=torch.float32) tfnm = RandomizedSmoothing(sigma=0.1, p=0.0) audio_aug = tfnm(audio) # Shapes are the same assert audio.shape == audio_aug.shape # Audio is unaffacted assert (audio == audio_aug).all() # Audio is unaffacted across batches assert (audio_aug[0] == audio_aug[1]).all()
def test_randomized_smoothing(sample_sigma): audio = torch.zeros(64, 4000, dtype=torch.float32) tfnm = RandomizedSmoothing(sigma=0.1, sample_sigma=sample_sigma, p=0.8) audio_aug = tfnm(audio) # Shapes are the same assert audio.shape == audio_aug.shape # All samples are different than the input audio assert (audio != audio_aug).any() # Different batch samples receive different augmentation: # we sum along the time axis and compare the summed values; # if all examples got the same augmentation, # there would have been just one unique value. assert len(set(audio_aug.sum(dim=1).tolist())) > 1
def test_k2_speech_recognition_on_the_fly_feature_extraction_with_randomized_smoothing( k2_cut_set, ): dataset = K2SpeechRecognitionDataset( input_strategy=OnTheFlyFeatures(extractor=Fbank(), )) rs_dataset = K2SpeechRecognitionDataset(input_strategy=OnTheFlyFeatures( extractor=Fbank(), # Use p=1.0 to ensure that smoothing is applied in this test. wave_transforms=[RandomizedSmoothing(sigma=0.5, p=1.0)], )) sampler = SingleCutSampler(k2_cut_set, shuffle=False, max_cuts=1) for cut_ids in sampler: batch = dataset[cut_ids] rs_batch = rs_dataset[cut_ids] # Additive noise should cause the energies to go up assert (rs_batch["inputs"] - batch["inputs"]).sum() > 0