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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. from mindspore import Tensor
  22. from mindspore.common import dtype as mstype
  23. import mindspore.nn as nn
  24. from mindspore.nn.metrics import Accuracy
  25. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
  26. from mindspore import load_checkpoint, load_param_into_net, export
  27. from mindspore.train import Model
  28. from mindspore.compression.quant import QuantizationAwareTraining
  29. from mindspore.compression.quant.quant_utils import load_nonquant_param_into_quant_net
  30. from dataset import create_dataset
  31. from config import nonquant_cfg, quant_cfg
  32. from lenet import LeNet5
  33. from lenet_fusion import LeNet5 as LeNet5Fusion
  34. import numpy as np
  35. device_target = 'GPU'
  36. data_path = "/home/workspace/mindspore_dataset/mnist"
  37. def train_lenet():
  38. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  39. cfg = nonquant_cfg
  40. ds_train = create_dataset(os.path.join(data_path, "train"),
  41. cfg.batch_size)
  42. network = LeNet5(cfg.num_classes)
  43. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  44. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  45. time_cb = TimeMonitor(data_size=ds_train.get_dataset_size())
  46. config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps,
  47. keep_checkpoint_max=cfg.keep_checkpoint_max)
  48. ckpoint_cb = ModelCheckpoint(prefix="ckpt_lenet_noquant", config=config_ck)
  49. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  50. print("============== Starting Training Lenet==============")
  51. model.train(cfg['epoch_size'], ds_train, callbacks=[time_cb, ckpoint_cb, LossMonitor()],
  52. dataset_sink_mode=True)
  53. def train_lenet_quant():
  54. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  55. cfg = quant_cfg
  56. ckpt_path = './ckpt_lenet_noquant-10_1875.ckpt'
  57. ds_train = create_dataset(os.path.join(data_path, "train"), cfg.batch_size, 1)
  58. step_size = ds_train.get_dataset_size()
  59. # define fusion network
  60. network = LeNet5Fusion(cfg.num_classes)
  61. # load quantization aware network checkpoint
  62. param_dict = load_checkpoint(ckpt_path)
  63. load_nonquant_param_into_quant_net(network, param_dict)
  64. # convert fusion network to quantization aware network
  65. quantizer = QuantizationAwareTraining(quant_delay=900,
  66. bn_fold=False,
  67. per_channel=[True, False],
  68. symmetric=[True, False])
  69. network = quantizer.quantize(network)
  70. # define network loss
  71. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  72. # define network optimization
  73. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  74. # call back and monitor
  75. config_ckpt = CheckpointConfig(save_checkpoint_steps=cfg.epoch_size * step_size,
  76. keep_checkpoint_max=cfg.keep_checkpoint_max)
  77. ckpt_callback = ModelCheckpoint(prefix="ckpt_lenet_quant", config=config_ckpt)
  78. # define model
  79. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  80. print("============== Starting Training ==============")
  81. model.train(cfg['epoch_size'], ds_train, callbacks=[ckpt_callback, LossMonitor()],
  82. dataset_sink_mode=True)
  83. print("============== End Training ==============")
  84. def eval_quant():
  85. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  86. cfg = quant_cfg
  87. ds_eval = create_dataset(os.path.join(data_path, "test"), cfg.batch_size, 1)
  88. ckpt_path = './ckpt_lenet_quant-10_937.ckpt'
  89. # define fusion network
  90. network = LeNet5Fusion(cfg.num_classes)
  91. # convert fusion network to quantization aware network
  92. quantizer = QuantizationAwareTraining(quant_delay=0,
  93. bn_fold=False,
  94. freeze_bn=10000,
  95. per_channel=[True, False],
  96. symmetric=[True, False])
  97. network = quantizer.quantize(network)
  98. # define loss
  99. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  100. # define network optimization
  101. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  102. # call back and monitor
  103. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  104. # load quantization aware network checkpoint
  105. param_dict = load_checkpoint(ckpt_path)
  106. not_load_param = load_param_into_net(network, param_dict)
  107. if not_load_param:
  108. raise ValueError("Load param into net fail!")
  109. print("============== Starting Testing ==============")
  110. acc = model.eval(ds_eval, dataset_sink_mode=True)
  111. print("============== {} ==============".format(acc))
  112. assert acc['Accuracy'] > 0.98
  113. def export_lenet():
  114. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  115. cfg = quant_cfg
  116. # define fusion network
  117. network = LeNet5Fusion(cfg.num_classes)
  118. # convert fusion network to quantization aware network
  119. quantizer = QuantizationAwareTraining(quant_delay=0,
  120. bn_fold=False,
  121. freeze_bn=10000,
  122. per_channel=[True, False],
  123. symmetric=[True, False])
  124. network = quantizer.quantize(network)
  125. # export network
  126. inputs = Tensor(np.ones([1, 1, cfg.image_height, cfg.image_width]), mstype.float32)
  127. export(network, inputs, file_name="lenet_quant.mindir", file_format='MINDIR', quant_mode='AUTO')
  128. @pytest.mark.level0
  129. @pytest.mark.platform_x86_gpu_training
  130. @pytest.mark.env_onecard
  131. def test_lenet_quant():
  132. train_lenet()
  133. train_lenet_quant()
  134. eval_quant()
  135. export_lenet()
  136. if __name__ == "__main__":
  137. train_lenet_quant()