1 /**
2 * Copyright 2019-2023 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "minddata/dataset/core/data_type.h"
17 #ifdef ENABLE_MINDDATA_PYTHON
18 #include "minddata/dataset/core/pybind_support.h"
19 #endif
20
21 #include "minddata/dataset/util/log_adapter.h"
22
23 namespace mindspore {
24 namespace dataset {
SizeInBytes() const25 uint8_t DataType::SizeInBytes() const {
26 if (type_ < DataType::NUM_OF_TYPES) {
27 return kTypeInfo[type_].sizeInBytes_;
28 } else {
29 return 0;
30 }
31 }
32
33 #ifdef ENABLE_MINDDATA_PYTHON
AsNumpyType() const34 py::dtype DataType::AsNumpyType() const {
35 if (type_ < DataType::NUM_OF_TYPES) {
36 return py::dtype(kTypeInfo[type_].pybindType_);
37 } else {
38 return py::dtype("unknown");
39 }
40 }
41 #endif
42
43 #if !defined(ENABLE_ANDROID) || defined(ENABLE_MINDDATA_PYTHON)
AsCVType() const44 uint8_t DataType::AsCVType() const {
45 uint8_t res = kCVInvalidType;
46 if (type_ < DataType::NUM_OF_TYPES) {
47 res = kTypeInfo[type_].cvType_;
48 }
49
50 if (res == kCVInvalidType) {
51 std::string type_name = "unknown";
52 if (type_ < DataType::NUM_OF_TYPES) {
53 type_name = std::string(kTypeInfo[type_].name_);
54 }
55 std::string err_msg = "Cannot convert [" + type_name + "] to OpenCV type.";
56 err_msg += " Currently unsupported data type: [uint32, int64, uint64, string]";
57 MS_LOG(ERROR) << err_msg;
58 }
59
60 return res;
61 }
62
FromCVType(int cv_type)63 DataType DataType::FromCVType(int cv_type) {
64 auto depth = static_cast<uchar>(cv_type) & static_cast<uchar>(CV_MAT_DEPTH_MASK);
65 switch (depth) {
66 case CV_8S:
67 return DataType(DataType::DE_INT8);
68 case CV_8U:
69 return DataType(DataType::DE_UINT8);
70 case CV_16S:
71 return DataType(DataType::DE_INT16);
72 case CV_16U:
73 return DataType(DataType::DE_UINT16);
74 case CV_32S:
75 return DataType(DataType::DE_INT32);
76 case CV_16F:
77 return DataType(DataType::DE_FLOAT16);
78 case CV_32F:
79 return DataType(DataType::DE_FLOAT32);
80 case CV_64F:
81 return DataType(DataType::DE_FLOAT64);
82 default:
83 std::string err_msg = "Cannot convert from OpenCV type, unknown CV type.";
84 err_msg += " Currently supported data type: [int8, uint8, int16, uint16, int32, float16, float32, float64]";
85 MS_LOG(ERROR) << err_msg;
86 return DataType(DataType::DE_UNKNOWN);
87 }
88 }
89 #endif
90
DataType(const std::string & type_str)91 DataType::DataType(const std::string &type_str) {
92 if (type_str == "bool") {
93 type_ = DE_BOOL;
94 } else if (type_str == "int8") {
95 type_ = DE_INT8;
96 } else if (type_str == "uint8") {
97 type_ = DE_UINT8;
98 } else if (type_str == "int16") {
99 type_ = DE_INT16;
100 } else if (type_str == "uint16") {
101 type_ = DE_UINT16;
102 } else if (type_str == "int32") {
103 type_ = DE_INT32;
104 } else if (type_str == "uint32") {
105 type_ = DE_UINT32;
106 } else if (type_str == "int64") {
107 type_ = DE_INT64;
108 } else if (type_str == "uint64") {
109 type_ = DE_UINT64;
110 } else if (type_str == "float16") {
111 type_ = DE_FLOAT16;
112 } else if (type_str == "float32") {
113 type_ = DE_FLOAT32;
114 } else if (type_str == "float64") {
115 type_ = DE_FLOAT64;
116 } else if (type_str == "string") {
117 type_ = DE_STRING;
118 } else if (type_str == "bytes") {
119 type_ = DE_BYTES;
120 #ifdef ENABLE_MINDDATA_PYTHON
121 } else if (type_str == "python") {
122 type_ = DE_PYTHON;
123 #endif
124 } else {
125 type_ = DE_UNKNOWN;
126 }
127 }
128
ToString() const129 std::string DataType::ToString() const {
130 if (type_ < DataType::NUM_OF_TYPES) {
131 return kTypeInfo[type_].name_;
132 } else {
133 return "unknown";
134 }
135 }
136
137 #ifdef ENABLE_MINDDATA_PYTHON
FromNpArray(const py::array & arr)138 DataType DataType::FromNpArray(const py::array &arr) {
139 if (py::isinstance<py::array_t<bool>>(arr)) {
140 return DataType(DataType::DE_BOOL);
141 } else if (py::isinstance<py::array_t<std::int8_t>>(arr)) {
142 return DataType(DataType::DE_INT8);
143 } else if (py::isinstance<py::array_t<std::uint8_t>>(arr)) {
144 return DataType(DataType::DE_UINT8);
145 } else if (py::isinstance<py::array_t<std::int16_t>>(arr)) {
146 return DataType(DataType::DE_INT16);
147 } else if (py::isinstance<py::array_t<std::uint16_t>>(arr)) {
148 return DataType(DataType::DE_UINT16);
149 } else if (py::isinstance<py::array_t<std::int32_t>>(arr)) {
150 return DataType(DataType::DE_INT32);
151 } else if (py::isinstance<py::array_t<std::uint32_t>>(arr)) {
152 return DataType(DataType::DE_UINT32);
153 } else if (py::isinstance<py::array_t<std::int64_t>>(arr)) {
154 return DataType(DataType::DE_INT64);
155 } else if (py::isinstance<py::array_t<std::uint64_t>>(arr)) {
156 return DataType(DataType::DE_UINT64);
157 } else if (py::isinstance<py::array_t<float16>>(arr)) {
158 return DataType(DataType::DE_FLOAT16);
159 } else if (py::isinstance<py::array_t<std::float_t>>(arr)) {
160 return DataType(DataType::DE_FLOAT32);
161 } else if (py::isinstance<py::array_t<std::double_t>>(arr)) {
162 return DataType(DataType::DE_FLOAT64);
163 } else if (arr.dtype().kind() == 'U') {
164 return DataType(DataType::DE_STRING);
165 } else if (arr.dtype().kind() == 'S') {
166 return DataType(DataType::DE_BYTES);
167 } else {
168 if (arr.size() == 0) {
169 MS_LOG(ERROR) << "Please check input data, the data of numpy array is empty.";
170 }
171 std::string err_msg = "Cannot convert from numpy type. Unknown data type is returned!";
172 err_msg +=
173 " Currently supported data type: [int8, uint8, int16, uint16, int32, uint32, int64, uint64, float16, float32, "
174 "float64, string, bytes]";
175 MS_LOG(ERROR) << err_msg;
176 return DataType(DataType::DE_UNKNOWN);
177 }
178 }
179
GetPybindFormat() const180 std::string DataType::GetPybindFormat() const {
181 std::string res;
182 if (type_ < DataType::NUM_OF_TYPES) {
183 res = kTypeInfo[type_].pybindFormatDescriptor_;
184 }
185
186 if (res.empty()) {
187 MS_LOG(ERROR) << "Cannot convert from data type to pybind format descriptor!";
188 }
189 return res;
190 }
191 #endif
192 } // namespace dataset
193 } // namespace mindspore
194