Elffuss BraidHub

kikocis/ncnn public

master · 3.4 KiBRawPermalink
1/* Tencent is pleased to support the open source community by making ncnn available.
2 *
3 * Copyright (C) 2020 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
16#ifndef PYBIND11_NCNN_MAT_H
17#define PYBIND11_NCNN_MAT_H
18
19#include <string>
20
21#include <pybind11/pybind11.h>
22
23#include <mat.h>
24
25namespace py = pybind11;
26
27std::string get_mat_format(const ncnn::Mat& m)
28{
29 std::string format;
30 if (m.elemsize == 4)
31 {
32 format = pybind11::format_descriptor<float>::format();
33 }
34 if (m.elemsize == 2)
35 {
36 // see https://docs.python.org/3/library/struct.html#format-characters
37 format = "e";
38 }
39 if (m.elemsize == 1)
40 {
41 format = pybind11::format_descriptor<int8_t>::format();
42 }
43 return format;
44}
45
46// possible values for format:
47// i (int32_t)
48// f (float)
49// d (double)
50// leave it to empty to use get_mat_format
51py::buffer_info to_buffer_info(ncnn::Mat& m, const std::string& format = "")
52{
53 if (m.elemsize != 1 && m.elemsize != 2 && m.elemsize != 4)
54 {
55 std::ostringstream ss;
56 ss << "Convert ncnn.Mat to numpy.ndarray. Support only elemsize 1, 2, 4; but given "
57 << m.elemsize;
58 py::pybind11_fail(ss.str());
59 }
60 if (m.elempack != 1)
61 {
62 std::ostringstream ss;
63 ss << "Convert ncnn.Mat to numpy.ndarray. Support only elempack == 1, but "
64 "given "
65 << m.elempack;
66 py::pybind11_fail(ss.str());
67 }
68 std::string _format(format);
69 if (_format.empty())
70 {
71 _format = get_mat_format(m);
72 }
73 std::vector<py::ssize_t> shape;
74 std::vector<py::ssize_t> strides;
75 if (m.dims == 1)
76 {
77 shape.push_back(m.w);
78 strides.push_back(m.elemsize);
79 }
80 else if (m.dims == 2)
81 {
82 shape.push_back(m.h);
83 shape.push_back(m.w);
84 strides.push_back(m.w * m.elemsize);
85 strides.push_back(m.elemsize);
86 }
87 else if (m.dims == 3)
88 {
89 shape.push_back(m.c);
90 shape.push_back(m.h);
91 shape.push_back(m.w);
92 strides.push_back(m.cstep * m.elemsize);
93 strides.push_back(m.w * m.elemsize);
94 strides.push_back(m.elemsize);
95 }
96 else if (m.dims == 4)
97 {
98 shape.push_back(m.c);
99 shape.push_back(m.d);
100 shape.push_back(m.h);
101 shape.push_back(m.w);
102 strides.push_back(m.cstep * m.elemsize);
103 strides.push_back(m.w * m.h * m.elemsize);
104 strides.push_back(m.w * m.elemsize);
105 strides.push_back(m.elemsize);
106 }
107 return py::buffer_info(m.data, /* Pointer to buffer */
108 m.elemsize, /* Size of one scalar */
109 _format, /* Python struct-style format descriptor */
110 m.dims, /* Number of dimensions */
111 shape, /* Buffer dimensions */
112 strides /* Strides (in bytes) for each index */
113 );
114}
115
116#endif