| 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 numpy as np |
| 16 | import pytest |
| 17 | |
| 18 | import ncnn |
| 19 | |
| 20 | |
| 21 | def test_net(): |
| 22 | dr = ncnn.DataReaderFromEmpty() |
| 23 | |
| 24 | with ncnn.Net() as net: |
| 25 | ret = net.load_param("tests/test.param") |
| 26 | net.load_model(dr) |
| 27 | assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3 |
| 28 | |
| 29 | input_names = net.input_names() |
| 30 | output_names = net.output_names() |
| 31 | assert len(input_names) > 0 and len(output_names) > 0 |
| 32 | |
| 33 | in_mat = ncnn.Mat((227, 227, 3)) |
| 34 | |
| 35 | with net.create_extractor() as ex: |
| 36 | ex.input("data", in_mat) |
| 37 | ret, out_mat = ex.extract("output") |
| 38 | |
| 39 | assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 |
| 40 | |
| 41 | net.clear() |
| 42 | assert len(net.blobs()) == 0 and len(net.layers()) == 0 |
| 43 | |
| 44 | |
| 45 | def test_net_mem(): |
| 46 | modelbin = bytearray(303940) |
| 47 | modelbin[0:4] = 71,107,48,1 |
| 48 | modelbin[180:184] = 71,107,48,1 |
| 49 | |
| 50 | with ncnn.Net() as net: |
| 51 | ret = net.load_param("tests/test.param") |
| 52 | net.load_model_mem(bytes(modelbin)) |
| 53 | assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3 |
| 54 | |
| 55 | input_names = net.input_names() |
| 56 | output_names = net.output_names() |
| 57 | assert len(input_names) > 0 and len(output_names) > 0 |
| 58 | |
| 59 | in_mat = ncnn.Mat((227, 227, 3)) |
| 60 | |
| 61 | with net.create_extractor() as ex: |
| 62 | ex.input("data", in_mat) |
| 63 | ret, out_mat = ex.extract("output") |
| 64 | |
| 65 | assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 |
| 66 | |
| 67 | net.clear() |
| 68 | assert len(net.blobs()) == 0 and len(net.layers()) == 0 |
| 69 | |
| 70 | |
| 71 | def test_net_vulkan(): |
| 72 | if not hasattr(ncnn, "get_gpu_count"): |
| 73 | return |
| 74 | |
| 75 | dr = ncnn.DataReaderFromEmpty() |
| 76 | |
| 77 | net = ncnn.Net() |
| 78 | net.opt.use_vulkan_compute = True |
| 79 | ret = net.load_param("tests/test.param") |
| 80 | net.load_model(dr) |
| 81 | assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3 |
| 82 | |
| 83 | in_mat = ncnn.Mat((227, 227, 3)) |
| 84 | |
| 85 | ex = net.create_extractor() |
| 86 | ex.input("data", in_mat) |
| 87 | ret, out_mat = ex.extract("output") |
| 88 | |
| 89 | assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 |
| 90 | |
| 91 | ex.clear() |
| 92 | |
| 93 | net.clear() |
| 94 | assert len(net.blobs()) == 0 and len(net.layers()) == 0 |
| 95 | |
| 96 | |
| 97 | def test_custom_layer(): |
| 98 | class CustomLayer(ncnn.Layer): |
| 99 | customLayers = [] |
| 100 | |
| 101 | def __init__(self): |
| 102 | ncnn.Layer.__init__(self) |
| 103 | self.one_blob_only = True |
| 104 | |
| 105 | self.customLayers.append(self) |
| 106 | |
| 107 | def forward(self, bottom_blob, top_blob, opt): |
| 108 | x = np.array(bottom_blob) |
| 109 | x += 1 |
| 110 | |
| 111 | top_blob.clone_from(ncnn.Mat(x), opt.blob_allocator) |
| 112 | if top_blob.empty(): |
| 113 | return -100 |
| 114 | |
| 115 | return 0 |
| 116 | |
| 117 | def CustomLayer_layer_creator(): |
| 118 | return CustomLayer() |
| 119 | |
| 120 | def CustomLayer_layer_destroyer(layer): |
| 121 | for i in range(len(CustomLayer.customLayers)): |
| 122 | if CustomLayer.customLayers[i] == layer: |
| 123 | del CustomLayer.customLayers[i] |
| 124 | break |
| 125 | |
| 126 | dr = ncnn.DataReaderFromEmpty() |
| 127 | |
| 128 | net = ncnn.Net() |
| 129 | net.register_custom_layer( |
| 130 | "CustomLayer", CustomLayer_layer_creator, CustomLayer_layer_destroyer |
| 131 | ) |
| 132 | ret = net.load_param("tests/custom_layer.param") |
| 133 | net.load_model(dr) |
| 134 | assert ret == 0 and len(net.blobs()) == 2 and len(net.layers()) == 2 |
| 135 | |
| 136 | in_mat = ncnn.Mat(1) |
| 137 | in_mat.fill(1.0) |
| 138 | |
| 139 | ex = net.create_extractor() |
| 140 | ex.input("data", in_mat) |
| 141 | ret, out_mat = ex.extract("output") |
| 142 | assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 and out_mat[0] == 2.0 |
| 143 | |
| 144 | ex.clear() |
| 145 | |
| 146 | net.clear() |
| 147 | assert len(net.blobs()) == 0 and len(net.layers()) == 0 |
| 148 | |
| 149 | |
| 150 | def test_vulkan_device_index(): |
| 151 | if not hasattr(ncnn, "get_gpu_count"): |
| 152 | return |
| 153 | |
| 154 | net = ncnn.Net() |
| 155 | assert net.vulkan_device() is None |
| 156 | |
| 157 | net.set_vulkan_device(0) |
| 158 | assert net.vulkan_device() is not None |
| 159 | |
| 160 | |
| 161 | def test_vulkan_device_vkdev(): |
| 162 | if not hasattr(ncnn, "get_gpu_count"): |
| 163 | return |
| 164 | |
| 165 | net = ncnn.Net() |
| 166 | assert net.vulkan_device() is None |
| 167 | |
| 168 | vkdev = ncnn.get_gpu_device(0) |
| 169 | net.set_vulkan_device(vkdev) |
| 170 | assert net.vulkan_device() is not None |