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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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_numpy():
  142. mat = ncnn.Mat(1)
  143. array = np.array(mat)
  144. assert mat.dims == array.ndim and mat.w == array.shape[0]
  145. mat = ncnn.Mat(2, 3)
  146. array = np.array(mat)
  147. assert (
  148. mat.dims == array.ndim and mat.w == array.shape[1] and mat.h == array.shape[0]
  149. )
  150. mat = ncnn.Mat(4, 5, 6)
  151. array = np.array(mat)
  152. assert (
  153. mat.dims == array.ndim
  154. and mat.w == array.shape[2]
  155. and mat.h == array.shape[1]
  156. and mat.c == array.shape[0]
  157. )
  158. mat = ncnn.Mat(1, elemsize=1)
  159. array = np.array(mat)
  160. assert array.dtype == np.int8
  161. mat = ncnn.Mat(1, elemsize=2)
  162. array = np.array(mat)
  163. assert array.dtype == np.float16
  164. # pybind11 def_buffer throw bug
  165. # with pytest.raises(RuntimeError) as execinfo:
  166. # mat = ncnn.Mat(1, elemsize=3)
  167. # array = np.array(mat)
  168. # assert "convert ncnn.Mat to numpy.ndarray only elemsize 1, 2, 4 support now, but given 3" in str(
  169. # execinfo.value
  170. # )
  171. assert array.dtype == np.float16
  172. mat = ncnn.Mat(1, elemsize=4)
  173. array = np.array(mat)
  174. assert array.dtype == np.float32
  175. mat = np.random.randint(0, 128, size=(12,)).astype(np.uint8)
  176. array = np.array(mat)
  177. assert (mat == array).all()
  178. mat = np.random.rand(12).astype(np.float32)
  179. array = np.array(mat)
  180. assert (mat == array).all()
  181. mat = np.random.randint(0, 128, size=(12, 11)).astype(np.uint8)
  182. array = np.array(mat)
  183. assert (mat == array).all()
  184. mat = np.random.rand(12, 11).astype(np.float32)
  185. array = np.array(mat)
  186. assert (mat == array).all()
  187. mat = np.random.randint(0, 256, size=(12, 11, 3)).astype(np.uint8)
  188. array = np.array(mat)
  189. assert (mat == array).all()
  190. mat = np.random.rand(12, 11, 3).astype(np.float32)
  191. array = np.array(mat)
  192. assert (mat == array).all()
  193. def test_fill():
  194. mat = ncnn.Mat(1)
  195. mat.fill(1.0)
  196. array = np.array(mat)
  197. assert np.abs(array[0] - 1.0) < sys.float_info.min
  198. def test_clone():
  199. mat1 = ncnn.Mat(1)
  200. mat2 = mat1.clone()
  201. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  202. mat1 = ncnn.Mat(2, 3)
  203. mat2 = mat1.clone()
  204. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  205. mat1 = ncnn.Mat(4, 5, 6)
  206. mat2 = mat1.clone()
  207. assert (
  208. mat1.dims == mat2.dims
  209. and mat1.w == mat2.w
  210. and mat1.h == mat2.h
  211. and mat1.c == mat2.c
  212. )
  213. mat1 = ncnn.Mat((1,))
  214. mat2 = mat1.clone()
  215. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  216. mat1 = ncnn.Mat((2, 3))
  217. mat2 = mat1.clone()
  218. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  219. mat1 = ncnn.Mat((4, 5, 6))
  220. mat2 = mat1.clone()
  221. assert (
  222. mat1.dims == mat2.dims
  223. and mat1.w == mat2.w
  224. and mat1.h == mat2.h
  225. and mat1.c == mat2.c
  226. )
  227. def test_clone_from():
  228. mat2 = ncnn.Mat()
  229. mat1 = ncnn.Mat(1)
  230. mat2.clone_from(mat1)
  231. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  232. mat1 = ncnn.Mat(2, 3)
  233. mat2.clone_from(mat1)
  234. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  235. mat1 = ncnn.Mat(4, 5, 6)
  236. mat2.clone_from(mat1)
  237. assert (
  238. mat1.dims == mat2.dims
  239. and mat1.w == mat2.w
  240. and mat1.h == mat2.h
  241. and mat1.c == mat2.c
  242. )
  243. mat1 = ncnn.Mat((1,))
  244. mat2.clone_from(mat1)
  245. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  246. mat1 = ncnn.Mat((2, 3))
  247. mat2.clone_from(mat1)
  248. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  249. mat1 = ncnn.Mat((4, 5, 6))
  250. mat2.clone_from(mat1)
  251. assert (
  252. mat1.dims == mat2.dims
  253. and mat1.w == mat2.w
  254. and mat1.h == mat2.h
  255. and mat1.c == mat2.c
  256. )
  257. def test_reshape():
  258. mat1 = ncnn.Mat()
  259. mat2 = mat1.reshape(1)
  260. assert mat2.dims == 0
  261. mat2 = mat1.reshape(1, 1)
  262. assert mat2.dims == 0
  263. mat2 = mat1.reshape(1, 1, 1)
  264. assert mat2.dims == 0
  265. mat1 = ncnn.Mat(1)
  266. mat2 = mat1.reshape(1, 1)
  267. assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
  268. mat2 = mat1.reshape(1, 1, 1)
  269. assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
  270. mat1 = ncnn.Mat(1, 2)
  271. mat2 = mat1.reshape(2)
  272. assert mat2.dims == 1 and mat2.w == 2
  273. mat2 = mat1.reshape(2, 1)
  274. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
  275. mat2 = mat1.reshape(2, 1, 1)
  276. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
  277. mat1 = ncnn.Mat(1, 2, 3)
  278. mat2 = mat1.reshape(6)
  279. assert mat2.dims == 1 and mat2.w == 6
  280. mat2 = mat1.reshape(2, 3)
  281. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3
  282. mat2 = mat1.reshape(2, 3, 1)
  283. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
  284. mat1 = ncnn.Mat((1,))
  285. mat2 = mat1.reshape((1, 1))
  286. assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
  287. mat2 = mat1.reshape((1, 1, 1))
  288. assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
  289. mat1 = ncnn.Mat((1, 2))
  290. mat2 = mat1.reshape((2,))
  291. assert mat2.dims == 1 and mat2.w == 2
  292. mat2 = mat1.reshape((2, 1))
  293. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
  294. mat2 = mat1.reshape((2, 1, 1))
  295. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
  296. mat1 = ncnn.Mat((1, 2, 3))
  297. mat2 = mat1.reshape((6,))
  298. assert mat2.dims == 1 and mat2.w == 6
  299. mat2 = mat1.reshape((2, 3))
  300. assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
  301. mat2 = mat1.reshape((2, 3, 1))
  302. assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
  303. with pytest.raises(RuntimeError) as execinfo:
  304. mat1.reshape((1, 1, 1, 1))
  305. assert "shape must be 1, 2 or 3 dims, not 4" in str(execinfo.value)
  306. def test_create():
  307. mat = ncnn.Mat()
  308. mat.create(1)
  309. assert mat.dims == 1 and mat.w == 1
  310. mat.create(2, 3)
  311. assert mat.dims == 2 and mat.w == 2 and mat.h == 3
  312. mat.create(4, 5, 6)
  313. assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
  314. mat.create((1,))
  315. assert mat.dims == 1 and mat.w == 1
  316. mat.create((2, 3))
  317. assert mat.dims == 2 and mat.w == 2 and mat.h == 3
  318. mat.create((4, 5, 6))
  319. assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
  320. mat.create((7, 8, 9), elemsize=4)
  321. def test_create_like():
  322. mat2 = ncnn.Mat()
  323. mat1 = ncnn.Mat(1)
  324. mat2.create_like(mat1)
  325. assert mat1.dims == mat2.dims and mat1.w == mat2.w
  326. mat1 = ncnn.Mat(2, 3)
  327. mat2.create_like(mat1)
  328. assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
  329. mat1 = ncnn.Mat(4, 5, 6)
  330. mat2.create_like(mat1)
  331. assert (
  332. mat1.dims == mat2.dims
  333. and mat1.w == mat2.w
  334. and mat1.h == mat2.h
  335. and mat1.c == mat2.c
  336. )
  337. def test_addref_release():
  338. mat = ncnn.Mat(1)
  339. assert mat.refcount == 1
  340. mat.addref()
  341. assert mat.refcount == 2
  342. mat.release()
  343. assert mat.refcount == None
  344. def test_empty():
  345. mat = ncnn.Mat()
  346. assert mat.empty() == True
  347. mat = ncnn.Mat(1)
  348. assert mat.empty() == False
  349. def test_total():
  350. mat = ncnn.Mat(1)
  351. assert mat.total() == 1
  352. mat = ncnn.Mat(2, 3)
  353. assert mat.total() == 2 * 3
  354. mat = ncnn.Mat(4, 5, 6)
  355. assert mat.total() == 4 * 5 * 6
  356. def test_elembits():
  357. mat = ncnn.Mat(1, elemsize=1, elempack=1)
  358. assert mat.elembits() == 8
  359. mat = ncnn.Mat(2, elemsize=2, elempack=1)
  360. assert mat.elembits() == 16
  361. mat = ncnn.Mat(3, elemsize=4, elempack=1)
  362. assert mat.elembits() == 32
  363. def test_shape():
  364. mat = ncnn.Mat(1)
  365. shape = mat.shape()
  366. assert shape.dims == 1 and shape.w == 1
  367. mat = ncnn.Mat(2, 3)
  368. shape = mat.shape()
  369. assert shape.dims == 2 and shape.w == 2 and shape.h == 3
  370. mat = ncnn.Mat(4, 5, 6)
  371. shape = mat.shape()
  372. assert shape.dims == 3 and shape.w == 4 and shape.h == 5 and shape.c == 6
  373. def test_channel_row():
  374. mat = ncnn.Mat(2, 3, 4)
  375. mat.fill(4.0)
  376. channel = mat.channel(1)
  377. assert channel.dims == 2 and channel.w == 2 and channel.h == 3 and channel.c == 1
  378. row = channel.row(1)
  379. assert len(row) == 2 and np.abs(row[0] - 4.0) < sys.float_info.min
  380. def test_channel_range():
  381. mat = ncnn.Mat(1, 2, 3)
  382. channel_range = mat.channel_range(0, 2)
  383. assert (
  384. channel_range.dims == 3
  385. and channel_range.w == 1
  386. and channel_range.h == 2
  387. and channel_range.c == 2
  388. )
  389. def test_row_range():
  390. mat = ncnn.Mat(1, 2)
  391. row_range = mat.row_range(0, 2)
  392. assert row_range.dims == 2 and row_range.w == 1 and row_range.h == 2
  393. def test_range():
  394. mat = ncnn.Mat(2)
  395. range = mat.range(0, 2)
  396. assert range.dims == 1 and range.w == 2
  397. def test_getitem_setitem():
  398. mat = ncnn.Mat(2)
  399. mat.fill(1)
  400. assert (
  401. np.abs(mat[0] - 1.0) < sys.float_info.min
  402. and np.abs(mat[1] - 1.0) < sys.float_info.min
  403. )
  404. mat[0] = 2.0
  405. assert (
  406. np.abs(mat[0] - 2.0) < sys.float_info.min
  407. and np.abs(mat[1] - 1.0) < sys.float_info.min
  408. )
  409. def test_from_pixels():
  410. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  411. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  412. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  413. assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
  414. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  415. assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]
  416. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  417. mat = ncnn.Mat.from_pixels(
  418. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, stride=500 * 3
  419. ) # chw
  420. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  421. assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
  422. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  423. assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]
  424. def test_from_pixels_resize():
  425. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  426. mat = ncnn.Mat.from_pixels_resize(
  427. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 200, 150
  428. ) # chw
  429. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  430. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  431. mat = ncnn.Mat.from_pixels_resize(
  432. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 400, 300
  433. ) # chw
  434. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  435. assert pixels[0, 0, 0] == mat.channel(2).row(0)[0]
  436. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  437. assert pixels[299, 399, 2] == mat.channel(0).row(299)[399]
  438. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  439. mat = ncnn.Mat.from_pixels_resize(
  440. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 200, 150
  441. ) # chw
  442. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  443. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  444. mat = ncnn.Mat.from_pixels_resize(
  445. pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 400, 300
  446. ) # chw
  447. assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
  448. assert pixels[0, 0, 0] == mat.channel(2).row(0)[0]
  449. assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
  450. assert pixels[299, 399, 2] == mat.channel(0).row(299)[399]
  451. def test_from_pixels_roi():
  452. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  453. mat = ncnn.Mat.from_pixels_roi(
  454. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150
  455. ) # chw
  456. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  457. assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
  458. assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
  459. assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]
  460. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  461. mat = ncnn.Mat.from_pixels_roi(
  462. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 500 * 3, 100, 75, 200, 150
  463. ) # chw
  464. assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
  465. assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
  466. assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
  467. assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]
  468. def test_from_pixels_roi_resize():
  469. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  470. mat = ncnn.Mat.from_pixels_roi_resize(
  471. pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150, 100, 75
  472. ) # chw
  473. assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3
  474. pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
  475. mat = ncnn.Mat.from_pixels_roi_resize(
  476. pixels,
  477. ncnn.Mat.PixelType.PIXEL_RGB,
  478. 400,
  479. 300,
  480. 500 * 3,
  481. 100,
  482. 75,
  483. 200,
  484. 150,
  485. 100,
  486. 75,
  487. ) # chw
  488. assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3
  489. def test_substract_mean_normalize():
  490. pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
  491. mean_vals = [127.5, 127.5, 127.5]
  492. norm_vals = [0.007843, 0.007843, 0.007843]
  493. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  494. mat.substract_mean_normalize([], norm_vals)
  495. assert np.abs(pixels[0, 0, 0] * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5
  496. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  497. mat.substract_mean_normalize(mean_vals, [])
  498. assert np.abs((pixels[0, 0, 0] - 127.5) - mat.channel(0).row(0)[0]) < 1e-5
  499. mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
  500. mat.substract_mean_normalize(mean_vals, norm_vals)
  501. assert (
  502. np.abs((pixels[0, 0, 0] - 127.5) * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5
  503. )