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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright 2021 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. Suppress Privacy model test.
  16. """
  17. import pytest
  18. import numpy as np
  19. from mindspore import nn
  20. from mindspore import context
  21. from mindspore.train.callback import ModelCheckpoint
  22. from mindspore.train.callback import CheckpointConfig
  23. from mindspore.train.callback import LossMonitor
  24. from mindspore.nn.metrics import Accuracy
  25. import mindspore.dataset as ds
  26. from ut.python.utils.mock_net import Net as LeNet5
  27. from mindarmour.privacy.sup_privacy import SuppressModel
  28. from mindarmour.privacy.sup_privacy import SuppressMasker
  29. from mindarmour.privacy.sup_privacy import SuppressPrivacyFactory
  30. from mindarmour.privacy.sup_privacy import MaskLayerDes
  31. def dataset_generator(batch_size, batches):
  32. """mock training data."""
  33. data = np.random.random((batches*batch_size, 1, 32, 32)).astype(
  34. np.float32)
  35. label = np.random.randint(0, 10, batches*batch_size).astype(np.int32)
  36. for i in range(batches):
  37. yield data[i*batch_size:(i + 1)*batch_size],\
  38. label[i*batch_size:(i + 1)*batch_size]
  39. @pytest.mark.level0
  40. @pytest.mark.platform_arm_ascend_training
  41. @pytest.mark.platform_x86_ascend_training
  42. @pytest.mark.env_card
  43. @pytest.mark.component_mindarmour
  44. def test_suppress_model_with_pynative_mode():
  45. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  46. networks_l5 = LeNet5()
  47. epochs = 5
  48. batch_num = 10
  49. batch_size = 32
  50. mask_times = 10
  51. lr = 0.01
  52. masklayers_lenet5 = []
  53. masklayers_lenet5.append(MaskLayerDes("conv1.weight", False, False, -1))
  54. suppress_ctrl_instance = SuppressPrivacyFactory().create(policy="local_train",
  55. end_epoch=epochs,
  56. batch_num=batch_num,
  57. start_epoch=1,
  58. mask_times=mask_times,
  59. networks=networks_l5,
  60. lr=lr,
  61. sparse_end=0.50,
  62. sparse_start=0.0,
  63. mask_layers=masklayers_lenet5)
  64. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  65. net_opt = nn.SGD(networks_l5.trainable_params(), lr)
  66. model_instance = SuppressModel(
  67. network=networks_l5,
  68. loss_fn=net_loss,
  69. optimizer=net_opt,
  70. metrics={"Accuracy": Accuracy()})
  71. model_instance.link_suppress_ctrl(suppress_ctrl_instance)
  72. suppress_masker = SuppressMasker(model=model_instance, suppress_ctrl=suppress_ctrl_instance)
  73. config_ck = CheckpointConfig(save_checkpoint_steps=batch_num, keep_checkpoint_max=10)
  74. ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet",
  75. directory="./trained_ckpt_file/",
  76. config=config_ck)
  77. ds_train = ds.GeneratorDataset(dataset_generator(batch_size, batch_num), ['data', 'label'])
  78. model_instance.train(epochs, ds_train, callbacks=[ckpoint_cb, LossMonitor(), suppress_masker],
  79. dataset_sink_mode=False)

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