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_monitor.py 8.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. DP-Monitor test.
  16. """
  17. import pytest
  18. import numpy as np
  19. import mindspore.nn as nn
  20. import mindspore.dataset as ds
  21. from mindspore.train import Model
  22. import mindspore.context as context
  23. from mindarmour.privacy.diff_privacy import PrivacyMonitorFactory
  24. from mindarmour.utils.logger import LogUtil
  25. from ut.python.utils.mock_net import Net
  26. LOGGER = LogUtil.get_instance()
  27. TAG = 'DP-Monitor Test'
  28. def dataset_generator():
  29. batch_size = 16
  30. batches = 128
  31. data = np.random.random((batches * batch_size, 1, 32, 32)).astype(
  32. np.float32)
  33. label = np.random.randint(0, 10, batches * batch_size).astype(np.int32)
  34. for i in range(batches):
  35. yield data[i * batch_size: (i + 1) * batch_size], \
  36. label[i * batch_size: (i + 1) * batch_size]
  37. @pytest.mark.level0
  38. @pytest.mark.platform_arm_ascend_training
  39. @pytest.mark.platform_x86_ascend_training
  40. @pytest.mark.env_card
  41. @pytest.mark.component_mindarmour
  42. def test_dp_monitor():
  43. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  44. batch_size = 16
  45. epochs = 1
  46. rdp = PrivacyMonitorFactory.create(policy='rdp', num_samples=60000,
  47. batch_size=batch_size,
  48. initial_noise_multiplier=0.4,
  49. noise_decay_rate=6e-3)
  50. suggest_epoch = rdp.max_epoch_suggest()
  51. LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
  52. suggest_epoch)
  53. network = Net()
  54. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  55. net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
  56. model = Model(network, net_loss, net_opt)
  57. LOGGER.info(TAG, "============== Starting Training ==============")
  58. ds1 = ds.GeneratorDataset(dataset_generator,
  59. ["data", "label"])
  60. model.train(epochs, ds1, callbacks=[rdp], dataset_sink_mode=False)
  61. @pytest.mark.level0
  62. @pytest.mark.platform_x86_gpu_inference
  63. @pytest.mark.env_card
  64. @pytest.mark.component_mindarmour
  65. def test_dp_monitor_gpu():
  66. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  67. batch_size = 16
  68. epochs = 1
  69. rdp = PrivacyMonitorFactory.create(policy='rdp', num_samples=60000,
  70. batch_size=batch_size,
  71. initial_noise_multiplier=0.4,
  72. noise_decay_rate=6e-3)
  73. suggest_epoch = rdp.max_epoch_suggest()
  74. LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
  75. suggest_epoch)
  76. network = Net()
  77. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  78. net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
  79. model = Model(network, net_loss, net_opt)
  80. LOGGER.info(TAG, "============== Starting Training ==============")
  81. ds1 = ds.GeneratorDataset(dataset_generator,
  82. ["data", "label"])
  83. model.train(epochs, ds1, callbacks=[rdp], dataset_sink_mode=False)
  84. @pytest.mark.level0
  85. @pytest.mark.platform_x86_cpu
  86. @pytest.mark.env_card
  87. @pytest.mark.component_mindarmour
  88. def test_dp_monitor_cpu():
  89. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  90. batch_size = 16
  91. epochs = 1
  92. rdp = PrivacyMonitorFactory.create(policy='rdp', num_samples=60000,
  93. batch_size=batch_size,
  94. initial_noise_multiplier=0.4,
  95. noise_decay_rate=6e-3)
  96. suggest_epoch = rdp.max_epoch_suggest()
  97. LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
  98. suggest_epoch)
  99. network = Net()
  100. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  101. net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
  102. model = Model(network, net_loss, net_opt)
  103. LOGGER.info(TAG, "============== Starting Training ==============")
  104. ds1 = ds.GeneratorDataset(dataset_generator,
  105. ["data", "label"])
  106. model.train(epochs, ds1, callbacks=[rdp], dataset_sink_mode=False)
  107. @pytest.mark.level0
  108. @pytest.mark.platform_arm_ascend_training
  109. @pytest.mark.platform_x86_ascend_training
  110. @pytest.mark.env_card
  111. @pytest.mark.component_mindarmour
  112. def test_dp_monitor_zcdp():
  113. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  114. batch_size = 16
  115. epochs = 1
  116. zcdp = PrivacyMonitorFactory.create(policy='zcdp', num_samples=60000,
  117. batch_size=batch_size,
  118. initial_noise_multiplier=0.4,
  119. noise_decay_rate=6e-3)
  120. suggest_epoch = zcdp.max_epoch_suggest()
  121. LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
  122. suggest_epoch)
  123. network = Net()
  124. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  125. net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
  126. model = Model(network, net_loss, net_opt)
  127. LOGGER.info(TAG, "============== Starting Training ==============")
  128. ds1 = ds.GeneratorDataset(dataset_generator,
  129. ["data", "label"])
  130. model.train(epochs, ds1, callbacks=[zcdp], dataset_sink_mode=False)
  131. @pytest.mark.level0
  132. @pytest.mark.platform_x86_gpu_inference
  133. @pytest.mark.env_card
  134. @pytest.mark.component_mindarmour
  135. def test_dp_monitor_zcdp_gpu():
  136. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  137. batch_size = 16
  138. epochs = 1
  139. zcdp = PrivacyMonitorFactory.create(policy='zcdp', num_samples=60000,
  140. batch_size=batch_size,
  141. initial_noise_multiplier=0.4,
  142. noise_decay_rate=6e-3)
  143. suggest_epoch = zcdp.max_epoch_suggest()
  144. LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
  145. suggest_epoch)
  146. network = Net()
  147. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  148. net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
  149. model = Model(network, net_loss, net_opt)
  150. LOGGER.info(TAG, "============== Starting Training ==============")
  151. ds1 = ds.GeneratorDataset(dataset_generator,
  152. ["data", "label"])
  153. model.train(epochs, ds1, callbacks=[zcdp], dataset_sink_mode=False)
  154. @pytest.mark.level0
  155. @pytest.mark.platform_x86_cpu
  156. @pytest.mark.env_card
  157. @pytest.mark.component_mindarmour
  158. def test_dp_monitor_zcdp_cpu():
  159. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  160. batch_size = 16
  161. epochs = 1
  162. zcdp = PrivacyMonitorFactory.create(policy='zcdp', num_samples=60000,
  163. batch_size=batch_size,
  164. initial_noise_multiplier=0.4,
  165. noise_decay_rate=6e-3)
  166. suggest_epoch = zcdp.max_epoch_suggest()
  167. LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
  168. suggest_epoch)
  169. network = Net()
  170. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  171. net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
  172. model = Model(network, net_loss, net_opt)
  173. LOGGER.info(TAG, "============== Starting Training ==============")
  174. ds1 = ds.GeneratorDataset(dataset_generator,
  175. ["data", "label"])
  176. model.train(epochs, ds1, callbacks=[zcdp], dataset_sink_mode=False)

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