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_spatial_smoothing.py 3.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. Spatial-smoothing detector test.
  16. """
  17. import numpy as np
  18. import pytest
  19. import mindspore.ops.operations as M
  20. from mindspore import Model
  21. from mindspore.nn import Cell
  22. from mindspore import context
  23. from mindarmour.detectors.spatial_smoothing import SpatialSmoothing
  24. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  25. # for use
  26. class Net(Cell):
  27. """
  28. Construct the network of target model.
  29. """
  30. def __init__(self):
  31. super(Net, self).__init__()
  32. self._softmax = M.Softmax()
  33. def construct(self, inputs):
  34. """
  35. Construct network.
  36. Args:
  37. inputs (Tensor): Input data.
  38. """
  39. return self._softmax(inputs)
  40. @pytest.mark.level0
  41. @pytest.mark.platform_arm_ascend_training
  42. @pytest.mark.platform_x86_ascend_training
  43. @pytest.mark.env_card
  44. @pytest.mark.component_mindarmour
  45. def test_spatial_smoothing():
  46. """
  47. Compute mindspore result.
  48. """
  49. input_shape = (50, 3)
  50. np.random.seed(1)
  51. input_np = np.random.randn(*input_shape).astype(np.float32)
  52. np.random.seed(2)
  53. adv_np = np.random.randn(*input_shape).astype(np.float32)
  54. # mock user model
  55. model = Model(Net())
  56. detector = SpatialSmoothing(model)
  57. # Training
  58. threshold = detector.fit(input_np)
  59. detector.set_threshold(threshold.item())
  60. detected_res = np.array(detector.detect(adv_np))
  61. idx = np.where(detected_res > 0)
  62. expected_value = np.array([10, 39, 48])
  63. assert np.all(idx == expected_value)
  64. @pytest.mark.level0
  65. @pytest.mark.platform_arm_ascend_training
  66. @pytest.mark.platform_x86_ascend_training
  67. @pytest.mark.env_card
  68. @pytest.mark.component_mindarmour
  69. def test_spatial_smoothing_diff():
  70. """
  71. Compute mindspore result.
  72. """
  73. input_shape = (50, 3)
  74. np.random.seed(1)
  75. input_np = np.random.randn(*input_shape).astype(np.float32)
  76. np.random.seed(2)
  77. adv_np = np.random.randn(*input_shape).astype(np.float32)
  78. # mock user model
  79. model = Model(Net())
  80. detector = SpatialSmoothing(model)
  81. # Training
  82. detector.fit(input_np)
  83. diffs = detector.detect_diff(adv_np)
  84. expected_value = np.array([0.20959496, 0.69537306, 0.13034256, 0.7421039,
  85. 0.41419053, 0.56346416, 0.4277994, 0.3240941,
  86. 0.048190027, 0.6806958, 1.1405756, 0.587804,
  87. 0.40533313, 0.2875523, 0.36801508, 0.61993587,
  88. 0.49286827, 0.13222921, 0.68012404, 0.4164942,
  89. 0.25758877, 0.6008735, 0.60623455, 0.34981924,
  90. 0.3945489, 0.879787, 0.3934811, 0.23387678,
  91. 0.63480926, 0.56435543, 0.16067612, 0.57489645,
  92. 0.21772699, 0.55924356, 0.5186635, 0.7094835,
  93. 0.0613693, 0.13305652, 0.11505881, 1.2404268,
  94. 0.50948, 0.15797901, 0.44473758, 0.2495422,
  95. 0.38254014, 0.543059, 0.06452079, 0.36902517,
  96. 1.1845329, 0.3870097])
  97. assert np.allclose(diffs, expected_value, 0.0001, 0.0001)

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