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

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.train.quant import quant as qat
  22. from mobilenetv2_combined import MobileNetV2
  23. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  24. class LeNet5(nn.Cell):
  25. """
  26. Lenet network
  27. Args:
  28. num_class (int): Num classes. Default: 10.
  29. Returns:
  30. Tensor, output tensor
  31. Examples:
  32. >>> LeNet(num_class=10)
  33. """
  34. def __init__(self, num_class=10):
  35. super(LeNet5, self).__init__()
  36. self.num_class = num_class
  37. self.conv1 = nn.Conv2dBnAct(1, 6, kernel_size=5, batchnorm=True, activation='relu6', pad_mode="valid")
  38. self.conv2 = nn.Conv2dBnAct(6, 16, kernel_size=5, activation='relu', pad_mode="valid")
  39. self.fc1 = nn.DenseBnAct(16 * 5 * 5, 120, activation='relu')
  40. self.fc2 = nn.DenseBnAct(120, 84, activation='relu')
  41. self.fc3 = nn.DenseBnAct(84, self.num_class)
  42. self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
  43. self.flatten = nn.Flatten()
  44. def construct(self, x):
  45. x = self.conv1(x)
  46. x = self.max_pool2d(x)
  47. x = self.conv2(x)
  48. x = self.max_pool2d(x)
  49. x = self.flatten(x)
  50. x = self.fc1(x)
  51. x = self.fc2(x)
  52. x = self.fc3(x)
  53. return x
  54. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  55. def test_qat_lenet():
  56. img = Tensor(np.ones((32, 1, 32, 32)).astype(np.float32))
  57. net = LeNet5()
  58. net = qat.convert_quant_network(
  59. net, quant_delay=0, bn_fold=False, freeze_bn=10000, num_bits=8)
  60. # should load the checkpoint. mock here
  61. for param in net.get_parameters():
  62. param.init_data()
  63. qat.export_geir(net, img, file_name="quant.pb")
  64. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  65. def test_qat_mobile():
  66. net = MobileNetV2()
  67. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  68. net = qat.convert_quant_network(
  69. net, quant_delay=0, bn_fold=True, freeze_bn=10000, num_bits=8)
  70. # should load the checkpoint. mock here
  71. for param in net.get_parameters():
  72. param.init_data()
  73. qat.export_geir(net, img, file_name="quant.pb")