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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 import load_checkpoint, load_param_into_net
  18. from mindarmour.fuzz_testing import Fuzzer
  19. from mindarmour.fuzz_testing import KMultisectionNeuronCoverage
  20. from mindarmour.utils import LogUtil
  21. from examples.common.dataset.data_processing import generate_mnist_dataset
  22. from examples.common.networks.lenet5.lenet5_net_for_fuzzing 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], 'bounds': [(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(output_numpy=True):
  57. images = data[0].astype(np.float32)
  58. train_images.append(images)
  59. train_images = np.concatenate(train_images, axis=0)
  60. # fuzz test with original test data
  61. # get test data
  62. data_list = "../common/dataset/MNIST/test"
  63. batch_size = 32
  64. ds = generate_mnist_dataset(data_list, batch_size, sparse=False)
  65. test_images = []
  66. test_labels = []
  67. for data in ds.create_tuple_iterator(output_numpy=True):
  68. images = data[0].astype(np.float32)
  69. labels = data[1]
  70. test_images.append(images)
  71. test_labels.append(labels)
  72. test_images = np.concatenate(test_images, axis=0)
  73. test_labels = np.concatenate(test_labels, axis=0)
  74. initial_seeds = []
  75. # make initial seeds
  76. for img, label in zip(test_images, test_labels):
  77. initial_seeds.append([img, label])
  78. coverage = KMultisectionNeuronCoverage(model, train_images, segmented_num=100, incremental=True)
  79. kmnc = coverage.get_metrics(test_images[:100])
  80. print('KMNC of initial seeds is: ', kmnc)
  81. initial_seeds = initial_seeds[:100]
  82. model_fuzz_test = Fuzzer(model)
  83. _, _, _, _, metrics = model_fuzz_test.fuzzing(mutate_config, initial_seeds, coverage, evaluate=True, max_iters=10,
  84. mutate_num_per_seed=20)
  85. if metrics:
  86. for key in metrics:
  87. print(key + ': ', metrics[key])
  88. if __name__ == '__main__':
  89. # device_target can be "CPU"GPU, "" or "Ascend"
  90. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  91. test_lenet_mnist_fuzzing()

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