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

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