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_lenet_quant.py 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. """
  16. train and infer lenet quantization network
  17. """
  18. import os
  19. import pytest
  20. from mindspore import context
  21. import mindspore.nn as nn
  22. from mindspore.nn.metrics import Accuracy
  23. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
  24. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  25. from mindspore.train import Model
  26. from mindspore.train.quant import quant
  27. from mindspore.train.quant.quant_utils import load_nonquant_param_into_quant_net
  28. from dataset import create_dataset
  29. from config import nonquant_cfg, quant_cfg
  30. from lenet import LeNet5
  31. from lenet_fusion import LeNet5 as LeNet5Fusion
  32. device_target = 'GPU'
  33. data_path = "/home/workspace/mindspore_dataset/mnist"
  34. def train_lenet():
  35. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  36. cfg = nonquant_cfg
  37. ds_train = create_dataset(os.path.join(data_path, "train"),
  38. cfg.batch_size)
  39. network = LeNet5(cfg.num_classes)
  40. net_loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction="mean")
  41. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  42. time_cb = TimeMonitor(data_size=ds_train.get_dataset_size())
  43. config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps,
  44. keep_checkpoint_max=cfg.keep_checkpoint_max)
  45. ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", config=config_ck)
  46. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  47. print("============== Starting Training Lenet==============")
  48. model.train(cfg['epoch_size'], ds_train, callbacks=[time_cb, ckpoint_cb, LossMonitor()],
  49. dataset_sink_mode=True)
  50. def train_lenet_quant():
  51. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  52. cfg = quant_cfg
  53. ckpt_path = './checkpoint_lenet-10_1875.ckpt'
  54. ds_train = create_dataset(os.path.join(data_path, "train"), cfg.batch_size, 1)
  55. step_size = ds_train.get_dataset_size()
  56. # define fusion network
  57. network = LeNet5Fusion(cfg.num_classes)
  58. # load quantization aware network checkpoint
  59. param_dict = load_checkpoint(ckpt_path)
  60. load_nonquant_param_into_quant_net(network, param_dict)
  61. # convert fusion network to quantization aware network
  62. network = quant.convert_quant_network(network, quant_delay=900, bn_fold=False, per_channel=[True, False],
  63. symmetric=[False, False])
  64. # define network loss
  65. net_loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction="mean")
  66. # define network optimization
  67. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  68. # call back and monitor
  69. config_ckpt = CheckpointConfig(save_checkpoint_steps=cfg.epoch_size * step_size,
  70. keep_checkpoint_max=cfg.keep_checkpoint_max)
  71. ckpt_callback = ModelCheckpoint(prefix="checkpoint_lenet", config=config_ckpt)
  72. # define model
  73. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  74. print("============== Starting Training ==============")
  75. model.train(cfg['epoch_size'], ds_train, callbacks=[ckpt_callback, LossMonitor()],
  76. dataset_sink_mode=True)
  77. print("============== End Training ==============")
  78. def eval_quant():
  79. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  80. cfg = quant_cfg
  81. ds_eval = create_dataset(os.path.join(data_path, "test"), cfg.batch_size, 1)
  82. ckpt_path = './checkpoint_lenet_1-10_937.ckpt'
  83. # define fusion network
  84. network = LeNet5Fusion(cfg.num_classes)
  85. # convert fusion network to quantization aware network
  86. network = quant.convert_quant_network(network, quant_delay=0, bn_fold=False, freeze_bn=10000,
  87. per_channel=[True, False])
  88. # define loss
  89. net_loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction="mean")
  90. # define network optimization
  91. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  92. # call back and monitor
  93. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  94. # load quantization aware network checkpoint
  95. param_dict = load_checkpoint(ckpt_path)
  96. not_load_param = load_param_into_net(network, param_dict)
  97. if not_load_param:
  98. raise ValueError("Load param into net fail!")
  99. print("============== Starting Testing ==============")
  100. acc = model.eval(ds_eval, dataset_sink_mode=True)
  101. print("============== {} ==============".format(acc))
  102. assert acc['Accuracy'] > 0.98
  103. @pytest.mark.level0
  104. @pytest.mark.platform_x86_gpu_training
  105. @pytest.mark.env_onecard
  106. def test_lenet_quant():
  107. train_lenet()
  108. train_lenet_quant()
  109. eval_quant()
  110. if __name__ == "__main__":
  111. train_lenet_quant()