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.

lenet5_mnist_fuzzing.py 4.2 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 numpy as np
  15. from mindspore import Model
  16. from mindspore import context
  17. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  18. from mindarmour.fuzz_testing import Fuzzer
  19. from mindarmour.fuzz_testing import ModelCoverageMetrics
  20. from mindarmour.utils.logger import LogUtil
  21. from examples.common.dataset.data_processing import generate_mnist_dataset
  22. from examples.common.networks.lenet5.lenet5_net import LeNet5
  23. LOGGER = LogUtil.get_instance()
  24. TAG = 'Fuzz_test'
  25. LOGGER.set_level('INFO')
  26. def test_lenet_mnist_fuzzing():
  27. # upload trained network
  28. ckpt_path = '../common/networks/lenet5/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
  29. net = LeNet5()
  30. load_dict = load_checkpoint(ckpt_path)
  31. load_param_into_net(net, load_dict)
  32. model = Model(net)
  33. mutate_config = [{'method': 'Blur',
  34. 'params': {'radius': [0.1, 0.2, 0.3],
  35. 'auto_param': [True, False]}},
  36. {'method': 'Contrast',
  37. 'params': {'auto_param': [True]}},
  38. {'method': 'Translate',
  39. 'params': {'auto_param': [True]}},
  40. {'method': 'Brightness',
  41. 'params': {'auto_param': [True]}},
  42. {'method': 'Noise',
  43. 'params': {'auto_param': [True]}},
  44. {'method': 'Scale',
  45. 'params': {'auto_param': [True]}},
  46. {'method': 'Shear',
  47. 'params': {'auto_param': [True]}},
  48. {'method': 'FGSM',
  49. 'params': {'eps': [0.3, 0.2, 0.4], 'alpha': [0.1]}}
  50. ]
  51. # get training data
  52. data_list = "../common/dataset/MNIST/train"
  53. batch_size = 32
  54. ds = generate_mnist_dataset(data_list, batch_size, sparse=False)
  55. train_images = []
  56. for data in ds.create_tuple_iterator():
  57. images = data[0].astype(np.float32)
  58. train_images.append(images)
  59. train_images = np.concatenate(train_images, axis=0)
  60. # initialize fuzz test with training dataset
  61. model_coverage_test = ModelCoverageMetrics(model, 10, 1000, train_images)
  62. # fuzz test with original test data
  63. # get test data
  64. data_list = "../common/dataset/MNIST/test"
  65. batch_size = 32
  66. ds = generate_mnist_dataset(data_list, batch_size, sparse=False)
  67. test_images = []
  68. test_labels = []
  69. for data in ds.create_tuple_iterator():
  70. images = data[0].astype(np.float32)
  71. labels = data[1]
  72. test_images.append(images)
  73. test_labels.append(labels)
  74. test_images = np.concatenate(test_images, axis=0)
  75. test_labels = np.concatenate(test_labels, axis=0)
  76. initial_seeds = []
  77. # make initial seeds
  78. for img, label in zip(test_images, test_labels):
  79. initial_seeds.append([img, label])
  80. initial_seeds = initial_seeds[:100]
  81. model_coverage_test.calculate_coverage(
  82. np.array(test_images[:100]).astype(np.float32))
  83. LOGGER.info(TAG, 'KMNC of this test is : %s',
  84. model_coverage_test.get_kmnc())
  85. model_fuzz_test = Fuzzer(model, train_images, 10, 1000)
  86. _, _, _, _, metrics = model_fuzz_test.fuzzing(mutate_config, initial_seeds,
  87. eval_metrics='auto')
  88. if metrics:
  89. for key in metrics:
  90. LOGGER.info(TAG, key + ': %s', metrics[key])
  91. if __name__ == '__main__':
  92. # device_target can be "CPU", "GPU" or "Ascend"
  93. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  94. test_lenet_mnist_fuzzing()

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