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.

mnist_attack_nes.py 5.2 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. import sys
  15. import numpy as np
  16. import pytest
  17. from mindspore import Tensor
  18. from mindspore import context
  19. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  20. from mindarmour.attacks.black.natural_evolutionary_strategy import NES
  21. from mindarmour.attacks.black.black_model import BlackModel
  22. from mindarmour.utils.logger import LogUtil
  23. from lenet5_net import LeNet5
  24. sys.path.append("..")
  25. from data_processing import generate_mnist_dataset
  26. context.set_context(mode=context.GRAPH_MODE)
  27. context.set_context(device_target="Ascend")
  28. LOGGER = LogUtil.get_instance()
  29. TAG = 'HopSkipJumpAttack'
  30. class ModelToBeAttacked(BlackModel):
  31. """model to be attack"""
  32. def __init__(self, network):
  33. super(ModelToBeAttacked, self).__init__()
  34. self._network = network
  35. def predict(self, inputs):
  36. """predict"""
  37. if len(inputs.shape) == 3:
  38. inputs = inputs[np.newaxis, :]
  39. result = self._network(Tensor(inputs.astype(np.float32)))
  40. return result.asnumpy()
  41. def random_target_labels(true_labels, labels_list):
  42. target_labels = []
  43. for label in true_labels:
  44. while True:
  45. target_label = np.random.choice(labels_list)
  46. if target_label != label:
  47. target_labels.append(target_label)
  48. break
  49. return target_labels
  50. def _pseudorandom_target(index, total_indices, true_class):
  51. """ pseudo random_target """
  52. rng = np.random.RandomState(index)
  53. target = true_class
  54. while target == true_class:
  55. target = rng.randint(0, total_indices)
  56. return target
  57. def create_target_images(dataset, data_labels, target_labels):
  58. res = []
  59. for label in target_labels:
  60. for i in range(len(data_labels)):
  61. if data_labels[i] == label:
  62. res.append(dataset[i])
  63. break
  64. return np.array(res)
  65. @pytest.mark.level1
  66. @pytest.mark.platform_arm_ascend_training
  67. @pytest.mark.platform_x86_ascend_training
  68. @pytest.mark.env_card
  69. @pytest.mark.component_mindarmour
  70. def test_nes_mnist_attack():
  71. """
  72. hsja-Attack test
  73. """
  74. # upload trained network
  75. ckpt_name = './trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
  76. net = LeNet5()
  77. load_dict = load_checkpoint(ckpt_name)
  78. load_param_into_net(net, load_dict)
  79. net.set_train(False)
  80. # get test data
  81. data_list = "./MNIST_unzip/test"
  82. batch_size = 32
  83. ds = generate_mnist_dataset(data_list, batch_size=batch_size)
  84. # prediction accuracy before attack
  85. model = ModelToBeAttacked(net)
  86. # the number of batches of attacking samples
  87. batch_num = 5
  88. test_images = []
  89. test_labels = []
  90. predict_labels = []
  91. i = 0
  92. for data in ds.create_tuple_iterator():
  93. i += 1
  94. images = data[0].astype(np.float32)
  95. labels = data[1]
  96. test_images.append(images)
  97. test_labels.append(labels)
  98. pred_labels = np.argmax(model.predict(images), axis=1)
  99. predict_labels.append(pred_labels)
  100. if i >= batch_num:
  101. break
  102. predict_labels = np.concatenate(predict_labels)
  103. true_labels = np.concatenate(test_labels)
  104. accuracy = np.mean(np.equal(predict_labels, true_labels))
  105. LOGGER.info(TAG, "prediction accuracy before attacking is : %s",
  106. accuracy)
  107. test_images = np.concatenate(test_images)
  108. # attacking
  109. scene = 'Query_Limit'
  110. if scene == 'Query_Limit':
  111. top_k = -1
  112. elif scene == 'Partial_Info':
  113. top_k = 5
  114. elif scene == 'Label_Only':
  115. top_k = 5
  116. success = 0
  117. queries_num = 0
  118. nes_instance = NES(model, scene, top_k=top_k)
  119. test_length = 32
  120. advs = []
  121. for img_index in range(test_length):
  122. # Initial image and class selection
  123. initial_img = test_images[img_index]
  124. orig_class = true_labels[img_index]
  125. initial_img = [initial_img]
  126. target_class = random_target_labels([orig_class], true_labels)
  127. target_image = create_target_images(test_images, true_labels,
  128. target_class)
  129. nes_instance.set_target_images(target_image)
  130. tag, adv, queries = nes_instance.generate(initial_img, target_class)
  131. if tag[0]:
  132. success += 1
  133. queries_num += queries[0]
  134. advs.append(adv)
  135. advs = np.reshape(advs, (len(advs), 1, 32, 32))
  136. adv_pred = np.argmax(model.predict(advs), axis=1)
  137. adv_accuracy = np.mean(np.equal(adv_pred, true_labels[:test_length]))
  138. LOGGER.info(TAG, "prediction accuracy after attacking is : %s",
  139. adv_accuracy)
  140. if __name__ == '__main__':
  141. test_nes_mnist_attack()

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