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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 model_zoo.official.cv.mobilenetv2_quant.src.mobilenetV2 import mobilenetV2
  24. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  25. class LeNet5(nn.Cell):
  26. """
  27. Lenet network
  28. Args:
  29. num_class (int): Num classes. Default: 10.
  30. Returns:
  31. Tensor, output tensor
  32. Examples:
  33. >>> LeNet(num_class=10)
  34. """
  35. def __init__(self, num_class=10):
  36. super(LeNet5, self).__init__()
  37. self.num_class = num_class
  38. self.conv1 = nn.Conv2dBnAct(1, 6, kernel_size=5, has_bn=True, activation='relu', pad_mode="valid")
  39. self.conv2 = nn.Conv2dBnAct(6, 16, kernel_size=5, activation='relu', pad_mode="valid")
  40. self.fc1 = nn.DenseBnAct(16 * 5 * 5, 120, activation='relu')
  41. self.fc2 = nn.DenseBnAct(120, 84, activation='relu')
  42. self.fc3 = nn.DenseBnAct(84, self.num_class)
  43. self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
  44. self.flatten = nn.Flatten()
  45. def construct(self, x):
  46. x = self.conv1(x)
  47. x = self.max_pool2d(x)
  48. x = self.conv2(x)
  49. x = self.max_pool2d(x)
  50. x = self.flatten(x)
  51. x = self.fc1(x)
  52. x = self.fc2(x)
  53. x = self.fc3(x)
  54. return x
  55. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  56. def test_qat_lenet():
  57. img = Tensor(np.ones((32, 1, 32, 32)).astype(np.float32))
  58. net = LeNet5()
  59. quantizer = QuantizationAwareTraining(bn_fold=True,
  60. per_channel=[True, False],
  61. symmetric=[True, False])
  62. net = quantizer.quantize(net)
  63. # should load the checkpoint. mock here
  64. net.init_parameters_data()
  65. quant_export.export(net, img, file_name="quant.pb")
  66. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  67. def test_qat_mobile_per_channel_tf():
  68. network = mobilenetV2(num_classes=1000)
  69. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  70. quantizer = QuantizationAwareTraining(bn_fold=True,
  71. per_channel=[True, False],
  72. symmetric=[True, False])
  73. network = quantizer.quantize(network)
  74. # should load the checkpoint. mock here
  75. network.init_parameters_data()
  76. quant_export.export(network, img, file_name="quant.pb")
  77. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  78. def test_qat_mobile_per_channel_ff():
  79. network = mobilenetV2(num_classes=1000)
  80. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  81. quantizer = QuantizationAwareTraining(bn_fold=True,
  82. per_channel=[False, False],
  83. symmetric=[True, False])
  84. network = quantizer.quantize(network)
  85. # should load the checkpoint. mock here
  86. network.init_parameters_data()
  87. quant_export.export(network, img, file_name="quant.pb")