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_lbfgs.py 2.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. LBFGS-Attack test.
  16. """
  17. import sys
  18. import numpy as np
  19. import pytest
  20. import os
  21. from mindspore import context
  22. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  23. from mindarmour.attacks.lbfgs import LBFGS
  24. from mindarmour.utils.logger import LogUtil
  25. sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),
  26. "../../../../"))
  27. from example.mnist_demo.lenet5_net import LeNet5
  28. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  29. LOGGER = LogUtil.get_instance()
  30. TAG = 'LBFGS_Test'
  31. LOGGER.set_level('DEBUG')
  32. @pytest.mark.level0
  33. @pytest.mark.platform_arm_ascend_training
  34. @pytest.mark.platform_x86_ascend_training
  35. @pytest.mark.env_card
  36. @pytest.mark.component_mindarmour
  37. def test_lbfgs_attack():
  38. """
  39. LBFGS-Attack test
  40. """
  41. np.random.seed(123)
  42. # upload trained network
  43. current_dir = os.path.dirname(os.path.abspath(__file__))
  44. ckpt_name = os.path.join(current_dir,
  45. '../test_data/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt')
  46. net = LeNet5()
  47. load_dict = load_checkpoint(ckpt_name)
  48. load_param_into_net(net, load_dict)
  49. # get one mnist image
  50. input_np = np.load(os.path.join(current_dir,
  51. '../test_data/test_images.npy'))[:1]
  52. label_np = np.load(os.path.join(current_dir,
  53. '../test_data/test_labels.npy'))[:1]
  54. LOGGER.debug(TAG, 'true label is :{}'.format(label_np[0]))
  55. classes = 10
  56. target_np = np.random.randint(0, classes, 1)
  57. while target_np == label_np[0]:
  58. target_np = np.random.randint(0, classes)
  59. target_np = np.eye(10)[target_np].astype(np.float32)
  60. attack = LBFGS(net, is_targeted=True)
  61. LOGGER.debug(TAG, 'target_np is :{}'.format(target_np[0]))
  62. adv_data = attack.generate(input_np, target_np)

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