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_container.py 4.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 container """
  16. from collections import OrderedDict
  17. import numpy as np
  18. import pytest
  19. import mindspore.nn as nn
  20. from mindspore import Tensor
  21. weight = Tensor(np.ones([2, 2]))
  22. conv2 = nn.Conv2d(3, 64, (3, 3), stride=2, padding=0)
  23. kernel_size = 3
  24. stride = 2
  25. padding = 1
  26. avg_pool = nn.AvgPool2d(kernel_size, stride)
  27. class TestSequentialCell():
  28. """ TestSequentialCell """
  29. def test_SequentialCell_init(self):
  30. m = nn.SequentialCell()
  31. assert type(m).__name__ == 'SequentialCell'
  32. def test_SequentialCell_init2(self):
  33. m = nn.SequentialCell([conv2])
  34. assert len(m) == 1
  35. def test_SequentialCell_init3(self):
  36. m = nn.SequentialCell([conv2, avg_pool])
  37. assert len(m) == 2
  38. def test_SequentialCell_init4(self):
  39. m = nn.SequentialCell(OrderedDict(
  40. [('cov2d', conv2), ('avg_pool', avg_pool)]))
  41. assert len(m) == 2
  42. def test_getitem1(self):
  43. m = nn.SequentialCell(OrderedDict(
  44. [('cov2d', conv2), ('avg_pool', avg_pool)]))
  45. assert m[0] == conv2
  46. def test_getitem2(self):
  47. m = nn.SequentialCell(OrderedDict(
  48. [('cov2d', conv2), ('avg_pool', avg_pool)]))
  49. assert len(m[0:2]) == 2
  50. assert m[:2][1] == avg_pool
  51. def test_setitem1(self):
  52. m = nn.SequentialCell(OrderedDict(
  53. [('cov2d', conv2), ('avg_pool', avg_pool)]))
  54. m[1] = conv2
  55. assert m[1] == m[0]
  56. def test_setitem2(self):
  57. m = nn.SequentialCell(OrderedDict(
  58. [('cov2d', conv2), ('avg_pool', avg_pool)]))
  59. with pytest.raises(TypeError):
  60. m[1.0] = conv2
  61. def test_delitem1(self):
  62. m = nn.SequentialCell(OrderedDict(
  63. [('cov2d', conv2), ('avg_pool', avg_pool)]))
  64. del m[0]
  65. assert len(m) == 1
  66. def test_delitem2(self):
  67. m = nn.SequentialCell(OrderedDict(
  68. [('cov2d', conv2), ('avg_pool', avg_pool)]))
  69. del m[:]
  70. assert type(m).__name__ == 'SequentialCell'
  71. class TestCellList():
  72. """ TestCellList """
  73. def test_init1(self):
  74. cell_list = nn.CellList([conv2, avg_pool])
  75. assert len(cell_list) == 2
  76. def test_init2(self):
  77. with pytest.raises(TypeError):
  78. nn.CellList(["test"])
  79. def test_getitem(self):
  80. cell_list = nn.CellList([conv2, avg_pool])
  81. assert cell_list[0] == conv2
  82. temp_cells = cell_list[:]
  83. assert temp_cells[1] == avg_pool
  84. def test_setitem(self):
  85. cell_list = nn.CellList([conv2, avg_pool])
  86. cell_list[0] = avg_pool
  87. assert cell_list[0] == cell_list[1]
  88. def test_delitem(self):
  89. cell_list = nn.CellList([conv2, avg_pool])
  90. del cell_list[0]
  91. assert len(cell_list) == 1
  92. del cell_list[:]
  93. assert type(cell_list).__name__ == 'CellList'
  94. def test_iter(self):
  95. cell_list = nn.CellList([conv2, avg_pool])
  96. for item in cell_list:
  97. cell = item
  98. assert type(cell).__name__ == 'AvgPool2d'
  99. def test_add(self):
  100. cell_list = nn.CellList([conv2, avg_pool])
  101. cell_list += [conv2]
  102. assert len(cell_list) == 3
  103. assert cell_list[0] == cell_list[2]
  104. def test_insert(self):
  105. cell_list = nn.CellList([conv2, avg_pool])
  106. cell_list.insert(0, avg_pool)
  107. assert len(cell_list) == 3
  108. assert cell_list[0] == cell_list[2]
  109. def test_append(self):
  110. cell_list = nn.CellList([conv2, avg_pool])
  111. cell_list.append(conv2)
  112. assert len(cell_list) == 3
  113. assert cell_list[0] == cell_list[2]
  114. def test_extend(self):
  115. cell_list = nn.CellList()
  116. cell_list.extend([conv2, avg_pool])
  117. assert len(cell_list) == 2
  118. assert cell_list[0] == conv2