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.

mnist_train.py 3.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. import os
  16. import sys
  17. import mindspore.nn as nn
  18. from mindspore import context, Tensor
  19. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
  20. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  21. from mindspore.train import Model
  22. from mindspore.nn.metrics import Accuracy
  23. from mindarmour.utils.logger import LogUtil
  24. from lenet5_net import LeNet5
  25. sys.path.append("..")
  26. from data_processing import generate_mnist_dataset
  27. LOGGER = LogUtil.get_instance()
  28. TAG = "Lenet5_train"
  29. def mnist_train(epoch_size, batch_size, lr, momentum):
  30. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
  31. enable_mem_reuse=False)
  32. lr = lr
  33. momentum = momentum
  34. epoch_size = epoch_size
  35. mnist_path = "./MNIST_unzip/"
  36. ds = generate_mnist_dataset(os.path.join(mnist_path, "train"),
  37. batch_size=batch_size, repeat_size=1)
  38. network = LeNet5()
  39. net_loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True,
  40. reduction="mean")
  41. net_opt = nn.Momentum(network.trainable_params(), lr, momentum)
  42. config_ck = CheckpointConfig(save_checkpoint_steps=1875,
  43. keep_checkpoint_max=10)
  44. ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet",
  45. directory="./trained_ckpt_file/",
  46. config=config_ck)
  47. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  48. LOGGER.info(TAG, "============== Starting Training ==============")
  49. model.train(epoch_size, ds, callbacks=[ckpoint_cb, LossMonitor()],
  50. dataset_sink_mode=False)
  51. LOGGER.info(TAG, "============== Starting Testing ==============")
  52. ckpt_file_name = "trained_ckpt_file/checkpoint_lenet-10_1875.ckpt"
  53. param_dict = load_checkpoint(ckpt_file_name)
  54. load_param_into_net(network, param_dict)
  55. ds_eval = generate_mnist_dataset(os.path.join(mnist_path, "test"),
  56. batch_size=batch_size)
  57. acc = model.eval(ds_eval, dataset_sink_mode=False)
  58. LOGGER.info(TAG, "============== Accuracy: %s ==============", acc)
  59. if __name__ == '__main__':
  60. mnist_train(10, 32, 0.01, 0.9)

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