Browse Source

adjust the project structure according to diff features

tags/v1.0.0
pkuliuliu 4 years ago
parent
commit
1e0bc59251
100 changed files with 417 additions and 186 deletions
  1. +1
    -1
      example/membership_inference_demo/main.py
  2. +4
    -4
      example/mnist_demo/lenet5_dp.py
  3. +3
    -3
      example/mnist_demo/lenet5_dp_ada_gaussian.py
  4. +3
    -3
      example/mnist_demo/lenet5_dp_ada_sgd_graph.py
  5. +4
    -4
      example/mnist_demo/lenet5_dp_optimizer.py
  6. +2
    -2
      example/mnist_demo/lenet5_mnist_coverage.py
  7. +2
    -2
      example/mnist_demo/lenet5_mnist_fuzzing.py
  8. +2
    -2
      example/mnist_demo/mnist_attack_cw.py
  9. +2
    -2
      example/mnist_demo/mnist_attack_deepfool.py
  10. +2
    -2
      example/mnist_demo/mnist_attack_fgsm.py
  11. +3
    -3
      example/mnist_demo/mnist_attack_genetic.py
  12. +2
    -2
      example/mnist_demo/mnist_attack_hsja.py
  13. +2
    -2
      example/mnist_demo/mnist_attack_jsma.py
  14. +2
    -2
      example/mnist_demo/mnist_attack_lbfgs.py
  15. +2
    -3
      example/mnist_demo/mnist_attack_mdi2fgsm.py
  16. +2
    -2
      example/mnist_demo/mnist_attack_nes.py
  17. +2
    -2
      example/mnist_demo/mnist_attack_pgd.py
  18. +3
    -3
      example/mnist_demo/mnist_attack_pointwise.py
  19. +3
    -3
      example/mnist_demo/mnist_attack_pso.py
  20. +3
    -3
      example/mnist_demo/mnist_attack_salt_and_pepper.py
  21. +2
    -2
      example/mnist_demo/mnist_defense_nad.py
  22. +7
    -7
      example/mnist_demo/mnist_evaluation.py
  23. +3
    -3
      example/mnist_demo/mnist_similarity_detector.py
  24. +13
    -7
      mindarmour/__init__.py
  25. +19
    -0
      mindarmour/adv_robustness/__init__.py
  26. +13
    -0
      mindarmour/adv_robustness/attacks/__init__.py
  27. +0
    -0
      mindarmour/adv_robustness/attacks/attack.py
  28. +16
    -0
      mindarmour/adv_robustness/attacks/black/__init__.py
  29. +0
    -0
      mindarmour/adv_robustness/attacks/black/black_model.py
  30. +2
    -3
      mindarmour/adv_robustness/attacks/black/genetic_attack.py
  31. +3
    -2
      mindarmour/adv_robustness/attacks/black/hop_skip_jump_attack.py
  32. +2
    -3
      mindarmour/adv_robustness/attacks/black/natural_evolutionary_strategy.py
  33. +3
    -4
      mindarmour/adv_robustness/attacks/black/pointwise_attack.py
  34. +2
    -3
      mindarmour/adv_robustness/attacks/black/pso_attack.py
  35. +2
    -2
      mindarmour/adv_robustness/attacks/black/salt_and_pepper_attack.py
  36. +2
    -3
      mindarmour/adv_robustness/attacks/carlini_wagner.py
  37. +2
    -3
      mindarmour/adv_robustness/attacks/deep_fool.py
  38. +3
    -5
      mindarmour/adv_robustness/attacks/gradient_method.py
  39. +4
    -6
      mindarmour/adv_robustness/attacks/iterative_gradient_method.py
  40. +2
    -4
      mindarmour/adv_robustness/attacks/jsma.py
  41. +2
    -3
      mindarmour/adv_robustness/attacks/lbfgs.py
  42. +13
    -0
      mindarmour/adv_robustness/defenses/__init__.py
  43. +3
    -4
      mindarmour/adv_robustness/defenses/adversarial_defense.py
  44. +0
    -0
      mindarmour/adv_robustness/defenses/defense.py
  45. +2
    -3
      mindarmour/adv_robustness/defenses/natural_adversarial_defense.py
  46. +2
    -4
      mindarmour/adv_robustness/defenses/projected_adversarial_defense.py
  47. +13
    -0
      mindarmour/adv_robustness/detectors/__init__.py
  48. +16
    -0
      mindarmour/adv_robustness/detectors/black/__init__.py
  49. +1
    -1
      mindarmour/adv_robustness/detectors/black/similarity_detector.py
  50. +0
    -0
      mindarmour/adv_robustness/detectors/detector.py
  51. +1
    -2
      mindarmour/adv_robustness/detectors/ensemble_detector.py
  52. +1
    -1
      mindarmour/adv_robustness/detectors/mag_net.py
  53. +1
    -1
      mindarmour/adv_robustness/detectors/region_based_detector.py
  54. +1
    -1
      mindarmour/adv_robustness/detectors/spatial_smoothing.py
  55. +27
    -0
      mindarmour/adv_robustness/evaluations/__init__.py
  56. +0
    -0
      mindarmour/adv_robustness/evaluations/attack_evaluation.py
  57. +13
    -0
      mindarmour/adv_robustness/evaluations/black/__init__.py
  58. +0
    -0
      mindarmour/adv_robustness/evaluations/black/defense_evaluation.py
  59. +0
    -0
      mindarmour/adv_robustness/evaluations/defense_evaluation.py
  60. +0
    -0
      mindarmour/adv_robustness/evaluations/visual_metrics.py
  61. +0
    -0
      mindarmour/attacks/black/__init__.py
  62. +0
    -0
      mindarmour/detectors/black/__init__.py
  63. +0
    -0
      mindarmour/diff_privacy/evaluation/__init__.py
  64. +0
    -0
      mindarmour/diff_privacy/mechanisms/__init__.py
  65. +0
    -0
      mindarmour/diff_privacy/monitor/__init__.py
  66. +0
    -0
      mindarmour/diff_privacy/optimizer/__init__.py
  67. +0
    -0
      mindarmour/diff_privacy/train/__init__.py
  68. +0
    -14
      mindarmour/evaluations/__init__.py
  69. +0
    -0
      mindarmour/evaluations/black/__init__.py
  70. +22
    -0
      mindarmour/fuzz_testing/__init__.py
  71. +10
    -10
      mindarmour/fuzz_testing/fuzzing.py
  72. +0
    -0
      mindarmour/fuzz_testing/image_transform.py
  73. +0
    -0
      mindarmour/fuzz_testing/model_coverage_metrics.py
  74. +0
    -8
      mindarmour/fuzzing/__init__.py
  75. +18
    -0
      mindarmour/privacy/__init__.py
  76. +15
    -4
      mindarmour/privacy/diff_privacy/__init__.py
  77. +16
    -0
      mindarmour/privacy/diff_privacy/mechanisms/__init__.py
  78. +0
    -0
      mindarmour/privacy/diff_privacy/mechanisms/mechanisms.py
  79. +16
    -0
      mindarmour/privacy/diff_privacy/monitor/__init__.py
  80. +0
    -0
      mindarmour/privacy/diff_privacy/monitor/monitor.py
  81. +16
    -0
      mindarmour/privacy/diff_privacy/optimizer/__init__.py
  82. +2
    -2
      mindarmour/privacy/diff_privacy/optimizer/optimizer.py
  83. +16
    -0
      mindarmour/privacy/diff_privacy/train/__init__.py
  84. +1
    -2
      mindarmour/privacy/diff_privacy/train/model.py
  85. +21
    -0
      mindarmour/privacy/evaluation/__init__.py
  86. +0
    -0
      mindarmour/privacy/evaluation/_check_config.py
  87. +0
    -0
      mindarmour/privacy/evaluation/attacker.py
  88. +0
    -0
      mindarmour/privacy/evaluation/membership_inference.py
  89. +2
    -1
      mindarmour/utils/__init__.py
  90. +1
    -1
      mindarmour/utils/_check_param.py
  91. +1
    -1
      mindarmour/utils/util.py
  92. +1
    -1
      tests/st/resnet50/test_cifar10_attack_fgsm.py
  93. +2
    -2
      tests/ut/python/attacks/black/test_genetic_attack.py
  94. +2
    -2
      tests/ut/python/attacks/black/test_hsja.py
  95. +2
    -2
      tests/ut/python/attacks/black/test_nes.py
  96. +2
    -2
      tests/ut/python/attacks/black/test_pointwise_attack.py
  97. +2
    -2
      tests/ut/python/attacks/black/test_pso_attack.py
  98. +2
    -3
      tests/ut/python/attacks/black/test_salt_and_pepper_attack.py
  99. +1
    -1
      tests/ut/python/attacks/test_batch_generate_attack.py
  100. +1
    -1
      tests/ut/python/attacks/test_cw.py

+ 1
- 1
example/membership_inference_demo/main.py View File

@@ -28,7 +28,7 @@ import numpy as np
from mindspore.train import Model
from mindspore.train.serialization import load_param_into_net, load_checkpoint
import mindspore.nn as nn
from mindarmour.diff_privacy.evaluation.membership_inference import MembershipInference
from mindarmour.privacy.evaluation import MembershipInference
from mindarmour.utils import LogUtil
logging = LogUtil.get_instance()
logging.set_level(20)


+ 4
- 4
example/mnist_demo/lenet5_dp.py View File

@@ -29,10 +29,10 @@ import mindspore.dataset.transforms.c_transforms as C
from mindspore.dataset.transforms.vision import Inter
import mindspore.common.dtype as mstype

from mindarmour.diff_privacy import DPModel
from mindarmour.diff_privacy import PrivacyMonitorFactory
from mindarmour.diff_privacy import NoiseMechanismsFactory
from mindarmour.diff_privacy import ClipMechanismsFactory
from mindarmour.privacy.diff_privacy import DPModel
from mindarmour.privacy.diff_privacy import PrivacyMonitorFactory
from mindarmour.privacy.diff_privacy import NoiseMechanismsFactory
from mindarmour.privacy.diff_privacy import ClipMechanismsFactory
from mindarmour.utils.logger import LogUtil
from lenet5_net import LeNet5
from lenet5_config import mnist_cfg as cfg


+ 3
- 3
example/mnist_demo/lenet5_dp_ada_gaussian.py View File

@@ -29,9 +29,9 @@ import mindspore.dataset.transforms.c_transforms as C
from mindspore.dataset.transforms.vision import Inter
import mindspore.common.dtype as mstype

from mindarmour.diff_privacy import DPModel
from mindarmour.diff_privacy import PrivacyMonitorFactory
from mindarmour.diff_privacy import NoiseMechanismsFactory
from mindarmour.privacy.diff_privacy import DPModel
from mindarmour.privacy.diff_privacy import PrivacyMonitorFactory
from mindarmour.privacy.diff_privacy import NoiseMechanismsFactory
from mindarmour.utils.logger import LogUtil
from lenet5_net import LeNet5
from dp_ada_gaussian_config import mnist_cfg as cfg


+ 3
- 3
example/mnist_demo/lenet5_dp_ada_sgd_graph.py View File

@@ -29,9 +29,9 @@ import mindspore.dataset.transforms.c_transforms as C
from mindspore.dataset.transforms.vision import Inter
import mindspore.common.dtype as mstype

from mindarmour.diff_privacy import DPModel
from mindarmour.diff_privacy import PrivacyMonitorFactory
from mindarmour.diff_privacy import NoiseMechanismsFactory
from mindarmour.privacy.diff_privacy import DPModel
from mindarmour.privacy.diff_privacy import PrivacyMonitorFactory
from mindarmour.privacy.diff_privacy import NoiseMechanismsFactory
from mindarmour.utils.logger import LogUtil
from lenet5_net import LeNet5
from dp_ada_sgd_graph_config import mnist_cfg as cfg


+ 4
- 4
example/mnist_demo/lenet5_dp_optimizer.py View File

@@ -29,10 +29,10 @@ import mindspore.dataset.transforms.c_transforms as C
from mindspore.dataset.transforms.vision import Inter
import mindspore.common.dtype as mstype

from mindarmour.diff_privacy import DPModel
from mindarmour.diff_privacy import PrivacyMonitorFactory
from mindarmour.diff_privacy import DPOptimizerClassFactory
from mindarmour.diff_privacy import ClipMechanismsFactory
from mindarmour.privacy.diff_privacy import DPModel
from mindarmour.privacy.diff_privacy import PrivacyMonitorFactory
from mindarmour.privacy.diff_privacy import DPOptimizerClassFactory
from mindarmour.privacy.diff_privacy import ClipMechanismsFactory
from mindarmour.utils.logger import LogUtil
from lenet5_net import LeNet5
from lenet5_config import mnist_cfg as cfg


+ 2
- 2
example/mnist_demo/lenet5_mnist_coverage.py View File

@@ -20,8 +20,8 @@ from mindspore.nn import SoftmaxCrossEntropyWithLogits
from mindspore.train.serialization import load_checkpoint, load_param_into_net

from lenet5_net import LeNet5
from mindarmour.attacks.gradient_method import FastGradientSignMethod
from mindarmour.fuzzing.model_coverage_metrics import ModelCoverageMetrics
from mindarmour.adv_robustness.attacks import FastGradientSignMethod
from mindarmour.fuzz_testing import ModelCoverageMetrics
from mindarmour.utils.logger import LogUtil

sys.path.append("..")


+ 2
- 2
example/mnist_demo/lenet5_mnist_fuzzing.py View File

@@ -19,8 +19,8 @@ from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net

from lenet5_net import LeNet5
from mindarmour.fuzzing.fuzzing import Fuzzer
from mindarmour.fuzzing.model_coverage_metrics import ModelCoverageMetrics
from mindarmour.fuzz_testing import Fuzzer
from mindarmour.fuzz_testing import ModelCoverageMetrics
from mindarmour.utils.logger import LogUtil

sys.path.append("..")


+ 2
- 2
example/mnist_demo/mnist_attack_cw.py View File

@@ -22,8 +22,8 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.carlini_wagner import CarliniWagnerL2Attack
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour.adv_robustness.attacks import CarliniWagnerL2Attack
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil




+ 2
- 2
example/mnist_demo/mnist_attack_deepfool.py View File

@@ -22,8 +22,8 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.deep_fool import DeepFool
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour.adv_robustness.attacks.deep_fool import DeepFool
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil




+ 2
- 2
example/mnist_demo/mnist_attack_fgsm.py View File

@@ -23,8 +23,8 @@ from mindspore.nn import SoftmaxCrossEntropyWithLogits
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.gradient_method import FastGradientSignMethod
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour.adv_robustness.attacks import FastGradientSignMethod
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil




+ 3
- 3
example/mnist_demo/mnist_attack_genetic.py View File

@@ -21,9 +21,9 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.genetic_attack import GeneticAttack
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour.adv_robustness.attacks import BlackModel
from mindarmour.adv_robustness.attacks.black.genetic_attack import GeneticAttack
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil




+ 2
- 2
example/mnist_demo/mnist_attack_hsja.py View File

@@ -19,8 +19,8 @@ from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net

from lenet5_net import LeNet5
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.hop_skip_jump_attack import HopSkipJumpAttack
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import HopSkipJumpAttack
from mindarmour.utils.logger import LogUtil

sys.path.append("..")


+ 2
- 2
example/mnist_demo/mnist_attack_jsma.py View File

@@ -22,8 +22,8 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.jsma import JSMAAttack
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour.adv_robustness.attacks import JSMAAttack
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil




+ 2
- 2
example/mnist_demo/mnist_attack_lbfgs.py View File

@@ -23,8 +23,8 @@ from mindspore.nn import SoftmaxCrossEntropyWithLogits
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.lbfgs import LBFGS
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour.adv_robustness.attacks import LBFGS
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil




+ 2
- 3
example/mnist_demo/mnist_attack_mdi2fgsm.py View File

@@ -23,9 +23,8 @@ from mindspore.nn import SoftmaxCrossEntropyWithLogits
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.iterative_gradient_method import \
MomentumDiverseInputIterativeMethod
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour.adv_robustness.attacks import MomentumDiverseInputIterativeMethod
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil




+ 2
- 2
example/mnist_demo/mnist_attack_nes.py View File

@@ -19,8 +19,8 @@ from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net

from lenet5_net import LeNet5
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.natural_evolutionary_strategy import NES
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import NES
from mindarmour.utils.logger import LogUtil

sys.path.append("..")


+ 2
- 2
example/mnist_demo/mnist_attack_pgd.py View File

@@ -23,8 +23,8 @@ from mindspore.nn import SoftmaxCrossEntropyWithLogits
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.iterative_gradient_method import ProjectedGradientDescent
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour.adv_robustness.attacks import ProjectedGradientDescent
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil




+ 3
- 3
example/mnist_demo/mnist_attack_pointwise.py View File

@@ -20,9 +20,9 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.pointwise_attack import PointWiseAttack
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import PointWiseAttack
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil

sys.path.append("..")


+ 3
- 3
example/mnist_demo/mnist_attack_pso.py View File

@@ -21,9 +21,9 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.pso_attack import PSOAttack
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks.black.pso_attack import PSOAttack
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil




+ 3
- 3
example/mnist_demo/mnist_attack_salt_and_pepper.py View File

@@ -20,9 +20,9 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.salt_and_pepper_attack import SaltAndPepperNoiseAttack
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import SaltAndPepperNoiseAttack
from mindarmour.adv_robustness.evaluations import AttackEvaluate
from mindarmour.utils.logger import LogUtil

sys.path.append("..")


+ 2
- 2
example/mnist_demo/mnist_defense_nad.py View File

@@ -23,8 +23,8 @@ from mindspore.nn import SoftmaxCrossEntropyWithLogits
from mindspore.train import Model
from mindspore.train.callback import LossMonitor

from mindarmour.attacks import FastGradientSignMethod
from mindarmour.defenses import NaturalAdversarialDefense
from mindarmour.adv_robustness.attacks import FastGradientSignMethod
from mindarmour.adv_robustness.defenses import NaturalAdversarialDefense
from mindarmour.utils.logger import LogUtil

from lenet5_net import LeNet5


+ 7
- 7
example/mnist_demo/mnist_evaluation.py View File

@@ -28,13 +28,13 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from scipy.special import softmax
from lenet5_net import LeNet5
from mindarmour.attacks import FastGradientSignMethod
from mindarmour.attacks import GeneticAttack
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.defenses import NaturalAdversarialDefense
from mindarmour.detectors.black.similarity_detector import SimilarityDetector
from mindarmour.evaluations import BlackDefenseEvaluate
from mindarmour.evaluations import DefenseEvaluate
from mindarmour.adv_robustness.attacks import FastGradientSignMethod
from mindarmour.adv_robustness.attacks import GeneticAttack
from mindarmour.adv_robustness.attacks import BlackModel
from mindarmour.adv_robustness.defenses import NaturalAdversarialDefense
from mindarmour.adv_robustness.detectors import SimilarityDetector
from mindarmour.adv_robustness.evaluations import BlackDefenseEvaluate
from mindarmour.adv_robustness.evaluations import DefenseEvaluate
from mindarmour.utils.logger import LogUtil
sys.path.append("..")


+ 3
- 3
example/mnist_demo/mnist_similarity_detector.py View File

@@ -23,9 +23,9 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from scipy.special import softmax

from lenet5_net import LeNet5
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.pso_attack import PSOAttack
from mindarmour.detectors.black.similarity_detector import SimilarityDetector
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks.black.pso_attack import PSOAttack
from mindarmour.adv_robustness.detectors import SimilarityDetector
from mindarmour.utils.logger import LogUtil




+ 13
- 7
mindarmour/__init__.py View File

@@ -1,13 +1,19 @@
"""
MindArmour, a tool box of MindSpore to enhance model security and
trustworthiness against adversarial examples.
MindArmour, a tool box of MindSpore to enhance model trustworthiness and achieve
privacy-preserving machine learning.
"""
from .attacks import Attack
from .attacks.black.black_model import BlackModel
from .defenses.defense import Defense
from .detectors.detector import Detector
from .adv_robustness.attacks import Attack
from .adv_robustness.attacks.black.black_model import BlackModel
from .adv_robustness.defenses.defense import Defense
from .adv_robustness.detectors.detector import Detector
from .fuzz_testing.fuzzing import Fuzzer
from .privacy.diff_privacy import DPModel
from .privacy.evaluation.membership_inference import MembershipInference

__all__ = ['Attack',
'BlackModel',
'Detector',
'Defense']
'Defense',
'Fuzzer',
'DPModel',
'MembershipInference']

+ 19
- 0
mindarmour/adv_robustness/__init__.py View File

@@ -0,0 +1,19 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Adversarial Robustness.

This module is a tool box to enhance model security and against adversarial
examples.
"""

mindarmour/attacks/__init__.py → mindarmour/adv_robustness/attacks/__init__.py View File

@@ -1,3 +1,16 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module includes classical black-box and white-box attack algorithms
in making adversarial examples.

mindarmour/attacks/attack.py → mindarmour/adv_robustness/attacks/attack.py View File


+ 16
- 0
mindarmour/adv_robustness/attacks/black/__init__.py View File

@@ -0,0 +1,16 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module is a submodule, which provides some black-box based adversarial method.
"""

mindarmour/attacks/black/black_model.py → mindarmour/adv_robustness/attacks/black/black_model.py View File


mindarmour/attacks/black/genetic_attack.py → mindarmour/adv_robustness/attacks/black/genetic_attack.py View File

@@ -17,13 +17,12 @@ Genetic-Attack.
import numpy as np
from scipy.special import softmax

from mindarmour.attacks.attack import Attack
from mindarmour.utils.logger import LogUtil
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.utils._check_param import check_numpy_param, check_model, \
check_pair_numpy_param, check_param_type, check_value_positive, \
check_int_positive, check_param_multi_types

from ..attack import Attack
from .black_model import BlackModel

LOGGER = LogUtil.get_instance()
TAG = 'GeneticAttack'

mindarmour/attacks/black/hop_skip_jump_attack.py → mindarmour/adv_robustness/attacks/black/hop_skip_jump_attack.py View File

@@ -16,12 +16,13 @@ Hop-skip-jump attack.
"""
import numpy as np

from mindarmour.attacks.attack import Attack
from mindarmour.utils.logger import LogUtil
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.utils._check_param import check_pair_numpy_param, check_model, \
check_numpy_param, check_int_positive, check_value_positive, \
check_value_non_negative, check_param_type
from ..attack import Attack
from .black_model import BlackModel

LOGGER = LogUtil.get_instance()
TAG = 'HopSkipJumpAttack'

mindarmour/attacks/black/natural_evolutionary_strategy.py → mindarmour/adv_robustness/attacks/black/natural_evolutionary_strategy.py View File

@@ -18,12 +18,11 @@ import time
import numpy as np
from scipy.special import softmax

from mindarmour.attacks.attack import Attack
from mindarmour.utils.logger import LogUtil
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.utils._check_param import check_pair_numpy_param, check_model, \
check_numpy_param, check_int_positive, check_value_positive, check_param_type

from ..attack import Attack
from .black_model import BlackModel

LOGGER = LogUtil.get_instance()
TAG = 'NES'

mindarmour/attacks/black/pointwise_attack.py → mindarmour/adv_robustness/attacks/black/pointwise_attack.py View File

@@ -16,13 +16,12 @@ Pointwise-Attack.
"""
import numpy as np

from mindarmour.attacks.attack import Attack
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.salt_and_pepper_attack import \
SaltAndPepperNoiseAttack
from mindarmour.utils._check_param import check_model, check_pair_numpy_param, \
check_int_positive, check_param_type
from mindarmour.utils.logger import LogUtil
from ..attack import Attack
from .black_model import BlackModel
from .salt_and_pepper_attack import SaltAndPepperNoiseAttack

LOGGER = LogUtil.get_instance()
TAG = 'PointWiseAttack'

mindarmour/attacks/black/pso_attack.py → mindarmour/adv_robustness/attacks/black/pso_attack.py View File

@@ -16,13 +16,12 @@ PSO-Attack.
"""
import numpy as np

from mindarmour.attacks.attack import Attack
from mindarmour.utils.logger import LogUtil
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.utils._check_param import check_model, check_pair_numpy_param, \
check_numpy_param, check_value_positive, check_int_positive, \
check_param_type, check_equal_shape, check_param_multi_types

from ..attack import Attack
from .black_model import BlackModel

LOGGER = LogUtil.get_instance()
TAG = 'PSOAttack'

mindarmour/attacks/black/salt_and_pepper_attack.py → mindarmour/adv_robustness/attacks/black/salt_and_pepper_attack.py View File

@@ -18,12 +18,12 @@ import time

import numpy as np

from mindarmour.attacks.attack import Attack
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.utils._check_param import check_model, check_pair_numpy_param, \
check_param_type, check_int_positive, check_param_multi_types
from mindarmour.utils._check_param import normalize_value
from mindarmour.utils.logger import LogUtil
from ..attack import Attack
from .black_model import BlackModel

LOGGER = LogUtil.get_instance()
TAG = 'SaltAndPepperNoise-Attack'

mindarmour/attacks/carlini_wagner.py → mindarmour/adv_robustness/attacks/carlini_wagner.py View File

@@ -19,13 +19,12 @@ import numpy as np
from mindspore import Tensor
from mindspore.nn import Cell

from mindarmour.attacks.attack import Attack
from mindarmour.utils.logger import LogUtil
from mindarmour.utils._check_param import check_numpy_param, check_model, \
check_pair_numpy_param, check_int_positive, check_param_type, \
check_param_multi_types, check_value_positive, check_equal_shape
from mindarmour.utils.util import GradWrap
from mindarmour.utils.util import jacobian_matrix
from mindarmour.utils.util import GradWrap, jacobian_matrix
from .attack import Attack

LOGGER = LogUtil.get_instance()
TAG = 'CW'

mindarmour/attacks/deep_fool.py → mindarmour/adv_robustness/attacks/deep_fool.py View File

@@ -19,13 +19,12 @@ import numpy as np
from mindspore import Tensor
from mindspore.nn import Cell

from mindarmour.attacks.attack import Attack
from mindarmour.utils.logger import LogUtil
from mindarmour.utils.util import GradWrap
from mindarmour.utils.util import jacobian_matrix
from mindarmour.utils.util import GradWrap, jacobian_matrix
from mindarmour.utils._check_param import check_pair_numpy_param, check_model, \
check_value_positive, check_int_positive, check_norm_level, \
check_param_multi_types, check_param_type
from .attack import Attack

LOGGER = LogUtil.get_instance()
TAG = 'DeepFool'

mindarmour/attacks/gradient_method.py → mindarmour/adv_robustness/attacks/gradient_method.py View File

@@ -19,16 +19,14 @@ from abc import abstractmethod
import numpy as np

from mindspore import Tensor
from mindspore.nn import Cell
from mindspore.nn import SoftmaxCrossEntropyWithLogits
from mindspore.nn import Cell, SoftmaxCrossEntropyWithLogits

from mindarmour.attacks.attack import Attack
from mindarmour.utils.util import WithLossCell
from mindarmour.utils.util import GradWrapWithLoss
from mindarmour.utils.util import WithLossCell, GradWrapWithLoss
from mindarmour.utils.logger import LogUtil
from mindarmour.utils._check_param import check_pair_numpy_param, check_model, \
normalize_value, check_value_positive, check_param_multi_types, \
check_norm_level, check_param_type
from .attack import Attack

LOGGER = LogUtil.get_instance()
TAG = 'SingleGrad'

mindarmour/attacks/iterative_gradient_method.py → mindarmour/adv_robustness/attacks/iterative_gradient_method.py View File

@@ -17,18 +17,16 @@ from abc import abstractmethod
import numpy as np
from PIL import Image, ImageOps

from mindspore.nn import SoftmaxCrossEntropyWithLogits
from mindspore.nn import Cell, SoftmaxCrossEntropyWithLogits
from mindspore import Tensor
from mindspore.nn import Cell

from mindarmour.attacks.attack import Attack
from mindarmour.attacks.gradient_method import FastGradientSignMethod
from mindarmour.utils.logger import LogUtil
from mindarmour.utils.util import WithLossCell
from mindarmour.utils.util import GradWrapWithLoss
from mindarmour.utils.util import WithLossCell, GradWrapWithLoss
from mindarmour.utils._check_param import check_pair_numpy_param, \
normalize_value, check_model, check_value_positive, check_int_positive, \
check_param_type, check_norm_level, check_param_multi_types
from .attack import Attack
from .gradient_method import FastGradientSignMethod

LOGGER = LogUtil.get_instance()
TAG = 'IterGrad'

mindarmour/attacks/jsma.py → mindarmour/adv_robustness/attacks/jsma.py View File

@@ -19,14 +19,12 @@ import numpy as np
from mindspore import Tensor
from mindspore.nn import Cell

from mindarmour.attacks.attack import Attack
from mindarmour.utils.util import GradWrap
from mindarmour.utils.util import jacobian_matrix
from mindarmour.utils.util import GradWrap, jacobian_matrix
from mindarmour.utils.logger import LogUtil
from mindarmour.utils._check_param import check_pair_numpy_param, check_model, \
check_param_type, check_int_positive, check_value_positive, \
check_value_non_negative
from .attack import Attack

LOGGER = LogUtil.get_instance()
TAG = 'JSMA'

mindarmour/attacks/lbfgs.py → mindarmour/adv_robustness/attacks/lbfgs.py View File

@@ -21,13 +21,12 @@ from mindspore import Tensor
from mindspore.nn import Cell
from mindspore.nn import SoftmaxCrossEntropyWithLogits

from mindarmour.attacks.attack import Attack
from mindarmour.utils.logger import LogUtil
from mindarmour.utils.util import WithLossCell
from mindarmour.utils.util import GradWrapWithLoss
from mindarmour.utils.util import WithLossCell, GradWrapWithLoss
from mindarmour.utils._check_param import check_pair_numpy_param, check_model, \
check_int_positive, check_value_positive, check_param_type, \
check_param_multi_types
from .attack import Attack

LOGGER = LogUtil.get_instance()
TAG = 'LBFGS'

mindarmour/defenses/__init__.py → mindarmour/adv_robustness/defenses/__init__.py View File

@@ -1,3 +1,16 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module includes classical defense algorithms in defencing adversarial
examples and enhancing model security and trustworthy.

mindarmour/defenses/adversarial_defense.py → mindarmour/adv_robustness/defenses/adversarial_defense.py View File

@@ -17,14 +17,13 @@ Adversarial Defense.
import numpy as np

from mindspore import Tensor
from mindspore.nn import Cell
from mindspore.nn.optim.momentum import Momentum
from mindspore.nn import SoftmaxCrossEntropyWithLogits
from mindspore.nn import Cell, SoftmaxCrossEntropyWithLogits
from mindspore.nn import WithLossCell, TrainOneStepCell
from mindspore.nn.optim.momentum import Momentum

from mindarmour.utils._check_param import check_pair_numpy_param, check_model, \
check_param_in_range, check_param_type, check_param_multi_types
from mindarmour.defenses.defense import Defense
from .defense import Defense


class AdversarialDefense(Defense):

mindarmour/defenses/defense.py → mindarmour/adv_robustness/defenses/defense.py View File


mindarmour/defenses/natural_adversarial_defense.py → mindarmour/adv_robustness/defenses/natural_adversarial_defense.py View File

@@ -14,9 +14,8 @@
"""
Natural Adversarial Defense.
"""
from mindarmour.defenses.adversarial_defense import \
AdversarialDefenseWithAttacks
from mindarmour.attacks.gradient_method import FastGradientSignMethod
from ..attacks.gradient_method import FastGradientSignMethod
from .adversarial_defense import AdversarialDefenseWithAttacks


class NaturalAdversarialDefense(AdversarialDefenseWithAttacks):

mindarmour/defenses/projected_adversarial_defense.py → mindarmour/adv_robustness/defenses/projected_adversarial_defense.py View File

@@ -14,10 +14,8 @@
"""
Projected Adversarial Defense.
"""
from mindarmour.defenses.adversarial_defense import \
AdversarialDefenseWithAttacks
from mindarmour.attacks.iterative_gradient_method import \
ProjectedGradientDescent
from ..attacks.iterative_gradient_method import ProjectedGradientDescent
from .adversarial_defense import AdversarialDefenseWithAttacks


class ProjectedAdversarialDefense(AdversarialDefenseWithAttacks):

mindarmour/detectors/__init__.py → mindarmour/adv_robustness/detectors/__init__.py View File

@@ -1,3 +1,16 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module includes detector methods on distinguishing adversarial examples
from benign examples.

+ 16
- 0
mindarmour/adv_robustness/detectors/black/__init__.py View File

@@ -0,0 +1,16 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
init black detector.
"""

mindarmour/detectors/black/similarity_detector.py → mindarmour/adv_robustness/detectors/black/similarity_detector.py View File

@@ -20,11 +20,11 @@ import numpy as np
from mindspore import Tensor
from mindspore import Model

from mindarmour.detectors.detector import Detector
from mindarmour.utils.logger import LogUtil
from mindarmour.utils._check_param import check_model, check_numpy_param, \
check_int_positive, check_value_positive, check_param_type, \
check_param_in_range
from ..detector import Detector

LOGGER = LogUtil.get_instance()
TAG = 'SimilarityDetector'

mindarmour/detectors/detector.py → mindarmour/adv_robustness/detectors/detector.py View File


mindarmour/detectors/ensemble_detector.py → mindarmour/adv_robustness/detectors/ensemble_detector.py View File

@@ -16,11 +16,10 @@ Ensemble Detector.
"""
import numpy as np

from mindarmour.detectors.detector import Detector
from mindarmour.utils.logger import LogUtil
from mindarmour.utils._check_param import check_numpy_param, \
check_param_multi_types
from .detector import Detector

LOGGER = LogUtil.get_instance()
TAG = 'EnsembleDetector'

mindarmour/detectors/mag_net.py → mindarmour/adv_robustness/detectors/mag_net.py View File

@@ -21,11 +21,11 @@ from scipy.special import softmax
from mindspore import Tensor
from mindspore import Model

from mindarmour.detectors.detector import Detector
from mindarmour.utils.logger import LogUtil
from mindarmour.utils._check_param import check_numpy_param, check_model, \
check_param_in_range, check_param_multi_types, check_int_positive, \
check_value_positive
from .detector import Detector

LOGGER = LogUtil.get_instance()
TAG = 'MagNet'

mindarmour/detectors/region_based_detector.py → mindarmour/adv_robustness/detectors/region_based_detector.py View File

@@ -21,12 +21,12 @@ import numpy as np
from mindspore import Model
from mindspore import Tensor

from mindarmour.detectors.detector import Detector
from mindarmour.utils.logger import LogUtil
from mindarmour.utils._check_param import check_numpy_param, check_param_type, \
check_pair_numpy_param, check_model, check_int_positive, \
check_value_positive, check_value_non_negative, check_param_in_range, \
check_equal_shape
from .detector import Detector

LOGGER = LogUtil.get_instance()
TAG = 'RegionBasedDetector'

mindarmour/detectors/spatial_smoothing.py → mindarmour/adv_robustness/detectors/spatial_smoothing.py View File

@@ -20,11 +20,11 @@ from scipy import ndimage
from mindspore import Model
from mindspore import Tensor

from mindarmour.detectors.detector import Detector
from mindarmour.utils.logger import LogUtil
from mindarmour.utils._check_param import check_model, check_numpy_param, \
check_pair_numpy_param, check_int_positive, check_param_type, \
check_param_in_range, check_equal_shape, check_value_positive
from .detector import Detector

LOGGER = LogUtil.get_instance()
TAG = 'SpatialSmoothing'

+ 27
- 0
mindarmour/adv_robustness/evaluations/__init__.py View File

@@ -0,0 +1,27 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module includes various metrics to evaluate the result of attacks or
defenses.
"""
from .attack_evaluation import AttackEvaluate
from .defense_evaluation import DefenseEvaluate
from .visual_metrics import RadarMetric
from . import black
from .black.defense_evaluation import BlackDefenseEvaluate

__all__ = ['AttackEvaluate',
'BlackDefenseEvaluate',
'DefenseEvaluate',
'RadarMetric']

mindarmour/evaluations/attack_evaluation.py → mindarmour/adv_robustness/evaluations/attack_evaluation.py View File


+ 13
- 0
mindarmour/adv_robustness/evaluations/black/__init__.py View File

@@ -0,0 +1,13 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

mindarmour/evaluations/black/defense_evaluation.py → mindarmour/adv_robustness/evaluations/black/defense_evaluation.py View File


mindarmour/evaluations/defense_evaluation.py → mindarmour/adv_robustness/evaluations/defense_evaluation.py View File


mindarmour/evaluations/visual_metrics.py → mindarmour/adv_robustness/evaluations/visual_metrics.py View File


+ 0
- 0
mindarmour/attacks/black/__init__.py View File


+ 0
- 0
mindarmour/detectors/black/__init__.py View File


+ 0
- 0
mindarmour/diff_privacy/evaluation/__init__.py View File


+ 0
- 0
mindarmour/diff_privacy/mechanisms/__init__.py View File


+ 0
- 0
mindarmour/diff_privacy/monitor/__init__.py View File


+ 0
- 0
mindarmour/diff_privacy/optimizer/__init__.py View File


+ 0
- 0
mindarmour/diff_privacy/train/__init__.py View File


+ 0
- 14
mindarmour/evaluations/__init__.py View File

@@ -1,14 +0,0 @@
"""
This module includes various metrics to evaluate the result of attacks or
defenses.
"""
from .attack_evaluation import AttackEvaluate
from .defense_evaluation import DefenseEvaluate
from .visual_metrics import RadarMetric
from . import black
from .black.defense_evaluation import BlackDefenseEvaluate

__all__ = ['AttackEvaluate',
'BlackDefenseEvaluate',
'DefenseEvaluate',
'RadarMetric']

+ 0
- 0
mindarmour/evaluations/black/__init__.py View File


+ 22
- 0
mindarmour/fuzz_testing/__init__.py View File

@@ -0,0 +1,22 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module provides a neuron coverage-gain based fuzz method to evaluate the
robustness of given model.
"""
from .fuzzing import Fuzzer
from .model_coverage_metrics import ModelCoverageMetrics

__all__ = ['Fuzzer',
'ModelCoverageMetrics']

mindarmour/fuzzing/fuzzing.py → mindarmour/fuzz_testing/fuzzing.py View File

@@ -20,15 +20,15 @@ import numpy as np
from mindspore import Model
from mindspore import Tensor

from mindarmour.fuzzing.model_coverage_metrics import ModelCoverageMetrics
from mindarmour.utils._check_param import check_model, check_numpy_param, \
check_param_multi_types, check_norm_level, check_param_in_range, \
check_param_type, check_int_positive
from mindarmour.fuzzing.image_transform import Contrast, Brightness, Blur, \
Noise, Translate, Scale, Shear, Rotate
from mindarmour.attacks import FastGradientSignMethod, \
MomentumDiverseInputIterativeMethod, ProjectedGradientDescent
from mindarmour.utils.logger import LogUtil
from ..adv_robustness.attacks import FastGradientSignMethod, \
MomentumDiverseInputIterativeMethod, ProjectedGradientDescent
from .image_transform import Contrast, Brightness, Blur, \
Noise, Translate, Scale, Shear, Rotate
from .model_coverage_metrics import ModelCoverageMetrics

LOGGER = LogUtil.get_instance()
TAG = 'Fuzzer'
@@ -95,7 +95,7 @@ class Fuzzer:
>>> {'method': 'FGSM', 'params': {'eps': 0.1, 'alpha': 0.1}}]
>>> train_images = np.random.rand(32, 1, 32, 32).astype(np.float32)
>>> model_fuzz_test = Fuzzer(model, train_images, 10, 1000)
>>> samples, labels, preds, strategies, report = model_fuzz_test.fuzzing(mutate_config, initial_seeds)
>>> samples, labels, preds, strategies, report = model_fuzz_test.fuzz_testing(mutate_config, initial_seeds)
"""

def __init__(self, target_model, train_dataset, neuron_num, segmented_num=1000):
@@ -151,7 +151,7 @@ class Fuzzer:
'Rotate'. Thirdly, attack methods include: 'FGSM', 'PGD' and 'MDIIM'.
`mutate_config` must have method in the type of pixel value based
transform methods. The way of setting parameters for first and
second type methods can be seen in 'mindarmour/fuzzing/image_transform.py'.
second type methods can be seen in 'mindarmour/fuzz_testing/image_transform.py'.
For third type methods, you can refer to the corresponding class.
initial_seeds (list[list]): Initial seeds used to generate mutated
samples. The format of initial seeds is [[image_data, label],
@@ -169,7 +169,7 @@ class Fuzzer:
Default: 20.

Returns:
- list, mutated samples in fuzzing.
- list, mutated samples in fuzz_testing.

- list, ground truth labels of mutated samples.

@@ -376,11 +376,11 @@ class Fuzzer:
def _evaluate(self, fuzz_samples, gt_labels, fuzz_preds,
fuzz_strategies, metrics):
"""
Evaluate generated fuzzing samples in three dimention: accuracy,
Evaluate generated fuzz_testing samples in three dimention: accuracy,
attack success rate and neural coverage.

Args:
fuzz_samples (numpy.ndarray): Generated fuzzing samples according to seeds.
fuzz_samples (numpy.ndarray): Generated fuzz_testing samples according to seeds.
gt_labels (numpy.ndarray): Ground Truth of seeds.
fuzz_preds (numpy.ndarray): Predictions of generated fuzz samples.
fuzz_strategies (numpy.ndarray): Mutate strategies of fuzz samples.

mindarmour/fuzzing/image_transform.py → mindarmour/fuzz_testing/image_transform.py View File


mindarmour/fuzzing/model_coverage_metrics.py → mindarmour/fuzz_testing/model_coverage_metrics.py View File


+ 0
- 8
mindarmour/fuzzing/__init__.py View File

@@ -1,8 +0,0 @@
"""
This module includes various metrics to fuzzing the test of DNN.
"""
from .fuzzing import Fuzzer
from .model_coverage_metrics import ModelCoverageMetrics

__all__ = ['Fuzzer',
'ModelCoverageMetrics']

+ 18
- 0
mindarmour/privacy/__init__.py View File

@@ -0,0 +1,18 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module provides various methods to protect the privacy of given model, and
also provides some methods to evaluate the risk of privacy leakage of given
model.
"""

mindarmour/diff_privacy/__init__.py → mindarmour/privacy/diff_privacy/__init__.py View File

@@ -1,5 +1,18 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module provide Differential Privacy feature to protect user privacy.
This module provides Differential Privacy feature to protect user privacy.
"""
from .mechanisms.mechanisms import NoiseGaussianRandom
from .mechanisms.mechanisms import NoiseAdaGaussianRandom
@@ -11,7 +24,6 @@ from .monitor.monitor import RDPMonitor
from .monitor.monitor import ZCDPMonitor
from .optimizer.optimizer import DPOptimizerClassFactory
from .train.model import DPModel
from .evaluation.membership_inference import MembershipInference

__all__ = ['NoiseGaussianRandom',
'NoiseAdaGaussianRandom',
@@ -22,5 +34,4 @@ __all__ = ['NoiseGaussianRandom',
'RDPMonitor',
'ZCDPMonitor',
'DPOptimizerClassFactory',
'DPModel',
'MembershipInference']
'DPModel']

+ 16
- 0
mindarmour/privacy/diff_privacy/mechanisms/__init__.py View File

@@ -0,0 +1,16 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
init mechanisms.
"""

mindarmour/diff_privacy/mechanisms/mechanisms.py → mindarmour/privacy/diff_privacy/mechanisms/mechanisms.py View File


+ 16
- 0
mindarmour/privacy/diff_privacy/monitor/__init__.py View File

@@ -0,0 +1,16 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
init monitor.
"""

mindarmour/diff_privacy/monitor/monitor.py → mindarmour/privacy/diff_privacy/monitor/monitor.py View File


+ 16
- 0
mindarmour/privacy/diff_privacy/optimizer/__init__.py View File

@@ -0,0 +1,16 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
init optimizer.
"""

mindarmour/diff_privacy/optimizer/optimizer.py → mindarmour/privacy/diff_privacy/optimizer/optimizer.py View File

@@ -22,9 +22,9 @@ from mindspore.ops import functional as F
from mindspore.common import dtype as mstype

from mindarmour.utils.logger import LogUtil
from mindarmour.diff_privacy import NoiseMechanismsFactory
from mindarmour.diff_privacy.mechanisms.mechanisms import _MechanismsParamsUpdater
from mindarmour.utils._check_param import check_int_positive
from ..mechanisms.mechanisms import NoiseMechanismsFactory
from ..mechanisms.mechanisms import _MechanismsParamsUpdater

LOGGER = LogUtil.get_instance()
TAG = 'DP optimizer'

+ 16
- 0
mindarmour/privacy/diff_privacy/train/__init__.py View File

@@ -0,0 +1,16 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
init train.
"""

mindarmour/diff_privacy/train/model.py → mindarmour/privacy/diff_privacy/train/model.py View File

@@ -48,10 +48,9 @@ from mindspore.nn import Cell
from mindspore import ParameterTuple

from mindarmour.utils.logger import LogUtil
from mindarmour.diff_privacy.mechanisms.mechanisms import \
_MechanismsParamsUpdater
from mindarmour.utils._check_param import check_value_positive, check_param_type
from mindarmour.utils._check_param import check_int_positive
from ..mechanisms.mechanisms import _MechanismsParamsUpdater

LOGGER = LogUtil.get_instance()
TAG = 'DP model'

+ 21
- 0
mindarmour/privacy/evaluation/__init__.py View File

@@ -0,0 +1,21 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module provides some methods to evaluate the risk of privacy leakage of
given model.
"""

from .membership_inference import MembershipInference

__all__ = ['MembershipInference']

mindarmour/diff_privacy/evaluation/_check_config.py → mindarmour/privacy/evaluation/_check_config.py View File


mindarmour/diff_privacy/evaluation/attacker.py → mindarmour/privacy/evaluation/attacker.py View File


mindarmour/diff_privacy/evaluation/membership_inference.py → mindarmour/privacy/evaluation/membership_inference.py View File


+ 2
- 1
mindarmour/utils/__init__.py View File

@@ -1,5 +1,6 @@
"""
Util methods of MindArmour."""
Util methods of MindArmour.
"""
from .logger import LogUtil
from .util import GradWrap
from .util import GradWrapWithLoss


+ 1
- 1
mindarmour/utils/_check_param.py View File

@@ -14,7 +14,7 @@
""" check parameters for MindArmour. """
import numpy as np

from mindarmour.utils.logger import LogUtil
from .logger import LogUtil

LOGGER = LogUtil.get_instance()
TAG = 'check parameters'


+ 1
- 1
mindarmour/utils/util.py View File

@@ -17,7 +17,7 @@ from mindspore import Tensor
from mindspore.nn import Cell
from mindspore.ops.composite import GradOperation

from mindarmour.utils.logger import LogUtil
from .logger import LogUtil

LOGGER = LogUtil.get_instance()
TAG = 'util'


+ 1
- 1
tests/st/resnet50/test_cifar10_attack_fgsm.py View File

@@ -29,7 +29,7 @@ from mindspore.common import dtype as mstype
from mindspore.ops import operations as P
from mindspore.ops import functional as F

from mindarmour.attacks.gradient_method import FastGradientSignMethod
from mindarmour.adv_robustness.attacks import FastGradientSignMethod

from resnet_cifar10 import resnet50_cifar10



+ 2
- 2
tests/ut/python/attacks/black/test_genetic_attack.py View File

@@ -21,8 +21,8 @@ from mindspore import Tensor
from mindspore import context
from mindspore.nn import Cell

from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.genetic_attack import GeneticAttack
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import GeneticAttack

context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")



+ 2
- 2
tests/ut/python/attacks/black/test_hsja.py View File

@@ -20,8 +20,8 @@ from mindspore import Tensor
from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net

from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.hop_skip_jump_attack import HopSkipJumpAttack
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import HopSkipJumpAttack
from mindarmour.utils.logger import LogUtil

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),


+ 2
- 2
tests/ut/python/attacks/black/test_nes.py View File

@@ -20,8 +20,8 @@ from mindspore import Tensor
from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net

from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.natural_evolutionary_strategy import NES
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import NES
from mindarmour.utils.logger import LogUtil

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),


+ 2
- 2
tests/ut/python/attacks/black/test_pointwise_attack.py View File

@@ -23,8 +23,8 @@ from mindspore import Tensor
from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net

from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.attacks.black.pointwise_attack import PointWiseAttack
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import PointWiseAttack
from mindarmour.utils.logger import LogUtil

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),


+ 2
- 2
tests/ut/python/attacks/black/test_pso_attack.py View File

@@ -22,8 +22,8 @@ import mindspore.nn as nn
from mindspore.nn import Cell
from mindspore import context

from mindarmour.attacks.black.pso_attack import PSOAttack
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour.adv_robustness.attacks import PSOAttack
from mindarmour import BlackModel


# for user


+ 2
- 3
tests/ut/python/attacks/black/test_salt_and_pepper_attack.py View File

@@ -22,9 +22,8 @@ from mindspore import Tensor
from mindspore.nn import Cell
from mindspore import context

from mindarmour.attacks.black.salt_and_pepper_attack import \
SaltAndPepperNoiseAttack
from mindarmour.attacks.black.black_model import BlackModel
from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import SaltAndPepperNoiseAttack

context.set_context(mode=context.GRAPH_MODE)
context.set_context(device_target="Ascend")


+ 1
- 1
tests/ut/python/attacks/test_batch_generate_attack.py View File

@@ -21,7 +21,7 @@ import mindspore.ops.operations as P
from mindspore.nn import Cell
import mindspore.context as context

from mindarmour.attacks.gradient_method import FastGradientMethod
from mindarmour.adv_robustness.attacks import FastGradientMethod


context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")


+ 1
- 1
tests/ut/python/attacks/test_cw.py View File

@@ -21,7 +21,7 @@ import mindspore.ops.operations as M
from mindspore.nn import Cell
from mindspore import context

from mindarmour.attacks.carlini_wagner import CarliniWagnerL2Attack
from mindarmour.adv_robustness.attacks import CarliniWagnerL2Attack


context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")


Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save