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 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 sys
  15. import numpy as np
  16. import pytest
  17. import ncnn
  18. def test_mat_dims1():
  19. mat = ncnn.Mat(1)
  20. assert mat.dims == 1 and mat.w == 1
  21. mat = ncnn.Mat(2, elemsize=4)
  22. assert mat.dims == 1 and mat.w == 2 and mat.elemsize == 4
  23. mat = ncnn.Mat(3, elemsize=4, elempack=1)
  24. assert mat.dims == 1 and mat.w == 3 and mat.elemsize == 4 and mat.elempack == 1
  25. mat = ncnn.Mat(4, elemsize=4, elempack=1, allocator=None)
  26. assert (
  27. mat.dims == 1
  28. and mat.w == 4
  29. and mat.elemsize == 4
  30. and mat.elempack == 1
  31. and mat.allocator == None
  32. )
  33. mat = ncnn.Mat((1,))
  34. assert mat.dims == 1 and mat.w == 1
  35. mat = ncnn.Mat((2,), elemsize=4)
  36. assert mat.dims == 1 and mat.w == 2 and mat.elemsize == 4
  37. mat = ncnn.Mat((3,), elemsize=4, elempack=1)
  38. assert mat.dims == 1 and mat.w == 3 and mat.elemsize == 4 and mat.elempack == 1
  39. mat = ncnn.Mat((4,), elemsize=4, elempack=1, allocator=None)
  40. assert (
  41. mat.dims == 1
  42. and mat.w == 4
  43. and mat.elemsize == 4
  44. and mat.elempack == 1
  45. and mat.allocator == None
  46. )
  47. def test_mat_dims2():
  48. mat = ncnn.Mat(1, 2)
  49. assert mat.dims == 2 and mat.w == 1 and mat.h == 2
  50. mat = ncnn.Mat(3, 4, elemsize=4)
  51. assert mat.dims == 2 and mat.w == 3 and mat.h == 4 and mat.elemsize == 4
  52. mat = ncnn.Mat(5, 6, elemsize=4, elempack=1)
  53. assert (
  54. mat.dims == 2
  55. and mat.w == 5
  56. and mat.h == 6
  57. and mat.elemsize == 4
  58. and mat.elempack == 1
  59. )
  60. mat = ncnn.Mat(7, 8, elemsize=4, elempack=1, allocator=None)
  61. assert (
  62. mat.dims == 2
  63. and mat.w == 7
  64. and mat.h == 8
  65. and mat.elemsize == 4
  66. and mat.elempack == 1
  67. and mat.allocator == None
  68. )
  69. mat = ncnn.Mat((1, 2))
  70. assert mat.dims == 2 and mat.w == 1 and mat.h == 2
  71. mat = ncnn.Mat((3, 4), elemsize=4)
  72. assert mat.dims == 2 and mat.w == 3 and mat.h == 4 and mat.elemsize == 4
  73. mat = ncnn.Mat((5, 6), elemsize=4, elempack=1)
  74. assert (
  75. mat.dims == 2
  76. and mat.w == 5
  77. and mat.h == 6
  78. and mat.elemsize == 4
  79. and mat.elempack == 1
  80. )
  81. mat = ncnn.Mat((7, 8), elemsize=4, elempack=1, allocator=None)
  82. assert (
  83. mat.dims == 2
  84. and mat.w == 7
  85. and mat.h == 8
  86. and mat.elemsize == 4
  87. and mat.elempack == 1
  88. and mat.allocator == None
  89. )
  90. def test_mat_dims3():
  91. mat = ncnn.Mat(1, 2, 3)
  92. assert mat.dims == 3 and mat.w == 1 and mat.h == 2 and mat.c == 3
  93. mat = ncnn.Mat(4, 5, 6, elemsize=4)
  94. assert (
  95. mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6 and mat.elemsize == 4
  96. )
  97. mat = ncnn.Mat(7, 8, 9, elemsize=4, elempack=1)
  98. assert (
  99. mat.dims == 3
  100. and mat.w == 7
  101. and mat.h == 8
  102. and mat.c == 9
  103. and mat.elemsize == 4
  104. and mat.elempack == 1
  105. )
  106. mat = ncnn.Mat(10, 11, 12, elemsize=4, elempack=1, allocator=None)
  107. assert (
  108. mat.dims == 3
  109. and mat.w == 10
  110. and mat.h == 11
  111. and mat.c == 12
  112. and mat.elemsize == 4
  113. and mat.elempack == 1
  114. and mat.allocator == None
  115. )
  116. mat = ncnn.Mat((1, 2, 3))
  117. assert mat.dims == 3 and mat.w == 1 and mat.h == 2 and mat.c == 3
  118. mat = ncnn.Mat((4, 5, 6), elemsize=4)
  119. assert (
  120. mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6 and mat.elemsize == 4
  121. )
  122. mat = ncnn.Mat((7, 8, 9), elemsize=4, elempack=1)
  123. assert (
  124. mat.dims == 3
  125. and mat.w == 7
  126. and mat.h == 8
  127. and mat.c == 9
  128. and mat.elemsize == 4
  129. and mat.elempack == 1
  130. )
  131. mat = ncnn.Mat((10, 11, 12), elemsize=4, elempack=1, allocator=None)
  132. assert (
  133. mat.dims == 3
  134. and mat.w == 10
  135. and mat.h == 11
  136. and mat.c == 12
  137. and mat.elemsize == 4
  138. and mat.elempack == 1
  139. and mat.allocator == None
  140. )
  141. def test_mat_dims4():
  142. mat = ncnn.Mat(1, 2, 3, 4)
  143. assert mat.dims == 4 and mat.w == 1 and mat.h == 2 and mat.d == 3 and mat.c == 4
  144. mat = ncnn.Mat(4, 5, 6, 7, elemsize=4)
  145. assert (
  146. mat.dims == 4 and mat.w == 4 and mat.h == 5 and mat.d == 6 and mat.c == 7 and mat.elemsize == 4
  147. )
  148. mat = ncnn.Mat(7, 8, 9, 10, elemsize=4, elempack=1)
  149. assert (
  150. mat.dims == 4
  151. and mat.w == 7
  152. and mat.h == 8
  153. and mat.d == 9
  154. and mat.c == 10
  155. and mat.elemsize == 4
  156. and mat.elempack == 1
  157. )
  158. mat = ncnn.Mat(10, 11, 12, 13, elemsize=4, elempack=1, allocator=None)
  159. assert (
  160. mat.dims == 4
  161. and mat.w == 10
  162. and mat.h == 11
  163. and mat.d == 12
  164. and mat.c == 13
  165. and mat.elemsize == 4
  166. and mat.elempack == 1
  167. and mat.allocator == None
  168. )
  169. mat = ncnn.Mat((1, 2, 3, 4))
  170. assert mat.dims == 4 and mat.w == 1 and mat.h == 2 and mat.d == 3 and mat.c == 4
  171. mat = ncnn.Mat((4, 5, 6, 7), elemsize=4)
  172. assert (
  173. mat.dims == 4 and mat.w == 4 and mat.h == 5 and mat.d == 6 and mat.c == 7 and mat.elemsize == 4
  174. )
  175. mat = ncnn.Mat((7, 8, 9, 10), elemsize=4, elempack=1)
  176. assert (
  177. mat.dims == 4
  178. and mat.w == 7
  179. and mat.h == 8
  180. and mat.d == 9
  181. and mat.c == 10
  182. and mat.elemsize == 4
  183. and mat.elempack == 1
  184. )
  185. mat = ncnn.Mat((10, 11, 12, 13), elemsize=4, elempack=1, allocator=None)
  186. assert (
  187. mat.dims == 4
  188. and mat.w == 10
  189. and mat.h == 11
  190. and mat.d == 12
  191. and mat.c == 13
  192. and mat.elemsize == 4
  193. and mat.elempack == 1
  194. and mat.allocator == None
  195. )
  196. def test_numpy():
  197. mat = ncnn.Mat(1)
  198. array = np.array(mat)
  199. assert mat.dims == array.ndim and mat.w == array.shape[0]
  200. mat = ncnn.Mat(2, 3)
  201. array = np.array(mat)
  202. assert (
  203. mat.dims == array.ndim and mat.w == array.shape[1] and mat.h == array.shape[0]
  204. )
  205. mat = ncnn.Mat(4, 5, 6)
  206. array = np.array(mat)
  207. assert (
  208. mat.dims == array.ndim
  209. and mat.w == array.shape[2]
  210. and mat.h == array.shape[1]
  211. and mat.c == array.shape[0]
  212. )
  213. mat = ncnn.Mat(7, 8, 9, 10)
  214. array = np.array(mat)
  215. assert (
  216. mat.dims == array.ndim
  217. and mat.w == array.shape[3]
  218. and mat.h == array.shape[2]
  219. and mat.d == array.shape[1]
  220. and mat.c == array.shape[0]
  221. )
  222. mat = ncnn.Mat(1, elemsize=1)
  223. array = np.array(mat)
  224. assert array.dtype == np.int8
  225. mat = ncnn.Mat(1, elemsize=2)
  226. array = np.array(mat)
  227. assert array.dtype == np.float16
  228. # pybind11 def_buffer throw bug
  229. # with pytest.raises(RuntimeError) as execinfo:
  230. # mat = ncnn.Mat(1, elemsize=3)
  231. # array = np.array(mat)
  232. # assert "convert ncnn.Mat to numpy.ndarray only elemsize 1, 2, 4 support now, but given 3" in str(
  233. # execinfo.value
  234. # )
  235. assert array.dtype == np.float16
  236. mat = ncnn.Mat(1, elemsize=4)
  237. array = np.array(mat)
  238. assert array.dtype == np.float32
  239. mat = np.random.randint(0, 128, size=(12,)).astype(np.uint8)
  240. array = np.array(mat)
  241. assert (mat == array).all()
  242. mat = np.random.rand(12).astype(np.float32)
  243. array = np.array(mat)
  244. assert (mat == array).all()
  245. mat = np.random.randint(0, 128, size=(12, 11)).astype(np.uint8)
  246. array = np.array(mat)
  247. assert (mat == array).all()
  248. mat = np.random.rand(12, 11).astype(np.float32)
  249. array = np.array(mat)
  250. assert (mat == array).all()
  251. mat = np.random.randint(0, 256, size=(12, 11, 3)).astype(np.uint8)
  252. array = np.array(mat)
  253. assert (mat == array).all()
  254. mat = np.random.rand(12, 11, 3).astype(np.float32)
  255. array = np.array(mat)
  256. assert (mat == array).all()
  257. mat = np.random.randint(0, 256, size=(12, 11, 7, 3)).astype(np.uint8)
  258. array = np.array(mat)
  259. assert (mat == array).all()
  260. mat = np.random.rand(12, 11, 7, 3).astype(np.float32)
  261. array = np.array(mat)
  262. assert (mat == array).all()
  263. def test_fill():
  264. mat = ncnn.Mat(1)
  265. mat.fill(1.0)
  266. array = np.array(mat)
  267. assert np.abs(array[0] - 1.0) < sys.float_info.min
  268. def test_clone():
  269. mat1 = ncnn.Mat(1)
  270. mat2 = mat1.clone()
  271. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  272. mat1 = ncnn.Mat(2, 3)
  273. mat2 = mat1.clone()
  274. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  275. mat1 = ncnn.Mat(4, 5, 6)
  276. mat2 = mat1.clone()
  277. assert (
  278. mat1.dims == mat2.dims
  279. and mat1.w == mat2.w
  280. and mat1.h == mat2.h
  281. and mat1.c == mat2.c
  282. )
  283. mat1 = ncnn.Mat(7, 8, 9, 10)
  284. mat2 = mat1.clone()
  285. assert (
  286. mat1.dims == mat2.dims
  287. and mat1.w == mat2.w
  288. and mat1.h == mat2.h
  289. and mat1.d == mat2.d
  290. and mat1.c == mat2.c
  291. )
  292. mat1 = ncnn.Mat((1,))
  293. mat2 = mat1.clone()
  294. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  295. mat1 = ncnn.Mat((2, 3))
  296. mat2 = mat1.clone()
  297. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  298. mat1 = ncnn.Mat((4, 5, 6))
  299. mat2 = mat1.clone()
  300. assert (
  301. mat1.dims == mat2.dims
  302. and mat1.w == mat2.w
  303. and mat1.h == mat2.h
  304. and mat1.c == mat2.c
  305. )
  306. mat1 = ncnn.Mat((7, 8, 9, 10))
  307. mat2 = mat1.clone()
  308. assert (
  309. mat1.dims == mat2.dims
  310. and mat1.w == mat2.w
  311. and mat1.h == mat2.h
  312. and mat1.d == mat2.d
  313. and mat1.c == mat2.c
  314. )
  315. def test_clone_from():
  316. mat2 = ncnn.Mat()
  317. mat1 = ncnn.Mat(1)
  318. mat2.clone_from(mat1)
  319. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  320. mat1 = ncnn.Mat(2, 3)
  321. mat2.clone_from(mat1)
  322. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  323. mat1 = ncnn.Mat(4, 5, 6)
  324. mat2.clone_from(mat1)
  325. assert (
  326. mat1.dims == mat2.dims
  327. and mat1.w == mat2.w
  328. and mat1.h == mat2.h
  329. and mat1.c == mat2.c
  330. )
  331. mat1 = ncnn.Mat(7, 8, 9, 10)
  332. mat2.clone_from(mat1)
  333. assert (
  334. mat1.dims == mat2.dims
  335. and mat1.w == mat2.w
  336. and mat1.h == mat2.h
  337. and mat1.d == mat2.d
  338. and mat1.c == mat2.c
  339. )
  340. mat1 = ncnn.Mat((1,))
  341. mat2.clone_from(mat1)
  342. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  343. mat1 = ncnn.Mat((2, 3))
  344. mat2.clone_from(mat1)
  345. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  346. mat1 = ncnn.Mat((4, 5, 6))
  347. mat2.clone_from(mat1)
  348. assert (
  349. mat1.dims == mat2.dims
  350. and mat1.w == mat2.w
  351. and mat1.h == mat2.h
  352. and mat1.c == mat2.c
  353. )
  354. mat1 = ncnn.Mat((7, 8, 9, 10))
  355. mat2.clone_from(mat1)
  356. assert (
  357. mat1.dims == mat2.dims
  358. and mat1.w == mat2.w
  359. and mat1.h == mat2.h
  360. and mat1.d == mat2.d
  361. and mat1.c == mat2.c
  362. )
  363. def test_reshape():
  364. mat1 = ncnn.Mat()
  365. mat2 = mat1.reshape(1)
  366. assert mat2.dims == 0
  367. mat2 = mat1.reshape(1, 1)
  368. assert mat2.dims == 0
  369. mat2 = mat1.reshape(1, 1, 1)
  370. assert mat2.dims == 0
  371. mat2 = mat1.reshape(1, 1, 1, 1)
  372. assert mat2.dims == 0
  373. mat1 = ncnn.Mat(1)
  374. mat2 = mat1.reshape(1, 1)
  375. assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
  376. mat2 = mat1.reshape(1, 1, 1)
  377. assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
  378. mat2 = mat1.reshape(1, 1, 1, 1)
  379. assert mat2.dims == 4 and mat2.w == 1 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
  380. mat1 = ncnn.Mat(1, 2)
  381. mat2 = mat1.reshape(2)
  382. assert mat2.dims == 1 and mat2.w == 2
  383. mat2 = mat1.reshape(2, 1)
  384. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
  385. mat2 = mat1.reshape(2, 1, 1)
  386. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
  387. mat2 = mat1.reshape(2, 1, 1, 1)
  388. assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
  389. mat1 = ncnn.Mat(1, 2, 3)
  390. mat2 = mat1.reshape(6)
  391. assert mat2.dims == 1 and mat2.w == 6
  392. mat2 = mat1.reshape(2, 3)
  393. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3
  394. mat2 = mat1.reshape(2, 3, 1)
  395. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
  396. mat2 = mat1.reshape(2, 1, 3, 1)
  397. assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 3 and mat2.c == 1
  398. mat1 = ncnn.Mat((1,))
  399. mat2 = mat1.reshape((1, 1))
  400. assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
  401. mat2 = mat1.reshape((1, 1, 1))
  402. assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
  403. mat2 = mat1.reshape((1, 1, 1, 1))
  404. assert mat2.dims == 4 and mat2.w == 1 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
  405. mat1 = ncnn.Mat((1, 2))
  406. mat2 = mat1.reshape((2,))
  407. assert mat2.dims == 1 and mat2.w == 2
  408. mat2 = mat1.reshape((2, 1))
  409. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
  410. mat2 = mat1.reshape((2, 1, 1))
  411. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
  412. mat2 = mat1.reshape((2, 1, 1, 1))
  413. assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
  414. mat1 = ncnn.Mat((1, 2, 3))
  415. mat2 = mat1.reshape((6,))
  416. assert mat2.dims == 1 and mat2.w == 6
  417. mat2 = mat1.reshape((2, 3))
  418. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
  419. mat2 = mat1.reshape((2, 3, 1))
  420. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
  421. mat2 = mat1.reshape((2, 1, 3, 1))
  422. assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 3 and mat2.c == 1
  423. with pytest.raises(RuntimeError) as execinfo:
  424. mat1.reshape((1, 1, 1, 1, 1))
  425. assert "shape must be 1, 2, 3 or 4 dims, not 5" in str(execinfo.value)
  426. def test_create():
  427. mat = ncnn.Mat()
  428. mat.create(1)
  429. assert mat.dims == 1 and mat.w == 1
  430. mat.create(2, 3)
  431. assert mat.dims == 2 and mat.w == 2 and mat.h == 3
  432. mat.create(4, 5, 6)
  433. assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
  434. mat.create(7, 8, 9, 10)
  435. assert mat.dims == 4 and mat.w == 7 and mat.h == 8 and mat.d == 9 and mat.c == 10
  436. mat.create((1,))
  437. assert mat.dims == 1 and mat.w == 1
  438. mat.create((2, 3))
  439. assert mat.dims == 2 and mat.w == 2 and mat.h == 3
  440. mat.create((4, 5, 6))
  441. assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
  442. mat.create((7, 8, 9, 10))
  443. assert mat.dims == 4 and mat.w == 7 and mat.h == 8 and mat.d == 9 and mat.c == 10
  444. def test_create_like():
  445. mat2 = ncnn.Mat()
  446. mat1 = ncnn.Mat(1)
  447. mat2.create_like(mat1)
  448. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  449. mat1 = ncnn.Mat(2, 3)
  450. mat2.create_like(mat1)
  451. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  452. mat1 = ncnn.Mat(4, 5, 6)
  453. mat2.create_like(mat1)
  454. assert (
  455. mat1.dims == mat2.dims
  456. and mat1.w == mat2.w
  457. and mat1.h == mat2.h
  458. and mat1.c == mat2.c
  459. )
  460. mat1 = ncnn.Mat(7, 8, 9, 10)
  461. mat2.create_like(mat1)
  462. assert (
  463. mat1.dims == mat2.dims
  464. and mat1.w == mat2.w
  465. and mat1.h == mat2.h
  466. and mat1.d == mat2.d
  467. and mat1.c == mat2.c
  468. )
  469. def test_addref_release():
  470. mat = ncnn.Mat(1)
  471. assert mat.refcount == 1
  472. mat.addref()
  473. assert mat.refcount == 2
  474. mat.release()
  475. assert mat.refcount == None
  476. def test_empty():
  477. mat = ncnn.Mat()
  478. assert mat.empty() == True
  479. mat = ncnn.Mat(1)
  480. assert mat.empty() == False
  481. def test_total():
  482. mat = ncnn.Mat(1)
  483. assert mat.total() == 1
  484. mat = ncnn.Mat(2, 3)
  485. assert mat.total() == 2 * 3
  486. mat = ncnn.Mat(4, 5, 6)
  487. assert mat.total() == 4 * 5 * 6
  488. mat = ncnn.Mat(7, 8, 9, 10)
  489. assert mat.total() == 7 * 8 * 9 * 10
  490. def test_elembits():
  491. mat = ncnn.Mat(1, elemsize=1, elempack=1)
  492. assert mat.elembits() == 8
  493. mat = ncnn.Mat(2, elemsize=2, elempack=1)
  494. assert mat.elembits() == 16
  495. mat = ncnn.Mat(3, elemsize=4, elempack=1)
  496. assert mat.elembits() == 32
  497. def test_shape():
  498. mat = ncnn.Mat(1)
  499. shape = mat.shape()
  500. assert shape.dims == 1 and shape.w == 1
  501. mat = ncnn.Mat(2, 3)
  502. shape = mat.shape()
  503. assert shape.dims == 2 and shape.w == 2 and shape.h == 3
  504. mat = ncnn.Mat(4, 5, 6)
  505. shape = mat.shape()
  506. assert shape.dims == 3 and shape.w == 4 and shape.h == 5 and shape.c == 6
  507. mat = ncnn.Mat(7, 8, 9, 10)
  508. shape = mat.shape()
  509. assert shape.dims == 4 and shape.w == 7 and shape.h == 8 and shape.d == 9 and shape.c == 10
  510. def test_channel_depth_row():
  511. mat = ncnn.Mat(2, 3, 4, 5)
  512. mat.fill(6.0)
  513. channel = mat.channel(1)
  514. assert channel.dims == 3 and channel.w == 2 and channel.h == 3 and channel.c == 4
  515. depth = channel.depth(1)
  516. assert depth.dims == 2 and depth.w == 2 and depth.h == 3
  517. row = depth.row(1)
  518. assert len(row) == 2 and np.abs(row[0] - 6.0) < sys.float_info.min
  519. def test_channel_row():
  520. mat = ncnn.Mat(2, 3, 4)
  521. mat.fill(4.0)
  522. channel = mat.channel(1)
  523. assert channel.dims == 2 and channel.w == 2 and channel.h == 3 and channel.c == 1
  524. row = channel.row(1)
  525. assert len(row) == 2 and np.abs(row[0] - 4.0) < sys.float_info.min
  526. def test_channel_range():
  527. mat = ncnn.Mat(1, 2, 3)
  528. channel_range = mat.channel_range(0, 2)
  529. assert (
  530. channel_range.dims == 3
  531. and channel_range.w == 1
  532. and channel_range.h == 2
  533. and channel_range.c == 2
  534. )
  535. def test_depth_range():
  536. mat = ncnn.Mat(1, 2, 3, 4)
  537. depth_range = mat.channel(1).depth_range(1, 2)
  538. assert (
  539. depth_range.dims == 3
  540. and depth_range.w == 1
  541. and depth_range.h == 2
  542. and depth_range.c == 2
  543. )
  544. def test_row_range():
  545. mat = ncnn.Mat(1, 2)
  546. row_range = mat.row_range(0, 2)
  547. assert row_range.dims == 2 and row_range.w == 1 and row_range.h == 2
  548. def test_range():
  549. mat = ncnn.Mat(2)
  550. range = mat.range(0, 2)
  551. assert range.dims == 1 and range.w == 2
  552. def test_getitem_setitem():
  553. mat = ncnn.Mat(2)
  554. mat.fill(1)
  555. assert (
  556. np.abs(mat[0] - 1.0) < sys.float_info.min
  557. and np.abs(mat[1] - 1.0) < sys.float_info.min
  558. )
  559. mat[0] = 2.0
  560. assert (
  561. np.abs(mat[0] - 2.0) < sys.float_info.min
  562. and np.abs(mat[1] - 1.0) < sys.float_info.min
  563. )
  564. def test_from_pixels():
  565. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  566. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  567. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  568. assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
  569. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  570. assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]
  571. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  572. mat = ncnn.Mat.from_pixels(
  573. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, stride=500 * 3
  574. ) # chw
  575. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  576. assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
  577. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  578. assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]
  579. def test_from_pixels_resize():
  580. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  581. mat = ncnn.Mat.from_pixels_resize(
  582. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 200, 150
  583. ) # chw
  584. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  585. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  586. mat = ncnn.Mat.from_pixels_resize(
  587. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 400, 300
  588. ) # chw
  589. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  590. assert pixels[0, 0, 0] == mat.channel(2).row(0)[0]
  591. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  592. assert pixels[299, 399, 2] == mat.channel(0).row(299)[399]
  593. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  594. mat = ncnn.Mat.from_pixels_resize(
  595. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 200, 150
  596. ) # chw
  597. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  598. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  599. mat = ncnn.Mat.from_pixels_resize(
  600. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 400, 300
  601. ) # chw
  602. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  603. assert pixels[0, 0, 0] == mat.channel(2).row(0)[0]
  604. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  605. assert pixels[299, 399, 2] == mat.channel(0).row(299)[399]
  606. def test_from_pixels_roi():
  607. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  608. mat = ncnn.Mat.from_pixels_roi(
  609. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150
  610. ) # chw
  611. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  612. assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
  613. assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
  614. assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]
  615. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  616. mat = ncnn.Mat.from_pixels_roi(
  617. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 500 * 3, 100, 75, 200, 150
  618. ) # chw
  619. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  620. assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
  621. assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
  622. assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]
  623. def test_from_pixels_roi_resize():
  624. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  625. mat = ncnn.Mat.from_pixels_roi_resize(
  626. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150, 100, 75
  627. ) # chw
  628. assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3
  629. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  630. mat = ncnn.Mat.from_pixels_roi_resize(
  631. pixels,
  632. ncnn.Mat.PixelType.PIXEL_RGB,
  633. 400,
  634. 300,
  635. 500 * 3,
  636. 100,
  637. 75,
  638. 200,
  639. 150,
  640. 100,
  641. 75,
  642. ) # chw
  643. assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3
  644. def test_substract_mean_normalize():
  645. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  646. mean_vals = [127.5, 127.5, 127.5]
  647. norm_vals = [0.007843, 0.007843, 0.007843]
  648. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  649. mat.substract_mean_normalize([], norm_vals)
  650. assert np.abs(pixels[0, 0, 0] * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5
  651. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  652. mat.substract_mean_normalize(mean_vals, [])
  653. assert np.abs((pixels[0, 0, 0] - 127.5) - mat.channel(0).row(0)[0]) < 1e-5
  654. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  655. mat.substract_mean_normalize(mean_vals, norm_vals)
  656. assert (
  657. np.abs((pixels[0, 0, 0] - 127.5) * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5
  658. )