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 6.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 import context
  21. import mindspore.dataset as ds
  22. from mindarmour.diff_privacy import DPModel
  23. from mindarmour.diff_privacy import NoiseMechanismsFactory
  24. from mindarmour.diff_privacy import ClipMechanismsFactory
  25. from mindarmour.diff_privacy import DPOptimizerClassFactory
  26. from test_network import LeNet5
  27. def dataset_generator(batch_size, batches):
  28. """mock training data."""
  29. data = np.random.random((batches*batch_size, 1, 32, 32)).astype(
  30. np.float32)
  31. label = np.random.randint(0, 10, batches*batch_size).astype(np.int32)
  32. for i in range(batches):
  33. yield data[i*batch_size:(i + 1)*batch_size],\
  34. label[i*batch_size:(i + 1)*batch_size]
  35. @pytest.mark.level0
  36. @pytest.mark.platform_arm_ascend_training
  37. @pytest.mark.platform_x86_ascend_training
  38. @pytest.mark.env_card
  39. @pytest.mark.component_mindarmour
  40. def test_dp_model_with_pynative_mode():
  41. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  42. norm_bound = 1.0
  43. initial_noise_multiplier = 0.01
  44. network = LeNet5()
  45. batch_size = 32
  46. batches = 128
  47. epochs = 1
  48. micro_batches = 2
  49. loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  50. factory_opt = DPOptimizerClassFactory(micro_batches=micro_batches)
  51. factory_opt.set_mechanisms('Gaussian',
  52. norm_bound=norm_bound,
  53. initial_noise_multiplier=initial_noise_multiplier)
  54. net_opt = factory_opt.create('Momentum')(network.trainable_params(),
  55. learning_rate=0.1, momentum=0.9)
  56. clip_mech = ClipMechanismsFactory().create('Gaussian',
  57. decay_policy='Linear',
  58. learning_rate=0.01,
  59. target_unclipped_quantile=0.9,
  60. fraction_stddev=0.01)
  61. model = DPModel(micro_batches=micro_batches,
  62. norm_bound=norm_bound,
  63. clip_mech=clip_mech,
  64. noise_mech=None,
  65. network=network,
  66. loss_fn=loss,
  67. optimizer=net_opt,
  68. metrics=None)
  69. ms_ds = ds.GeneratorDataset(dataset_generator(batch_size, batches),
  70. ['data', 'label'])
  71. ms_ds.set_dataset_size(batch_size*batches)
  72. model.train(epochs, ms_ds, dataset_sink_mode=False)
  73. @pytest.mark.level0
  74. @pytest.mark.platform_arm_ascend_training
  75. @pytest.mark.platform_x86_ascend_training
  76. @pytest.mark.env_card
  77. @pytest.mark.component_mindarmour
  78. def test_dp_model_with_graph_mode():
  79. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  80. norm_bound = 1.0
  81. initial_noise_multiplier = 0.01
  82. network = LeNet5()
  83. batch_size = 32
  84. batches = 128
  85. epochs = 1
  86. loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  87. noise_mech = NoiseMechanismsFactory().create('Gaussian',
  88. norm_bound=norm_bound,
  89. initial_noise_multiplier=initial_noise_multiplier)
  90. clip_mech = ClipMechanismsFactory().create('Gaussian',
  91. decay_policy='Linear',
  92. learning_rate=0.01,
  93. target_unclipped_quantile=0.9,
  94. fraction_stddev=0.01)
  95. net_opt = nn.Momentum(network.trainable_params(), learning_rate=0.1,
  96. momentum=0.9)
  97. model = DPModel(micro_batches=2,
  98. clip_mech=clip_mech,
  99. norm_bound=norm_bound,
  100. noise_mech=noise_mech,
  101. network=network,
  102. loss_fn=loss,
  103. optimizer=net_opt,
  104. metrics=None)
  105. ms_ds = ds.GeneratorDataset(dataset_generator(batch_size, batches),
  106. ['data', 'label'])
  107. ms_ds.set_dataset_size(batch_size*batches)
  108. model.train(epochs, ms_ds, dataset_sink_mode=False)
  109. @pytest.mark.level0
  110. @pytest.mark.platform_arm_ascend_training
  111. @pytest.mark.platform_x86_ascend_training
  112. @pytest.mark.env_card
  113. @pytest.mark.component_mindarmour
  114. def test_dp_model_with_graph_mode_ada_gaussian():
  115. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  116. norm_bound = 1.0
  117. initial_noise_multiplier = 0.01
  118. network = LeNet5()
  119. batch_size = 32
  120. batches = 128
  121. epochs = 1
  122. alpha = 0.8
  123. loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  124. noise_mech = NoiseMechanismsFactory().create('AdaGaussian',
  125. norm_bound=norm_bound,
  126. initial_noise_multiplier=initial_noise_multiplier,
  127. noise_decay_rate=alpha,
  128. noise_update='Exp')
  129. clip_mech = None
  130. net_opt = nn.Momentum(network.trainable_params(), learning_rate=0.1,
  131. momentum=0.9)
  132. model = DPModel(micro_batches=2,
  133. clip_mech=clip_mech,
  134. norm_bound=norm_bound,
  135. noise_mech=noise_mech,
  136. network=network,
  137. loss_fn=loss,
  138. optimizer=net_opt,
  139. metrics=None)
  140. ms_ds = ds.GeneratorDataset(dataset_generator(batch_size, batches),
  141. ['data', 'label'])
  142. ms_ds.set_dataset_size(batch_size*batches)
  143. model.train(epochs, ms_ds, dataset_sink_mode=False)

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