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.4 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 model_zoo.official.cv.mobilenetv2_quant.src.mobilenetV2 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, has_bn=True, activation='relu', 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, bn_fold=True, per_channel=[True, False], symmetric=[True, False])
  60. # should load the checkpoint. mock here
  61. net.init_parameters_data()
  62. qat.export(net, img, file_name="quant.pb")
  63. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  64. def test_qat_mobile_per_channel_tf():
  65. network = mobilenetV2(num_classes=1000)
  66. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  67. network = qat.convert_quant_network(network, bn_fold=True, per_channel=[True, False], symmetric=[True, False])
  68. # should load the checkpoint. mock here
  69. network.init_parameters_data()
  70. qat.export(network, img, file_name="quant.pb")
  71. @pytest.mark.skip(reason="no `te.lang.cce` in ut env")
  72. def test_qat_mobile_per_channel_ff():
  73. network = mobilenetV2(num_classes=1000)
  74. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  75. network = qat.convert_quant_network(network, bn_fold=True, per_channel=[False, False], symmetric=[True, False])
  76. # should load the checkpoint. mock here
  77. network.init_parameters_data()
  78. qat.export(network, img, file_name="quant.pb")