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

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