|
|
@@ -29,9 +29,6 @@ from mindarmour.adv_robustness.attacks import IterativeGradientMethod |
|
|
|
from mindarmour.adv_robustness.attacks import DiverseInputIterativeMethod |
|
|
|
from mindarmour.adv_robustness.attacks import MomentumDiverseInputIterativeMethod |
|
|
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") |
|
|
|
|
|
|
|
|
|
|
|
# for user |
|
|
|
class Net(Cell): |
|
|
|
""" |
|
|
@@ -65,6 +62,7 @@ def test_basic_iterative_method(): |
|
|
|
""" |
|
|
|
Basic iterative method unit test. |
|
|
|
""" |
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") |
|
|
|
input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32) |
|
|
|
label = np.asarray([2], np.int32) |
|
|
|
label = np.eye(3)[label].astype(np.float32) |
|
|
@@ -87,6 +85,7 @@ def test_momentum_iterative_method(): |
|
|
|
""" |
|
|
|
Momentum iterative method unit test. |
|
|
|
""" |
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") |
|
|
|
input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32) |
|
|
|
label = np.asarray([2], np.int32) |
|
|
|
label = np.eye(3)[label].astype(np.float32) |
|
|
@@ -108,6 +107,53 @@ def test_projected_gradient_descent_method(): |
|
|
|
""" |
|
|
|
Projected gradient descent method unit test. |
|
|
|
""" |
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") |
|
|
|
input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32) |
|
|
|
label = np.asarray([2], np.int32) |
|
|
|
label = np.eye(3)[label].astype(np.float32) |
|
|
|
|
|
|
|
for i in range(5): |
|
|
|
attack = ProjectedGradientDescent(Net(), nb_iter=i + 1, loss_fn=SoftmaxCrossEntropyWithLogits(sparse=False)) |
|
|
|
ms_adv_x = attack.generate(input_np, label) |
|
|
|
|
|
|
|
assert np.any( |
|
|
|
ms_adv_x != input_np), 'Projected gradient descent method: ' \ |
|
|
|
'generate value must not be equal to' \ |
|
|
|
' original value.' |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0 |
|
|
|
@pytest.mark.platform_x86_gpu_training |
|
|
|
@pytest.mark.env_card |
|
|
|
@pytest.mark.component_mindarmour |
|
|
|
def test_projected_gradient_descent_method_gpu(): |
|
|
|
""" |
|
|
|
Projected gradient descent method unit test. |
|
|
|
""" |
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="GPU") |
|
|
|
input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32) |
|
|
|
label = np.asarray([2], np.int32) |
|
|
|
label = np.eye(3)[label].astype(np.float32) |
|
|
|
|
|
|
|
for i in range(5): |
|
|
|
attack = ProjectedGradientDescent(Net(), nb_iter=i + 1, loss_fn=SoftmaxCrossEntropyWithLogits(sparse=False)) |
|
|
|
ms_adv_x = attack.generate(input_np, label) |
|
|
|
|
|
|
|
assert np.any( |
|
|
|
ms_adv_x != input_np), 'Projected gradient descent method: ' \ |
|
|
|
'generate value must not be equal to' \ |
|
|
|
' original value.' |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0 |
|
|
|
@pytest.mark.platform_x86_cpu |
|
|
|
@pytest.mark.env_card |
|
|
|
@pytest.mark.component_mindarmour |
|
|
|
def test_projected_gradient_descent_method_cpu(): |
|
|
|
""" |
|
|
|
Projected gradient descent method unit test. |
|
|
|
""" |
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="CPU") |
|
|
|
input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32) |
|
|
|
label = np.asarray([2], np.int32) |
|
|
|
label = np.eye(3)[label].astype(np.float32) |
|
|
@@ -131,6 +177,7 @@ def test_diverse_input_iterative_method(): |
|
|
|
""" |
|
|
|
Diverse input iterative method unit test. |
|
|
|
""" |
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") |
|
|
|
input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32) |
|
|
|
label = np.asarray([2], np.int32) |
|
|
|
label = np.eye(3)[label].astype(np.float32) |
|
|
@@ -151,6 +198,7 @@ def test_momentum_diverse_input_iterative_method(): |
|
|
|
""" |
|
|
|
Momentum diverse input iterative method unit test. |
|
|
|
""" |
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") |
|
|
|
input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32) |
|
|
|
label = np.asarray([2], np.int32) |
|
|
|
label = np.eye(3)[label].astype(np.float32) |
|
|
@@ -168,6 +216,7 @@ def test_momentum_diverse_input_iterative_method(): |
|
|
|
@pytest.mark.env_card |
|
|
|
@pytest.mark.component_mindarmour |
|
|
|
def test_error(): |
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") |
|
|
|
attack = IterativeGradientMethod(Net(), bounds=(0.0, 1.0), loss_fn=SoftmaxCrossEntropyWithLogits(sparse=False)) |
|
|
|
with pytest.raises(NotImplementedError): |
|
|
|
input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32) |
|
|
|