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_genetic_attack.py 4.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Genetic-Attack test.
  16. """
  17. import numpy as np
  18. import pytest
  19. import mindspore.ops.operations as M
  20. from mindspore import Tensor
  21. from mindspore.nn import Cell
  22. from mindspore import context
  23. from mindarmour.attacks.black.genetic_attack import GeneticAttack
  24. from mindarmour.attacks.black.black_model import BlackModel
  25. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  26. # for user
  27. class ModelToBeAttacked(BlackModel):
  28. """model to be attack"""
  29. def __init__(self, network):
  30. super(ModelToBeAttacked, self).__init__()
  31. self._network = network
  32. def predict(self, inputs):
  33. """predict"""
  34. result = self._network(Tensor(inputs.astype(np.float32)))
  35. return result.asnumpy()
  36. class SimpleNet(Cell):
  37. """
  38. Construct the network of target model.
  39. Examples:
  40. >>> net = SimpleNet()
  41. """
  42. def __init__(self):
  43. """
  44. Introduce the layers used for network construction.
  45. """
  46. super(SimpleNet, self).__init__()
  47. self._softmax = M.Softmax()
  48. def construct(self, inputs):
  49. """
  50. Construct network.
  51. Args:
  52. inputs (Tensor): Input data.
  53. """
  54. out = self._softmax(inputs)
  55. return out
  56. @pytest.mark.level0
  57. @pytest.mark.platform_arm_ascend_training
  58. @pytest.mark.platform_x86_ascend_training
  59. @pytest.mark.env_card
  60. @pytest.mark.component_mindarmour
  61. def test_genetic_attack():
  62. """
  63. Genetic_Attack test
  64. """
  65. batch_size = 6
  66. net = SimpleNet()
  67. inputs = np.random.rand(batch_size, 10)
  68. model = ModelToBeAttacked(net)
  69. labels = np.random.randint(low=0, high=10, size=batch_size)
  70. labels = np.eye(10)[labels]
  71. labels = labels.astype(np.float32)
  72. attack = GeneticAttack(model, pop_size=6, mutation_rate=0.05,
  73. per_bounds=0.1, step_size=0.25, temp=0.1,
  74. sparse=False)
  75. _, adv_data, _ = attack.generate(inputs, labels)
  76. assert np.any(inputs != adv_data)
  77. @pytest.mark.level0
  78. @pytest.mark.platform_arm_ascend_training
  79. @pytest.mark.platform_x86_ascend_training
  80. @pytest.mark.env_card
  81. @pytest.mark.component_mindarmour
  82. def test_supplement():
  83. batch_size = 6
  84. net = SimpleNet()
  85. inputs = np.random.rand(batch_size, 10)
  86. model = ModelToBeAttacked(net)
  87. labels = np.random.randint(low=0, high=10, size=batch_size)
  88. labels = np.eye(10)[labels]
  89. labels = labels.astype(np.float32)
  90. attack = GeneticAttack(model, pop_size=6, mutation_rate=0.05,
  91. per_bounds=0.1, step_size=0.25, temp=0.1,
  92. adaptive=True,
  93. sparse=False)
  94. # raise error
  95. _, adv_data, _ = attack.generate(inputs, labels)
  96. @pytest.mark.level0
  97. @pytest.mark.platform_arm_ascend_training
  98. @pytest.mark.platform_x86_ascend_training
  99. @pytest.mark.env_card
  100. @pytest.mark.component_mindarmour
  101. def test_value_error():
  102. """test that exception is raised for invalid labels"""
  103. batch_size = 6
  104. net = SimpleNet()
  105. inputs = np.random.rand(batch_size, 10)
  106. model = ModelToBeAttacked(net)
  107. labels = np.random.randint(low=0, high=10, size=batch_size)
  108. # labels = np.eye(10)[labels]
  109. labels = labels.astype(np.float32)
  110. attack = GeneticAttack(model, pop_size=6, mutation_rate=0.05,
  111. per_bounds=0.1, step_size=0.25, temp=0.1,
  112. adaptive=True,
  113. sparse=False)
  114. # raise error
  115. with pytest.raises(ValueError) as e:
  116. assert attack.generate(inputs, labels)

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