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_pointwise.py 5.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. from mindspore import Tensor
  17. from mindspore import context
  18. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  19. from scipy.special import softmax
  20. from lenet5_net import LeNet5
  21. from mindarmour.attacks.black.black_model import BlackModel
  22. from mindarmour.attacks.black.pointwise_attack import PointWiseAttack
  23. from mindarmour.evaluations.attack_evaluation import AttackEvaluate
  24. from mindarmour.utils.logger import LogUtil
  25. sys.path.append("..")
  26. from data_processing import generate_mnist_dataset
  27. LOGGER = LogUtil.get_instance()
  28. TAG = 'Pointwise_Attack'
  29. LOGGER.set_level('INFO')
  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 test_pointwise_attack_on_mnist():
  42. """
  43. Salt-and-Pepper-Attack test
  44. """
  45. # upload trained network
  46. ckpt_name = './trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
  47. net = LeNet5()
  48. load_dict = load_checkpoint(ckpt_name)
  49. load_param_into_net(net, load_dict)
  50. # get test data
  51. data_list = "./MNIST_unzip/test"
  52. batch_size = 32
  53. ds = generate_mnist_dataset(data_list, batch_size=batch_size)
  54. # prediction accuracy before attack
  55. model = ModelToBeAttacked(net)
  56. batch_num = 3 # the number of batches of attacking samples
  57. test_images = []
  58. test_labels = []
  59. predict_labels = []
  60. i = 0
  61. for data in ds.create_tuple_iterator():
  62. i += 1
  63. images = data[0].astype(np.float32)
  64. labels = data[1]
  65. test_images.append(images)
  66. test_labels.append(labels)
  67. pred_labels = np.argmax(model.predict(images), axis=1)
  68. predict_labels.append(pred_labels)
  69. if i >= batch_num:
  70. break
  71. predict_labels = np.concatenate(predict_labels)
  72. true_labels = np.concatenate(test_labels)
  73. accuracy = np.mean(np.equal(predict_labels, true_labels))
  74. LOGGER.info(TAG, "prediction accuracy before attacking is : %g", accuracy)
  75. # attacking
  76. is_target = False
  77. attack = PointWiseAttack(model=model, is_targeted=is_target)
  78. if is_target:
  79. targeted_labels = np.random.randint(0, 10, size=len(true_labels))
  80. for i, true_l in enumerate(true_labels):
  81. if targeted_labels[i] == true_l:
  82. targeted_labels[i] = (targeted_labels[i] + 1) % 10
  83. else:
  84. targeted_labels = true_labels
  85. success_list, adv_data, query_list = attack.generate(
  86. np.concatenate(test_images), targeted_labels)
  87. success_list = np.arange(success_list.shape[0])[success_list]
  88. LOGGER.info(TAG, 'success_list: %s', success_list)
  89. LOGGER.info(TAG, 'average of query times is : %s', np.mean(query_list))
  90. adv_preds = []
  91. for ite_data in adv_data:
  92. pred_logits_adv = model.predict(ite_data)
  93. # rescale predict confidences into (0, 1).
  94. pred_logits_adv = softmax(pred_logits_adv, axis=1)
  95. adv_preds.extend(pred_logits_adv)
  96. accuracy_adv = np.mean(np.equal(np.max(adv_preds, axis=1), true_labels))
  97. LOGGER.info(TAG, "prediction accuracy after attacking is : %g",
  98. accuracy_adv)
  99. test_labels_onehot = np.eye(10)[true_labels]
  100. attack_evaluate = AttackEvaluate(np.concatenate(test_images),
  101. test_labels_onehot, adv_data,
  102. adv_preds, targeted=is_target,
  103. target_label=targeted_labels)
  104. LOGGER.info(TAG, 'mis-classification rate of adversaries is : %s',
  105. attack_evaluate.mis_classification_rate())
  106. LOGGER.info(TAG, 'The average confidence of adversarial class is : %s',
  107. attack_evaluate.avg_conf_adv_class())
  108. LOGGER.info(TAG, 'The average confidence of true class is : %s',
  109. attack_evaluate.avg_conf_true_class())
  110. LOGGER.info(TAG, 'The average distance (l0, l2, linf) between original '
  111. 'samples and adversarial samples are: %s',
  112. attack_evaluate.avg_lp_distance())
  113. if __name__ == '__main__':
  114. # device_target can be "CPU", "GPU" or "Ascend"
  115. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  116. test_pointwise_attack_on_mnist()

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