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_mat.py 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # Tencent is pleased to support the open source community by making ncnn available.
  2. #
  3. # Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. #
  5. # Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. # in compliance with the License. You may obtain a copy of the License at
  7. #
  8. # https://opensource.org/licenses/BSD-3-Clause
  9. #
  10. # Unless required by applicable law or agreed to in writing, software distributed
  11. # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. # CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. # specific language governing permissions and limitations under the License.
  14. import numpy as np
  15. import pytest
  16. import ncnn
  17. def test_mat_dims1():
  18. mat = ncnn.Mat(1)
  19. assert mat.dims == 1 and mat.w == 1
  20. mat = ncnn.Mat(2, elemsize=4)
  21. assert mat.dims == 1 and mat.w == 2 and mat.elemsize == 4
  22. mat = ncnn.Mat(3, elemsize=4, elempack=1)
  23. assert mat.dims == 1 and mat.w == 3 and mat.elemsize == 4 and mat.elempack == 1
  24. mat = ncnn.Mat(4, elemsize=4, elempack=1, allocator=None)
  25. assert (
  26. mat.dims == 1
  27. and mat.w == 4
  28. and mat.elemsize == 4
  29. and mat.elempack == 1
  30. and mat.allocator == None
  31. )
  32. mat = ncnn.Mat((1,))
  33. assert mat.dims == 1 and mat.w == 1
  34. mat = ncnn.Mat((2,), elemsize=4)
  35. assert mat.dims == 1 and mat.w == 2 and mat.elemsize == 4
  36. mat = ncnn.Mat((3,), elemsize=4, elempack=1)
  37. assert mat.dims == 1 and mat.w == 3 and mat.elemsize == 4 and mat.elempack == 1
  38. mat = ncnn.Mat((4,), elemsize=4, elempack=1, allocator=None)
  39. assert (
  40. mat.dims == 1
  41. and mat.w == 4
  42. and mat.elemsize == 4
  43. and mat.elempack == 1
  44. and mat.allocator == None
  45. )
  46. def test_mat_dims2():
  47. mat = ncnn.Mat(1, 2)
  48. assert mat.dims == 2 and mat.w == 1 and mat.h == 2
  49. mat = ncnn.Mat(3, 4, elemsize=4)
  50. assert mat.dims == 2 and mat.w == 3 and mat.h == 4 and mat.elemsize == 4
  51. mat = ncnn.Mat(5, 6, elemsize=4, elempack=1)
  52. assert (
  53. mat.dims == 2
  54. and mat.w == 5
  55. and mat.h == 6
  56. and mat.elemsize == 4
  57. and mat.elempack == 1
  58. )
  59. mat = ncnn.Mat(7, 8, elemsize=4, elempack=1, allocator=None)
  60. assert (
  61. mat.dims == 2
  62. and mat.w == 7
  63. and mat.h == 8
  64. and mat.elemsize == 4
  65. and mat.elempack == 1
  66. and mat.allocator == None
  67. )
  68. mat = ncnn.Mat((1, 2))
  69. assert mat.dims == 2 and mat.w == 1 and mat.h == 2
  70. mat = ncnn.Mat((3, 4), elemsize=4)
  71. assert mat.dims == 2 and mat.w == 3 and mat.h == 4 and mat.elemsize == 4
  72. mat = ncnn.Mat((5, 6), elemsize=4, elempack=1)
  73. assert (
  74. mat.dims == 2
  75. and mat.w == 5
  76. and mat.h == 6
  77. and mat.elemsize == 4
  78. and mat.elempack == 1
  79. )
  80. mat = ncnn.Mat((7, 8), elemsize=4, elempack=1, allocator=None)
  81. assert (
  82. mat.dims == 2
  83. and mat.w == 7
  84. and mat.h == 8
  85. and mat.elemsize == 4
  86. and mat.elempack == 1
  87. and mat.allocator == None
  88. )
  89. def test_mat_dims3():
  90. mat = ncnn.Mat(1, 2, 3)
  91. assert mat.dims == 3 and mat.w == 1 and mat.h == 2 and mat.c == 3
  92. mat = ncnn.Mat(4, 5, 6, elemsize=4)
  93. assert (
  94. mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6 and mat.elemsize == 4
  95. )
  96. mat = ncnn.Mat(7, 8, 9, elemsize=4, elempack=1)
  97. assert (
  98. mat.dims == 3
  99. and mat.w == 7
  100. and mat.h == 8
  101. and mat.c == 9
  102. and mat.elemsize == 4
  103. and mat.elempack == 1
  104. )
  105. mat = ncnn.Mat(10, 11, 12, elemsize=4, elempack=1, allocator=None)
  106. assert (
  107. mat.dims == 3
  108. and mat.w == 10
  109. and mat.h == 11
  110. and mat.c == 12
  111. and mat.elemsize == 4
  112. and mat.elempack == 1
  113. and mat.allocator == None
  114. )
  115. mat = ncnn.Mat((1, 2, 3))
  116. assert mat.dims == 3 and mat.w == 1 and mat.h == 2 and mat.c == 3
  117. mat = ncnn.Mat((4, 5, 6), elemsize=4)
  118. assert (
  119. mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6 and mat.elemsize == 4
  120. )
  121. mat = ncnn.Mat((7, 8, 9), elemsize=4, elempack=1)
  122. assert (
  123. mat.dims == 3
  124. and mat.w == 7
  125. and mat.h == 8
  126. and mat.c == 9
  127. and mat.elemsize == 4
  128. and mat.elempack == 1
  129. )
  130. mat = ncnn.Mat((10, 11, 12), elemsize=4, elempack=1, allocator=None)
  131. assert (
  132. mat.dims == 3
  133. and mat.w == 10
  134. and mat.h == 11
  135. and mat.c == 12
  136. and mat.elemsize == 4
  137. and mat.elempack == 1
  138. and mat.allocator == None
  139. )
  140. def test_mat2np():
  141. mat = ncnn.Mat(1)
  142. array = np.array(mat)
  143. assert mat.dims == array.ndim and mat.w == array.shape[0]
  144. mat = ncnn.Mat(1, 2)
  145. array = np.array(mat)
  146. assert (
  147. mat.dims == array.ndim and mat.w == array.shape[1] and mat.h == array.shape[0]
  148. )
  149. mat = ncnn.Mat(1, 2, 3)
  150. array = np.array(mat)
  151. assert (
  152. mat.dims == array.ndim
  153. and mat.w == array.shape[2]
  154. and mat.h == array.shape[1]
  155. and mat.c == array.shape[0]
  156. )
  157. mat = ncnn.Mat(1, elemsize=1)
  158. array = np.array(mat)
  159. assert array.dtype == np.int8
  160. mat = ncnn.Mat(1, elemsize=2)
  161. array = np.array(mat)
  162. assert array.dtype == np.float16
  163. mat = ncnn.Mat(1, elemsize=4)
  164. array = np.array(mat)
  165. assert array.dtype == np.float32
  166. def test_create():
  167. mat = ncnn.Mat()
  168. mat.create(1)
  169. assert mat.dims == 1 and mat.w == 1
  170. mat.create(2, 3)
  171. assert mat.dims == 2 and mat.w == 2 and mat.h == 3
  172. mat.create(4, 5, 6)
  173. assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
  174. mat.create(7, 8, 9, elemsize=4)
  175. assert (
  176. mat.dims == 3 and mat.w == 7 and mat.h == 8 and mat.c == 9 and mat.elemsize == 4
  177. )
  178. mat = ncnn.Mat((10, 11, 12), elemsize=4, elempack=1)
  179. assert (
  180. mat.dims == 3
  181. and mat.w == 10
  182. and mat.h == 11
  183. and mat.c == 12
  184. and mat.elemsize == 4
  185. and mat.elempack == 1
  186. )
  187. mat = ncnn.Mat((10, 11, 12), elemsize=4, elempack=1, allocator=None)
  188. assert (
  189. mat.dims == 3
  190. and mat.w == 10
  191. and mat.h == 11
  192. and mat.c == 12
  193. and mat.elemsize == 4
  194. and mat.elempack == 1
  195. and mat.allocator == None
  196. )