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 3.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 mindspore.nn import SoftmaxCrossEntropyWithLogits
  20. from mindarmour.attacks.gradient_method import FastGradientSignMethod
  21. from mindarmour.utils.logger import LogUtil
  22. from mindarmour.fuzzing.model_coverage_metrics import ModelCoverageMetrics
  23. from mindarmour.fuzzing.fuzzing import Fuzzing
  24. from lenet5_net import LeNet5
  25. sys.path.append("..")
  26. from data_processing import generate_mnist_dataset
  27. LOGGER = LogUtil.get_instance()
  28. TAG = 'Fuzz_test'
  29. LOGGER.set_level('INFO')
  30. def test_lenet_mnist_fuzzing():
  31. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  32. # upload trained network
  33. ckpt_name = './trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
  34. net = LeNet5()
  35. load_dict = load_checkpoint(ckpt_name)
  36. load_param_into_net(net, load_dict)
  37. model = Model(net)
  38. # get training data
  39. data_list = "./MNIST_datasets/train"
  40. batch_size = 32
  41. ds = generate_mnist_dataset(data_list, batch_size, sparse=True)
  42. train_images = []
  43. for data in ds.create_tuple_iterator():
  44. images = data[0].astype(np.float32)
  45. train_images.append(images)
  46. train_images = np.concatenate(train_images, axis=0)
  47. # initialize fuzz test with training dataset
  48. model_coverage_test = ModelCoverageMetrics(model, 1000, 10, train_images)
  49. # fuzz test with original test data
  50. # get test data
  51. data_list = "./MNIST_datasets/test"
  52. batch_size = 32
  53. ds = generate_mnist_dataset(data_list, batch_size, sparse=True)
  54. test_images = []
  55. test_labels = []
  56. for data in ds.create_tuple_iterator():
  57. images = data[0].astype(np.float32)
  58. labels = data[1]
  59. test_images.append(images)
  60. test_labels.append(labels)
  61. test_images = np.concatenate(test_images, axis=0)
  62. test_labels = np.concatenate(test_labels, axis=0)
  63. initial_seeds = []
  64. # make initial seeds
  65. for img, label in zip(test_images, test_labels):
  66. initial_seeds.append([img, label, 0])
  67. initial_seeds = initial_seeds[:100]
  68. model_coverage_test.test_adequacy_coverage_calculate(np.array(test_images[:100]).astype(np.float32))
  69. LOGGER.info(TAG, 'KMNC of this test is : %s', model_coverage_test.get_kmnc())
  70. model_fuzz_test = Fuzzing(initial_seeds, model, train_images, 20)
  71. failed_tests = model_fuzz_test.fuzzing()
  72. model_coverage_test.test_adequacy_coverage_calculate(np.array(failed_tests).astype(np.float32))
  73. LOGGER.info(TAG, 'KMNC of this test is : %s', model_coverage_test.get_kmnc())
  74. if __name__ == '__main__':
  75. test_lenet_mnist_fuzzing()

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