def attack_switcher(att, fmodel): """ Initialize different attacks. """ switcher = { "fgsm": fa.GradientSignAttack(fmodel, distance=Linf), "bim": fa.LinfinityBasicIterativeAttack(fmodel, distance=Linf), "mim": fa.MomentumIterativeAttack(fmodel, distance=Linf), "df": LimitedDeepFoolL2Attack(fmodel), "cw": LimitedCarliniWagnerL2Attack(fmodel), "hsj": LimitedHopSkipJumpAttack(fmodel, distance=Linf), "ga": fa.GenAttack(fmodel, criterion=TargetClass(9), distance=Linf), } return switcher.get(att)
uses_grad=True, ), AttackTestTarget(fa.DDNAttack(init_epsilon=2.0, steps=20), uses_grad=True), # TODO: targeted EADAttack currently fails repeatedly on MobileNetv2 AttackTestTarget( fa.EADAttack( binary_search_steps=3, steps=20, abort_early=True, regularization=0, initial_const=1e1, ), uses_grad=True, ), AttackTestTarget( fa.GenAttack(steps=100, population=6, reduced_dims=(7, 7)), epsilon=0.5, requires_real_model=True, ), AttackTestTarget(fa.PGD(), Linf(1.0), uses_grad=True), AttackTestTarget(fa.L2PGD(), L2(50.0), uses_grad=True), AttackTestTarget(fa.L1PGD(), 5000.0, uses_grad=True), AttackTestTarget(fa.LinfBasicIterativeAttack(abs_stepsize=0.2), Linf(1.0), uses_grad=True), AttackTestTarget(fa.L2BasicIterativeAttack(), L2(50.0), uses_grad=True), AttackTestTarget(fa.L1BasicIterativeAttack(), 5000.0, uses_grad=True), AttackTestTarget(fa.SparseL1DescentAttack(), 5000.0, uses_grad=True), ]
), (fa.DDNAttack(init_epsilon=2.0, steps=20), None, True, False), # TODO: targeted EADAttack currently fails repeatedly on MobileNetv2 ( fa.EADAttack( binary_search_steps=3, steps=20, abort_early=True, regularization=0, initial_const=1e1, ), None, True, False, ), (fa.GenAttack(steps=100, population=6, reduced_dims=(14, 14)), 0.3, False, True), ] @pytest.mark.parametrize("attack_eps_grad_real", targeted_attacks, ids=get_attack_id) def test_targeted_attacks( fmodel_and_data_ext_for_attacks: Tuple[ Tuple[fbn.Model, ep.Tensor, ep.Tensor], bool ], attack_eps_grad_real: Tuple[fbn.Attack, Optional[float], bool, bool], ) -> None: attack, eps, attack_uses_grad, requires_real_model = attack_eps_grad_real (fmodel, x, y), real = fmodel_and_data_ext_for_attacks if requires_real_model and not real: pytest.skip()
), (fa.DDNAttack(init_epsilon=2.0, steps=20), None, True, False), # TODO: targeted EADAttack currently fails repeatedly on MobileNetv2 ( fa.EADAttack( binary_search_steps=3, steps=20, abort_early=True, regularization=0, initial_const=1e1, ), None, True, False, ), (fa.GenAttack(steps=100, population=6, reduced_dims=(7, 7)), 0.5, False, True), ] @pytest.mark.parametrize("attack_eps_grad_real", targeted_attacks, ids=get_attack_id) def test_targeted_attacks( fmodel_and_data_ext_for_attacks: Tuple[ Tuple[fbn.Model, ep.Tensor, ep.Tensor], bool ], attack_eps_grad_real: Tuple[fbn.Attack, Optional[float], bool, bool], ) -> None: attack, eps, attack_uses_grad, requires_real_model = attack_eps_grad_real (fmodel, x, y), real = fmodel_and_data_ext_for_attacks if requires_real_model and not real: pytest.skip()