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_jsma.py 4.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. JSMA-Attack test.
  16. """
  17. import numpy as np
  18. import pytest
  19. import mindspore.nn as nn
  20. from mindspore.nn import Cell
  21. from mindspore import context
  22. from mindspore import Tensor
  23. from mindarmour.attacks.jsma import JSMAAttack
  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._relu = nn.ReLU()
  37. def construct(self, inputs):
  38. """
  39. Construct network.
  40. Args:
  41. inputs (Tensor): Input data.
  42. """
  43. out = self._relu(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_jsma_attack():
  51. """
  52. JSMA-Attack test
  53. """
  54. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  55. net = Net()
  56. input_shape = (1, 5)
  57. batch_size, classes = input_shape
  58. np.random.seed(5)
  59. input_np = np.random.random(input_shape).astype(np.float32)
  60. label_np = np.random.randint(classes, size=batch_size)
  61. ori_label = np.argmax(net(Tensor(input_np)).asnumpy(), axis=1)
  62. for i in range(batch_size):
  63. if label_np[i] == ori_label[i]:
  64. if label_np[i] < classes - 1:
  65. label_np[i] += 1
  66. else:
  67. label_np[i] -= 1
  68. attack = JSMAAttack(net, classes, max_iteration=5)
  69. adv_data = attack.generate(input_np, label_np)
  70. assert np.any(input_np != adv_data)
  71. @pytest.mark.level0
  72. @pytest.mark.platform_arm_ascend_training
  73. @pytest.mark.platform_x86_ascend_training
  74. @pytest.mark.env_card
  75. @pytest.mark.component_mindarmour
  76. def test_jsma_attack_2():
  77. """
  78. JSMA-Attack test
  79. """
  80. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  81. net = Net()
  82. input_shape = (1, 5)
  83. batch_size, classes = input_shape
  84. np.random.seed(5)
  85. input_np = np.random.random(input_shape).astype(np.float32)
  86. label_np = np.random.randint(classes, size=batch_size)
  87. ori_label = np.argmax(net(Tensor(input_np)).asnumpy(), axis=1)
  88. for i in range(batch_size):
  89. if label_np[i] == ori_label[i]:
  90. if label_np[i] < classes - 1:
  91. label_np[i] += 1
  92. else:
  93. label_np[i] -= 1
  94. attack = JSMAAttack(net, classes, max_iteration=5, increase=False)
  95. adv_data = attack.generate(input_np, label_np)
  96. assert np.any(input_np != adv_data)
  97. @pytest.mark.level0
  98. @pytest.mark.platform_x86_gpu_inference
  99. @pytest.mark.env_card
  100. @pytest.mark.component_mindarmour
  101. def test_jsma_attack_gpu():
  102. """
  103. JSMA-Attack test
  104. """
  105. context.set_context(device_target="GPU")
  106. net = Net()
  107. input_shape = (1, 5)
  108. batch_size, classes = input_shape
  109. np.random.seed(5)
  110. input_np = np.random.random(input_shape).astype(np.float32)
  111. label_np = np.random.randint(classes, size=batch_size)
  112. ori_label = np.argmax(net(Tensor(input_np)).asnumpy(), axis=1)
  113. for i in range(batch_size):
  114. if label_np[i] == ori_label[i]:
  115. if label_np[i] < classes - 1:
  116. label_np[i] += 1
  117. else:
  118. label_np[i] -= 1
  119. attack = JSMAAttack(net, classes, max_iteration=5)
  120. adv_data = attack.generate(input_np, label_np)
  121. assert np.any(input_np != adv_data)
  122. @pytest.mark.level0
  123. @pytest.mark.platform_x86_cpu
  124. @pytest.mark.env_card
  125. @pytest.mark.component_mindarmour
  126. def test_jsma_attack_cpu():
  127. """
  128. JSMA-Attack test
  129. """
  130. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  131. net = Net()
  132. input_shape = (1, 5)
  133. batch_size, classes = input_shape
  134. np.random.seed(5)
  135. input_np = np.random.random(input_shape).astype(np.float32)
  136. label_np = np.random.randint(classes, size=batch_size)
  137. ori_label = np.argmax(net(Tensor(input_np)).asnumpy(), axis=1)
  138. for i in range(batch_size):
  139. if label_np[i] == ori_label[i]:
  140. if label_np[i] < classes - 1:
  141. label_np[i] += 1
  142. else:
  143. label_np[i] -= 1
  144. attack = JSMAAttack(net, classes, max_iteration=5)
  145. adv_data = attack.generate(input_np, label_np)
  146. assert np.any(input_np != adv_data)

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