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

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 mindarmour.adv_robustness.attacks import FastGradientSignMethod
  24. from mindarmour.utils.logger import LogUtil
  25. from mindarmour.fuzz_testing import ModelCoverageMetrics
  26. LOGGER = LogUtil.get_instance()
  27. TAG = 'Neuron coverage test'
  28. LOGGER.set_level('INFO')
  29. # for user
  30. class Net(Cell):
  31. """
  32. Construct the network of target model.
  33. Examples:
  34. >>> net = Net()
  35. """
  36. def __init__(self):
  37. """
  38. Introduce the layers used for network construction.
  39. """
  40. super(Net, self).__init__()
  41. self._relu = nn.ReLU()
  42. def construct(self, inputs):
  43. """
  44. Construct network.
  45. Args:
  46. inputs (Tensor): Input data.
  47. """
  48. out = self._relu(inputs)
  49. return out
  50. @pytest.mark.level0
  51. @pytest.mark.platform_x86_cpu
  52. @pytest.mark.env_card
  53. @pytest.mark.component_mindarmour
  54. def test_lenet_mnist_coverage_cpu():
  55. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  56. # load network
  57. net = Net()
  58. model = Model(net)
  59. # initialize fuzz test with training dataset
  60. training_data = (np.random.random((10000, 10))*20).astype(np.float32)
  61. model_fuzz_test = ModelCoverageMetrics(model, 10, 1000, training_data)
  62. # fuzz test with original test data
  63. # get test data
  64. test_data = (np.random.random((2000, 10))*20).astype(np.float32)
  65. test_labels = np.random.randint(0, 10, 2000).astype(np.int32)
  66. model_fuzz_test.calculate_coverage(test_data)
  67. LOGGER.info(TAG, 'KMNC of this test is : %s', model_fuzz_test.get_kmnc())
  68. LOGGER.info(TAG, 'NBC of this test is : %s', model_fuzz_test.get_nbc())
  69. LOGGER.info(TAG, 'SNAC of this test is : %s', model_fuzz_test.get_snac())
  70. # generate adv_data
  71. loss = SoftmaxCrossEntropyWithLogits(sparse=True)
  72. attack = FastGradientSignMethod(net, eps=0.3, loss_fn=loss)
  73. adv_data = attack.batch_generate(test_data, test_labels, batch_size=32)
  74. model_fuzz_test.calculate_coverage(adv_data, bias_coefficient=0.5)
  75. LOGGER.info(TAG, 'KMNC of this test is : %s', model_fuzz_test.get_kmnc())
  76. LOGGER.info(TAG, 'NBC of this test is : %s', model_fuzz_test.get_nbc())
  77. LOGGER.info(TAG, 'SNAC of this test is : %s', model_fuzz_test.get_snac())
  78. @pytest.mark.level0
  79. @pytest.mark.platform_arm_ascend_training
  80. @pytest.mark.platform_x86_ascend_training
  81. @pytest.mark.env_card
  82. @pytest.mark.component_mindarmour
  83. def test_lenet_mnist_coverage_ascend():
  84. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  85. # load network
  86. net = Net()
  87. model = Model(net)
  88. # initialize fuzz test with training dataset
  89. training_data = (np.random.random((10000, 10))*20).astype(np.float32)
  90. model_fuzz_test = ModelCoverageMetrics(model, 10, 1000, training_data)
  91. # fuzz test with original test data
  92. # get test data
  93. test_data = (np.random.random((2000, 10))*20).astype(np.float32)
  94. test_labels = np.random.randint(0, 10, 2000)
  95. test_labels = (np.eye(10)[test_labels]).astype(np.float32)
  96. model_fuzz_test.calculate_coverage(test_data)
  97. LOGGER.info(TAG, 'KMNC of this test is : %s', model_fuzz_test.get_kmnc())
  98. LOGGER.info(TAG, 'NBC of this test is : %s', model_fuzz_test.get_nbc())
  99. LOGGER.info(TAG, 'SNAC of this test is : %s', model_fuzz_test.get_snac())
  100. # generate adv_data
  101. attack = FastGradientSignMethod(net, eps=0.3, loss_fn=nn.SoftmaxCrossEntropyWithLogits(sparse=False))
  102. adv_data = attack.batch_generate(test_data, test_labels, batch_size=32)
  103. model_fuzz_test.calculate_coverage(adv_data, bias_coefficient=0.5)
  104. LOGGER.info(TAG, 'KMNC of this test is : %s', model_fuzz_test.get_kmnc())
  105. LOGGER.info(TAG, 'NBC of this test is : %s', model_fuzz_test.get_nbc())
  106. LOGGER.info(TAG, 'SNAC of this test is : %s', model_fuzz_test.get_snac())

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