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_batch_generate_attack.py 2.1 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. Batch-generate-attack test.
  16. """
  17. import numpy as np
  18. import pytest
  19. import mindspore.ops.operations as P
  20. from mindspore.nn import Cell
  21. import mindspore.context as context
  22. from mindarmour.attacks.gradient_method import FastGradientMethod
  23. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  24. # for user
  25. class Net(Cell):
  26. """
  27. Construct the network of target model.
  28. Examples:
  29. >>> net = Net()
  30. """
  31. def __init__(self):
  32. """
  33. Introduce the layers used for network construction.
  34. """
  35. super(Net, self).__init__()
  36. self._softmax = P.Softmax()
  37. def construct(self, inputs):
  38. """
  39. Construct network.
  40. Args:
  41. inputs (Tensor): Input data.
  42. """
  43. out = self._softmax(inputs)
  44. return out
  45. @pytest.mark.level0
  46. @pytest.mark.platform_arm_ascend_training
  47. @pytest.mark.platform_x86_ascend_training
  48. @pytest.mark.env_card
  49. @pytest.mark.component_mindarmour
  50. def test_batch_generate_attack():
  51. """
  52. Attack with batch-generate.
  53. """
  54. input_np = np.random.random((128, 10)).astype(np.float32)
  55. label = np.random.randint(0, 10, 128).astype(np.int32)
  56. label = np.eye(10)[label].astype(np.float32)
  57. attack = FastGradientMethod(Net())
  58. ms_adv_x = attack.batch_generate(input_np, label, batch_size=32)
  59. assert np.any(ms_adv_x != input_np), 'Fast gradient method: generate value' \
  60. ' must not be equal to original value.'

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