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.4 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 numpy as np
  15. from mindspore import Model
  16. from mindspore import context
  17. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  18. from mindarmour.fuzz_testing.model_coverage_metrics import NeuronCoverage, TopKNeuronCoverage, NeuronBoundsCoverage,\
  19. SuperNeuronActivateCoverage, KMultisectionNeuronCoverage
  20. from mindarmour.utils.logger 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 = 'Neuron coverage test'
  25. LOGGER.set_level('INFO')
  26. def test_lenet_mnist_coverage():
  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. # get training data
  34. data_list = "../common/dataset/MNIST/train"
  35. batch_size = 32
  36. ds = generate_mnist_dataset(data_list, batch_size, sparse=True)
  37. train_images = []
  38. for data in ds.create_tuple_iterator(output_numpy=True):
  39. images = data[0].astype(np.float32)
  40. train_images.append(images)
  41. train_images = np.concatenate(train_images, axis=0)
  42. # fuzz test with original test data
  43. # get test data
  44. data_list = "../common/dataset/MNIST/test"
  45. batch_size = 32
  46. ds = generate_mnist_dataset(data_list, batch_size, sparse=True)
  47. test_images = []
  48. test_labels = []
  49. for data in ds.create_tuple_iterator(output_numpy=True):
  50. images = data[0].astype(np.float32)
  51. labels = data[1]
  52. test_images.append(images)
  53. test_labels.append(labels)
  54. test_images = np.concatenate(test_images, axis=0)
  55. # initialize fuzz test with training dataset
  56. nc = NeuronCoverage(model, threshold=0.1)
  57. nc_metric = nc.get_metrics(test_images)
  58. tknc = TopKNeuronCoverage(model, top_k=3)
  59. tknc_metrics = tknc.get_metrics(test_images)
  60. snac = SuperNeuronActivateCoverage(model, train_images)
  61. snac_metrics = snac.get_metrics(test_images)
  62. nbc = NeuronBoundsCoverage(model, train_images)
  63. nbc_metrics = nbc.get_metrics(test_images)
  64. kmnc = KMultisectionNeuronCoverage(model, train_images, segmented_num=100)
  65. kmnc_metrics = kmnc.get_metrics(test_images)
  66. print('KMNC of this test is: ', kmnc_metrics)
  67. print('NBC of this test is: ', nbc_metrics)
  68. print('SNAC of this test is: ', snac_metrics)
  69. print('NC of this test is: ', nc_metric)
  70. print('TKNC of this test is: ', tknc_metrics)
  71. if __name__ == '__main__':
  72. # device_target can be "CPU", "GPU" or "Ascend"
  73. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  74. test_lenet_mnist_coverage()

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