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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. """ut for batchnorm layer"""
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Parameter
  20. from mindspore.common.api import _executor
  21. def test_bn_pars_valid1():
  22. """ut of BatchNorm parameters' validation"""
  23. with pytest.raises(ValueError):
  24. nn.BatchNorm2d(num_features=0)
  25. def test_bn_pars_valid2():
  26. """ut of BatchNorm parameters' validation"""
  27. with pytest.raises(ValueError):
  28. nn.BatchNorm2d(num_features=3, momentum=-0.1)
  29. def test_bn_init():
  30. """ut of BatchNorm parameters' validation"""
  31. bn = nn.BatchNorm2d(num_features=3)
  32. assert isinstance(bn.gamma, Parameter)
  33. assert isinstance(bn.beta, Parameter)
  34. assert isinstance(bn.moving_mean, Parameter)
  35. assert isinstance(bn.moving_variance, Parameter)
  36. class Net(nn.Cell):
  37. def __init__(self):
  38. super(Net, self).__init__()
  39. self.bn = nn.BatchNorm2d(num_features=3)
  40. def construct(self, input_x):
  41. return self.bn(input_x)
  42. def test_compile():
  43. net = Net()
  44. input_data = Tensor(np.random.randint(0, 255, [1, 3, 224, 224]).astype(np.float32))
  45. _executor.compile(net, input_data)
  46. class GroupNet(nn.Cell):
  47. def __init__(self):
  48. super(GroupNet, self).__init__()
  49. self.group_bn = nn.GroupNorm()
  50. def construct(self, x):
  51. return self.group_bn(x)
  52. def test_compile_groupnorm():
  53. net = nn.GroupNorm(16, 64)
  54. input_data = Tensor(np.random.rand(1, 64, 256, 256).astype(np.float32))
  55. _executor.compile(net, input_data)