diff --git a/example/membership_inference_demo/main.py b/example/membership_inference_demo/main.py index d3b876b..cbce936 100644 --- a/example/membership_inference_demo/main.py +++ b/example/membership_inference_demo/main.py @@ -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) diff --git a/example/mnist_demo/lenet5_dp.py b/example/mnist_demo/lenet5_dp.py index ee2f711..d8249b9 100644 --- a/example/mnist_demo/lenet5_dp.py +++ b/example/mnist_demo/lenet5_dp.py @@ -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 diff --git a/example/mnist_demo/lenet5_dp_ada_gaussian.py b/example/mnist_demo/lenet5_dp_ada_gaussian.py index 10faa88..fdd7c9e 100644 --- a/example/mnist_demo/lenet5_dp_ada_gaussian.py +++ b/example/mnist_demo/lenet5_dp_ada_gaussian.py @@ -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 diff --git a/example/mnist_demo/lenet5_dp_ada_sgd_graph.py b/example/mnist_demo/lenet5_dp_ada_sgd_graph.py index 491962a..1475555 100644 --- a/example/mnist_demo/lenet5_dp_ada_sgd_graph.py +++ b/example/mnist_demo/lenet5_dp_ada_sgd_graph.py @@ -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 diff --git a/example/mnist_demo/lenet5_dp_optimizer.py b/example/mnist_demo/lenet5_dp_optimizer.py index 2181251..2eec90c 100644 --- a/example/mnist_demo/lenet5_dp_optimizer.py +++ b/example/mnist_demo/lenet5_dp_optimizer.py @@ -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 diff --git a/example/mnist_demo/lenet5_mnist_coverage.py b/example/mnist_demo/lenet5_mnist_coverage.py index 35830ea..b74c810 100644 --- a/example/mnist_demo/lenet5_mnist_coverage.py +++ b/example/mnist_demo/lenet5_mnist_coverage.py @@ -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("..") diff --git a/example/mnist_demo/lenet5_mnist_fuzzing.py b/example/mnist_demo/lenet5_mnist_fuzzing.py index ab04ee3..8c3c72f 100644 --- a/example/mnist_demo/lenet5_mnist_fuzzing.py +++ b/example/mnist_demo/lenet5_mnist_fuzzing.py @@ -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("..") diff --git a/example/mnist_demo/mnist_attack_cw.py b/example/mnist_demo/mnist_attack_cw.py index 8ccb974..7f1a996 100644 --- a/example/mnist_demo/mnist_attack_cw.py +++ b/example/mnist_demo/mnist_attack_cw.py @@ -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 diff --git a/example/mnist_demo/mnist_attack_deepfool.py b/example/mnist_demo/mnist_attack_deepfool.py index dcdfab0..3d0da97 100644 --- a/example/mnist_demo/mnist_attack_deepfool.py +++ b/example/mnist_demo/mnist_attack_deepfool.py @@ -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 diff --git a/example/mnist_demo/mnist_attack_fgsm.py b/example/mnist_demo/mnist_attack_fgsm.py index 090b563..f735c9d 100644 --- a/example/mnist_demo/mnist_attack_fgsm.py +++ b/example/mnist_demo/mnist_attack_fgsm.py @@ -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 diff --git a/example/mnist_demo/mnist_attack_genetic.py b/example/mnist_demo/mnist_attack_genetic.py index beb4bad..e30ab59 100644 --- a/example/mnist_demo/mnist_attack_genetic.py +++ b/example/mnist_demo/mnist_attack_genetic.py @@ -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 diff --git a/example/mnist_demo/mnist_attack_hsja.py b/example/mnist_demo/mnist_attack_hsja.py index 9f82ed2..a30b1eb 100644 --- a/example/mnist_demo/mnist_attack_hsja.py +++ b/example/mnist_demo/mnist_attack_hsja.py @@ -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("..") diff --git a/example/mnist_demo/mnist_attack_jsma.py b/example/mnist_demo/mnist_attack_jsma.py index f24b839..14e5d86 100644 --- a/example/mnist_demo/mnist_attack_jsma.py +++ b/example/mnist_demo/mnist_attack_jsma.py @@ -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 diff --git a/example/mnist_demo/mnist_attack_lbfgs.py b/example/mnist_demo/mnist_attack_lbfgs.py index dace1dd..259c593 100644 --- a/example/mnist_demo/mnist_attack_lbfgs.py +++ b/example/mnist_demo/mnist_attack_lbfgs.py @@ -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 diff --git a/example/mnist_demo/mnist_attack_mdi2fgsm.py b/example/mnist_demo/mnist_attack_mdi2fgsm.py index c717c08..90e317d 100644 --- a/example/mnist_demo/mnist_attack_mdi2fgsm.py +++ b/example/mnist_demo/mnist_attack_mdi2fgsm.py @@ -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 diff --git a/example/mnist_demo/mnist_attack_nes.py b/example/mnist_demo/mnist_attack_nes.py index 62c85db..7d24eff 100644 --- a/example/mnist_demo/mnist_attack_nes.py +++ b/example/mnist_demo/mnist_attack_nes.py @@ -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("..") diff --git a/example/mnist_demo/mnist_attack_pgd.py b/example/mnist_demo/mnist_attack_pgd.py index d64aa9c..5f3851a 100644 --- a/example/mnist_demo/mnist_attack_pgd.py +++ b/example/mnist_demo/mnist_attack_pgd.py @@ -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 diff --git a/example/mnist_demo/mnist_attack_pointwise.py b/example/mnist_demo/mnist_attack_pointwise.py index 746b6ef..d4099d5 100644 --- a/example/mnist_demo/mnist_attack_pointwise.py +++ b/example/mnist_demo/mnist_attack_pointwise.py @@ -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("..") diff --git a/example/mnist_demo/mnist_attack_pso.py b/example/mnist_demo/mnist_attack_pso.py index 29285fe..cbcc239 100644 --- a/example/mnist_demo/mnist_attack_pso.py +++ b/example/mnist_demo/mnist_attack_pso.py @@ -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 diff --git a/example/mnist_demo/mnist_attack_salt_and_pepper.py b/example/mnist_demo/mnist_attack_salt_and_pepper.py index a551c7e..ec5068f 100644 --- a/example/mnist_demo/mnist_attack_salt_and_pepper.py +++ b/example/mnist_demo/mnist_attack_salt_and_pepper.py @@ -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("..") diff --git a/example/mnist_demo/mnist_defense_nad.py b/example/mnist_demo/mnist_defense_nad.py index 62fb458..3b1e364 100644 --- a/example/mnist_demo/mnist_defense_nad.py +++ b/example/mnist_demo/mnist_defense_nad.py @@ -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 diff --git a/example/mnist_demo/mnist_evaluation.py b/example/mnist_demo/mnist_evaluation.py index 38d75a7..012e6cf 100644 --- a/example/mnist_demo/mnist_evaluation.py +++ b/example/mnist_demo/mnist_evaluation.py @@ -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("..") diff --git a/example/mnist_demo/mnist_similarity_detector.py b/example/mnist_demo/mnist_similarity_detector.py index 23fc378..59c7964 100644 --- a/example/mnist_demo/mnist_similarity_detector.py +++ b/example/mnist_demo/mnist_similarity_detector.py @@ -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 diff --git a/mindarmour/__init__.py b/mindarmour/__init__.py index 65069e4..0526847 100644 --- a/mindarmour/__init__.py +++ b/mindarmour/__init__.py @@ -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'] diff --git a/mindarmour/adv_robustness/__init__.py b/mindarmour/adv_robustness/__init__.py new file mode 100644 index 0000000..6db9bd2 --- /dev/null +++ b/mindarmour/adv_robustness/__init__.py @@ -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. +""" diff --git a/mindarmour/attacks/__init__.py b/mindarmour/adv_robustness/attacks/__init__.py similarity index 70% rename from mindarmour/attacks/__init__.py rename to mindarmour/adv_robustness/attacks/__init__.py index 2e66469..377c2ce 100644 --- a/mindarmour/attacks/__init__.py +++ b/mindarmour/adv_robustness/attacks/__init__.py @@ -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. diff --git a/mindarmour/attacks/attack.py b/mindarmour/adv_robustness/attacks/attack.py similarity index 100% rename from mindarmour/attacks/attack.py rename to mindarmour/adv_robustness/attacks/attack.py diff --git a/mindarmour/adv_robustness/attacks/black/__init__.py b/mindarmour/adv_robustness/attacks/black/__init__.py new file mode 100644 index 0000000..05c8d77 --- /dev/null +++ b/mindarmour/adv_robustness/attacks/black/__init__.py @@ -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. +""" diff --git a/mindarmour/attacks/black/black_model.py b/mindarmour/adv_robustness/attacks/black/black_model.py similarity index 100% rename from mindarmour/attacks/black/black_model.py rename to mindarmour/adv_robustness/attacks/black/black_model.py diff --git a/mindarmour/attacks/black/genetic_attack.py b/mindarmour/adv_robustness/attacks/black/genetic_attack.py similarity index 98% rename from mindarmour/attacks/black/genetic_attack.py rename to mindarmour/adv_robustness/attacks/black/genetic_attack.py index 6e2c403..6d80aa2 100644 --- a/mindarmour/attacks/black/genetic_attack.py +++ b/mindarmour/adv_robustness/attacks/black/genetic_attack.py @@ -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' diff --git a/mindarmour/attacks/black/hop_skip_jump_attack.py b/mindarmour/adv_robustness/attacks/black/hop_skip_jump_attack.py similarity index 99% rename from mindarmour/attacks/black/hop_skip_jump_attack.py rename to mindarmour/adv_robustness/attacks/black/hop_skip_jump_attack.py index 1bd4625..22638bb 100644 --- a/mindarmour/attacks/black/hop_skip_jump_attack.py +++ b/mindarmour/adv_robustness/attacks/black/hop_skip_jump_attack.py @@ -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' diff --git a/mindarmour/attacks/black/natural_evolutionary_strategy.py b/mindarmour/adv_robustness/attacks/black/natural_evolutionary_strategy.py similarity index 99% rename from mindarmour/attacks/black/natural_evolutionary_strategy.py rename to mindarmour/adv_robustness/attacks/black/natural_evolutionary_strategy.py index 91f6ab9..8d3a18f 100644 --- a/mindarmour/attacks/black/natural_evolutionary_strategy.py +++ b/mindarmour/adv_robustness/attacks/black/natural_evolutionary_strategy.py @@ -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' diff --git a/mindarmour/attacks/black/pointwise_attack.py b/mindarmour/adv_robustness/attacks/black/pointwise_attack.py similarity index 98% rename from mindarmour/attacks/black/pointwise_attack.py rename to mindarmour/adv_robustness/attacks/black/pointwise_attack.py index 3a45df5..f9fd06c 100644 --- a/mindarmour/attacks/black/pointwise_attack.py +++ b/mindarmour/adv_robustness/attacks/black/pointwise_attack.py @@ -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' diff --git a/mindarmour/attacks/black/pso_attack.py b/mindarmour/adv_robustness/attacks/black/pso_attack.py similarity index 99% rename from mindarmour/attacks/black/pso_attack.py rename to mindarmour/adv_robustness/attacks/black/pso_attack.py index 7fdc812..3627ab4 100644 --- a/mindarmour/attacks/black/pso_attack.py +++ b/mindarmour/adv_robustness/attacks/black/pso_attack.py @@ -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' diff --git a/mindarmour/attacks/black/salt_and_pepper_attack.py b/mindarmour/adv_robustness/attacks/black/salt_and_pepper_attack.py similarity index 98% rename from mindarmour/attacks/black/salt_and_pepper_attack.py rename to mindarmour/adv_robustness/attacks/black/salt_and_pepper_attack.py index c0ce75a..332352e 100644 --- a/mindarmour/attacks/black/salt_and_pepper_attack.py +++ b/mindarmour/adv_robustness/attacks/black/salt_and_pepper_attack.py @@ -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' diff --git a/mindarmour/attacks/carlini_wagner.py b/mindarmour/adv_robustness/attacks/carlini_wagner.py similarity index 99% rename from mindarmour/attacks/carlini_wagner.py rename to mindarmour/adv_robustness/attacks/carlini_wagner.py index b765d7e..09c4046 100644 --- a/mindarmour/attacks/carlini_wagner.py +++ b/mindarmour/adv_robustness/attacks/carlini_wagner.py @@ -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' diff --git a/mindarmour/attacks/deep_fool.py b/mindarmour/adv_robustness/attacks/deep_fool.py similarity index 97% rename from mindarmour/attacks/deep_fool.py rename to mindarmour/adv_robustness/attacks/deep_fool.py index 322fe12..dc3a556 100644 --- a/mindarmour/attacks/deep_fool.py +++ b/mindarmour/adv_robustness/attacks/deep_fool.py @@ -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' diff --git a/mindarmour/attacks/gradient_method.py b/mindarmour/adv_robustness/attacks/gradient_method.py similarity index 98% rename from mindarmour/attacks/gradient_method.py rename to mindarmour/adv_robustness/attacks/gradient_method.py index 7188c8a..5412631 100644 --- a/mindarmour/attacks/gradient_method.py +++ b/mindarmour/adv_robustness/attacks/gradient_method.py @@ -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' diff --git a/mindarmour/attacks/iterative_gradient_method.py b/mindarmour/adv_robustness/attacks/iterative_gradient_method.py similarity index 98% rename from mindarmour/attacks/iterative_gradient_method.py rename to mindarmour/adv_robustness/attacks/iterative_gradient_method.py index 3011603..6f94d3d 100644 --- a/mindarmour/attacks/iterative_gradient_method.py +++ b/mindarmour/adv_robustness/attacks/iterative_gradient_method.py @@ -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' diff --git a/mindarmour/attacks/jsma.py b/mindarmour/adv_robustness/attacks/jsma.py similarity index 98% rename from mindarmour/attacks/jsma.py rename to mindarmour/adv_robustness/attacks/jsma.py index 88d2188..996f031 100644 --- a/mindarmour/attacks/jsma.py +++ b/mindarmour/adv_robustness/attacks/jsma.py @@ -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' diff --git a/mindarmour/attacks/lbfgs.py b/mindarmour/adv_robustness/attacks/lbfgs.py similarity index 98% rename from mindarmour/attacks/lbfgs.py rename to mindarmour/adv_robustness/attacks/lbfgs.py index fb73961..86f4f33 100644 --- a/mindarmour/attacks/lbfgs.py +++ b/mindarmour/adv_robustness/attacks/lbfgs.py @@ -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' diff --git a/mindarmour/defenses/__init__.py b/mindarmour/adv_robustness/defenses/__init__.py similarity index 52% rename from mindarmour/defenses/__init__.py rename to mindarmour/adv_robustness/defenses/__init__.py index b9d59c8..a618b77 100644 --- a/mindarmour/defenses/__init__.py +++ b/mindarmour/adv_robustness/defenses/__init__.py @@ -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. diff --git a/mindarmour/defenses/adversarial_defense.py b/mindarmour/adv_robustness/defenses/adversarial_defense.py similarity index 98% rename from mindarmour/defenses/adversarial_defense.py rename to mindarmour/adv_robustness/defenses/adversarial_defense.py index 7c066e7..6ef6648 100644 --- a/mindarmour/defenses/adversarial_defense.py +++ b/mindarmour/adv_robustness/defenses/adversarial_defense.py @@ -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): diff --git a/mindarmour/defenses/defense.py b/mindarmour/adv_robustness/defenses/defense.py similarity index 100% rename from mindarmour/defenses/defense.py rename to mindarmour/adv_robustness/defenses/defense.py diff --git a/mindarmour/defenses/natural_adversarial_defense.py b/mindarmour/adv_robustness/defenses/natural_adversarial_defense.py similarity index 93% rename from mindarmour/defenses/natural_adversarial_defense.py rename to mindarmour/adv_robustness/defenses/natural_adversarial_defense.py index 315a407..cb13dfd 100644 --- a/mindarmour/defenses/natural_adversarial_defense.py +++ b/mindarmour/adv_robustness/defenses/natural_adversarial_defense.py @@ -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): diff --git a/mindarmour/defenses/projected_adversarial_defense.py b/mindarmour/adv_robustness/defenses/projected_adversarial_defense.py similarity index 93% rename from mindarmour/defenses/projected_adversarial_defense.py rename to mindarmour/adv_robustness/defenses/projected_adversarial_defense.py index e39c0e6..0b44869 100644 --- a/mindarmour/defenses/projected_adversarial_defense.py +++ b/mindarmour/adv_robustness/defenses/projected_adversarial_defense.py @@ -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): diff --git a/mindarmour/detectors/__init__.py b/mindarmour/adv_robustness/detectors/__init__.py similarity index 51% rename from mindarmour/detectors/__init__.py rename to mindarmour/adv_robustness/detectors/__init__.py index 86b8631..a676eac 100644 --- a/mindarmour/detectors/__init__.py +++ b/mindarmour/adv_robustness/detectors/__init__.py @@ -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. diff --git a/mindarmour/adv_robustness/detectors/black/__init__.py b/mindarmour/adv_robustness/detectors/black/__init__.py new file mode 100644 index 0000000..46a073d --- /dev/null +++ b/mindarmour/adv_robustness/detectors/black/__init__.py @@ -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. +""" diff --git a/mindarmour/detectors/black/similarity_detector.py b/mindarmour/adv_robustness/detectors/black/similarity_detector.py similarity index 99% rename from mindarmour/detectors/black/similarity_detector.py rename to mindarmour/adv_robustness/detectors/black/similarity_detector.py index f0e8890..4c38092 100644 --- a/mindarmour/detectors/black/similarity_detector.py +++ b/mindarmour/adv_robustness/detectors/black/similarity_detector.py @@ -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' diff --git a/mindarmour/detectors/detector.py b/mindarmour/adv_robustness/detectors/detector.py similarity index 100% rename from mindarmour/detectors/detector.py rename to mindarmour/adv_robustness/detectors/detector.py diff --git a/mindarmour/detectors/ensemble_detector.py b/mindarmour/adv_robustness/detectors/ensemble_detector.py similarity index 98% rename from mindarmour/detectors/ensemble_detector.py rename to mindarmour/adv_robustness/detectors/ensemble_detector.py index f1e8ba8..4579858 100644 --- a/mindarmour/detectors/ensemble_detector.py +++ b/mindarmour/adv_robustness/detectors/ensemble_detector.py @@ -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' diff --git a/mindarmour/detectors/mag_net.py b/mindarmour/adv_robustness/detectors/mag_net.py similarity index 99% rename from mindarmour/detectors/mag_net.py rename to mindarmour/adv_robustness/detectors/mag_net.py index 1a6268e..23357fd 100644 --- a/mindarmour/detectors/mag_net.py +++ b/mindarmour/adv_robustness/detectors/mag_net.py @@ -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' diff --git a/mindarmour/detectors/region_based_detector.py b/mindarmour/adv_robustness/detectors/region_based_detector.py similarity index 99% rename from mindarmour/detectors/region_based_detector.py rename to mindarmour/adv_robustness/detectors/region_based_detector.py index 626c8c6..e7e7140 100644 --- a/mindarmour/detectors/region_based_detector.py +++ b/mindarmour/adv_robustness/detectors/region_based_detector.py @@ -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' diff --git a/mindarmour/detectors/spatial_smoothing.py b/mindarmour/adv_robustness/detectors/spatial_smoothing.py similarity index 99% rename from mindarmour/detectors/spatial_smoothing.py rename to mindarmour/adv_robustness/detectors/spatial_smoothing.py index 7597aad..548b7f7 100644 --- a/mindarmour/detectors/spatial_smoothing.py +++ b/mindarmour/adv_robustness/detectors/spatial_smoothing.py @@ -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' diff --git a/mindarmour/adv_robustness/evaluations/__init__.py b/mindarmour/adv_robustness/evaluations/__init__.py new file mode 100644 index 0000000..3afeeda --- /dev/null +++ b/mindarmour/adv_robustness/evaluations/__init__.py @@ -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'] diff --git a/mindarmour/evaluations/attack_evaluation.py b/mindarmour/adv_robustness/evaluations/attack_evaluation.py similarity index 100% rename from mindarmour/evaluations/attack_evaluation.py rename to mindarmour/adv_robustness/evaluations/attack_evaluation.py diff --git a/mindarmour/adv_robustness/evaluations/black/__init__.py b/mindarmour/adv_robustness/evaluations/black/__init__.py new file mode 100644 index 0000000..e55f409 --- /dev/null +++ b/mindarmour/adv_robustness/evaluations/black/__init__.py @@ -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. diff --git a/mindarmour/evaluations/black/defense_evaluation.py b/mindarmour/adv_robustness/evaluations/black/defense_evaluation.py similarity index 100% rename from mindarmour/evaluations/black/defense_evaluation.py rename to mindarmour/adv_robustness/evaluations/black/defense_evaluation.py diff --git a/mindarmour/evaluations/defense_evaluation.py b/mindarmour/adv_robustness/evaluations/defense_evaluation.py similarity index 100% rename from mindarmour/evaluations/defense_evaluation.py rename to mindarmour/adv_robustness/evaluations/defense_evaluation.py diff --git a/mindarmour/evaluations/visual_metrics.py b/mindarmour/adv_robustness/evaluations/visual_metrics.py similarity index 100% rename from mindarmour/evaluations/visual_metrics.py rename to mindarmour/adv_robustness/evaluations/visual_metrics.py diff --git a/mindarmour/attacks/black/__init__.py b/mindarmour/attacks/black/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mindarmour/detectors/black/__init__.py b/mindarmour/detectors/black/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mindarmour/diff_privacy/evaluation/__init__.py b/mindarmour/diff_privacy/evaluation/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mindarmour/diff_privacy/mechanisms/__init__.py b/mindarmour/diff_privacy/mechanisms/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mindarmour/diff_privacy/monitor/__init__.py b/mindarmour/diff_privacy/monitor/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mindarmour/diff_privacy/optimizer/__init__.py b/mindarmour/diff_privacy/optimizer/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mindarmour/diff_privacy/train/__init__.py b/mindarmour/diff_privacy/train/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mindarmour/evaluations/__init__.py b/mindarmour/evaluations/__init__.py deleted file mode 100644 index ebd1d8d..0000000 --- a/mindarmour/evaluations/__init__.py +++ /dev/null @@ -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'] diff --git a/mindarmour/evaluations/black/__init__.py b/mindarmour/evaluations/black/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mindarmour/fuzz_testing/__init__.py b/mindarmour/fuzz_testing/__init__.py new file mode 100644 index 0000000..25026dc --- /dev/null +++ b/mindarmour/fuzz_testing/__init__.py @@ -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'] diff --git a/mindarmour/fuzzing/fuzzing.py b/mindarmour/fuzz_testing/fuzzing.py similarity index 97% rename from mindarmour/fuzzing/fuzzing.py rename to mindarmour/fuzz_testing/fuzzing.py index 75627ab..38c840e 100644 --- a/mindarmour/fuzzing/fuzzing.py +++ b/mindarmour/fuzz_testing/fuzzing.py @@ -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. diff --git a/mindarmour/fuzzing/image_transform.py b/mindarmour/fuzz_testing/image_transform.py similarity index 100% rename from mindarmour/fuzzing/image_transform.py rename to mindarmour/fuzz_testing/image_transform.py diff --git a/mindarmour/fuzzing/model_coverage_metrics.py b/mindarmour/fuzz_testing/model_coverage_metrics.py similarity index 100% rename from mindarmour/fuzzing/model_coverage_metrics.py rename to mindarmour/fuzz_testing/model_coverage_metrics.py diff --git a/mindarmour/fuzzing/__init__.py b/mindarmour/fuzzing/__init__.py deleted file mode 100644 index ba54b51..0000000 --- a/mindarmour/fuzzing/__init__.py +++ /dev/null @@ -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'] diff --git a/mindarmour/privacy/__init__.py b/mindarmour/privacy/__init__.py new file mode 100644 index 0000000..87ee8ad --- /dev/null +++ b/mindarmour/privacy/__init__.py @@ -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. +""" diff --git a/mindarmour/diff_privacy/__init__.py b/mindarmour/privacy/diff_privacy/__init__.py similarity index 54% rename from mindarmour/diff_privacy/__init__.py rename to mindarmour/privacy/diff_privacy/__init__.py index 6dbd1e2..26c5888 100644 --- a/mindarmour/diff_privacy/__init__.py +++ b/mindarmour/privacy/diff_privacy/__init__.py @@ -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'] diff --git a/mindarmour/privacy/diff_privacy/mechanisms/__init__.py b/mindarmour/privacy/diff_privacy/mechanisms/__init__.py new file mode 100644 index 0000000..3dcc52d --- /dev/null +++ b/mindarmour/privacy/diff_privacy/mechanisms/__init__.py @@ -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. +""" diff --git a/mindarmour/diff_privacy/mechanisms/mechanisms.py b/mindarmour/privacy/diff_privacy/mechanisms/mechanisms.py similarity index 100% rename from mindarmour/diff_privacy/mechanisms/mechanisms.py rename to mindarmour/privacy/diff_privacy/mechanisms/mechanisms.py diff --git a/mindarmour/privacy/diff_privacy/monitor/__init__.py b/mindarmour/privacy/diff_privacy/monitor/__init__.py new file mode 100644 index 0000000..8984a2a --- /dev/null +++ b/mindarmour/privacy/diff_privacy/monitor/__init__.py @@ -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. +""" diff --git a/mindarmour/diff_privacy/monitor/monitor.py b/mindarmour/privacy/diff_privacy/monitor/monitor.py similarity index 100% rename from mindarmour/diff_privacy/monitor/monitor.py rename to mindarmour/privacy/diff_privacy/monitor/monitor.py diff --git a/mindarmour/privacy/diff_privacy/optimizer/__init__.py b/mindarmour/privacy/diff_privacy/optimizer/__init__.py new file mode 100644 index 0000000..fce47ba --- /dev/null +++ b/mindarmour/privacy/diff_privacy/optimizer/__init__.py @@ -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. +""" diff --git a/mindarmour/diff_privacy/optimizer/optimizer.py b/mindarmour/privacy/diff_privacy/optimizer/optimizer.py similarity index 97% rename from mindarmour/diff_privacy/optimizer/optimizer.py rename to mindarmour/privacy/diff_privacy/optimizer/optimizer.py index da5deb2..7be5647 100644 --- a/mindarmour/diff_privacy/optimizer/optimizer.py +++ b/mindarmour/privacy/diff_privacy/optimizer/optimizer.py @@ -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' diff --git a/mindarmour/privacy/diff_privacy/train/__init__.py b/mindarmour/privacy/diff_privacy/train/__init__.py new file mode 100644 index 0000000..d1f730f --- /dev/null +++ b/mindarmour/privacy/diff_privacy/train/__init__.py @@ -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. +""" diff --git a/mindarmour/diff_privacy/train/model.py b/mindarmour/privacy/diff_privacy/train/model.py similarity index 99% rename from mindarmour/diff_privacy/train/model.py rename to mindarmour/privacy/diff_privacy/train/model.py index e2ad470..2dd9c6c 100644 --- a/mindarmour/diff_privacy/train/model.py +++ b/mindarmour/privacy/diff_privacy/train/model.py @@ -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' diff --git a/mindarmour/privacy/evaluation/__init__.py b/mindarmour/privacy/evaluation/__init__.py new file mode 100644 index 0000000..ec8b921 --- /dev/null +++ b/mindarmour/privacy/evaluation/__init__.py @@ -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'] diff --git a/mindarmour/diff_privacy/evaluation/_check_config.py b/mindarmour/privacy/evaluation/_check_config.py similarity index 100% rename from mindarmour/diff_privacy/evaluation/_check_config.py rename to mindarmour/privacy/evaluation/_check_config.py diff --git a/mindarmour/diff_privacy/evaluation/attacker.py b/mindarmour/privacy/evaluation/attacker.py old mode 100755 new mode 100644 similarity index 100% rename from mindarmour/diff_privacy/evaluation/attacker.py rename to mindarmour/privacy/evaluation/attacker.py diff --git a/mindarmour/diff_privacy/evaluation/membership_inference.py b/mindarmour/privacy/evaluation/membership_inference.py old mode 100755 new mode 100644 similarity index 100% rename from mindarmour/diff_privacy/evaluation/membership_inference.py rename to mindarmour/privacy/evaluation/membership_inference.py diff --git a/mindarmour/utils/__init__.py b/mindarmour/utils/__init__.py index 0a7c1c5..fbe5fa5 100644 --- a/mindarmour/utils/__init__.py +++ b/mindarmour/utils/__init__.py @@ -1,5 +1,6 @@ """ -Util methods of MindArmour.""" +Util methods of MindArmour. +""" from .logger import LogUtil from .util import GradWrap from .util import GradWrapWithLoss diff --git a/mindarmour/utils/_check_param.py b/mindarmour/utils/_check_param.py index 36ebc87..1b93ea2 100644 --- a/mindarmour/utils/_check_param.py +++ b/mindarmour/utils/_check_param.py @@ -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' diff --git a/mindarmour/utils/util.py b/mindarmour/utils/util.py index 41f76bc..b5b4b97 100644 --- a/mindarmour/utils/util.py +++ b/mindarmour/utils/util.py @@ -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' diff --git a/tests/st/resnet50/test_cifar10_attack_fgsm.py b/tests/st/resnet50/test_cifar10_attack_fgsm.py index 6591faa..5c2bdac 100644 --- a/tests/st/resnet50/test_cifar10_attack_fgsm.py +++ b/tests/st/resnet50/test_cifar10_attack_fgsm.py @@ -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 diff --git a/tests/ut/python/attacks/black/test_genetic_attack.py b/tests/ut/python/attacks/black/test_genetic_attack.py index 85fe57e..27a7201 100644 --- a/tests/ut/python/attacks/black/test_genetic_attack.py +++ b/tests/ut/python/attacks/black/test_genetic_attack.py @@ -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") diff --git a/tests/ut/python/attacks/black/test_hsja.py b/tests/ut/python/attacks/black/test_hsja.py index 9bb42fe..b19e203 100644 --- a/tests/ut/python/attacks/black/test_hsja.py +++ b/tests/ut/python/attacks/black/test_hsja.py @@ -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__)), diff --git a/tests/ut/python/attacks/black/test_nes.py b/tests/ut/python/attacks/black/test_nes.py index eca3c64..6a84920 100644 --- a/tests/ut/python/attacks/black/test_nes.py +++ b/tests/ut/python/attacks/black/test_nes.py @@ -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__)), diff --git a/tests/ut/python/attacks/black/test_pointwise_attack.py b/tests/ut/python/attacks/black/test_pointwise_attack.py index 29ddbd5..28c2c06 100644 --- a/tests/ut/python/attacks/black/test_pointwise_attack.py +++ b/tests/ut/python/attacks/black/test_pointwise_attack.py @@ -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__)), diff --git a/tests/ut/python/attacks/black/test_pso_attack.py b/tests/ut/python/attacks/black/test_pso_attack.py index 1763580..aa27cb5 100644 --- a/tests/ut/python/attacks/black/test_pso_attack.py +++ b/tests/ut/python/attacks/black/test_pso_attack.py @@ -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 diff --git a/tests/ut/python/attacks/black/test_salt_and_pepper_attack.py b/tests/ut/python/attacks/black/test_salt_and_pepper_attack.py index bed4141..ad08b1b 100644 --- a/tests/ut/python/attacks/black/test_salt_and_pepper_attack.py +++ b/tests/ut/python/attacks/black/test_salt_and_pepper_attack.py @@ -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") diff --git a/tests/ut/python/attacks/test_batch_generate_attack.py b/tests/ut/python/attacks/test_batch_generate_attack.py index 6855e72..23e5838 100644 --- a/tests/ut/python/attacks/test_batch_generate_attack.py +++ b/tests/ut/python/attacks/test_batch_generate_attack.py @@ -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") diff --git a/tests/ut/python/attacks/test_cw.py b/tests/ut/python/attacks/test_cw.py index 7135e9b..da0eece 100644 --- a/tests/ut/python/attacks/test_cw.py +++ b/tests/ut/python/attacks/test_cw.py @@ -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") diff --git a/tests/ut/python/attacks/test_deep_fool.py b/tests/ut/python/attacks/test_deep_fool.py index 74f94b1..7f82a0b 100644 --- a/tests/ut/python/attacks/test_deep_fool.py +++ b/tests/ut/python/attacks/test_deep_fool.py @@ -17,12 +17,12 @@ DeepFool-Attack test. import numpy as np import pytest -import mindspore.ops.operations as M +import mindspore.ops.operations as P from mindspore.nn import Cell from mindspore import context from mindspore import Tensor -from mindarmour.attacks.deep_fool import DeepFool +from mindarmour.adv_robustness.attacks import DeepFool context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") @@ -41,7 +41,7 @@ class Net(Cell): Introduce the layers used for network construction. """ super(Net, self).__init__() - self._softmax = M.Softmax() + self._softmax = P.Softmax() def construct(self, inputs): """ diff --git a/tests/ut/python/attacks/test_gradient_method.py b/tests/ut/python/attacks/test_gradient_method.py index 075617e..b9fa9ad 100644 --- a/tests/ut/python/attacks/test_gradient_method.py +++ b/tests/ut/python/attacks/test_gradient_method.py @@ -22,12 +22,12 @@ from mindspore.nn import Cell import mindspore.context as context from mindspore.nn import SoftmaxCrossEntropyWithLogits -from mindarmour.attacks.gradient_method import FastGradientMethod -from mindarmour.attacks.gradient_method import FastGradientSignMethod -from mindarmour.attacks.gradient_method import LeastLikelyClassMethod -from mindarmour.attacks.gradient_method import RandomFastGradientMethod -from mindarmour.attacks.gradient_method import RandomFastGradientSignMethod -from mindarmour.attacks.gradient_method import RandomLeastLikelyClassMethod +from mindarmour.adv_robustness.attacks import FastGradientMethod +from mindarmour.adv_robustness.attacks import FastGradientSignMethod +from mindarmour.adv_robustness.attacks import LeastLikelyClassMethod +from mindarmour.adv_robustness.attacks import RandomFastGradientMethod +from mindarmour.adv_robustness.attacks import RandomFastGradientSignMethod +from mindarmour.adv_robustness.attacks import RandomLeastLikelyClassMethod context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") diff --git a/tests/ut/python/attacks/test_iterative_gradient_method.py b/tests/ut/python/attacks/test_iterative_gradient_method.py index 34cc2e3..8263468 100644 --- a/tests/ut/python/attacks/test_iterative_gradient_method.py +++ b/tests/ut/python/attacks/test_iterative_gradient_method.py @@ -21,12 +21,12 @@ from mindspore.ops import operations as P from mindspore.nn import Cell from mindspore import context -from mindarmour.attacks import BasicIterativeMethod -from mindarmour.attacks import MomentumIterativeMethod -from mindarmour.attacks import ProjectedGradientDescent -from mindarmour.attacks import IterativeGradientMethod -from mindarmour.attacks import DiverseInputIterativeMethod -from mindarmour.attacks import MomentumDiverseInputIterativeMethod +from mindarmour.adv_robustness.attacks import BasicIterativeMethod +from mindarmour.adv_robustness.attacks import MomentumIterativeMethod +from mindarmour.adv_robustness.attacks import ProjectedGradientDescent +from mindarmour.adv_robustness.attacks import IterativeGradientMethod +from mindarmour.adv_robustness.attacks import DiverseInputIterativeMethod +from mindarmour.adv_robustness.attacks import MomentumDiverseInputIterativeMethod context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") diff --git a/tests/ut/python/attacks/test_jsma.py b/tests/ut/python/attacks/test_jsma.py index ef76125..5457a98 100644 --- a/tests/ut/python/attacks/test_jsma.py +++ b/tests/ut/python/attacks/test_jsma.py @@ -21,7 +21,7 @@ import mindspore.nn as nn from mindspore.nn import Cell from mindspore import context from mindspore import Tensor -from mindarmour.attacks.jsma import JSMAAttack +from mindarmour.adv_robustness.attacks import JSMAAttack # for user diff --git a/tests/ut/python/attacks/test_lbfgs.py b/tests/ut/python/attacks/test_lbfgs.py index d1387e2..097df4e 100644 --- a/tests/ut/python/attacks/test_lbfgs.py +++ b/tests/ut/python/attacks/test_lbfgs.py @@ -22,7 +22,7 @@ import pytest from mindspore import context from mindspore.train.serialization import load_checkpoint, load_param_into_net -from mindarmour.attacks.lbfgs import LBFGS +from mindarmour.adv_robustness.attacks import LBFGS from mindarmour.utils.logger import LogUtil sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), diff --git a/tests/ut/python/defenses/mock_net.py b/tests/ut/python/defenses/mock_net.py index a67609a..db49ebc 100644 --- a/tests/ut/python/defenses/mock_net.py +++ b/tests/ut/python/defenses/mock_net.py @@ -23,7 +23,7 @@ from mindspore.nn.optim.momentum import Momentum from mindspore import context from mindspore.common.initializer import TruncatedNormal -from mindarmour.attacks import FastGradientSignMethod +from mindarmour.adv_robustness.attacks import FastGradientSignMethod def conv(in_channels, out_channels, kernel_size, stride=1, padding=0): diff --git a/tests/ut/python/defenses/test_ad.py b/tests/ut/python/defenses/test_ad.py index 1c3383d..d13b809 100644 --- a/tests/ut/python/defenses/test_ad.py +++ b/tests/ut/python/defenses/test_ad.py @@ -24,7 +24,7 @@ from mindspore import nn from mindspore.nn.optim.momentum import Momentum from mock_net import Net -from mindarmour.defenses.adversarial_defense import AdversarialDefense +from mindarmour.adv_robustness.defenses import AdversarialDefense from mindarmour.utils.logger import LogUtil LOGGER = LogUtil.get_instance() diff --git a/tests/ut/python/defenses/test_ead.py b/tests/ut/python/defenses/test_ead.py index a5a205c..ecf9dfa 100644 --- a/tests/ut/python/defenses/test_ead.py +++ b/tests/ut/python/defenses/test_ead.py @@ -23,10 +23,10 @@ from mindspore import nn from mindspore.nn.optim.momentum import Momentum from mock_net import Net -from mindarmour.attacks.gradient_method import FastGradientSignMethod -from mindarmour.attacks.iterative_gradient_method import \ +from mindarmour.adv_robustness.attacks import FastGradientSignMethod +from mindarmour.adv_robustness.attacks import \ ProjectedGradientDescent -from mindarmour.defenses.adversarial_defense import EnsembleAdversarialDefense +from mindarmour.adv_robustness.defenses import EnsembleAdversarialDefense from mindarmour.utils.logger import LogUtil LOGGER = LogUtil.get_instance() diff --git a/tests/ut/python/defenses/test_nad.py b/tests/ut/python/defenses/test_nad.py index ab9d59c..c61fef8 100644 --- a/tests/ut/python/defenses/test_nad.py +++ b/tests/ut/python/defenses/test_nad.py @@ -23,8 +23,7 @@ from mindspore import nn from mindspore.nn.optim.momentum import Momentum from mock_net import Net -from mindarmour.defenses.natural_adversarial_defense import \ - NaturalAdversarialDefense +from mindarmour.adv_robustness.defenses import NaturalAdversarialDefense from mindarmour.utils.logger import LogUtil LOGGER = LogUtil.get_instance() diff --git a/tests/ut/python/defenses/test_pad.py b/tests/ut/python/defenses/test_pad.py index b38a01e..c8ea77d 100644 --- a/tests/ut/python/defenses/test_pad.py +++ b/tests/ut/python/defenses/test_pad.py @@ -23,8 +23,7 @@ from mindspore import nn from mindspore.nn.optim.momentum import Momentum from mock_net import Net -from mindarmour.defenses.projected_adversarial_defense import \ - ProjectedAdversarialDefense +from mindarmour.adv_robustness.defenses import ProjectedAdversarialDefense from mindarmour.utils.logger import LogUtil LOGGER = LogUtil.get_instance() diff --git a/tests/ut/python/detectors/black/test_similarity_detector.py b/tests/ut/python/detectors/black/test_similarity_detector.py index 284d4f8..e73630f 100644 --- a/tests/ut/python/detectors/black/test_similarity_detector.py +++ b/tests/ut/python/detectors/black/test_similarity_detector.py @@ -22,7 +22,7 @@ from mindspore import Model from mindspore import context from mindspore.ops.operations import TensorAdd -from mindarmour.detectors.black.similarity_detector import SimilarityDetector +from mindarmour.adv_robustness.detectors import SimilarityDetector context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") diff --git a/tests/ut/python/detectors/test_ensemble_detector.py b/tests/ut/python/detectors/test_ensemble_detector.py index 28d306c..084d260 100644 --- a/tests/ut/python/detectors/test_ensemble_detector.py +++ b/tests/ut/python/detectors/test_ensemble_detector.py @@ -22,9 +22,9 @@ from mindspore.ops.operations import TensorAdd from mindspore.train.model import Model from mindspore import context -from mindarmour.detectors.mag_net import ErrorBasedDetector -from mindarmour.detectors.region_based_detector import RegionBasedDetector -from mindarmour.detectors.ensemble_detector import EnsembleDetector +from mindarmour.adv_robustness.detectors import ErrorBasedDetector +from mindarmour.adv_robustness.detectors import RegionBasedDetector +from mindarmour.adv_robustness.detectors import EnsembleDetector context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") diff --git a/tests/ut/python/detectors/test_mag_net.py b/tests/ut/python/detectors/test_mag_net.py index 13916ca..e52708b 100644 --- a/tests/ut/python/detectors/test_mag_net.py +++ b/tests/ut/python/detectors/test_mag_net.py @@ -23,8 +23,8 @@ from mindspore.ops.operations import TensorAdd from mindspore import Model from mindspore import context -from mindarmour.detectors.mag_net import ErrorBasedDetector -from mindarmour.detectors.mag_net import DivergenceBasedDetector +from mindarmour.adv_robustness.detectors import ErrorBasedDetector +from mindarmour.adv_robustness.detectors import DivergenceBasedDetector context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") diff --git a/tests/ut/python/detectors/test_region_based_detector.py b/tests/ut/python/detectors/test_region_based_detector.py index f4b891a..845a99e 100644 --- a/tests/ut/python/detectors/test_region_based_detector.py +++ b/tests/ut/python/detectors/test_region_based_detector.py @@ -22,8 +22,7 @@ from mindspore import Model from mindspore import context from mindspore.ops.operations import TensorAdd -from mindarmour.detectors.region_based_detector import \ - RegionBasedDetector +from mindarmour.adv_robustness.detectors import RegionBasedDetector context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") diff --git a/tests/ut/python/detectors/test_spatial_smoothing.py b/tests/ut/python/detectors/test_spatial_smoothing.py index 4ed8b88..5dc6f4e 100644 --- a/tests/ut/python/detectors/test_spatial_smoothing.py +++ b/tests/ut/python/detectors/test_spatial_smoothing.py @@ -17,12 +17,12 @@ Spatial-smoothing detector test. import numpy as np import pytest -import mindspore.ops.operations as M +import mindspore.ops.operations as P from mindspore import Model from mindspore.nn import Cell from mindspore import context -from mindarmour.detectors.spatial_smoothing import SpatialSmoothing +from mindarmour.adv_robustness.detectors import SpatialSmoothing context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") @@ -34,7 +34,7 @@ class Net(Cell): """ def __init__(self): super(Net, self).__init__() - self._softmax = M.Softmax() + self._softmax = P.Softmax() def construct(self, inputs): """ diff --git a/tests/ut/python/diff_privacy/test_attacker.py b/tests/ut/python/diff_privacy/test_attacker.py index 2c00901..013b491 100644 --- a/tests/ut/python/diff_privacy/test_attacker.py +++ b/tests/ut/python/diff_privacy/test_attacker.py @@ -18,7 +18,7 @@ import pytest import numpy as np -from mindarmour.diff_privacy.evaluation.attacker import get_attack_model +from mindarmour.privacy.evaluation.attacker import get_attack_model @pytest.mark.level0 diff --git a/tests/ut/python/diff_privacy/test_mechanisms.py b/tests/ut/python/diff_privacy/test_mechanisms.py index 2e2f7f7..ef30fc3 100644 --- a/tests/ut/python/diff_privacy/test_mechanisms.py +++ b/tests/ut/python/diff_privacy/test_mechanisms.py @@ -19,10 +19,10 @@ import pytest from mindspore import context from mindspore import Tensor from mindspore.common import dtype as mstype -from mindarmour.diff_privacy import NoiseAdaGaussianRandom -from mindarmour.diff_privacy import AdaClippingWithGaussianRandom -from mindarmour.diff_privacy import NoiseMechanismsFactory -from mindarmour.diff_privacy import ClipMechanismsFactory +from mindarmour.privacy.diff_privacy import NoiseAdaGaussianRandom +from mindarmour.privacy.diff_privacy import AdaClippingWithGaussianRandom +from mindarmour.privacy.diff_privacy import NoiseMechanismsFactory +from mindarmour.privacy.diff_privacy import ClipMechanismsFactory @pytest.mark.level0 diff --git a/tests/ut/python/diff_privacy/test_membership_inference.py b/tests/ut/python/diff_privacy/test_membership_inference.py index 088baef..d4f925c 100644 --- a/tests/ut/python/diff_privacy/test_membership_inference.py +++ b/tests/ut/python/diff_privacy/test_membership_inference.py @@ -26,7 +26,7 @@ from mindspore import nn from mindspore.train import Model import mindspore.context as context -from mindarmour.diff_privacy.evaluation.membership_inference import MembershipInference +from mindarmour.privacy.evaluation import MembershipInference sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../")) from defenses.mock_net import Net diff --git a/tests/ut/python/diff_privacy/test_model_train.py b/tests/ut/python/diff_privacy/test_model_train.py index bdfde27..7608aaa 100644 --- a/tests/ut/python/diff_privacy/test_model_train.py +++ b/tests/ut/python/diff_privacy/test_model_train.py @@ -21,10 +21,10 @@ from mindspore import nn from mindspore import context import mindspore.dataset as ds -from mindarmour.diff_privacy import DPModel -from mindarmour.diff_privacy import NoiseMechanismsFactory -from mindarmour.diff_privacy import ClipMechanismsFactory -from mindarmour.diff_privacy import DPOptimizerClassFactory +from mindarmour.privacy.diff_privacy import DPModel +from mindarmour.privacy.diff_privacy import NoiseMechanismsFactory +from mindarmour.privacy.diff_privacy import ClipMechanismsFactory +from mindarmour.privacy.diff_privacy import DPOptimizerClassFactory from test_network import LeNet5 diff --git a/tests/ut/python/diff_privacy/test_monitor.py b/tests/ut/python/diff_privacy/test_monitor.py index 4f9dcdd..b0cc0cc 100644 --- a/tests/ut/python/diff_privacy/test_monitor.py +++ b/tests/ut/python/diff_privacy/test_monitor.py @@ -22,7 +22,7 @@ import mindspore.dataset as ds from mindspore.train import Model import mindspore.context as context -from mindarmour.diff_privacy import PrivacyMonitorFactory +from mindarmour.privacy.diff_privacy import PrivacyMonitorFactory from mindarmour.utils.logger import LogUtil from test_network import LeNet5 diff --git a/tests/ut/python/diff_privacy/test_optimizer.py b/tests/ut/python/diff_privacy/test_optimizer.py index a0c70b9..128ba0e 100644 --- a/tests/ut/python/diff_privacy/test_optimizer.py +++ b/tests/ut/python/diff_privacy/test_optimizer.py @@ -17,7 +17,7 @@ from mindspore import nn from mindspore import context from mindspore.train.model import Model -from mindarmour.diff_privacy import DPOptimizerClassFactory +from mindarmour.privacy.diff_privacy import DPOptimizerClassFactory from test_network import LeNet5 diff --git a/tests/ut/python/evaluations/black/test_black_defense_eval.py b/tests/ut/python/evaluations/black/test_black_defense_eval.py index 3cb925d..db59a6d 100644 --- a/tests/ut/python/evaluations/black/test_black_defense_eval.py +++ b/tests/ut/python/evaluations/black/test_black_defense_eval.py @@ -17,7 +17,7 @@ Black-box defense evaluation test. import numpy as np import pytest -from mindarmour.evaluations.black.defense_evaluation import BlackDefenseEvaluate +from mindarmour.adv_robustness.evaluations import BlackDefenseEvaluate @pytest.mark.level0 diff --git a/tests/ut/python/evaluations/test_attack_eval.py b/tests/ut/python/evaluations/test_attack_eval.py index 645f5d8..7af53d5 100644 --- a/tests/ut/python/evaluations/test_attack_eval.py +++ b/tests/ut/python/evaluations/test_attack_eval.py @@ -17,7 +17,7 @@ Attack evaluation test. import numpy as np import pytest -from mindarmour.evaluations.attack_evaluation import AttackEvaluate +from mindarmour.adv_robustness.evaluations import AttackEvaluate @pytest.mark.level0 diff --git a/tests/ut/python/evaluations/test_defense_eval.py b/tests/ut/python/evaluations/test_defense_eval.py index 97fee9b..c151b27 100644 --- a/tests/ut/python/evaluations/test_defense_eval.py +++ b/tests/ut/python/evaluations/test_defense_eval.py @@ -17,7 +17,7 @@ Defense evaluation test. import numpy as np import pytest -from mindarmour.evaluations.defense_evaluation import DefenseEvaluate +from mindarmour.adv_robustness.evaluations import DefenseEvaluate @pytest.mark.level0 diff --git a/tests/ut/python/evaluations/test_radar_metric.py b/tests/ut/python/evaluations/test_radar_metric.py index 5324a22..f43db5e 100644 --- a/tests/ut/python/evaluations/test_radar_metric.py +++ b/tests/ut/python/evaluations/test_radar_metric.py @@ -15,7 +15,7 @@ Radar map test. """ import pytest -from mindarmour.evaluations.visual_metrics import RadarMetric +from mindarmour.adv_robustness.evaluations import RadarMetric @pytest.mark.level0 @pytest.mark.platform_arm_ascend_training @pytest.mark.platform_x86_ascend_training diff --git a/tests/ut/python/fuzzing/test_coverage_metrics.py b/tests/ut/python/fuzzing/test_coverage_metrics.py index de2e727..282e1f4 100644 --- a/tests/ut/python/fuzzing/test_coverage_metrics.py +++ b/tests/ut/python/fuzzing/test_coverage_metrics.py @@ -17,15 +17,14 @@ Model-fuzz coverage test. import numpy as np import pytest +from mindspore import nn +from mindspore.nn import Cell, SoftmaxCrossEntropyWithLogits from mindspore.train import Model -import mindspore.nn as nn -from mindspore.nn import Cell from mindspore import context -from mindspore.nn import SoftmaxCrossEntropyWithLogits -from mindarmour.attacks.gradient_method import FastGradientSignMethod +from mindarmour.adv_robustness.attacks import FastGradientSignMethod from mindarmour.utils.logger import LogUtil -from mindarmour.fuzzing.model_coverage_metrics import ModelCoverageMetrics +from mindarmour.fuzz_testing import ModelCoverageMetrics LOGGER = LogUtil.get_instance() TAG = 'Neuron coverage test' diff --git a/tests/ut/python/fuzzing/test_fuzzer.py b/tests/ut/python/fuzzing/test_fuzzer.py index c971ace..3d7b252 100644 --- a/tests/ut/python/fuzzing/test_fuzzer.py +++ b/tests/ut/python/fuzzing/test_fuzzer.py @@ -22,8 +22,8 @@ from mindspore.common.initializer import TruncatedNormal from mindspore.ops import operations as P from mindspore.train import Model -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 LOGGER = LogUtil.get_instance() diff --git a/tests/ut/python/fuzzing/test_image_transform.py b/tests/ut/python/fuzzing/test_image_transform.py index 77d3d18..9360746 100644 --- a/tests/ut/python/fuzzing/test_image_transform.py +++ b/tests/ut/python/fuzzing/test_image_transform.py @@ -18,8 +18,8 @@ import numpy as np import pytest from mindarmour.utils.logger import LogUtil -from mindarmour.fuzzing.image_transform import Contrast, Brightness, Blur, Noise, \ - Translate, Scale, Shear, Rotate +from mindarmour.fuzz_testing.image_transform import Contrast, Brightness, \ + Blur, Noise, Translate, Scale, Shear, Rotate LOGGER = LogUtil.get_instance() TAG = 'Image transform test'