Elffuss BraidHub

kikocis/ncnn public

e5ca598b9f4c · 3.9 KiBRawPermalink
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
15import numpy as np
16import pytest
17
18import ncnn
19
20
21def 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
45def test_net_vulkan():
46 if not hasattr(ncnn, "get_gpu_count"):
47 return
48
49 dr = ncnn.DataReaderFromEmpty()
50
51 net = ncnn.Net()
52 net.opt.use_vulkan_compute = True
53 ret = net.load_param("tests/test.param")
54 net.load_model(dr)
55 assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3
56
57 in_mat = ncnn.Mat((227, 227, 3))
58
59 ex = net.create_extractor()
60 ex.input("data", in_mat)
61 ret, out_mat = ex.extract("output")
62
63 assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
64
65 ex.clear()
66
67 net.clear()
68 assert len(net.blobs()) == 0 and len(net.layers()) == 0
69
70
71def test_custom_layer():
72 class CustomLayer(ncnn.Layer):
73 customLayers = []
74
75 def __init__(self):
76 ncnn.Layer.__init__(self)
77 self.one_blob_only = True
78
79 self.customLayers.append(self)
80
81 def forward(self, bottom_blob, top_blob, opt):
82 x = np.array(bottom_blob)
83 x += 1
84
85 top_blob.clone_from(ncnn.Mat(x), opt.blob_allocator)
86 if top_blob.empty():
87 return -100
88
89 return 0
90
91 def CustomLayer_layer_creator():
92 return CustomLayer()
93
94 def CustomLayer_layer_destroyer(layer):
95 for i in range(len(CustomLayer.customLayers)):
96 if CustomLayer.customLayers[i] == layer:
97 del CustomLayer.customLayers[i]
98 break
99
100 dr = ncnn.DataReaderFromEmpty()
101
102 net = ncnn.Net()
103 net.register_custom_layer(
104 "CustomLayer", CustomLayer_layer_creator, CustomLayer_layer_destroyer
105 )
106 ret = net.load_param("tests/custom_layer.param")
107 net.load_model(dr)
108 assert ret == 0 and len(net.blobs()) == 2 and len(net.layers()) == 2
109
110 in_mat = ncnn.Mat(1)
111 in_mat.fill(1.0)
112
113 ex = net.create_extractor()
114 ex.input("data", in_mat)
115 ret, out_mat = ex.extract("output")
116 assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 and out_mat[0] == 2.0
117
118 ex.clear()
119
120 net.clear()
121 assert len(net.blobs()) == 0 and len(net.layers()) == 0
122
123
124def test_vulkan_device_index():
125 if not hasattr(ncnn, "get_gpu_count"):
126 return
127
128 net = ncnn.Net()
129 assert net.vulkan_device() is None
130
131 net.set_vulkan_device(0)
132 assert net.vulkan_device() is not None
133
134
135def test_vulkan_device_vkdev():
136 if not hasattr(ncnn, "get_gpu_count"):
137 return
138
139 net = ncnn.Net()
140 assert net.vulkan_device() is None
141
142 vkdev = ncnn.get_gpu_device(0)
143 net.set_vulkan_device(vkdev)
144 assert net.vulkan_device() is not None