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_coverage.py 3.5 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.nn import SoftmaxCrossEntropyWithLogits
  18. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  19. from mindarmour.adv_robustness.attacks import FastGradientSignMethod
  20. from mindarmour.fuzz_testing import ModelCoverageMetrics
  21. from mindarmour.utils.logger import LogUtil
  22. from examples.common.dataset.data_processing import generate_mnist_dataset
  23. from examples.common.networks.lenet5.lenet5_net import LeNet5
  24. LOGGER = LogUtil.get_instance()
  25. TAG = 'Neuron coverage test'
  26. LOGGER.set_level('INFO')
  27. def test_lenet_mnist_coverage():
  28. # upload trained network
  29. ckpt_path = '../common/networks/lenet5/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
  30. net = LeNet5()
  31. load_dict = load_checkpoint(ckpt_path)
  32. load_param_into_net(net, load_dict)
  33. model = Model(net)
  34. # get training data
  35. data_list = "../common/dataset/MNIST/train"
  36. batch_size = 32
  37. ds = generate_mnist_dataset(data_list, batch_size, sparse=True)
  38. train_images = []
  39. for data in ds.create_tuple_iterator():
  40. images = data[0].astype(np.float32)
  41. train_images.append(images)
  42. train_images = np.concatenate(train_images, axis=0)
  43. # initialize fuzz test with training dataset
  44. model_fuzz_test = ModelCoverageMetrics(model, 10, 1000, train_images)
  45. # fuzz test with original test data
  46. # get test data
  47. data_list = "../common/dataset/MNIST/test"
  48. batch_size = 32
  49. ds = generate_mnist_dataset(data_list, batch_size, sparse=True)
  50. test_images = []
  51. test_labels = []
  52. for data in ds.create_tuple_iterator():
  53. images = data[0].astype(np.float32)
  54. labels = data[1]
  55. test_images.append(images)
  56. test_labels.append(labels)
  57. test_images = np.concatenate(test_images, axis=0)
  58. test_labels = np.concatenate(test_labels, axis=0)
  59. model_fuzz_test.calculate_coverage(test_images)
  60. LOGGER.info(TAG, 'KMNC of this test is : %s', model_fuzz_test.get_kmnc())
  61. LOGGER.info(TAG, 'NBC of this test is : %s', model_fuzz_test.get_nbc())
  62. LOGGER.info(TAG, 'SNAC of this test is : %s', model_fuzz_test.get_snac())
  63. # generate adv_data
  64. loss = SoftmaxCrossEntropyWithLogits(sparse=True)
  65. attack = FastGradientSignMethod(net, eps=0.3, loss_fn=loss)
  66. adv_data = attack.batch_generate(test_images, test_labels, batch_size=32)
  67. model_fuzz_test.calculate_coverage(adv_data, bias_coefficient=0.5)
  68. LOGGER.info(TAG, 'KMNC of this adv data is : %s', model_fuzz_test.get_kmnc())
  69. LOGGER.info(TAG, 'NBC of this adv data is : %s', model_fuzz_test.get_nbc())
  70. LOGGER.info(TAG, 'SNAC of this adv data is : %s', model_fuzz_test.get_snac())
  71. if __name__ == '__main__':
  72. # device_target can be "CPU", "GPU" or "Ascend"
  73. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  74. test_lenet_mnist_coverage()

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