@@ -105,9 +105,9 @@ class DeepFool(Attack): | |||
max_iters (int): Max iterations, which should be | |||
greater than zero. Default: 50. | |||
overshoot (float): Overshoot parameter. Default: 0.02. | |||
norm_level (int): Order of the vector norm. Possible values: np.inf | |||
norm_level (Union[int, str]): Order of the vector norm. Possible values: np.inf | |||
or 2. Default: 2. | |||
bounds (tuple): Upper and lower bounds of data range. In form of (clip_min, | |||
bounds (Union[tuple, list]): Upper and lower bounds of data range. In form of (clip_min, | |||
clip_max). Default: None. | |||
sparse (bool): If True, input labels are sparse-coded. If False, | |||
input labels are onehot-coded. Default: True. | |||
@@ -149,13 +149,11 @@ def check_numpy_param(arg_name, arg_value): | |||
ValueError: If value type is not in (list, tuple, numpy.ndarray). | |||
""" | |||
_ = _check_array_not_empty(arg_name, arg_value) | |||
if isinstance(arg_value, (list, tuple)): | |||
arg_value = np.asarray(arg_value) | |||
elif isinstance(arg_value, np.ndarray): | |||
if isinstance(arg_value, np.ndarray): | |||
arg_value = np.copy(arg_value) | |||
else: | |||
msg = 'type of {} must be in (list, tuple, numpy.ndarray)'.format( | |||
arg_name) | |||
msg = 'type of {} must be numpy.ndarray, but got {}'.format( | |||
arg_name, type(arg_value)) | |||
LOGGER.error(TAG, msg) | |||
raise TypeError(msg) | |||
return arg_value | |||
@@ -220,6 +218,8 @@ def check_norm_level(norm_level): | |||
""" | |||
check norm_level of regularization. | |||
""" | |||
if not isinstance(norm_level, (int, str)): | |||
msg = 'Type of norm_level must be in [int, str], but got {}'.format(type(norm_level)) | |||
accept_norm = [1, 2, '1', '2', 'l1', 'l2', 'inf', 'linf', np.inf] | |||
if norm_level not in accept_norm: | |||
msg = 'norm_level must be in {}, but got {}'.format(accept_norm, | |||
@@ -147,7 +147,7 @@ def nes_mnist_attack(scene, top_k): | |||
target_class) | |||
nes_instance.set_target_images(target_image) | |||
tag, adv, queries = nes_instance.generate(initial_img, target_class) | |||
tag, adv, queries = nes_instance.generate(np.array(initial_img), np.array(target_class)) | |||
if tag[0]: | |||
success += 1 | |||
queries_num += queries[0] | |||
@@ -17,9 +17,10 @@ Batch-generate-attack test. | |||
import numpy as np | |||
import pytest | |||
import mindspore.context as context | |||
import mindspore.ops.operations as P | |||
from mindspore.ops.composite import GradOperation | |||
from mindspore.nn import Cell, SoftmaxCrossEntropyWithLogits | |||
import mindspore.context as context | |||
from mindarmour.adv_robustness.attacks import FastGradientMethod | |||
@@ -54,6 +55,60 @@ class Net(Cell): | |||
return out | |||
class Net2(Cell): | |||
""" | |||
Construct the network of target model. A network with multiple input data. | |||
Examples: | |||
>>> net = Net2() | |||
""" | |||
def __init__(self): | |||
super(Net2, self).__init__() | |||
self._softmax = P.Softmax() | |||
def construct(self, inputs1, inputs2): | |||
out1 = self._softmax(inputs1) | |||
out2 = self._softmax(inputs2) | |||
return out1 + out2, out1 - out2 | |||
class LossNet(Cell): | |||
""" | |||
Loss function for test. | |||
""" | |||
def construct(self, loss1, loss2, labels1, labels2): | |||
return loss1 + loss2 - labels1 - labels2 | |||
class WithLossCell(Cell): | |||
"""Wrap the network with loss function""" | |||
def __init__(self, backbone, loss_fn): | |||
super(WithLossCell, self).__init__(auto_prefix=False) | |||
self._backbone = backbone | |||
self._loss_fn = loss_fn | |||
def construct(self, inputs1, inputs2, labels1, labels2): | |||
out = self._backbone(inputs1, inputs2) | |||
return self._loss_fn(*out, labels1, labels2) | |||
class GradWrapWithLoss(Cell): | |||
""" | |||
Construct a network to compute the gradient of loss function in \ | |||
input space and weighted by 'weight'. | |||
""" | |||
def __init__(self, network): | |||
super(GradWrapWithLoss, self).__init__() | |||
self._grad_all = GradOperation(get_all=True, sens_param=False) | |||
self._network = network | |||
def construct(self, *inputs): | |||
gout = self._grad_all(self._network)(*inputs) | |||
return gout[0] | |||
@pytest.mark.level0 | |||
@pytest.mark.platform_arm_ascend_training | |||
@pytest.mark.platform_x86_ascend_training | |||
@@ -71,4 +126,30 @@ def test_batch_generate_attack(): | |||
ms_adv_x = attack.batch_generate(input_np, label, batch_size=32) | |||
assert np.any(ms_adv_x != input_np), 'Fast gradient method: generate value' \ | |||
' must not be equal to original value.' | |||
@pytest.mark.level0 | |||
@pytest.mark.platform_arm_ascend_training | |||
@pytest.mark.platform_x86_ascend_training | |||
@pytest.mark.env_card | |||
@pytest.mark.component_mindarmour | |||
def test_batch_generate_attack_multi_inputs(): | |||
""" | |||
Attack with batch-generate by multi-inputs. | |||
""" | |||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") | |||
inputs1 = np.random.random((128, 10)).astype(np.float32) | |||
inputs2 = np.random.random((128, 10)).astype(np.float32) | |||
labels1 = np.random.randint(0, 10, 128).astype(np.int32) | |||
labels2 = np.random.randint(0, 10, 128).astype(np.int32) | |||
labels1 = np.eye(10)[labels1].astype(np.float32) | |||
labels2 = np.eye(10)[labels2].astype(np.float32) | |||
with_loss_cell = WithLossCell(Net2(), LossNet()) | |||
grad_with_loss_net = GradWrapWithLoss(with_loss_cell) | |||
attack = FastGradientMethod(grad_with_loss_net) | |||
ms_adv_x = attack.batch_generate((inputs1, inputs2), (labels1, labels2), batch_size=32) | |||
assert np.any(ms_adv_x != inputs1), 'Fast gradient method: generate value' \ | |||
' must not be equal to original value.' |
@@ -312,52 +312,6 @@ def test_fast_gradient_method_multi_inputs(): | |||
@pytest.mark.platform_x86_ascend_training | |||
@pytest.mark.env_card | |||
@pytest.mark.component_mindarmour | |||
def test_batch_generate(): | |||
""" | |||
Fast gradient method unit test. | |||
""" | |||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") | |||
input_np = np.random.random([10, 3]).astype(np.float32) | |||
label = np.random.randint(0, 3, [10]) | |||
label = np.eye(3)[label].astype(np.float32) | |||
loss_fn = SoftmaxCrossEntropyWithLogits(sparse=False) | |||
attack = FastGradientMethod(Net(), loss_fn=loss_fn) | |||
ms_adv_x = attack.batch_generate(input_np, label, 4) | |||
assert np.any(ms_adv_x != input_np), 'Fast gradient method: generate value' \ | |||
' must not be equal to original value.' | |||
@pytest.mark.level0 | |||
@pytest.mark.platform_arm_ascend_training | |||
@pytest.mark.platform_x86_ascend_training | |||
@pytest.mark.env_card | |||
@pytest.mark.component_mindarmour | |||
def test_batch_generate_multi_inputs(): | |||
""" | |||
Fast gradient method unit test. | |||
""" | |||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") | |||
inputs1 = np.asarray([[0.1, 0.2, 0.7]]).astype(np.float32) | |||
inputs2 = np.asarray([[0.4, 0.8, 0.5]]).astype(np.float32) | |||
labels1 = np.expand_dims(np.eye(3)[1].astype(np.float32), axis=0) | |||
labels2 = np.expand_dims(np.eye(3)[2].astype(np.float32), axis=0) | |||
with_loss_cell = WithLossCell(Net2(), LossNet()) | |||
grad_with_loss_net = GradWrapWithLoss(with_loss_cell) | |||
attack = FastGradientMethod(grad_with_loss_net) | |||
ms_adv_x = attack.generate((inputs1, inputs2), (labels1, labels2)) | |||
assert np.any(ms_adv_x != inputs1), 'Fast gradient method: generate value' \ | |||
' must not be equal to original value.' | |||
@pytest.mark.level0 | |||
@pytest.mark.platform_arm_ascend_training | |||
@pytest.mark.platform_x86_ascend_training | |||
@pytest.mark.env_card | |||
@pytest.mark.component_mindarmour | |||
def test_assert_error(): | |||
""" | |||
Random least likely class method unit test. | |||
@@ -14,6 +14,7 @@ | |||
""" | |||
Radar map test. | |||
""" | |||
import numpy as np | |||
import pytest | |||
from mindarmour.adv_robustness.evaluations import RadarMetric | |||
@@ -28,7 +29,7 @@ def test_radar_metric(): | |||
metrics_name = ['MR', 'ACAC', 'ASS', 'NTE', 'RGB'] | |||
def_metrics = [0.9, 0.85, 0.6, 0.7, 0.8] | |||
raw_metrics = [0.5, 0.3, 0.55, 0.65, 0.7] | |||
metrics_data = [def_metrics, raw_metrics] | |||
metrics_data = np.array([def_metrics, raw_metrics]) | |||
metrics_labels = ['before', 'after'] | |||
# create obj | |||
@@ -46,7 +47,7 @@ def test_value_error(): | |||
metrics_name = ['MR', 'ACAC', 'ASS', 'NTE', 'RGB'] | |||
def_metrics = [0.9, 0.85, 0.6, 0.7, 0.8] | |||
raw_metrics = [0.5, 0.3, 0.55, 0.65, 0.7] | |||
metrics_data = [def_metrics, raw_metrics] | |||
metrics_data = np.array([def_metrics, raw_metrics]) | |||
metrics_labels = ['before', 'after'] | |||
with pytest.raises(ValueError): | |||