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_quant.py 6.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. """ tests for quant """
  16. import numpy as np
  17. import pytest
  18. import mindspore.context as context
  19. from mindspore import Tensor
  20. from mindspore import nn
  21. from mindspore.compression.quant import QuantizationAwareTraining
  22. from mindspore.compression.export import quant_export
  23. from mindspore.compression.quant.quantizer import OptimizeOption
  24. from model_zoo.official.cv.mobilenetv2_quant.src.mobilenetV2 import mobilenetV2
  25. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  26. class LeNet5(nn.Cell):
  27. """
  28. Lenet network
  29. Args:
  30. num_class (int): Num classes. Default: 10.
  31. Returns:
  32. Tensor, output tensor
  33. Examples:
  34. >>> LeNet(num_class=10)
  35. """
  36. def __init__(self, num_class=10):
  37. super(LeNet5, self).__init__()
  38. self.num_class = num_class
  39. self.conv1 = nn.Conv2dBnAct(1, 6, kernel_size=5, has_bn=True, activation='relu', pad_mode="valid")
  40. self.conv2 = nn.Conv2dBnAct(6, 16, kernel_size=5, activation='relu', pad_mode="valid")
  41. self.fc1 = nn.DenseBnAct(16 * 5 * 5, 120, activation='relu')
  42. self.fc2 = nn.DenseBnAct(120, 84, activation='relu')
  43. self.fc3 = nn.DenseBnAct(84, self.num_class)
  44. self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
  45. self.flatten = nn.Flatten()
  46. def construct(self, x):
  47. x = self.conv1(x)
  48. x = self.max_pool2d(x)
  49. x = self.conv2(x)
  50. x = self.max_pool2d(x)
  51. x = self.flatten(x)
  52. x = self.fc1(x)
  53. x = self.fc2(x)
  54. x = self.fc3(x)
  55. return x
  56. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  57. def test_qat_lenet():
  58. img = Tensor(np.ones((32, 1, 32, 32)).astype(np.float32))
  59. net = LeNet5()
  60. quantizer = QuantizationAwareTraining(bn_fold=True,
  61. per_channel=[True, False],
  62. symmetric=[True, False])
  63. net = quantizer.quantize(net)
  64. # should load the checkpoint. mock here
  65. net.init_parameters_data()
  66. quant_export.export(net, img, file_name="quant.pb")
  67. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  68. def test_qat_mobile_per_channel_tf():
  69. network = mobilenetV2(num_classes=1000)
  70. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  71. quantizer = QuantizationAwareTraining(bn_fold=True,
  72. per_channel=[True, False],
  73. symmetric=[True, False])
  74. network = quantizer.quantize(network)
  75. # should load the checkpoint. mock here
  76. network.init_parameters_data()
  77. quant_export.export(network, img, file_name="quant.pb")
  78. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  79. def test_qat_mobile_per_channel_ff():
  80. network = mobilenetV2(num_classes=1000)
  81. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  82. quantizer = QuantizationAwareTraining(bn_fold=True,
  83. per_channel=[False, False],
  84. symmetric=[True, False])
  85. network = quantizer.quantize(network)
  86. # should load the checkpoint. mock here
  87. network.init_parameters_data()
  88. quant_export.export(network, img, file_name="quant.pb")
  89. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  90. def test_lsq_lenet():
  91. img = Tensor(np.ones((32, 1, 32, 32)).astype(np.float32))
  92. net = LeNet5()
  93. quantizer = QuantizationAwareTraining(bn_fold=True,
  94. per_channel=[True, False],
  95. symmetric=[True, True],
  96. narrow_range=[True, True],
  97. freeze_bn=0,
  98. quant_delay=0,
  99. one_conv_fold=True,
  100. optimize_option=OptimizeOption.LEARNED_SCALE)
  101. net = quantizer.quantize(net)
  102. # should load the checkpoint. mock here
  103. net.init_parameters_data()
  104. quant_export.export(net, img, file_name="quant.pb")
  105. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  106. def test_lsq_mobile_per_channel_tf():
  107. network = mobilenetV2(num_classes=1000)
  108. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  109. quantizer = QuantizationAwareTraining(bn_fold=True,
  110. per_channel=[True, False],
  111. symmetric=[True, True],
  112. narrow_range=[True, True],
  113. freeze_bn=0,
  114. quant_delay=0,
  115. one_conv_fold=True,
  116. optimize_option=OptimizeOption.LEARNED_SCALE)
  117. network = quantizer.quantize(network)
  118. # should load the checkpoint. mock here
  119. network.init_parameters_data()
  120. quant_export.export(network, img, file_name="quant.pb")
  121. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  122. def test_lsq_mobile_per_channel_ff():
  123. network = mobilenetV2(num_classes=1000)
  124. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  125. quantizer = QuantizationAwareTraining(bn_fold=True,
  126. per_channel=[False, 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=OptimizeOption.LEARNED_SCALE)
  133. network = quantizer.quantize(network)
  134. # should load the checkpoint. mock here
  135. network.init_parameters_data()
  136. quant_export.export(network, img, file_name="quant.pb")