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 5.2 kB

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

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