예제 #1
0
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
예제 #2
0
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
예제 #3
0
    def __call__(self, img):
        img_h = img.shape[0]
        img_w = img.shape[1]

        mat_in = ncnn.Mat.from_pixels_resize(img, ncnn.Mat.PixelType.PIXEL_BGR,
                                             img.shape[1], img.shape[0],
                                             self.target_size,
                                             self.target_size)
        mat_in.substract_mean_normalize(self.mean_vals, self.norm_vals)

        ex = self.net.create_extractor()
        ex.set_num_threads(self.num_threads)

        ex.input("data", mat_in)

        mat_out = ncnn.Mat()
        ex.extract("detection_out", mat_out)

        objects = []

        #printf("%d %d %d\n", mat_out.w, mat_out.h, mat_out.c)

        #method 1, use ncnn.Mat.row to get the result, no memory copy
        for i in range(mat_out.h):
            values = mat_out.row(i)

            obj = Detect_Object()
            obj.label = values[0]
            obj.prob = values[1]
            obj.rect.x = values[2] * img_w
            obj.rect.y = values[3] * img_h
            obj.rect.w = values[4] * img_w - obj.rect.x
            obj.rect.h = values[5] * img_h - obj.rect.y

            objects.append(obj)
        '''
        #method 2, use ncnn.Mat->numpy.array to get the result, no memory copy too
        out = np.array(mat_out)
        for i in range(len(out)):
            values = out[i]
            obj = Detect_Object()
            obj.label = values[0]
            obj.prob = values[1]
            obj.rect.x = values[2] * img_w
            obj.rect.y = values[3] * img_h
            obj.rect.w = values[4] * img_w - obj.rect.x
            obj.rect.h = values[5] * img_h - obj.rect.y
            objects.append(obj)
        '''

        seg_out = ncnn.Mat()
        ex.extract("sigmoid", seg_out)

        resized = ncnn.Mat()
        ncnn.resize_bilinear(seg_out, resized, img_w, img_h)

        return objects, resized
예제 #4
0
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
예제 #5
0
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
예제 #6
0
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)
예제 #7
0
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)
예제 #8
0
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)
예제 #9
0
def test_paramdict():
    pd = ncnn.ParamDict()
    assert pd.type(0) == 0
    assert pd.get(0, -1) == -1

    pd.set(1, 1)
    assert pd.type(1) == 2 and pd.get(1, -1) == 1

    pd.set(2, 2.0)
    assert pd.type(2) == 3 and pd.get(2, -2.0) == 2.0

    mat = ncnn.Mat(1)
    pd.set(3, mat)
    assert pd.type(3) == 4 and pd.get(3, ncnn.Mat()).dims == mat.dims
예제 #10
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
예제 #11
0
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()
예제 #12
0
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
예제 #13
0
    def generate_anchors(self, base_size, ratios, scales):
        num_ratio = ratios.w
        num_scale = scales.w

        #anchors = ncnn.Mat()
        #anchors.create(w=4, h=num_ratio * num_scale)

        anchors_np = np.zeros((2, 4), dtype=np.float32)

        cx = base_size * 0.5
        cy = base_size * 0.5

        for i in range(num_ratio):
            ar = ratios[i]

            r_w = np.round(base_size / np.sqrt(ar))
            r_h = np.round(r_w * ar)  # round(base_size * np.sqrt(ar))

            for j in range(num_scale):
                scale = scales[j]

                rs_w = r_w * scale
                rs_h = r_h * scale

                anchor = anchors_np[i * num_scale + j]

                anchor[0] = cx - rs_w * 0.5
                anchor[1] = cy - rs_h * 0.5
                anchor[2] = cx + rs_w * 0.5
                anchor[3] = cy + rs_h * 0.5

        anchors = ncnn.Mat(anchors_np)
        return anchors
예제 #14
0
    def __call__(self, img):
        img_h = img.shape[0]
        img_w = img.shape[1]

        mat_in = ncnn.Mat.from_pixels_resize(img, ncnn.Mat.PixelType.PIXEL_BGR,
                                             img.shape[1], img.shape[0],
                                             self.target_size,
                                             self.target_size)
        mat_in.substract_mean_normalize(self.mean_vals, self.norm_vals)

        ex = self.net.create_extractor()
        ex.set_num_threads(self.num_threads)

        ex.input("data", mat_in)

        mat_out = ncnn.Mat()
        ex.extract("fc", mat_out)

        # manually call softmax on the fc output
        # convert result into probability
        # skip if your model already has softmax operation
        softmax = ncnn.create_layer("Softmax")

        pd = ncnn.ParamDict()
        softmax.load_param(pd)

        softmax.forward_inplace(mat_out, self.net.opt)

        mat_out = mat_out.reshape(mat_out.w * mat_out.h * mat_out.c)

        cls_scores = np.array(mat_out)
        return cls_scores
예제 #15
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
예제 #16
0
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
예제 #17
0
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((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)
예제 #18
0
def test_numpy():
    mat = ncnn.Mat(1)
    array = np.array(mat)
    assert mat.dims == array.ndim and mat.w == array.shape[0]
    mat = ncnn.Mat(2, 3)
    array = np.array(mat)
    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 = np.array(mat)
    assert array.dtype == np.int8
    mat = ncnn.Mat(1, elemsize=2)
    array = np.array(mat)
    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 = np.array(mat)
    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()
예제 #19
0
파일: test_net.py 프로젝트: zyzlimit/ncnn
        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
예제 #20
0
    def detect_stride16(self, ex):
        score_blob, bbox_blob, landmark_blob = ncnn.Mat(), ncnn.Mat(), ncnn.Mat()
        ex.extract("face_rpn_cls_prob_reshape_stride16", score_blob)
        ex.extract("face_rpn_bbox_pred_stride16", bbox_blob)
        ex.extract("face_rpn_landmark_pred_stride16", landmark_blob)

        base_size = 16
        feat_stride = 16
        ratios = ncnn.Mat(1)
        ratios[0] = 1.0
        scales = ncnn.Mat(2)
        scales[0] = 8.0
        scales[1] = 4.0
        anchors = self.generate_anchors(base_size, ratios, scales)

        faceobjects16 = self.generate_proposals(anchors, feat_stride, score_blob, bbox_blob, landmark_blob, self.prob_threshold)

        return faceobjects16
예제 #21
0
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)
예제 #22
0
파일: test_mat.py 프로젝트: yueyedeai/ncnn
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, elemsize=4)
    assert (mat.dims == 3 and mat.w == 7 and mat.h == 8 and mat.c == 9
            and mat.elemsize == 4)
    mat = ncnn.Mat((10, 11, 12), elemsize=4, elempack=1)
    assert (mat.dims == 3 and mat.w == 10 and mat.h == 11 and mat.c == 12
            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)
예제 #23
0
def test_addref_release():
    mat = ncnn.Mat(1)
    assert mat.refcount == 1

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

    mat.release()
    assert mat.refcount == None
예제 #24
0
def benchmark(comment, _in, opt):
    _in.fill(0.01)

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

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

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

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

    g_blob_pool_allocator.clear()
    g_workspace_pool_allocator.clear()

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

    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))
예제 #25
0
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
예제 #26
0
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

    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

    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

    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

    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

    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

    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
    with pytest.raises(RuntimeError) as execinfo:
        mat1.reshape((1, 1, 1, 1))
    assert "shape must be 1, 2 or 3 dims, not 4" in str(execinfo.value)
예제 #27
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
예제 #28
0
파일: yolov5.py 프로젝트: zzzzt634/ncnn
    def forward(self, bottom_blob, top_blob, opt):
        x = np.array(bottom_blob)
        x = np.concatenate([
            x[..., ::2, ::2],
            x[..., 1::2, ::2],
            x[..., ::2, 1::2],
            x[..., 1::2, 1::2],
        ])

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

        return 0
예제 #29
0
def test_blob():
    blob = ncnn.Blob()

    blob.name = "myblob"
    assert blob.name == "myblob"

    blob.producer = 0
    assert blob.producer == 0

    blob.consumer = 0
    assert blob.consumer == 0

    blob.shape = ncnn.Mat(1)
    assert blob.shape.dims == 1 and blob.shape.w == 1
예제 #30
0
    def infer_image(self, img_input):
        import ncnn
        mat_in = ncnn.Mat(img_input.squeeze())
        ex = self.net.create_extractor()
        ex.input(self.input_name, mat_in)

        score_out_name = ["792", "814", "836"]
        scores = [np.array(ex.extract(x)[1]) for x in score_out_name]
        scores = [np.reshape(x, (-1, 80)) for x in scores]

        boxes_out_name = ["795", "817", "839"]
        raw_boxes = [np.array(ex.extract(x)[1]) for x in boxes_out_name]
        raw_boxes = [np.reshape(x, (-1, 32)) for x in raw_boxes]

        return scores, raw_boxes