def test_approximate_power_spectral_density_tf(self, art_warning, asr_dummy_estimator, audio_batch_padded): try: import tensorflow.compat.v1 as tf1 tf1.reset_default_graph() test_delta = audio_batch_padded test_psd_maximum = np.ones((test_delta.shape[0], 28)) masker = PsychoacousticMasker() imperceptible_asr = ImperceptibleASR( estimator=asr_dummy_estimator(), masker=masker, ) feed_dict = { imperceptible_asr._delta: test_delta, imperceptible_asr._power_spectral_density_maximum_tf: test_psd_maximum, } approximate_psd_tf = imperceptible_asr._approximate_power_spectral_density_tf( imperceptible_asr._delta, imperceptible_asr._power_spectral_density_maximum_tf) with tf1.Session() as sess: psd_approximated = sess.run(approximate_psd_tf, feed_dict) assert psd_approximated.ndim == 3 assert psd_approximated.shape[0] == test_delta.shape[ 0] # batch_size assert psd_approximated.shape[1] == masker.window_size // 2 + 1 except ARTTestException as e: art_warning(e)
def test_create_adversarial(self, art_warning, mocker, asr_dummy_estimator, audio_data): try: test_input, test_target = audio_data estimator = asr_dummy_estimator() mocker.patch.object(estimator, "predict", return_value=test_target) mocker.patch.object( ImperceptibleASR, "_loss_gradient_masking_threshold", return_value=(np.zeros_like(audio_data), [0] * test_input.shape[0]), ) # learning rate of zero ensures that adversarial example equals test input imperceptible_asr = ImperceptibleASR(estimator=estimator, masker=PsychoacousticMasker(), max_iter_1=15, learning_rate_1=0.5) # learning rate of zero ensures that adversarial example equals test input imperceptible_asr.learning_rate_1 = 0 adversarial = imperceptible_asr._create_adversarial( test_input, test_target) # test shape and adversarial example result assert [x.shape for x in test_input] == [a.shape for a in adversarial] assert [(a - x).sum() for a, x in zip(adversarial, test_input) ] == [0.0] * test_input.shape[0] except ARTTestException as e: art_warning(e)
def test_generate(self, art_warning, mocker, asr_dummy_estimator, audio_data): try: test_input, test_target = audio_data mocker.patch.object(ImperceptibleASR, "_generate_batch") imperceptible_asr = ImperceptibleASR(estimator=asr_dummy_estimator(), masker=PsychoacousticMasker()) _ = imperceptible_asr.generate(test_input, test_target) imperceptible_asr._generate_batch.assert_called() except ARTTestException as e: art_warning(e)
def test_loss_gradient_masking_threshold_torch(self, art_warning, asr_dummy_estimator, audio_batch_padded): try: test_delta = audio_batch_padded test_psd_maximum = np.ones((test_delta.shape[0], 1, 1)) test_masking_threshold = np.zeros((test_delta.shape[0], 1025, 28)) imperceptible_asr = ImperceptibleASR(estimator=asr_dummy_estimator(), masker=PsychoacousticMasker()) loss_gradient, loss = imperceptible_asr._loss_gradient_masking_threshold_torch( test_delta, test_psd_maximum, test_masking_threshold ) assert loss_gradient.shape == test_delta.shape assert loss.ndim == 1 and loss.shape[0] == test_delta.shape[0] except ARTTestException as e: art_warning(e)
def test_implements_abstract_methods(self, art_warning, asr_dummy_estimator): try: ImperceptibleASR(estimator=asr_dummy_estimator(), masker=PsychoacousticMasker()) except ARTTestException as e: art_warning(e)
def test_loss_gradient_masking_threshold_tf(self, art_warning, asr_dummy_estimator, audio_batch_padded): try: import tensorflow.compat.v1 as tf1 tf1.reset_default_graph() test_delta = audio_batch_padded test_psd_maximum = np.ones((test_delta.shape[0], 28)) test_masking_threshold = np.zeros((test_delta.shape[0], 1025, 28)) imperceptible_asr = ImperceptibleASR( estimator=asr_dummy_estimator(), masker=PsychoacousticMasker(), ) feed_dict = { imperceptible_asr._delta: test_delta, imperceptible_asr._power_spectral_density_maximum_tf: test_psd_maximum, imperceptible_asr._masking_threshold_tf: test_masking_threshold, } with tf1.Session() as sess: loss_gradient, loss = sess.run( imperceptible_asr._loss_gradient_masking_threshold_op_tf, feed_dict) assert loss_gradient.shape == test_delta.shape assert loss.ndim == 1 and loss.shape[0] == test_delta.shape[0] except ARTTestException as e: art_warning(e)
def test_loss_gradient_masking_threshold(self, art_warning, asr_dummy_estimator, audio_data): try: test_input, _ = audio_data test_delta = test_input * 0 imperceptible_asr = ImperceptibleASR(estimator=asr_dummy_estimator(), masker=PsychoacousticMasker()) masking_threshold, psd_maximum = imperceptible_asr._stabilized_threshold_and_psd_maximum(test_input) loss_gradient, loss = imperceptible_asr._loss_gradient_masking_threshold( test_delta, test_input, masking_threshold, psd_maximum ) assert [g.shape for g in loss_gradient] == [d.shape for d in test_delta] assert loss.ndim == 1 and loss.shape == test_delta.shape except ARTTestException as e: art_warning(e)
def test_approximate_power_spectral_density_torch(self, art_warning, asr_dummy_estimator, audio_batch_padded): try: import torch test_delta = audio_batch_padded test_psd_maximum = np.ones((test_delta.shape[0], 1, 1)) masker = PsychoacousticMasker() imperceptible_asr = ImperceptibleASR(estimator=asr_dummy_estimator(), masker=masker) approximate_psd_torch = imperceptible_asr._approximate_power_spectral_density_torch( torch.from_numpy(test_delta), torch.from_numpy(test_psd_maximum) ) psd_approximated = approximate_psd_torch.numpy() assert psd_approximated.ndim == 3 assert psd_approximated.shape[0] == test_delta.shape[0] # batch_size assert psd_approximated.shape[1] == masker.window_size // 2 + 1 except ARTTestException as e: art_warning(e)
def test_check_params(self, art_warning, image_dl_estimator_for_attack, asr_dummy_estimator): try: from art.attacks.evasion.imperceptible_asr.imperceptible_asr import ImperceptibleASR masker = PsychoacousticMasker() with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, eps=-1) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, max_iter_1=1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, max_iter_1=-1) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, max_iter_2=1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, max_iter_2=-1) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, learning_rate_1="1") with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, learning_rate_1=-1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, learning_rate_2="1") with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, learning_rate_2=-1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, loss_theta_min="1") with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, decrease_factor_eps="1") with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, decrease_factor_eps=-1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, num_iter_decrease_eps=1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, num_iter_decrease_eps=-1) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, alpha="1") with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, alpha=-1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, increase_factor_alpha="1") with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, increase_factor_alpha=-1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, num_iter_increase_alpha=1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, num_iter_increase_alpha=-1) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, decrease_factor_alpha="1") with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, decrease_factor_alpha=-1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, num_iter_decrease_alpha=1.0) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, num_iter_decrease_alpha=-1) with pytest.raises(ValueError): _ = ImperceptibleASR(asr_dummy_estimator(), masker=masker, batch_size=-1) except ARTTestException as e: art_warning(e)