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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 = mat.numpy()
  199. assert mat.dims == array.ndim and mat.w == array.shape[0]
  200. mat = ncnn.Mat(2, 3)
  201. array = mat.numpy()
  202. assert array.dtype == np.float32
  203. assert (
  204. mat.dims == array.ndim and mat.w == array.shape[1] and mat.h == array.shape[0]
  205. )
  206. mat = ncnn.Mat(4, 5, 6)
  207. array = np.array(mat)
  208. assert (
  209. mat.dims == array.ndim
  210. and mat.w == array.shape[2]
  211. and mat.h == array.shape[1]
  212. and mat.c == array.shape[0]
  213. )
  214. mat = ncnn.Mat(7, 8, 9, 10)
  215. array = np.array(mat)
  216. assert (
  217. mat.dims == array.ndim
  218. and mat.w == array.shape[3]
  219. and mat.h == array.shape[2]
  220. and mat.d == array.shape[1]
  221. and mat.c == array.shape[0]
  222. )
  223. mat = ncnn.Mat(1, elemsize=1)
  224. array = mat.numpy()
  225. assert array.dtype == np.int8
  226. mat = ncnn.Mat(1, elemsize=2)
  227. array = mat.numpy()
  228. assert array.dtype == np.float16
  229. # pybind11 def_buffer throw bug
  230. # with pytest.raises(RuntimeError) as execinfo:
  231. # mat = ncnn.Mat(1, elemsize=3)
  232. # array = np.array(mat)
  233. # assert "convert ncnn.Mat to numpy.ndarray only elemsize 1, 2, 4 support now, but given 3" in str(
  234. # execinfo.value
  235. # )
  236. assert array.dtype == np.float16
  237. mat = ncnn.Mat(1, elemsize=4)
  238. array = mat.numpy()
  239. assert array.dtype == np.float32
  240. mat = np.random.randint(0, 128, size=(12,)).astype(np.uint8)
  241. array = np.array(mat)
  242. assert (mat == array).all()
  243. mat = np.random.rand(12).astype(np.float32)
  244. array = np.array(mat)
  245. assert (mat == array).all()
  246. mat = np.random.randint(0, 128, size=(12, 11)).astype(np.uint8)
  247. array = np.array(mat)
  248. assert (mat == array).all()
  249. mat = np.random.rand(12, 11).astype(np.float32)
  250. array = np.array(mat)
  251. assert (mat == array).all()
  252. mat = np.random.randint(0, 256, size=(12, 11, 3)).astype(np.uint8)
  253. array = np.array(mat)
  254. assert (mat == array).all()
  255. mat = np.random.rand(12, 11, 3).astype(np.float32)
  256. array = np.array(mat)
  257. assert (mat == array).all()
  258. mat = np.random.randint(0, 256, size=(12, 11, 7, 3)).astype(np.uint8)
  259. array = np.array(mat)
  260. assert (mat == array).all()
  261. mat = np.random.rand(12, 11, 7, 3).astype(np.float32)
  262. array = np.array(mat)
  263. assert (mat == array).all()
  264. array = np.array([1, 2, 3], dtype=np.int32)
  265. mat = ncnn.Mat(array)
  266. array2 = mat.numpy(format='i')
  267. assert array2.dtype == np.int32
  268. array[0] = 10
  269. assert array2[0] == 10
  270. array = np.array([1, 2, 3], dtype=np.float32)
  271. mat = ncnn.Mat(array)
  272. array2 = mat.numpy(format='f')
  273. assert array2.dtype == np.float32
  274. array2[0] = 100
  275. assert array[0] == 100
  276. def test_fill():
  277. mat = ncnn.Mat(1)
  278. mat.fill(1.0)
  279. array = np.array(mat)
  280. assert np.abs(array[0] - 1.0) < np.finfo(np.float32).eps
  281. def test_clone():
  282. mat1 = ncnn.Mat(1)
  283. mat2 = mat1.clone()
  284. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  285. mat1 = ncnn.Mat(2, 3)
  286. mat2 = mat1.clone()
  287. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  288. mat1 = ncnn.Mat(4, 5, 6)
  289. mat2 = mat1.clone()
  290. assert (
  291. mat1.dims == mat2.dims
  292. and mat1.w == mat2.w
  293. and mat1.h == mat2.h
  294. and mat1.c == mat2.c
  295. )
  296. mat1 = ncnn.Mat(7, 8, 9, 10)
  297. mat2 = mat1.clone()
  298. assert (
  299. mat1.dims == mat2.dims
  300. and mat1.w == mat2.w
  301. and mat1.h == mat2.h
  302. and mat1.d == mat2.d
  303. and mat1.c == mat2.c
  304. )
  305. mat1 = ncnn.Mat((1,))
  306. mat2 = mat1.clone()
  307. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  308. mat1 = ncnn.Mat((2, 3))
  309. mat2 = mat1.clone()
  310. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  311. mat1 = ncnn.Mat((4, 5, 6))
  312. mat2 = mat1.clone()
  313. assert (
  314. mat1.dims == mat2.dims
  315. and mat1.w == mat2.w
  316. and mat1.h == mat2.h
  317. and mat1.c == mat2.c
  318. )
  319. mat1 = ncnn.Mat((7, 8, 9, 10))
  320. mat2 = mat1.clone()
  321. assert (
  322. mat1.dims == mat2.dims
  323. and mat1.w == mat2.w
  324. and mat1.h == mat2.h
  325. and mat1.d == mat2.d
  326. and mat1.c == mat2.c
  327. )
  328. def test_clone_from():
  329. mat2 = ncnn.Mat()
  330. mat1 = ncnn.Mat(1)
  331. mat2.clone_from(mat1)
  332. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  333. mat1 = ncnn.Mat(2, 3)
  334. mat2.clone_from(mat1)
  335. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  336. mat1 = ncnn.Mat(4, 5, 6)
  337. mat2.clone_from(mat1)
  338. assert (
  339. mat1.dims == mat2.dims
  340. and mat1.w == mat2.w
  341. and mat1.h == mat2.h
  342. and mat1.c == mat2.c
  343. )
  344. mat1 = ncnn.Mat(7, 8, 9, 10)
  345. mat2.clone_from(mat1)
  346. assert (
  347. mat1.dims == mat2.dims
  348. and mat1.w == mat2.w
  349. and mat1.h == mat2.h
  350. and mat1.d == mat2.d
  351. and mat1.c == mat2.c
  352. )
  353. mat1 = ncnn.Mat((1,))
  354. mat2.clone_from(mat1)
  355. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  356. mat1 = ncnn.Mat((2, 3))
  357. mat2.clone_from(mat1)
  358. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  359. mat1 = ncnn.Mat((4, 5, 6))
  360. mat2.clone_from(mat1)
  361. assert (
  362. mat1.dims == mat2.dims
  363. and mat1.w == mat2.w
  364. and mat1.h == mat2.h
  365. and mat1.c == mat2.c
  366. )
  367. mat1 = ncnn.Mat((7, 8, 9, 10))
  368. mat2.clone_from(mat1)
  369. assert (
  370. mat1.dims == mat2.dims
  371. and mat1.w == mat2.w
  372. and mat1.h == mat2.h
  373. and mat1.d == mat2.d
  374. and mat1.c == mat2.c
  375. )
  376. def test_reshape():
  377. mat1 = ncnn.Mat()
  378. mat2 = mat1.reshape(1)
  379. assert mat2.dims == 0
  380. mat2 = mat1.reshape(1, 1)
  381. assert mat2.dims == 0
  382. mat2 = mat1.reshape(1, 1, 1)
  383. assert mat2.dims == 0
  384. mat2 = mat1.reshape(1, 1, 1, 1)
  385. assert mat2.dims == 0
  386. mat1 = ncnn.Mat(1)
  387. mat2 = mat1.reshape(1, 1)
  388. assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
  389. mat2 = mat1.reshape(1, 1, 1)
  390. assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
  391. mat2 = mat1.reshape(1, 1, 1, 1)
  392. assert mat2.dims == 4 and mat2.w == 1 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
  393. mat1 = ncnn.Mat(1, 2)
  394. mat2 = mat1.reshape(2)
  395. assert mat2.dims == 1 and mat2.w == 2
  396. mat2 = mat1.reshape(2, 1)
  397. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
  398. mat2 = mat1.reshape(2, 1, 1)
  399. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
  400. mat2 = mat1.reshape(2, 1, 1, 1)
  401. assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
  402. mat1 = ncnn.Mat(1, 2, 3)
  403. mat2 = mat1.reshape(6)
  404. assert mat2.dims == 1 and mat2.w == 6
  405. mat2 = mat1.reshape(2, 3)
  406. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3
  407. mat2 = mat1.reshape(2, 3, 1)
  408. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
  409. mat2 = mat1.reshape(2, 1, 3, 1)
  410. assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 3 and mat2.c == 1
  411. mat1 = ncnn.Mat((1,))
  412. mat2 = mat1.reshape((1, 1))
  413. assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
  414. mat2 = mat1.reshape((1, 1, 1))
  415. assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
  416. mat2 = mat1.reshape((1, 1, 1, 1))
  417. assert mat2.dims == 4 and mat2.w == 1 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
  418. mat1 = ncnn.Mat((1, 2))
  419. mat2 = mat1.reshape((2,))
  420. assert mat2.dims == 1 and mat2.w == 2
  421. mat2 = mat1.reshape((2, 1))
  422. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
  423. mat2 = mat1.reshape((2, 1, 1))
  424. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
  425. mat2 = mat1.reshape((2, 1, 1, 1))
  426. assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
  427. mat1 = ncnn.Mat((1, 2, 3))
  428. mat2 = mat1.reshape((6,))
  429. assert mat2.dims == 1 and mat2.w == 6
  430. mat2 = mat1.reshape((2, 3))
  431. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
  432. mat2 = mat1.reshape((2, 3, 1))
  433. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
  434. mat2 = mat1.reshape((2, 1, 3, 1))
  435. assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 3 and mat2.c == 1
  436. with pytest.raises(RuntimeError) as execinfo:
  437. mat1.reshape((1, 1, 1, 1, 1))
  438. assert "shape must be 1, 2, 3 or 4 dims, not 5" in str(execinfo.value)
  439. def test_create():
  440. mat = ncnn.Mat()
  441. mat.create(1)
  442. assert mat.dims == 1 and mat.w == 1
  443. mat.create(2, 3)
  444. assert mat.dims == 2 and mat.w == 2 and mat.h == 3
  445. mat.create(4, 5, 6)
  446. assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
  447. mat.create(7, 8, 9, 10)
  448. assert mat.dims == 4 and mat.w == 7 and mat.h == 8 and mat.d == 9 and mat.c == 10
  449. mat.create((1,))
  450. assert mat.dims == 1 and mat.w == 1
  451. mat.create((2, 3))
  452. assert mat.dims == 2 and mat.w == 2 and mat.h == 3
  453. mat.create((4, 5, 6))
  454. assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
  455. mat.create((7, 8, 9, 10))
  456. assert mat.dims == 4 and mat.w == 7 and mat.h == 8 and mat.d == 9 and mat.c == 10
  457. def test_create_like():
  458. mat2 = ncnn.Mat()
  459. mat1 = ncnn.Mat(1)
  460. mat2.create_like(mat1)
  461. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  462. mat1 = ncnn.Mat(2, 3)
  463. mat2.create_like(mat1)
  464. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  465. mat1 = ncnn.Mat(4, 5, 6)
  466. mat2.create_like(mat1)
  467. assert (
  468. mat1.dims == mat2.dims
  469. and mat1.w == mat2.w
  470. and mat1.h == mat2.h
  471. and mat1.c == mat2.c
  472. )
  473. mat1 = ncnn.Mat(7, 8, 9, 10)
  474. mat2.create_like(mat1)
  475. assert (
  476. mat1.dims == mat2.dims
  477. and mat1.w == mat2.w
  478. and mat1.h == mat2.h
  479. and mat1.d == mat2.d
  480. and mat1.c == mat2.c
  481. )
  482. def test_addref_release():
  483. mat = ncnn.Mat(1)
  484. assert mat.refcount == 1
  485. mat.addref()
  486. assert mat.refcount == 2
  487. mat.release()
  488. assert mat.refcount == None
  489. def test_empty():
  490. mat = ncnn.Mat()
  491. assert mat.empty() == True
  492. mat = ncnn.Mat(1)
  493. assert mat.empty() == False
  494. def test_total():
  495. mat = ncnn.Mat(1)
  496. assert mat.total() == 1
  497. mat = ncnn.Mat(2, 3)
  498. assert mat.total() == 2 * 3
  499. mat = ncnn.Mat(4, 5, 6)
  500. assert mat.total() == 4 * 5 * 6
  501. mat = ncnn.Mat(7, 8, 9, 10)
  502. assert mat.total() == 7 * 8 * 9 * 10
  503. def test_elembits():
  504. mat = ncnn.Mat(1, elemsize=1, elempack=1)
  505. assert mat.elembits() == 8
  506. mat = ncnn.Mat(2, elemsize=2, elempack=1)
  507. assert mat.elembits() == 16
  508. mat = ncnn.Mat(3, elemsize=4, elempack=1)
  509. assert mat.elembits() == 32
  510. def test_shape():
  511. mat = ncnn.Mat(1)
  512. shape = mat.shape()
  513. assert shape.dims == 1 and shape.w == 1
  514. mat = ncnn.Mat(2, 3)
  515. shape = mat.shape()
  516. assert shape.dims == 2 and shape.w == 2 and shape.h == 3
  517. mat = ncnn.Mat(4, 5, 6)
  518. shape = mat.shape()
  519. assert shape.dims == 3 and shape.w == 4 and shape.h == 5 and shape.c == 6
  520. mat = ncnn.Mat(7, 8, 9, 10)
  521. shape = mat.shape()
  522. assert shape.dims == 4 and shape.w == 7 and shape.h == 8 and shape.d == 9 and shape.c == 10
  523. def test_channel_depth_row():
  524. mat = ncnn.Mat(2, 3, 4, 5)
  525. mat.fill(6.0)
  526. channel = mat.channel(1)
  527. assert channel.dims == 3 and channel.w == 2 and channel.h == 3 and channel.c == 4
  528. depth = channel.depth(1)
  529. assert depth.dims == 2 and depth.w == 2 and depth.h == 3
  530. row = depth.row(1)
  531. assert len(row) == 2 and np.abs(row[0] - 6.0) < sys.float_info.min
  532. def test_channel_row():
  533. mat = ncnn.Mat(2, 3, 4)
  534. mat.fill(4.0)
  535. channel = mat.channel(1)
  536. assert channel.dims == 2 and channel.w == 2 and channel.h == 3 and channel.c == 1
  537. row = channel.row(1)
  538. assert len(row) == 2 and np.abs(row[0] - 4.0) < sys.float_info.min
  539. def test_channel_range():
  540. mat = ncnn.Mat(1, 2, 3)
  541. channel_range = mat.channel_range(0, 2)
  542. assert (
  543. channel_range.dims == 3
  544. and channel_range.w == 1
  545. and channel_range.h == 2
  546. and channel_range.c == 2
  547. )
  548. def test_depth_range():
  549. mat = ncnn.Mat(1, 2, 3, 4)
  550. depth_range = mat.channel(1).depth_range(1, 2)
  551. assert (
  552. depth_range.dims == 3
  553. and depth_range.w == 1
  554. and depth_range.h == 2
  555. and depth_range.c == 2
  556. )
  557. def test_row_range():
  558. mat = ncnn.Mat(1, 2)
  559. row_range = mat.row_range(0, 2)
  560. assert row_range.dims == 2 and row_range.w == 1 and row_range.h == 2
  561. def test_range():
  562. mat = ncnn.Mat(2)
  563. range = mat.range(0, 2)
  564. assert range.dims == 1 and range.w == 2
  565. def test_getitem_setitem():
  566. mat = ncnn.Mat(2)
  567. mat.fill(1)
  568. assert (
  569. np.abs(mat[0] - 1.0) < sys.float_info.min
  570. and np.abs(mat[1] - 1.0) < sys.float_info.min
  571. )
  572. mat[0] = 2.0
  573. assert (
  574. np.abs(mat[0] - 2.0) < sys.float_info.min
  575. and np.abs(mat[1] - 1.0) < sys.float_info.min
  576. )
  577. def test_from_pixels():
  578. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  579. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  580. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  581. assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
  582. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  583. assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]
  584. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  585. mat = ncnn.Mat.from_pixels(
  586. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, stride=500 * 3
  587. ) # chw
  588. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  589. assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
  590. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  591. assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]
  592. def test_from_pixels_resize():
  593. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  594. mat = ncnn.Mat.from_pixels_resize(
  595. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 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, 400, 3)).astype(np.uint8) # hwc
  599. mat = ncnn.Mat.from_pixels_resize(
  600. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 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. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  607. mat = ncnn.Mat.from_pixels_resize(
  608. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 200, 150
  609. ) # chw
  610. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  611. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  612. mat = ncnn.Mat.from_pixels_resize(
  613. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 400, 300
  614. ) # chw
  615. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  616. assert pixels[0, 0, 0] == mat.channel(2).row(0)[0]
  617. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  618. assert pixels[299, 399, 2] == mat.channel(0).row(299)[399]
  619. def test_from_pixels_roi():
  620. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  621. mat = ncnn.Mat.from_pixels_roi(
  622. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150
  623. ) # chw
  624. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  625. assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
  626. assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
  627. assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]
  628. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  629. mat = ncnn.Mat.from_pixels_roi(
  630. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 500 * 3, 100, 75, 200, 150
  631. ) # chw
  632. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  633. assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
  634. assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
  635. assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]
  636. def test_from_pixels_roi_resize():
  637. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  638. mat = ncnn.Mat.from_pixels_roi_resize(
  639. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150, 100, 75
  640. ) # chw
  641. assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3
  642. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  643. mat = ncnn.Mat.from_pixels_roi_resize(
  644. pixels,
  645. ncnn.Mat.PixelType.PIXEL_RGB,
  646. 400,
  647. 300,
  648. 500 * 3,
  649. 100,
  650. 75,
  651. 200,
  652. 150,
  653. 100,
  654. 75,
  655. ) # chw
  656. assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3
  657. def test_substract_mean_normalize():
  658. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  659. mean_vals = [127.5, 127.5, 127.5]
  660. norm_vals = [0.007843, 0.007843, 0.007843]
  661. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  662. mat.substract_mean_normalize([], norm_vals)
  663. assert np.abs(pixels[0, 0, 0] * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5
  664. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  665. mat.substract_mean_normalize(mean_vals, [])
  666. assert np.abs((pixels[0, 0, 0] - 127.5) - mat.channel(0).row(0)[0]) < 1e-5
  667. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  668. mat.substract_mean_normalize(mean_vals, norm_vals)
  669. assert (
  670. np.abs((pixels[0, 0, 0] - 127.5) * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5
  671. )