| 1 | # Tencent is pleased to support the open source community by making ncnn available. |
| 2 | # |
| 3 | # Copyright (C) 2021 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 | |
| 15 | import pytest |
| 16 | |
| 17 | import ncnn |
| 18 | |
| 19 | alloctor = ncnn.PoolAllocator() |
| 20 | |
| 21 | |
| 22 | def test_extractor(): |
| 23 | with pytest.raises(TypeError, match="No constructor"): |
| 24 | ex = ncnn.Extractor() |
| 25 | |
| 26 | dr = ncnn.DataReaderFromEmpty() |
| 27 | |
| 28 | net = ncnn.Net() |
| 29 | net.load_param("tests/test.param") |
| 30 | net.load_model(dr) |
| 31 | |
| 32 | in_mat = ncnn.Mat((227, 227, 3)) |
| 33 | with net.create_extractor() as ex: |
| 34 | ex.set_light_mode(True) |
| 35 | ex.set_num_threads(2) |
| 36 | |
| 37 | ex.set_blob_allocator(alloctor) |
| 38 | ex.set_workspace_allocator(alloctor) |
| 39 | |
| 40 | ex.input("data", in_mat) |
| 41 | ret, out_mat = ex.extract("conv0_fwd") |
| 42 | assert ( |
| 43 | ret == 0 |
| 44 | and out_mat.dims == 3 |
| 45 | and out_mat.w == 225 |
| 46 | and out_mat.h == 225 |
| 47 | and out_mat.c == 3 |
| 48 | ) |
| 49 | |
| 50 | ret, out_mat = ex.extract("output") |
| 51 | assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 |
| 52 | |
| 53 | |
| 54 | def test_extractor_index(): |
| 55 | with pytest.raises(TypeError, match="No constructor"): |
| 56 | ex = ncnn.Extractor() |
| 57 | |
| 58 | dr = ncnn.DataReaderFromEmpty() |
| 59 | |
| 60 | net = ncnn.Net() |
| 61 | net.load_param("tests/test.param") |
| 62 | net.load_model(dr) |
| 63 | |
| 64 | in_mat = ncnn.Mat((227, 227, 3)) |
| 65 | ex = net.create_extractor() |
| 66 | ex.set_light_mode(True) |
| 67 | ex.set_num_threads(2) |
| 68 | |
| 69 | ex.set_blob_allocator(alloctor) |
| 70 | ex.set_workspace_allocator(alloctor) |
| 71 | |
| 72 | ex.input(0, in_mat) |
| 73 | ret, out_mat = ex.extract(1) |
| 74 | assert ( |
| 75 | ret == 0 |
| 76 | and out_mat.dims == 3 |
| 77 | and out_mat.w == 225 |
| 78 | and out_mat.h == 225 |
| 79 | and out_mat.c == 3 |
| 80 | ) |
| 81 | |
| 82 | ret, out_mat = ex.extract(2) |
| 83 | assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 |
| 84 | |
| 85 | # not use with sentence, call clear manually to ensure ex destruct before net |
| 86 | ex.clear() |