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.

test_coverage_metrics.py 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. """
  15. Model-fuzz coverage test.
  16. """
  17. import numpy as np
  18. import pytest
  19. from mindspore import nn
  20. from mindspore.nn import Cell, SoftmaxCrossEntropyWithLogits
  21. from mindspore.train import Model
  22. from mindspore import context
  23. from mindspore.ops import TensorSummary
  24. from mindarmour.adv_robustness.attacks import FastGradientSignMethod
  25. from mindarmour.utils.logger import LogUtil
  26. from mindarmour.fuzz_testing import NeuronCoverage, TopKNeuronCoverage, SuperNeuronActivateCoverage, \
  27. NeuronBoundsCoverage, KMultisectionNeuronCoverage
  28. LOGGER = LogUtil.get_instance()
  29. TAG = 'Neuron coverage test'
  30. LOGGER.set_level('INFO')
  31. # for user
  32. class Net(Cell):
  33. """
  34. Construct the network of target model.
  35. Examples:
  36. >>> net = Net()
  37. """
  38. def __init__(self):
  39. """
  40. Introduce the layers used for network construction.
  41. """
  42. super(Net, self).__init__()
  43. self._relu = nn.ReLU()
  44. self.summary = TensorSummary()
  45. def construct(self, inputs):
  46. """
  47. Construct network.
  48. Args:
  49. inputs (Tensor): Input data.
  50. """
  51. self.summary('input', inputs)
  52. out = self._relu(inputs)
  53. self.summary('1', out)
  54. return out
  55. @pytest.mark.level0
  56. @pytest.mark.platform_x86_cpu
  57. @pytest.mark.env_card
  58. @pytest.mark.component_mindarmour
  59. def test_lenet_mnist_coverage_cpu():
  60. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  61. # load network
  62. net = Net()
  63. model = Model(net)
  64. # initialize fuzz test with training dataset
  65. training_data = (np.random.random((10000, 10))*20).astype(np.float32)
  66. # fuzz test with original test data
  67. # get test data
  68. test_data = (np.random.random((2000, 10))*20).astype(np.float32)
  69. test_labels = np.random.randint(0, 10, 2000).astype(np.int32)
  70. nc = NeuronCoverage(model, threshold=0.1)
  71. nc_metric = nc.get_metrics(test_data)
  72. tknc = TopKNeuronCoverage(model, top_k=3)
  73. tknc_metrics = tknc.get_metrics(test_data)
  74. snac = SuperNeuronActivateCoverage(model, training_data)
  75. snac_metrics = snac.get_metrics(test_data)
  76. nbc = NeuronBoundsCoverage(model, training_data)
  77. nbc_metrics = nbc.get_metrics(test_data)
  78. kmnc = KMultisectionNeuronCoverage(model, training_data, segmented_num=100)
  79. kmnc_metrics = kmnc.get_metrics(test_data)
  80. print('KMNC of this test is: ', kmnc_metrics)
  81. print('NBC of this test is: ', nbc_metrics)
  82. print('SNAC of this test is: ', snac_metrics)
  83. print('NC of this test is: ', nc_metric)
  84. print('TKNC of this test is: ', tknc_metrics)
  85. # generate adv_data
  86. loss = SoftmaxCrossEntropyWithLogits(sparse=True)
  87. attack = FastGradientSignMethod(net, eps=0.3, loss_fn=loss)
  88. adv_data = attack.batch_generate(test_data, test_labels, batch_size=32)
  89. nc_metric = nc.get_metrics(adv_data)
  90. tknc_metrics = tknc.get_metrics(adv_data)
  91. snac_metrics = snac.get_metrics(adv_data)
  92. nbc_metrics = nbc.get_metrics(adv_data)
  93. kmnc_metrics = kmnc.get_metrics(adv_data)
  94. print('KMNC of adv data is: ', kmnc_metrics)
  95. print('NBC of adv data is: ', nbc_metrics)
  96. print('SNAC of adv data is: ', snac_metrics)
  97. print('NC of adv data is: ', nc_metric)
  98. print('TKNC of adv data is: ', tknc_metrics)
  99. @pytest.mark.level0
  100. @pytest.mark.platform_arm_ascend_training
  101. @pytest.mark.platform_x86_ascend_training
  102. @pytest.mark.env_card
  103. @pytest.mark.component_mindarmour
  104. def test_lenet_mnist_coverage_ascend():
  105. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  106. # load network
  107. net = Net()
  108. model = Model(net)
  109. # initialize fuzz test with training dataset
  110. training_data = (np.random.random((10000, 10))*20).astype(np.float32)
  111. # fuzz test with original test data
  112. # get test data
  113. test_data = (np.random.random((2000, 10))*20).astype(np.float32)
  114. nc = NeuronCoverage(model, threshold=0.1)
  115. nc_metric = nc.get_metrics(test_data)
  116. tknc = TopKNeuronCoverage(model, top_k=3)
  117. tknc_metrics = tknc.get_metrics(test_data)
  118. snac = SuperNeuronActivateCoverage(model, training_data)
  119. snac_metrics = snac.get_metrics(test_data)
  120. nbc = NeuronBoundsCoverage(model, training_data)
  121. nbc_metrics = nbc.get_metrics(test_data)
  122. kmnc = KMultisectionNeuronCoverage(model, training_data, segmented_num=100)
  123. kmnc_metrics = kmnc.get_metrics(test_data)
  124. print('KMNC of this test is: ', kmnc_metrics)
  125. print('NBC of this test is: ', nbc_metrics)
  126. print('SNAC of this test is: ', snac_metrics)
  127. print('NC of this test is: ', nc_metric)
  128. print('TKNC of this test is: ', tknc_metrics)

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