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_cw.py 2.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. CW-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 mindarmour.adv_robustness.attacks import CarliniWagnerL2Attack
  23. # for user
  24. class Net(Cell):
  25. """
  26. Construct the network of target model.
  27. Examples:
  28. >>> net = Net()
  29. """
  30. def __init__(self):
  31. """
  32. Introduce the layers used for network construction.
  33. """
  34. super(Net, self).__init__()
  35. self._softmax = M.Softmax()
  36. def construct(self, inputs):
  37. """
  38. Construct network.
  39. Args:
  40. inputs (Tensor): Input data.
  41. """
  42. out = self._softmax(inputs)
  43. return out
  44. @pytest.mark.level0
  45. @pytest.mark.platform_arm_ascend_training
  46. @pytest.mark.platform_x86_ascend_training
  47. @pytest.mark.env_card
  48. @pytest.mark.component_mindarmour
  49. def test_cw_attack():
  50. """
  51. CW-Attack test
  52. """
  53. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  54. net = Net()
  55. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  56. label_np = np.array([3]).astype(np.int64)
  57. num_classes = input_np.shape[1]
  58. attack = CarliniWagnerL2Attack(net, num_classes, targeted=False)
  59. adv_data = attack.generate(input_np, label_np)
  60. assert np.any(input_np != adv_data)
  61. @pytest.mark.level0
  62. @pytest.mark.platform_arm_ascend_training
  63. @pytest.mark.platform_x86_ascend_training
  64. @pytest.mark.env_card
  65. @pytest.mark.component_mindarmour
  66. def test_cw_attack_targeted():
  67. """
  68. CW-Attack test
  69. """
  70. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  71. net = Net()
  72. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  73. target_np = np.array([1]).astype(np.int64)
  74. num_classes = input_np.shape[1]
  75. attack = CarliniWagnerL2Attack(net, num_classes, targeted=True)
  76. adv_data = attack.generate(input_np, target_np)
  77. assert np.any(input_np != adv_data)

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