You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_deep_fool.py 3.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. DeepFool-Attack test.
  16. """
  17. import numpy as np
  18. import pytest
  19. import mindspore.ops.operations as M
  20. from mindspore.nn import Cell
  21. from mindspore import context
  22. from mindspore import Tensor
  23. from mindarmour.attacks.deep_fool import DeepFool
  24. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  25. # for user
  26. class Net(Cell):
  27. """
  28. Construct the network of target model.
  29. Examples:
  30. >>> net = Net()
  31. """
  32. def __init__(self):
  33. """
  34. Introduce the layers used for network construction.
  35. """
  36. super(Net, self).__init__()
  37. self._softmax = M.Softmax()
  38. def construct(self, inputs):
  39. """
  40. Construct network.
  41. Args:
  42. inputs (Tensor): Input data.
  43. """
  44. out = self._softmax(inputs)
  45. return out
  46. @pytest.mark.level0
  47. @pytest.mark.platform_arm_ascend_training
  48. @pytest.mark.platform_x86_ascend_training
  49. @pytest.mark.env_card
  50. @pytest.mark.component_mindarmour
  51. def test_deepfool_attack():
  52. """
  53. Deepfool-Attack test
  54. """
  55. net = Net()
  56. input_shape = (1, 5)
  57. _, classes = input_shape
  58. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  59. input_me = Tensor(input_np)
  60. true_labels = np.argmax(net(input_me).asnumpy(), axis=1)
  61. attack = DeepFool(net, classes, max_iters=10, norm_level=2,
  62. bounds=(0.0, 1.0))
  63. adv_data = attack.generate(input_np, true_labels)
  64. # expected adv value
  65. expect_value = np.asarray([[0.10300991, 0.20332647, 0.59308802, 0.59651263,
  66. 0.40406296]])
  67. assert np.allclose(adv_data, expect_value), 'mindspore deepfool_method' \
  68. ' implementation error, ms_adv_x != expect_value'
  69. @pytest.mark.level0
  70. @pytest.mark.platform_arm_ascend_training
  71. @pytest.mark.platform_x86_ascend_training
  72. @pytest.mark.env_card
  73. @pytest.mark.component_mindarmour
  74. def test_deepfool_attack_inf():
  75. """
  76. Deepfool-Attack test
  77. """
  78. net = Net()
  79. input_shape = (1, 5)
  80. _, classes = input_shape
  81. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  82. input_me = Tensor(input_np)
  83. true_labels = np.argmax(net(input_me).asnumpy(), axis=1)
  84. attack = DeepFool(net, classes, max_iters=10, norm_level=np.inf,
  85. bounds=(0.0, 1.0))
  86. adv_data = attack.generate(input_np, true_labels)
  87. assert np.any(input_np != adv_data)
  88. @pytest.mark.level0
  89. @pytest.mark.platform_arm_ascend_training
  90. @pytest.mark.platform_x86_ascend_training
  91. @pytest.mark.env_card
  92. @pytest.mark.component_mindarmour
  93. def test_value_error():
  94. net = Net()
  95. input_shape = (1, 5)
  96. _, classes = input_shape
  97. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  98. input_me = Tensor(input_np)
  99. true_labels = np.argmax(net(input_me).asnumpy(), axis=1)
  100. with pytest.raises(NotImplementedError):
  101. # norm_level=0 is not available
  102. attack = DeepFool(net, classes, max_iters=10, norm_level=1,
  103. bounds=(0.0, 1.0))
  104. assert attack.generate(input_np, true_labels)

MindArmour关注AI的安全和隐私问题。致力于增强模型的安全可信、保护用户的数据隐私。主要包含3个模块:对抗样本鲁棒性模块、Fuzz Testing模块、隐私保护与评估模块。 对抗样本鲁棒性模块 对抗样本鲁棒性模块用于评估模型对于对抗样本的鲁棒性,并提供模型增强方法用于增强模型抗对抗样本攻击的能力,提升模型鲁棒性。对抗样本鲁棒性模块包含了4个子模块:对抗样本的生成、对抗样本的检测、模型防御、攻防评估。