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_pso.py 4.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 time
  16. import numpy as np
  17. from mindspore import Tensor
  18. from mindspore import context
  19. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  20. from scipy.special import softmax
  21. from lenet5_net import LeNet5
  22. from mindarmour import BlackModel
  23. from mindarmour.adv_robustness.attacks.black.pso_attack import PSOAttack
  24. from mindarmour.adv_robustness.evaluations import AttackEvaluate
  25. from mindarmour.utils.logger import LogUtil
  26. sys.path.append("..")
  27. from data_processing import generate_mnist_dataset
  28. LOGGER = LogUtil.get_instance()
  29. LOGGER.set_level('INFO')
  30. TAG = 'PSO_Attack'
  31. class ModelToBeAttacked(BlackModel):
  32. """model to be attack"""
  33. def __init__(self, network):
  34. super(ModelToBeAttacked, self).__init__()
  35. self._network = network
  36. def predict(self, inputs):
  37. """predict"""
  38. result = self._network(Tensor(inputs.astype(np.float32)))
  39. return result.asnumpy()
  40. def test_pso_attack_on_mnist():
  41. """
  42. PSO-Attack test
  43. """
  44. # upload trained network
  45. ckpt_name = './trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
  46. net = LeNet5()
  47. load_dict = load_checkpoint(ckpt_name)
  48. load_param_into_net(net, load_dict)
  49. # get test data
  50. data_list = "./MNIST_unzip/test"
  51. batch_size = 32
  52. ds = generate_mnist_dataset(data_list, batch_size=batch_size)
  53. # prediction accuracy before attack
  54. model = ModelToBeAttacked(net)
  55. batch_num = 3 # the number of batches of attacking samples
  56. test_images = []
  57. test_labels = []
  58. predict_labels = []
  59. i = 0
  60. for data in ds.create_tuple_iterator():
  61. i += 1
  62. images = data[0].astype(np.float32)
  63. labels = data[1]
  64. test_images.append(images)
  65. test_labels.append(labels)
  66. pred_labels = np.argmax(model.predict(images), axis=1)
  67. predict_labels.append(pred_labels)
  68. if i >= batch_num:
  69. break
  70. predict_labels = np.concatenate(predict_labels)
  71. true_labels = np.concatenate(test_labels)
  72. accuracy = np.mean(np.equal(predict_labels, true_labels))
  73. LOGGER.info(TAG, "prediction accuracy before attacking is : %s", accuracy)
  74. # attacking
  75. attack = PSOAttack(model, bounds=(0.0, 1.0), pm=0.5, sparse=True)
  76. start_time = time.clock()
  77. success_list, adv_data, query_list = attack.generate(
  78. np.concatenate(test_images), np.concatenate(test_labels))
  79. stop_time = time.clock()
  80. LOGGER.info(TAG, 'success_list: %s', success_list)
  81. LOGGER.info(TAG, 'average of query times is : %s', np.mean(query_list))
  82. pred_logits_adv = model.predict(adv_data)
  83. # rescale predict confidences into (0, 1).
  84. pred_logits_adv = softmax(pred_logits_adv, axis=1)
  85. pred_labels_adv = np.argmax(pred_logits_adv, axis=1)
  86. accuracy_adv = np.mean(np.equal(pred_labels_adv, true_labels))
  87. LOGGER.info(TAG, "prediction accuracy after attacking is : %s",
  88. accuracy_adv)
  89. test_labels_onehot = np.eye(10)[np.concatenate(test_labels)]
  90. attack_evaluate = AttackEvaluate(np.concatenate(test_images),
  91. test_labels_onehot, adv_data,
  92. pred_logits_adv)
  93. LOGGER.info(TAG, 'mis-classification rate of adversaries is : %s',
  94. attack_evaluate.mis_classification_rate())
  95. LOGGER.info(TAG, 'The average confidence of adversarial class is : %s',
  96. attack_evaluate.avg_conf_adv_class())
  97. LOGGER.info(TAG, 'The average confidence of true class is : %s',
  98. attack_evaluate.avg_conf_true_class())
  99. LOGGER.info(TAG, 'The average distance (l0, l2, linf) between original '
  100. 'samples and adversarial samples are: %s',
  101. attack_evaluate.avg_lp_distance())
  102. LOGGER.info(TAG, 'The average structural similarity between original '
  103. 'samples and adversarial samples are: %s',
  104. attack_evaluate.avg_ssim())
  105. LOGGER.info(TAG, 'The average costing time is %s',
  106. (stop_time - start_time)/(batch_num*batch_size))
  107. if __name__ == '__main__':
  108. # device_target can be "CPU", "GPU" or "Ascend"
  109. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  110. test_pso_attack_on_mnist()

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