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

5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. from mobilenetv2_combined import MobileNetV2
  18. import mindspore.context as context
  19. import mindspore.ops.operations as P
  20. from mindspore import Tensor
  21. from mindspore import nn
  22. from mindspore.nn.layer import combined
  23. from mindspore.train.quant import quant as qat
  24. context.set_context(mode=context.GRAPH_MODE)
  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 = combined.Conv2d(
  39. 1, 6, kernel_size=5, batchnorm=True, activation='relu6')
  40. self.conv2 = combined.Conv2d(6, 16, kernel_size=5, activation='relu')
  41. self.fc1 = combined.Dense(16 * 5 * 5, 120, activation='relu')
  42. self.fc2 = combined.Dense(120, 84, activation='relu')
  43. self.fc3 = combined.Dense(84, self.num_class)
  44. self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
  45. self.flattern = nn.Flatten()
  46. def construct(self, x):
  47. x = self.conv1(x)
  48. x = self.bn(x)
  49. x = self.relu(x)
  50. x = self.max_pool2d(x)
  51. x = self.conv2(x)
  52. x = self.max_pool2d(x)
  53. x = self.flattern(x)
  54. x = self.fc1(x)
  55. x = self.fc2(x)
  56. x = self.fc3(x)
  57. return x
  58. def test_qat_lenet():
  59. net = LeNet5()
  60. net = qat.convert_quant_network(
  61. net, quant_delay=0, bn_fold=False, freeze_bn=10000, weight_bits=8, act_bits=8)
  62. def test_qat_mobile():
  63. net = MobileNetV2()
  64. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  65. net = qat.convert_quant_network(
  66. net, quant_delay=0, bn_fold=False, freeze_bn=10000, weight_bits=8, act_bits=8)
  67. net(img)
  68. def test_qat_mobile_train():
  69. net = MobileNetV2(num_class=10)
  70. img = Tensor(np.ones((1, 3, 224, 224)).astype(np.float32))
  71. label = Tensor(np.ones((1, 10)).astype(np.float32))
  72. net = qat.convert_quant_network(
  73. net, quant_delay=0, bn_fold=False, freeze_bn=10000, weight_bits=8, act_bits=8)
  74. loss = nn.SoftmaxCrossEntropyWithLogits(reduction='mean')
  75. optimizer = nn.Momentum(net.trainable_params(),
  76. learning_rate=0.1, momentum=0.9)
  77. net = nn.WithLossCell(net, loss)
  78. net = nn.TrainOneStepCell(net, optimizer)
  79. net(img, label)