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

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

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