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_Conv2dBnFoldQuant.py 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2021 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. """
  16. train Conv2dBnFoldQuant Cell
  17. """
  18. import pytest
  19. import numpy as np
  20. from mindspore import nn
  21. from mindspore import context
  22. from mindspore import Tensor
  23. from mindspore.common import set_seed
  24. from mindspore.compression.quant import create_quant_config
  25. class Net(nn.Cell):
  26. def __init__(self, qconfig):
  27. super(Net, self).__init__()
  28. self.conv = nn.Conv2dBnFoldQuant(2, 3, kernel_size=(2, 2), stride=(1, 1),
  29. pad_mode='valid', quant_config=qconfig)
  30. def construct(self, x):
  31. return self.conv(x)
  32. def test_conv2d_bn_fold_quant():
  33. set_seed(1)
  34. quant_config = create_quant_config()
  35. network = Net(quant_config)
  36. inputs = Tensor(np.ones([1, 2, 5, 5]).astype(np.float32))
  37. label = Tensor(np.ones([1, 3, 4, 4]).astype(np.int32))
  38. opt = nn.Momentum(filter(lambda x: x.requires_grad, network.get_parameters()), learning_rate=0.1, momentum=0.9)
  39. loss = nn.MSELoss()
  40. net_with_loss = nn.WithLossCell(network, loss)
  41. train_network = nn.TrainOneStepCell(net_with_loss, opt)
  42. train_network.set_train()
  43. out_loss = train_network(inputs, label)
  44. expect_loss = np.array([0.940427])
  45. error = np.array([0.1])
  46. diff = out_loss.asnumpy() - expect_loss
  47. assert np.all(abs(diff) < error)
  48. @pytest.mark.level0
  49. @pytest.mark.platform_arm_ascend_training
  50. @pytest.mark.platform_x86_ascend_training
  51. @pytest.mark.env_onecard
  52. def test_conv2d_bn_fold_quant_ascend():
  53. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  54. test_conv2d_bn_fold_quant()