File size: 24,308 Bytes
be903e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

import sys
import numpy as np
import pytest

import ncnn


def test_mat_dims1():
    mat = ncnn.Mat(1)
    assert mat.dims == 1 and mat.w == 1
    mat = ncnn.Mat(2, elemsize=4)
    assert mat.dims == 1 and mat.w == 2 and mat.elemsize == 4
    mat = ncnn.Mat(3, elemsize=4, elempack=1)
    assert mat.dims == 1 and mat.w == 3 and mat.elemsize == 4 and mat.elempack == 1
    mat = ncnn.Mat(4, elemsize=4, elempack=1, allocator=None)
    assert (
        mat.dims == 1
        and mat.w == 4
        and mat.elemsize == 4
        and mat.elempack == 1
        and mat.allocator == None
    )

    mat = ncnn.Mat((1,))
    assert mat.dims == 1 and mat.w == 1
    mat = ncnn.Mat((2,), elemsize=4)
    assert mat.dims == 1 and mat.w == 2 and mat.elemsize == 4
    mat = ncnn.Mat((3,), elemsize=4, elempack=1)
    assert mat.dims == 1 and mat.w == 3 and mat.elemsize == 4 and mat.elempack == 1
    mat = ncnn.Mat((4,), elemsize=4, elempack=1, allocator=None)
    assert (
        mat.dims == 1
        and mat.w == 4
        and mat.elemsize == 4
        and mat.elempack == 1
        and mat.allocator == None
    )


def test_mat_dims2():
    mat = ncnn.Mat(1, 2)
    assert mat.dims == 2 and mat.w == 1 and mat.h == 2
    mat = ncnn.Mat(3, 4, elemsize=4)
    assert mat.dims == 2 and mat.w == 3 and mat.h == 4 and mat.elemsize == 4
    mat = ncnn.Mat(5, 6, elemsize=4, elempack=1)
    assert (
        mat.dims == 2
        and mat.w == 5
        and mat.h == 6
        and mat.elemsize == 4
        and mat.elempack == 1
    )
    mat = ncnn.Mat(7, 8, elemsize=4, elempack=1, allocator=None)
    assert (
        mat.dims == 2
        and mat.w == 7
        and mat.h == 8
        and mat.elemsize == 4
        and mat.elempack == 1
        and mat.allocator == None
    )

    mat = ncnn.Mat((1, 2))
    assert mat.dims == 2 and mat.w == 1 and mat.h == 2
    mat = ncnn.Mat((3, 4), elemsize=4)
    assert mat.dims == 2 and mat.w == 3 and mat.h == 4 and mat.elemsize == 4
    mat = ncnn.Mat((5, 6), elemsize=4, elempack=1)
    assert (
        mat.dims == 2
        and mat.w == 5
        and mat.h == 6
        and mat.elemsize == 4
        and mat.elempack == 1
    )
    mat = ncnn.Mat((7, 8), elemsize=4, elempack=1, allocator=None)
    assert (
        mat.dims == 2
        and mat.w == 7
        and mat.h == 8
        and mat.elemsize == 4
        and mat.elempack == 1
        and mat.allocator == None
    )


def test_mat_dims3():
    mat = ncnn.Mat(1, 2, 3)
    assert mat.dims == 3 and mat.w == 1 and mat.h == 2 and mat.c == 3
    mat = ncnn.Mat(4, 5, 6, elemsize=4)
    assert (
        mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6 and mat.elemsize == 4
    )
    mat = ncnn.Mat(7, 8, 9, elemsize=4, elempack=1)
    assert (
        mat.dims == 3
        and mat.w == 7
        and mat.h == 8
        and mat.c == 9
        and mat.elemsize == 4
        and mat.elempack == 1
    )
    mat = ncnn.Mat(10, 11, 12, elemsize=4, elempack=1, allocator=None)
    assert (
        mat.dims == 3
        and mat.w == 10
        and mat.h == 11
        and mat.c == 12
        and mat.elemsize == 4
        and mat.elempack == 1
        and mat.allocator == None
    )

    mat = ncnn.Mat((1, 2, 3))
    assert mat.dims == 3 and mat.w == 1 and mat.h == 2 and mat.c == 3
    mat = ncnn.Mat((4, 5, 6), elemsize=4)
    assert (
        mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6 and mat.elemsize == 4
    )
    mat = ncnn.Mat((7, 8, 9), elemsize=4, elempack=1)
    assert (
        mat.dims == 3
        and mat.w == 7
        and mat.h == 8
        and mat.c == 9
        and mat.elemsize == 4
        and mat.elempack == 1
    )
    mat = ncnn.Mat((10, 11, 12), elemsize=4, elempack=1, allocator=None)
    assert (
        mat.dims == 3
        and mat.w == 10
        and mat.h == 11
        and mat.c == 12
        and mat.elemsize == 4
        and mat.elempack == 1
        and mat.allocator == None
    )


def test_mat_dims4():
    mat = ncnn.Mat(1, 2, 3, 4)
    assert mat.dims == 4 and mat.w == 1 and mat.h == 2 and mat.d == 3 and mat.c == 4
    mat = ncnn.Mat(4, 5, 6, 7, elemsize=4)
    assert (
        mat.dims == 4 and mat.w == 4 and mat.h == 5 and mat.d == 6 and mat.c == 7 and mat.elemsize == 4
    )
    mat = ncnn.Mat(7, 8, 9, 10, elemsize=4, elempack=1)
    assert (
        mat.dims == 4
        and mat.w == 7
        and mat.h == 8
        and mat.d == 9
        and mat.c == 10
        and mat.elemsize == 4
        and mat.elempack == 1
    )
    mat = ncnn.Mat(10, 11, 12, 13, elemsize=4, elempack=1, allocator=None)
    assert (
        mat.dims == 4
        and mat.w == 10
        and mat.h == 11
        and mat.d == 12
        and mat.c == 13
        and mat.elemsize == 4
        and mat.elempack == 1
        and mat.allocator == None
    )

    mat = ncnn.Mat((1, 2, 3, 4))
    assert mat.dims == 4 and mat.w == 1 and mat.h == 2 and mat.d == 3 and mat.c == 4
    mat = ncnn.Mat((4, 5, 6, 7), elemsize=4)
    assert (
        mat.dims == 4 and mat.w == 4 and mat.h == 5 and mat.d == 6 and mat.c == 7 and mat.elemsize == 4
    )
    mat = ncnn.Mat((7, 8, 9, 10), elemsize=4, elempack=1)
    assert (
        mat.dims == 4
        and mat.w == 7
        and mat.h == 8
        and mat.d == 9
        and mat.c == 10
        and mat.elemsize == 4
        and mat.elempack == 1
    )
    mat = ncnn.Mat((10, 11, 12, 13), elemsize=4, elempack=1, allocator=None)
    assert (
        mat.dims == 4
        and mat.w == 10
        and mat.h == 11
        and mat.d == 12
        and mat.c == 13
        and mat.elemsize == 4
        and mat.elempack == 1
        and mat.allocator == None
    )


def test_numpy():
    mat = ncnn.Mat(1)
    array = mat.numpy()
    assert mat.dims == array.ndim and mat.w == array.shape[0]
    mat = ncnn.Mat(2, 3)
    array = mat.numpy()
    assert array.dtype == np.float32
    assert (
        mat.dims == array.ndim and mat.w == array.shape[1] and mat.h == array.shape[0]
    )
    mat = ncnn.Mat(4, 5, 6)
    array = np.array(mat)
    assert (
        mat.dims == array.ndim
        and mat.w == array.shape[2]
        and mat.h == array.shape[1]
        and mat.c == array.shape[0]
    )
    mat = ncnn.Mat(7, 8, 9, 10)
    array = np.array(mat)
    assert (
        mat.dims == array.ndim
        and mat.w == array.shape[3]
        and mat.h == array.shape[2]
        and mat.d == array.shape[1]
        and mat.c == array.shape[0]
    )

    mat = ncnn.Mat(1, elemsize=1)
    array = mat.numpy()
    assert array.dtype == np.int8
    mat = ncnn.Mat(1, elemsize=2)
    array = mat.numpy()
    assert array.dtype == np.float16
    # pybind11 def_buffer throw bug
    # with pytest.raises(RuntimeError) as execinfo:
    #     mat = ncnn.Mat(1, elemsize=3)
    #     array = np.array(mat)
    #     assert "convert ncnn.Mat to numpy.ndarray only elemsize 1, 2, 4 support now, but given 3" in str(
    #         execinfo.value
    #     )
    assert array.dtype == np.float16
    mat = ncnn.Mat(1, elemsize=4)
    array = mat.numpy()
    assert array.dtype == np.float32

    mat = np.random.randint(0, 128, size=(12,)).astype(np.uint8)
    array = np.array(mat)
    assert (mat == array).all()
    mat = np.random.rand(12).astype(np.float32)
    array = np.array(mat)
    assert (mat == array).all()
    mat = np.random.randint(0, 128, size=(12, 11)).astype(np.uint8)
    array = np.array(mat)
    assert (mat == array).all()
    mat = np.random.rand(12, 11).astype(np.float32)
    array = np.array(mat)
    assert (mat == array).all()
    mat = np.random.randint(0, 256, size=(12, 11, 3)).astype(np.uint8)
    array = np.array(mat)
    assert (mat == array).all()
    mat = np.random.rand(12, 11, 3).astype(np.float32)
    array = np.array(mat)
    assert (mat == array).all()
    mat = np.random.randint(0, 256, size=(12, 11, 7, 3)).astype(np.uint8)
    array = np.array(mat)
    assert (mat == array).all()
    mat = np.random.rand(12, 11, 7, 3).astype(np.float32)
    array = np.array(mat)
    assert (mat == array).all()

    array = np.array([1, 2, 3], dtype=np.int32)
    mat = ncnn.Mat(array)
    array2 = mat.numpy(format='i')
    assert array2.dtype == np.int32
    array[0] = 10
    assert array2[0] == 10

    array = np.array([1, 2, 3], dtype=np.float32)
    mat = ncnn.Mat(array)
    array2 = mat.numpy(format='f')
    assert array2.dtype == np.float32
    array2[0] = 100
    assert array[0] == 100

def test_fill():
    mat = ncnn.Mat(1)
    mat.fill(1.0)
    array = np.array(mat)
    assert np.abs(array[0] - 1.0) < sys.float_info.min


def test_clone():
    mat1 = ncnn.Mat(1)
    mat2 = mat1.clone()
    assert mat1.dims == mat2.dims and mat1.w == mat2.w

    mat1 = ncnn.Mat(2, 3)
    mat2 = mat1.clone()
    assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h

    mat1 = ncnn.Mat(4, 5, 6)
    mat2 = mat1.clone()
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.c == mat2.c
    )

    mat1 = ncnn.Mat(7, 8, 9, 10)
    mat2 = mat1.clone()
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.d == mat2.d
        and mat1.c == mat2.c
    )

    mat1 = ncnn.Mat((1,))
    mat2 = mat1.clone()
    assert mat1.dims == mat2.dims and mat1.w == mat2.w

    mat1 = ncnn.Mat((2, 3))
    mat2 = mat1.clone()
    assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h

    mat1 = ncnn.Mat((4, 5, 6))
    mat2 = mat1.clone()
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.c == mat2.c
    )

    mat1 = ncnn.Mat((7, 8, 9, 10))
    mat2 = mat1.clone()
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.d == mat2.d
        and mat1.c == mat2.c
    )


def test_clone_from():
    mat2 = ncnn.Mat()

    mat1 = ncnn.Mat(1)
    mat2.clone_from(mat1)
    assert mat1.dims == mat2.dims and mat1.w == mat2.w

    mat1 = ncnn.Mat(2, 3)
    mat2.clone_from(mat1)
    assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h

    mat1 = ncnn.Mat(4, 5, 6)
    mat2.clone_from(mat1)
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.c == mat2.c
    )

    mat1 = ncnn.Mat(7, 8, 9, 10)
    mat2.clone_from(mat1)
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.d == mat2.d
        and mat1.c == mat2.c
    )

    mat1 = ncnn.Mat((1,))
    mat2.clone_from(mat1)
    assert mat1.dims == mat2.dims and mat1.w == mat2.w

    mat1 = ncnn.Mat((2, 3))
    mat2.clone_from(mat1)
    assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h

    mat1 = ncnn.Mat((4, 5, 6))
    mat2.clone_from(mat1)
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.c == mat2.c
    )

    mat1 = ncnn.Mat((7, 8, 9, 10))
    mat2.clone_from(mat1)
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.d == mat2.d
        and mat1.c == mat2.c
    )


def test_reshape():
    mat1 = ncnn.Mat()
    mat2 = mat1.reshape(1)
    assert mat2.dims == 0
    mat2 = mat1.reshape(1, 1)
    assert mat2.dims == 0
    mat2 = mat1.reshape(1, 1, 1)
    assert mat2.dims == 0
    mat2 = mat1.reshape(1, 1, 1, 1)
    assert mat2.dims == 0

    mat1 = ncnn.Mat(1)
    mat2 = mat1.reshape(1, 1)
    assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
    mat2 = mat1.reshape(1, 1, 1)
    assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
    mat2 = mat1.reshape(1, 1, 1, 1)
    assert mat2.dims == 4 and mat2.w == 1 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1

    mat1 = ncnn.Mat(1, 2)
    mat2 = mat1.reshape(2)
    assert mat2.dims == 1 and mat2.w == 2
    mat2 = mat1.reshape(2, 1)
    assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
    mat2 = mat1.reshape(2, 1, 1)
    assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
    mat2 = mat1.reshape(2, 1, 1, 1)
    assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1

    mat1 = ncnn.Mat(1, 2, 3)
    mat2 = mat1.reshape(6)
    assert mat2.dims == 1 and mat2.w == 6
    mat2 = mat1.reshape(2, 3)
    assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3
    mat2 = mat1.reshape(2, 3, 1)
    assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
    mat2 = mat1.reshape(2, 1, 3, 1)
    assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 3 and mat2.c == 1

    mat1 = ncnn.Mat((1,))
    mat2 = mat1.reshape((1, 1))
    assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
    mat2 = mat1.reshape((1, 1, 1))
    assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
    mat2 = mat1.reshape((1, 1, 1, 1))
    assert mat2.dims == 4 and mat2.w == 1 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1

    mat1 = ncnn.Mat((1, 2))
    mat2 = mat1.reshape((2,))
    assert mat2.dims == 1 and mat2.w == 2
    mat2 = mat1.reshape((2, 1))
    assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
    mat2 = mat1.reshape((2, 1, 1))
    assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
    mat2 = mat1.reshape((2, 1, 1, 1))
    assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1

    mat1 = ncnn.Mat((1, 2, 3))
    mat2 = mat1.reshape((6,))
    assert mat2.dims == 1 and mat2.w == 6
    mat2 = mat1.reshape((2, 3))
    assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
    mat2 = mat1.reshape((2, 3, 1))
    assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
    mat2 = mat1.reshape((2, 1, 3, 1))
    assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 3 and mat2.c == 1

    with pytest.raises(RuntimeError) as execinfo:
        mat1.reshape((1, 1, 1, 1, 1))
    assert "shape must be 1, 2, 3 or 4 dims, not 5" in str(execinfo.value)


def test_create():
    mat = ncnn.Mat()
    mat.create(1)
    assert mat.dims == 1 and mat.w == 1
    mat.create(2, 3)
    assert mat.dims == 2 and mat.w == 2 and mat.h == 3
    mat.create(4, 5, 6)
    assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
    mat.create(7, 8, 9, 10)
    assert mat.dims == 4 and mat.w == 7 and mat.h == 8 and mat.d == 9 and mat.c == 10

    mat.create((1,))
    assert mat.dims == 1 and mat.w == 1
    mat.create((2, 3))
    assert mat.dims == 2 and mat.w == 2 and mat.h == 3
    mat.create((4, 5, 6))
    assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
    mat.create((7, 8, 9, 10))
    assert mat.dims == 4 and mat.w == 7 and mat.h == 8 and mat.d == 9 and mat.c == 10


def test_create_like():
    mat2 = ncnn.Mat()

    mat1 = ncnn.Mat(1)
    mat2.create_like(mat1)
    assert mat1.dims == mat2.dims and mat1.w == mat2.w
    mat1 = ncnn.Mat(2, 3)
    mat2.create_like(mat1)
    assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
    mat1 = ncnn.Mat(4, 5, 6)
    mat2.create_like(mat1)
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.c == mat2.c
    )
    mat1 = ncnn.Mat(7, 8, 9, 10)
    mat2.create_like(mat1)
    assert (
        mat1.dims == mat2.dims
        and mat1.w == mat2.w
        and mat1.h == mat2.h
        and mat1.d == mat2.d
        and mat1.c == mat2.c
    )


def test_addref_release():
    mat = ncnn.Mat(1)
    assert mat.refcount == 1

    mat.addref()
    assert mat.refcount == 2

    mat.release()
    assert mat.refcount == None


def test_empty():
    mat = ncnn.Mat()
    assert mat.empty() == True

    mat = ncnn.Mat(1)
    assert mat.empty() == False


def test_total():
    mat = ncnn.Mat(1)
    assert mat.total() == 1
    mat = ncnn.Mat(2, 3)
    assert mat.total() == 2 * 3
    mat = ncnn.Mat(4, 5, 6)
    assert mat.total() == 4 * 5 * 6
    mat = ncnn.Mat(7, 8, 9, 10)
    assert mat.total() == 7 * 8 * 9 * 10


def test_elembits():
    mat = ncnn.Mat(1, elemsize=1, elempack=1)
    assert mat.elembits() == 8
    mat = ncnn.Mat(2, elemsize=2, elempack=1)
    assert mat.elembits() == 16
    mat = ncnn.Mat(3, elemsize=4, elempack=1)
    assert mat.elembits() == 32


def test_shape():
    mat = ncnn.Mat(1)
    shape = mat.shape()
    assert shape.dims == 1 and shape.w == 1
    mat = ncnn.Mat(2, 3)
    shape = mat.shape()
    assert shape.dims == 2 and shape.w == 2 and shape.h == 3
    mat = ncnn.Mat(4, 5, 6)
    shape = mat.shape()
    assert shape.dims == 3 and shape.w == 4 and shape.h == 5 and shape.c == 6
    mat = ncnn.Mat(7, 8, 9, 10)
    shape = mat.shape()
    assert shape.dims == 4 and shape.w == 7 and shape.h == 8 and shape.d == 9 and shape.c == 10


def test_channel_depth_row():
    mat = ncnn.Mat(2, 3, 4, 5)
    mat.fill(6.0)
    channel = mat.channel(1)
    assert channel.dims == 3 and channel.w == 2 and channel.h == 3 and channel.c == 4

    depth = channel.depth(1)
    assert depth.dims == 2 and depth.w == 2 and depth.h == 3

    row = depth.row(1)
    assert len(row) == 2 and np.abs(row[0] - 6.0) < sys.float_info.min


def test_channel_row():
    mat = ncnn.Mat(2, 3, 4)
    mat.fill(4.0)
    channel = mat.channel(1)
    assert channel.dims == 2 and channel.w == 2 and channel.h == 3 and channel.c == 1

    row = channel.row(1)
    assert len(row) == 2 and np.abs(row[0] - 4.0) < sys.float_info.min


def test_channel_range():
    mat = ncnn.Mat(1, 2, 3)
    channel_range = mat.channel_range(0, 2)
    assert (
        channel_range.dims == 3
        and channel_range.w == 1
        and channel_range.h == 2
        and channel_range.c == 2
    )


def test_depth_range():
    mat = ncnn.Mat(1, 2, 3, 4)
    depth_range = mat.channel(1).depth_range(1, 2)
    assert (
        depth_range.dims == 3
        and depth_range.w == 1
        and depth_range.h == 2
        and depth_range.c == 2
    )


def test_row_range():
    mat = ncnn.Mat(1, 2)
    row_range = mat.row_range(0, 2)
    assert row_range.dims == 2 and row_range.w == 1 and row_range.h == 2


def test_range():
    mat = ncnn.Mat(2)
    range = mat.range(0, 2)
    assert range.dims == 1 and range.w == 2


def test_getitem_setitem():
    mat = ncnn.Mat(2)
    mat.fill(1)
    assert (
        np.abs(mat[0] - 1.0) < sys.float_info.min
        and np.abs(mat[1] - 1.0) < sys.float_info.min
    )

    mat[0] = 2.0
    assert (
        np.abs(mat[0] - 2.0) < sys.float_info.min
        and np.abs(mat[1] - 1.0) < sys.float_info.min
    )


def test_from_pixels():
    pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300)  # chw
    assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
    assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
    assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
    assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]

    pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels(
        pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, stride=500 * 3
    )  # chw
    assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
    assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
    assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
    assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]


def test_from_pixels_resize():
    pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels_resize(
        pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 200, 150
    )  # chw
    assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3

    pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels_resize(
        pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 400, 300
    )  # chw
    assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
    assert pixels[0, 0, 0] == mat.channel(2).row(0)[0]
    assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
    assert pixels[299, 399, 2] == mat.channel(0).row(299)[399]

    pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels_resize(
        pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 200, 150
    )  # chw
    assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3

    pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels_resize(
        pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 400, 300
    )  # chw
    assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
    assert pixels[0, 0, 0] == mat.channel(2).row(0)[0]
    assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
    assert pixels[299, 399, 2] == mat.channel(0).row(299)[399]


def test_from_pixels_roi():
    pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels_roi(
        pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150
    )  # chw
    assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
    assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
    assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
    assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]

    pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels_roi(
        pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 500 * 3, 100, 75, 200, 150
    )  # chw
    assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
    assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
    assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
    assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]


def test_from_pixels_roi_resize():
    pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels_roi_resize(
        pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150, 100, 75
    )  # chw
    assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3

    pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8)  # hwc
    mat = ncnn.Mat.from_pixels_roi_resize(
        pixels,
        ncnn.Mat.PixelType.PIXEL_RGB,
        400,
        300,
        500 * 3,
        100,
        75,
        200,
        150,
        100,
        75,
    )  # chw
    assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3


def test_substract_mean_normalize():
    pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8)  # hwc
    mean_vals = [127.5, 127.5, 127.5]
    norm_vals = [0.007843, 0.007843, 0.007843]

    mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300)  # chw
    mat.substract_mean_normalize([], norm_vals)
    assert np.abs(pixels[0, 0, 0] * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5

    mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300)  # chw
    mat.substract_mean_normalize(mean_vals, [])
    assert np.abs((pixels[0, 0, 0] - 127.5) - mat.channel(0).row(0)[0]) < 1e-5

    mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300)  # chw
    mat.substract_mean_normalize(mean_vals, norm_vals)
    assert (
        np.abs((pixels[0, 0, 0] - 127.5) * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5
    )