You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_ead.py 2.4 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. ensemble adversarial defense test.
  16. """
  17. import numpy as np
  18. import pytest
  19. import logging
  20. from mindspore import nn
  21. from mindspore import context
  22. from mindspore.nn.optim.momentum import Momentum
  23. from mindarmour.attacks.gradient_method import FastGradientSignMethod
  24. from mindarmour.attacks.iterative_gradient_method import \
  25. ProjectedGradientDescent
  26. from mindarmour.defenses.adversarial_defense import EnsembleAdversarialDefense
  27. from mindarmour.utils.logger import LogUtil
  28. from mock_net import Net
  29. LOGGER = LogUtil.get_instance()
  30. TAG = 'Ead_Test'
  31. @pytest.mark.level0
  32. @pytest.mark.platform_arm_ascend_training
  33. @pytest.mark.platform_x86_ascend_training
  34. @pytest.mark.env_card
  35. @pytest.mark.component_mindarmour
  36. def test_ead():
  37. """UT for ensemble adversarial defense."""
  38. num_classes = 10
  39. batch_size = 16
  40. sparse = False
  41. context.set_context(mode=context.GRAPH_MODE)
  42. context.set_context(device_target='Ascend')
  43. # create test data
  44. inputs = np.random.rand(batch_size, 1, 32, 32).astype(np.float32)
  45. labels = np.random.randint(num_classes, size=batch_size).astype(np.int32)
  46. if not sparse:
  47. labels = np.eye(num_classes)[labels].astype(np.float32)
  48. net = Net()
  49. loss_fn = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=sparse)
  50. optimizer = Momentum(net.trainable_params(), 0.001, 0.9)
  51. net = Net()
  52. fgsm = FastGradientSignMethod(net)
  53. pgd = ProjectedGradientDescent(net)
  54. ead = EnsembleAdversarialDefense(net, [fgsm, pgd], loss_fn=loss_fn,
  55. optimizer=optimizer)
  56. LOGGER.set_level(logging.DEBUG)
  57. LOGGER.debug(TAG, '---start ensemble adversarial defense--')
  58. loss = ead.defense(inputs, labels)
  59. LOGGER.debug(TAG, '---end ensemble adversarial defense--')
  60. assert np.any(loss >= 0.0)

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