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_conv.py 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. """ test conv """
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from ..ut_filter import non_graph_engine
  21. weight = Tensor(np.ones([2, 2]))
  22. in_channels = 3
  23. out_channels = 64
  24. class Net(nn.Cell):
  25. """ Net definition """
  26. def __init__(self,
  27. cin,
  28. cout,
  29. kernel_size,
  30. stride=1,
  31. pad_mode="valid",
  32. padding=0,
  33. dilation=1,
  34. group=1,
  35. has_bias=True,
  36. weight_init='normal',
  37. bias_init='zeros'):
  38. super(Net, self).__init__()
  39. self.conv = nn.Conv2d(cin,
  40. cout,
  41. kernel_size,
  42. stride,
  43. pad_mode,
  44. padding,
  45. dilation,
  46. group,
  47. has_bias,
  48. weight_init,
  49. bias_init)
  50. def construct(self, input_x):
  51. return self.conv(input_x)
  52. @non_graph_engine
  53. def test_compile():
  54. net = Net(3, 64, 3, bias_init='zeros')
  55. input_data = Tensor(np.ones([1, 3, 16, 50], np.float32))
  56. net(input_data)
  57. def test_compile_nobias():
  58. net = Net(3, 64, 4, has_bias=False, weight_init='normal')
  59. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  60. net(input_data)
  61. def test_compile_nobias2():
  62. net = Net(3, 64, (3, 5), has_bias=False, weight_init='normal')
  63. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  64. net(input_data)
  65. def test_compile_pad_same():
  66. net = Net(3, 64, (3, 5), pad_mode="same", padding=0, has_bias=False, weight_init='normal')
  67. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  68. net(input_data)
  69. def test_compile_pad_valid():
  70. net = Net(3, 64, (3, 5), pad_mode="valid", padding=0, has_bias=False, weight_init='normal')
  71. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  72. net(input_data)
  73. def test_compile_pad_pad():
  74. net = Net(3, 64, (3, 5), pad_mode="pad", padding=1, has_bias=False, weight_init='normal')
  75. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  76. net(input_data)
  77. def test_conv_group_error():
  78. with pytest.raises(ValueError):
  79. nn.Conv2d(6, 8, 3, group=3)
  80. with pytest.raises(ValueError):
  81. nn.Conv2d(6, 9, 3, group=2)
  82. def test_conv_check():
  83. """ test_conv_check """
  84. with pytest.raises(ValueError):
  85. Net(3, 64, 4, pad_mode='sane')
  86. with pytest.raises(ValueError):
  87. Net(3, 0, 4)
  88. with pytest.raises(ValueError):
  89. Net(3, 1, 4, group=-1)
  90. with pytest.raises(ValueError):
  91. Net(3, 1, 4, dilation=-1)
  92. with pytest.raises(ValueError):
  93. Net(3, 1, kernel_size=-1)
  94. with pytest.raises(ValueError):
  95. Net(3, 1, 4, stride=0)
  96. with pytest.raises(ValueError):
  97. Net(0, 1, 4)
  98. class NetConv2dTranspose(nn.Cell):
  99. def __init__(self,
  100. cin,
  101. cout,
  102. kernel_size,
  103. stride=1,
  104. pad_mode="same",
  105. padding=0,
  106. dilation=1,
  107. group=1,
  108. has_bias=False,
  109. weight_init='normal',
  110. bias_init='zeros'):
  111. super(NetConv2dTranspose, self).__init__()
  112. self.conv = nn.Conv2dTranspose(cin,
  113. cout,
  114. kernel_size,
  115. stride,
  116. pad_mode,
  117. padding,
  118. dilation,
  119. group,
  120. has_bias,
  121. weight_init,
  122. bias_init)
  123. def construct(self, input_x):
  124. return self.conv(input_x)
  125. def test_compile_transpose():
  126. net = NetConv2dTranspose(3, 64, 4, weight_init='normal')
  127. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  128. net(input_data)
  129. def test_compile_transpose_bias():
  130. net = NetConv2dTranspose(3, 64, 4, has_bias=True, weight_init='normal')
  131. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  132. net(input_data)
  133. def test_compile_transpose_valid():
  134. net = NetConv2dTranspose(3, 64, 4, pad_mode='valid', weight_init='normal')
  135. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  136. net(input_data)
  137. def test_compile_transpose_pad():
  138. net = NetConv2dTranspose(3, 64, 4, pad_mode='pad', weight_init='normal')
  139. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  140. net(input_data)
  141. def test_compile_transpose_stride2():
  142. net = NetConv2dTranspose(3, 64, 4, stride=2, weight_init='normal')
  143. input_data = Tensor(np.ones([1, 3, 16, 50], dtype=np.float32))
  144. net(input_data)