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_attacker.py 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2020 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. attacker test
  16. """
  17. import pytest
  18. import numpy as np
  19. from mindarmour.privacy.evaluation.attacker import get_attack_model
  20. @pytest.mark.level0
  21. @pytest.mark.platform_x86_ascend_training
  22. @pytest.mark.platform_arm_ascend_training
  23. @pytest.mark.env_onecard
  24. @pytest.mark.component_mindarmour
  25. def test_get_knn_model():
  26. features = np.random.randint(0, 10, [100, 10])
  27. labels = np.random.randint(0, 2, [100])
  28. config_knn = {
  29. "method": "KNN",
  30. "params": {
  31. "n_neighbors": [3, 5, 7],
  32. }
  33. }
  34. knn_attacker = get_attack_model(features, labels, config_knn, -1)
  35. pred = knn_attacker.predict(features)
  36. assert pred is not None
  37. @pytest.mark.level0
  38. @pytest.mark.platform_x86_ascend_training
  39. @pytest.mark.platform_arm_ascend_training
  40. @pytest.mark.env_onecard
  41. @pytest.mark.component_mindarmour
  42. def test_get_lr_model():
  43. features = np.random.randint(0, 10, [100, 10])
  44. labels = np.random.randint(0, 2, [100])
  45. config_lr = {
  46. "method": "LR",
  47. "params": {
  48. "C": np.logspace(-4, 2, 10),
  49. }
  50. }
  51. lr_attacker = get_attack_model(features, labels, config_lr, -1)
  52. pred = lr_attacker.predict(features)
  53. assert pred is not None
  54. @pytest.mark.level0
  55. @pytest.mark.platform_x86_ascend_training
  56. @pytest.mark.platform_arm_ascend_training
  57. @pytest.mark.env_onecard
  58. @pytest.mark.component_mindarmour
  59. def test_get_mlp_model():
  60. features = np.random.randint(0, 10, [100, 10])
  61. labels = np.random.randint(0, 2, [100])
  62. config_mlpc = {
  63. "method": "MLP",
  64. "params": {
  65. "hidden_layer_sizes": [(64,), (32, 32)],
  66. "solver": ["adam"],
  67. "alpha": [0.0001, 0.001, 0.01],
  68. }
  69. }
  70. mlpc_attacker = get_attack_model(features, labels, config_mlpc, -1)
  71. pred = mlpc_attacker.predict(features)
  72. assert pred is not None
  73. @pytest.mark.level0
  74. @pytest.mark.platform_x86_ascend_training
  75. @pytest.mark.platform_arm_ascend_training
  76. @pytest.mark.env_onecard
  77. @pytest.mark.component_mindarmour
  78. def test_get_rf_model():
  79. features = np.random.randint(0, 10, [100, 10])
  80. labels = np.random.randint(0, 2, [100])
  81. config_rf = {
  82. "method": "RF",
  83. "params": {
  84. "n_estimators": [100],
  85. "max_features": ["auto", "sqrt"],
  86. "max_depth": [None, 5, 10, 20],
  87. "min_samples_split": [2, 5, 10],
  88. "min_samples_leaf": [1, 2, 4],
  89. }
  90. }
  91. rf_attacker = get_attack_model(features, labels, config_rf, -1)
  92. pred = rf_attacker.predict(features)
  93. assert pred is not None

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