コード例 #1
0
def test_extractor_index():
    with pytest.raises(TypeError) as execinfo:
        ex = ncnn.Extractor()
        assert "No constructor" in str(execinfo.value)

    dr = ncnn.DataReaderFromEmpty()

    net = ncnn.Net()
    net.load_param("tests/test.param")
    net.load_model(dr)

    in_mat = ncnn.Mat((227, 227, 3))
    ex = net.create_extractor()
    ex.set_light_mode(True)
    ex.set_num_threads(2)

    ex.set_blob_allocator(alloctor)
    ex.set_workspace_allocator(alloctor)

    ex.input(0, in_mat)
    ret, out_mat = ex.extract(1)
    assert (ret == 0 and out_mat.dims == 3 and out_mat.w == 225
            and out_mat.h == 225 and out_mat.c == 3)

    ret, out_mat = ex.extract(2)
    assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
コード例 #2
0
ファイル: test_extractor.py プロジェクト: zylo117/ncnn
def test_extractor_index():
    with pytest.raises(TypeError, match="No constructor"):
        ex = ncnn.Extractor()

    dr = ncnn.DataReaderFromEmpty()

    net = ncnn.Net()
    net.load_param("tests/test.param")
    net.load_model(dr)

    in_mat = ncnn.Mat((227, 227, 3))
    ex = net.create_extractor()
    ex.set_light_mode(True)
    ex.set_num_threads(2)

    ex.set_blob_allocator(alloctor)
    ex.set_workspace_allocator(alloctor)

    ex.input(0, in_mat)
    ret, out_mat = ex.extract(1)
    assert (ret == 0 and out_mat.dims == 3 and out_mat.w == 225
            and out_mat.h == 225 and out_mat.c == 3)

    ret, out_mat = ex.extract(2)
    assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1

    # not use with sentence, call clear manually to ensure ex destruct before net
    ex.clear()
コード例 #3
0
ファイル: test_net.py プロジェクト: leron-lee/ncnn
def test_net_vulkan():
    if not hasattr(ncnn, "get_gpu_count"):
        return

    dr = ncnn.DataReaderFromEmpty()

    net = ncnn.Net()
    net.opt.use_vulkan_compute = True
    ret = net.load_param("tests/test.param")
    net.load_model(dr)
    assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3

    in_mat = ncnn.Mat((227, 227, 3))

    ex = net.create_extractor()
    ex.input("data", in_mat)
    ret, out_mat = ex.extract("output")

    assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1

    net.clear()
    assert len(net.blobs()) == 0 and len(net.layers()) == 0

    # ensure ex release before net when use vulkan
    ex = None
    net = None
コード例 #4
0
ファイル: test_extractor.py プロジェクト: zylo117/ncnn
def test_extractor():
    with pytest.raises(TypeError, match="No constructor"):
        ex = ncnn.Extractor()

    dr = ncnn.DataReaderFromEmpty()

    net = ncnn.Net()
    net.load_param("tests/test.param")
    net.load_model(dr)

    in_mat = ncnn.Mat((227, 227, 3))
    with net.create_extractor() as ex:
        ex.set_light_mode(True)
        ex.set_num_threads(2)

        ex.set_blob_allocator(alloctor)
        ex.set_workspace_allocator(alloctor)

        ex.input("data", in_mat)
        ret, out_mat = ex.extract("conv0_fwd")
        assert (ret == 0 and out_mat.dims == 3 and out_mat.w == 225
                and out_mat.h == 225 and out_mat.c == 3)

        ret, out_mat = ex.extract("output")
        assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
コード例 #5
0
ファイル: benchmark.py プロジェクト: zylo117/ncnn
def benchmark(comment, _in, opt):
    _in.fill(0.01)

    g_blob_pool_allocator.clear()
    g_workspace_pool_allocator.clear()

    if opt.use_vulkan_compute:
        g_blob_vkallocator.clear()
        g_staging_vkallocator.clear()

    net = ncnn.Net()
    net.opt = opt

    if net.opt.use_vulkan_compute:
        net.set_vulkan_device(g_vkdev)

    net.load_param(param_root + comment + ".param")

    dr = ncnn.DataReaderFromEmpty()
    net.load_model(dr)

    input_names = net.input_names()
    output_names = net.output_names()

    if g_enable_cooling_down:
        time.sleep(10)

    # warm up
    for i in range(g_warmup_loop_count):
        # test with statement
        with net.create_extractor() as ex:
            ex.input(input_names[0], _in)
            ex.extract(output_names[0])

    time_min = sys.float_info.max
    time_max = -sys.float_info.max
    time_avg = 0.0

    for i in range(g_loop_count):
        start = time.time()

        # test net keep alive until ex freed
        ex = net.create_extractor()
        ex.input(input_names[0], _in)
        ex.extract(output_names[0])

        end = time.time()

        timespan = end - start

        time_min = timespan if timespan < time_min else time_min
        time_max = timespan if timespan > time_max else time_max
        time_avg += timespan

    time_avg /= g_loop_count

    print("%20s  min = %7.2f  max = %7.2f  avg = %7.2f" %
          (comment, time_min * 1000, time_max * 1000, time_avg * 1000))
コード例 #6
0
ファイル: benchmark.py プロジェクト: zzzzt634/ncnn
def benchmark(comment, _in, opt):
    _in.fill(0.01)

    g_blob_pool_allocator.clear()
    g_workspace_pool_allocator.clear()

    if opt.use_vulkan_compute:
        g_blob_vkallocator.clear()
        g_staging_vkallocator.clear()

    net = ncnn.Net()
    net.opt = opt

    if net.opt.use_vulkan_compute:
        net.set_vulkan_device(g_vkdev)

    net.load_param(param_root + comment + ".param")

    dr = ncnn.DataReaderFromEmpty()
    net.load_model(dr)

    if g_enable_cooling_down:
        time.sleep(10)

    # warm up
    for i in range(g_warmup_loop_count):
        ex = net.create_extractor()
        ex.input("data", _in)
        ex.extract("output")

    time_min = sys.float_info.max
    time_max = -sys.float_info.max
    time_avg = 0.0

    for i in range(g_loop_count):
        start = time.time()

        ex = net.create_extractor()
        ex.input("data", _in)
        ex.extract("output")

        end = time.time()

        timespan = end - start

        time_min = timespan if timespan < time_min else time_min
        time_max = timespan if timespan > time_max else time_max
        time_avg += timespan

    # extractor need relese manually when build ncnn with vuklan,
    # due to python relese ex after net, but in extractor.destruction use net
    ex = None

    time_avg /= g_loop_count

    print("%20s  min = %7.2f  max = %7.2f  avg = %7.2f" %
          (comment, time_min * 1000, time_max * 1000, time_avg * 1000))
コード例 #7
0
ファイル: test_net.py プロジェクト: zyzlimit/ncnn
def test_custom_layer():
    class CustomLayer(ncnn.Layer):
        customLayers = []

        def __init__(self):
            ncnn.Layer.__init__(self)
            self.one_blob_only = True

            self.customLayers.append(self)

        def forward(self, bottom_blob, top_blob, opt):
            x = np.array(bottom_blob)
            x += 1

            top_blob.clone_from(ncnn.Mat(x), opt.blob_allocator)
            if top_blob.empty():
                return -100

            return 0

    def CustomLayer_layer_creator():
        return CustomLayer()

    def CustomLayer_layer_destroyer(layer):
        for i in range(len(CustomLayer.customLayers)):
            if CustomLayer.customLayers[i] == layer:
                del CustomLayer.customLayers[i]
                break

    dr = ncnn.DataReaderFromEmpty()

    net = ncnn.Net()
    net.register_custom_layer("CustomLayer", CustomLayer_layer_creator,
                              CustomLayer_layer_destroyer)
    ret = net.load_param("tests/custom_layer.param")
    net.load_model(dr)
    assert ret == 0 and len(net.blobs()) == 2 and len(net.layers()) == 2

    in_mat = ncnn.Mat(1)
    in_mat.fill(1.0)

    ex = net.create_extractor()
    ex.input("data", in_mat)
    ret, out_mat = ex.extract("output")
    assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 and out_mat[
        0] == 2.0

    ex.clear()

    net.clear()
    assert len(net.blobs()) == 0 and len(net.layers()) == 0
コード例 #8
0
ファイル: benchmark.py プロジェクト: zyg11/pyncnn
def benchmark(comment, _in, opt):
    _in.fill(0.01)

    net = ncnn.Net()
    net.opt = opt

    net.load_param("params/" + comment + ".param")

    dr = ncnn.DataReaderFromEmpty()
    net.load_model(dr)

    out = ncnn.Mat()

    #warm up
    for i in range(g_warmup_loop_count):
        ex = net.create_extractor()
        ex.input("data", _in)
        ex.extract("output", out)

    time_min = sys.float_info.max
    time_max = -sys.float_info.max
    time_avg = 0.0

    for i in range(g_loop_count):
        start = time.time()

        ex = net.create_extractor()
        ex.input("data", _in)
        ex.extract("output", out)

        end = time.time()

        timespan = end - start

        time_min = timespan if timespan < time_min else time_min
        time_max = timespan if timespan > time_max else time_max
        time_avg += timespan

    # extractor need relese manually when build ncnn with vuklan,
    # due to python relese ex after net, but in extractor.destruction use net
    ex = None

    time_avg /= g_loop_count

    print("%20s  min = %7.2f  max = %7.2f  avg = %7.2f" %
          (comment, time_min * 1000, time_max * 1000, time_avg * 1000))
コード例 #9
0
ファイル: test_net.py プロジェクト: zyzlimit/ncnn
def test_net():
    dr = ncnn.DataReaderFromEmpty()

    with ncnn.Net() as net:
        ret = net.load_param("tests/test.param")
        net.load_model(dr)
        assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3

        in_mat = ncnn.Mat((227, 227, 3))

        with net.create_extractor() as ex:
            ex.input("data", in_mat)
            ret, out_mat = ex.extract("output")

        assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1

        net.clear()
        assert len(net.blobs()) == 0 and len(net.layers()) == 0
コード例 #10
0
# 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 time
import ncnn

dr = ncnn.DataReaderFromEmpty()

net = ncnn.Net()
net.load_param("test.param")
net.load_model(dr)

in_mat = ncnn.Mat((227, 227, 3))

start = time.time()

ex = net.create_extractor()
ex.input("data", in_mat)
ret, out_mat = ex.extract("output")

end = time.time()
print("timespan = ", end - start)