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_model_train.py 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. DP-Model test.
  16. """
  17. import pytest
  18. import numpy as np
  19. from mindspore import nn
  20. from mindspore.model_zoo.lenet import LeNet5
  21. from mindspore import context
  22. import mindspore.dataset as ds
  23. from mindarmour.diff_privacy import DPOptimizerClassFactory
  24. from mindarmour.diff_privacy import DPModel
  25. def dataset_generator(batch_size, batches):
  26. data = np.random.random((batches * batch_size, 1, 32, 32)).astype(np.float32)
  27. label = np.random.randint(0, 10, batches * batch_size).astype(np.int32)
  28. for i in range(batches):
  29. yield data[i * batch_size:(i + 1) * batch_size], label[i * batch_size:(i + 1) * batch_size]
  30. @pytest.mark.level0
  31. @pytest.mark.platform_arm_ascend_training
  32. @pytest.mark.platform_x86_ascend_training
  33. @pytest.mark.env_card
  34. @pytest.mark.component_mindarmour
  35. def test_dp_model():
  36. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  37. l2_norm_bound = 1.0
  38. initial_noise_multiplier = 0.01
  39. network = LeNet5()
  40. batch_size = 32
  41. batches = 128
  42. epochs = 1
  43. loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  44. gaussian_mech = DPOptimizerClassFactory(micro_batches=2)
  45. gaussian_mech.set_mechanisms('Gaussian',
  46. norm_bound=l2_norm_bound,
  47. initial_noise_multiplier=initial_noise_multiplier)
  48. net_opt = gaussian_mech.create('SGD')(params=network.trainable_params(),
  49. learning_rate=0.1,
  50. momentum=0.9)
  51. model = DPModel(micro_batches=2,
  52. norm_clip=l2_norm_bound,
  53. dp_mech=gaussian_mech.mech,
  54. network=network,
  55. loss_fn=loss,
  56. optimizer=net_opt,
  57. metrics=None)
  58. ms_ds = ds.GeneratorDataset(dataset_generator(batch_size, batches), ['data', 'label'])
  59. ms_ds.set_dataset_size(batch_size * batches)
  60. model.train(epochs, ms_ds)

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