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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.quantizer import OptimizeOption
  30. from mindspore.compression.quant.quant_utils import load_nonquant_param_into_quant_net
  31. from dataset import create_dataset
  32. from config import nonquant_cfg, quant_cfg
  33. from lenet import LeNet5
  34. from lenet_fusion import LeNet5 as LeNet5Fusion
  35. import numpy as np
  36. device_target = 'GPU'
  37. data_path = "/home/workspace/mindspore_dataset/mnist"
  38. def train_lenet():
  39. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  40. cfg = nonquant_cfg
  41. ds_train = create_dataset(os.path.join(data_path, "train"),
  42. cfg.batch_size)
  43. network = LeNet5(cfg.num_classes)
  44. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  45. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  46. time_cb = TimeMonitor(data_size=ds_train.get_dataset_size())
  47. config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps,
  48. keep_checkpoint_max=cfg.keep_checkpoint_max)
  49. ckpoint_cb = ModelCheckpoint(prefix="ckpt_lenet_noquant", config=config_ck)
  50. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  51. print("============== Starting Training Lenet==============")
  52. model.train(cfg['epoch_size'], ds_train, callbacks=[time_cb, ckpoint_cb, LossMonitor()],
  53. dataset_sink_mode=True)
  54. def eval_lenet():
  55. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  56. cfg = nonquant_cfg
  57. ds_eval = create_dataset(os.path.join(data_path, "test"), cfg.batch_size, 1)
  58. ckpt_path = './ckpt_lenet_noquant-10_1875.ckpt'
  59. # define fusion network
  60. network = LeNet5(cfg.num_classes)
  61. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  62. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  63. # call back and monitor
  64. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  65. # load quantization aware network checkpoint
  66. param_dict = load_checkpoint(ckpt_path)
  67. not_load_param = load_param_into_net(network, param_dict)
  68. if not_load_param:
  69. raise ValueError("Load param into net fail!")
  70. print("============== Starting Testing ==============")
  71. acc = model.eval(ds_eval, dataset_sink_mode=True)
  72. print("============== {} ==============".format(acc))
  73. def train_lenet_quant(optim_option="QAT"):
  74. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  75. cfg = quant_cfg
  76. ckpt_path = './ckpt_lenet_noquant-10_1875.ckpt'
  77. ds_train = create_dataset(os.path.join(data_path, "train"), cfg.batch_size, 1)
  78. step_size = ds_train.get_dataset_size()
  79. # define fusion network
  80. network = LeNet5Fusion(cfg.num_classes)
  81. # load quantization aware network checkpoint
  82. param_dict = load_checkpoint(ckpt_path)
  83. load_nonquant_param_into_quant_net(network, param_dict)
  84. # convert fusion network to quantization aware network
  85. if optim_option == "LEARNED_SCALE":
  86. quant_optim_otions = OptimizeOption.LEARNED_SCALE
  87. quantizer = QuantizationAwareTraining(bn_fold=False,
  88. per_channel=[True, False],
  89. symmetric=[True, True],
  90. narrow_range=[True, True],
  91. freeze_bn=0,
  92. quant_delay=0,
  93. one_conv_fold=True,
  94. optimize_option=quant_optim_otions)
  95. else:
  96. quantizer = QuantizationAwareTraining(quant_delay=900,
  97. bn_fold=False,
  98. per_channel=[True, False],
  99. symmetric=[True, False])
  100. network = quantizer.quantize(network)
  101. # define network loss
  102. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  103. # define network optimization
  104. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  105. # call back and monitor
  106. config_ckpt = CheckpointConfig(save_checkpoint_steps=cfg.epoch_size * step_size,
  107. keep_checkpoint_max=cfg.keep_checkpoint_max)
  108. ckpt_callback = ModelCheckpoint(prefix="ckpt_lenet_quant"+optim_option, config=config_ckpt)
  109. # define model
  110. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  111. print("============== Starting Training ==============")
  112. model.train(cfg['epoch_size'], ds_train, callbacks=[ckpt_callback, LossMonitor()],
  113. dataset_sink_mode=True)
  114. print("============== End Training ==============")
  115. def eval_quant(optim_option="QAT"):
  116. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  117. cfg = quant_cfg
  118. ds_eval = create_dataset(os.path.join(data_path, "test"), cfg.batch_size, 1)
  119. ckpt_path = './ckpt_lenet_quant'+optim_option+'-10_937.ckpt'
  120. # define fusion network
  121. network = LeNet5Fusion(cfg.num_classes)
  122. # convert fusion network to quantization aware network
  123. if optim_option == "LEARNED_SCALE":
  124. quant_optim_otions = OptimizeOption.LEARNED_SCALE
  125. quantizer = QuantizationAwareTraining(bn_fold=False,
  126. per_channel=[True, False],
  127. symmetric=[True, True],
  128. narrow_range=[True, True],
  129. freeze_bn=0,
  130. quant_delay=0,
  131. one_conv_fold=True,
  132. optimize_option=quant_optim_otions)
  133. else:
  134. quantizer = QuantizationAwareTraining(quant_delay=0,
  135. bn_fold=False,
  136. freeze_bn=10000,
  137. per_channel=[True, False],
  138. symmetric=[True, False])
  139. network = quantizer.quantize(network)
  140. # define loss
  141. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  142. # define network optimization
  143. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  144. # call back and monitor
  145. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  146. # load quantization aware network checkpoint
  147. param_dict = load_checkpoint(ckpt_path)
  148. not_load_param = load_param_into_net(network, param_dict)
  149. if not_load_param:
  150. raise ValueError("Load param into net fail!")
  151. print("============== Starting Testing ==============")
  152. acc = model.eval(ds_eval, dataset_sink_mode=True)
  153. print("============== {} ==============".format(acc))
  154. assert acc['Accuracy'] > 0.98
  155. def export_lenet(optim_option="QAT"):
  156. context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
  157. cfg = quant_cfg
  158. # define fusion network
  159. network = LeNet5Fusion(cfg.num_classes)
  160. # convert fusion network to quantization aware network
  161. if optim_option == "LEARNED_SCALE":
  162. quant_optim_otions = OptimizeOption.LEARNED_SCALE
  163. quantizer = QuantizationAwareTraining(bn_fold=False,
  164. per_channel=[True, False],
  165. symmetric=[True, True],
  166. narrow_range=[True, True],
  167. freeze_bn=0,
  168. quant_delay=0,
  169. one_conv_fold=True,
  170. optimize_option=quant_optim_otions)
  171. else:
  172. quantizer = QuantizationAwareTraining(quant_delay=0,
  173. bn_fold=False,
  174. freeze_bn=10000,
  175. per_channel=[True, False],
  176. symmetric=[True, False])
  177. network = quantizer.quantize(network)
  178. # export network
  179. inputs = Tensor(np.ones([1, 1, cfg.image_height, cfg.image_width]), mstype.float32)
  180. export(network, inputs, file_name="lenet_quant", file_format='MINDIR', quant_mode='AUTO')
  181. @pytest.mark.level0
  182. @pytest.mark.platform_x86_gpu_training
  183. @pytest.mark.env_onecard
  184. def test_lenet_quant():
  185. train_lenet()
  186. eval_lenet()
  187. train_lenet_quant()
  188. eval_quant()
  189. export_lenet()
  190. train_lenet_quant(optim_option="LEARNED_SCALE")
  191. eval_quant(optim_option="LEARNED_SCALE")
  192. export_lenet(optim_option="LEARNED_SCALE")
  193. if __name__ == "__main__":
  194. train_lenet_quant()