Elffuss BraidHub

kikocis/ncnn public

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