-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python type mismatch #1082
Python type mismatch #1082
Changes from 28 commits
3784788
1a4b8ba
32b8ea1
5d8eeeb
59202d4
5f4dba6
534b122
3d08053
d29b922
c1e3cde
69b8c0a
1a4c1e3
584154f
9ec071c
18c1ecf
a4cb609
48db165
98d4afc
cec87c4
b330336
6dd14ac
de1fb34
945f0d6
aea2f7a
0e3daa6
3eb3d25
d1d8125
f275e55
b062bd7
62dea76
f03c5f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,7 +198,7 @@ class NNData : public Buffer { | |
*/ | ||
span<std::uint8_t> emplaceTensor(TensorInfo& tensor); | ||
|
||
#ifdef DEPTHAI_XTENSOR_SUPPORT | ||
//#ifdef DEPTHAI_XTENSOR_SUPPORT | ||
/** | ||
* @brief Add a tensor to this NNData object. | ||
* The provided array is stored as a 1xN tensor where N is the length of the array. | ||
|
@@ -208,8 +208,47 @@ class NNData : public Buffer { | |
* @return NNData&: reference to this object | ||
*/ | ||
template <typename _Ty = double> | ||
NNData& addTensor(const std::string& name, const std::vector<_Ty>& data) { | ||
return addTensor<_Ty>(name, xt::adapt(data, std::vector<size_t>{1, data.size()})); | ||
NNData& addTensor(const std::string& name, const std::vector<_Ty>& data, dai::TensorInfo::DataType dataType) { | ||
return addTensor<_Ty>(name, xt::adapt(data, std::vector<size_t>{1, data.size()}), dataType); | ||
}; | ||
// addTensor vector overloads | ||
NNData& addTensor(const std::string& name, const std::vector<int>& tensor) { | ||
return addTensor<int>(name, tensor, dai::TensorInfo::DataType::INT); | ||
}; | ||
NNData& addTensor(const std::string& name, const std::vector<uint16_t>& tensor) { | ||
return addTensor<uint16_t>(name, tensor, dai::TensorInfo::DataType::FP16); | ||
}; | ||
NNData& addTensor(const std::string& name, const std::vector<float>& tensor) { | ||
return addTensor<float>(name, tensor, dai::TensorInfo::DataType::FP32); | ||
}; | ||
NNData& addTensor(const std::string& name, const std::vector<double>& tensor) { | ||
return addTensor<double>(name, tensor, dai::TensorInfo::DataType::FP64); | ||
}; | ||
NNData& addTensor(const std::string& name, const std::vector<std::int8_t>& tensor) { | ||
return addTensor<std::int8_t>(name, tensor, dai::TensorInfo::DataType::I8); | ||
}; | ||
NNData& addTensor(const std::string& name, const std::vector<std::uint8_t>& tensor) { | ||
return addTensor<std::uint8_t>(name, tensor, dai::TensorInfo::DataType::U8F); | ||
}; | ||
|
||
// addTensor overloads | ||
NNData& addTensor(const std::string& name, const xt::xarray<int>& tensor) { | ||
return addTensor<int>(name, tensor, dai::TensorInfo::DataType::INT); | ||
}; | ||
NNData& addTensor(const std::string& name, const xt::xarray<uint16_t>& tensor) { | ||
return addTensor<uint16_t>(name, tensor, dai::TensorInfo::DataType::FP16); | ||
}; | ||
NNData& addTensor(const std::string& name, const xt::xarray<float>& tensor) { | ||
return addTensor<float>(name, tensor, dai::TensorInfo::DataType::FP32); | ||
}; | ||
NNData& addTensor(const std::string& name, const xt::xarray<double>& tensor) { | ||
return addTensor<double>(name, tensor, dai::TensorInfo::DataType::FP64); | ||
}; | ||
NNData& addTensor(const std::string& name, const xt::xarray<std::int8_t>& tensor) { | ||
return addTensor<std::int8_t>(name, tensor, dai::TensorInfo::DataType::I8); | ||
}; | ||
NNData& addTensor(const std::string& name, const xt::xarray<std::uint8_t>& tensor) { | ||
return addTensor<std::uint8_t>(name, tensor, dai::TensorInfo::DataType::U8F); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add tests for these too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
|
||
/** | ||
|
@@ -226,6 +265,22 @@ class NNData : public Buffer { | |
return addTensor<_Ty>(name, xt::adapt(data, std::vector<size_t>{1, data.size()}), order); | ||
}; | ||
|
||
/** | ||
* @brief Add a tensor to this NNData object. | ||
* Implicitly adds a TensorInfo::DataType | ||
* | ||
* @param name: Name of the tensor | ||
* @param data: array | ||
* @param order: Storage order of the tensor | ||
* @return NNData&: reference to this object | ||
*/ | ||
|
||
template <typename _Ty = double> | ||
NNData& addTensor(const std::string& name, const xt::xarray<_Ty>& data, TensorInfo::StorageOrder order) { | ||
auto dataType = std::is_integral<_Ty>::value ? dai::TensorInfo::DataType::U8F : dai::TensorInfo::DataType::FP16; | ||
return addTensor<_Ty>(name, data, dataType, order); | ||
}; | ||
|
||
/** | ||
* @brief Add a tensor to this NNData object. The storage order is picked based on the number of dimensions of the tensor. | ||
* Float values are converted to FP16 and integers are cast to bytes. | ||
|
@@ -235,7 +290,7 @@ class NNData : public Buffer { | |
* @return NNData&: reference to this object | ||
*/ | ||
template <typename _Ty = double> | ||
NNData& addTensor(const std::string& name, const xt::xarray<_Ty>& tensor) { | ||
NNData& addTensor(const std::string& name, const xt::xarray<_Ty>& tensor, dai::TensorInfo::DataType dataType) { | ||
TensorInfo::StorageOrder order; | ||
switch(tensor.shape().size()) { | ||
case 1: | ||
|
@@ -253,7 +308,7 @@ class NNData : public Buffer { | |
default: | ||
throw std::runtime_error("Unsupported tensor shape. Only 1D, 2D, 3D and 4D tensors are supported"); | ||
} | ||
return addTensor(name, tensor, order); | ||
return addTensor(name, tensor, dataType, order); | ||
} | ||
|
||
/** | ||
|
@@ -266,8 +321,15 @@ class NNData : public Buffer { | |
* @return NNData&: reference to this object | ||
*/ | ||
template <typename _Ty = double> | ||
NNData& addTensor(const std::string& name, const xt::xarray<_Ty>& tensor, const TensorInfo::StorageOrder order) { | ||
NNData& addTensor(const std::string& name, const xt::xarray<_Ty>& tensor, dai::TensorInfo::DataType dataType, const TensorInfo::StorageOrder order) { | ||
static_assert(std::is_integral<_Ty>::value || std::is_floating_point<_Ty>::value, "Tensor type needs to be integral or floating point"); | ||
//if(dataType==dai::TensorInfo::DataType::FP32) std::cout<<"FP32\n"; | ||
//else if(dataType==dai::TensorInfo::DataType::FP16) std::cout<<"FP16\n"; | ||
//else if(dataType==dai::TensorInfo::DataType::INT) std::cout<<"INT\n"; | ||
//else if(dataType==dai::TensorInfo::DataType::I8) std::cout<<"I8\n"; | ||
//else if(dataType==dai::TensorInfo::DataType::U8F) std::cout<<"U8F\n"; | ||
//else if(dataType==dai::TensorInfo::DataType::FP64) std::cout<<"FP64\n"; | ||
//else std::cout<<"Unsupported type\n"; | ||
|
||
// Check if data is vector type of data | ||
if(std::dynamic_pointer_cast<VectorMemory>(data) == nullptr) { | ||
|
@@ -277,7 +339,23 @@ class NNData : public Buffer { | |
auto vecData = std::dynamic_pointer_cast<VectorMemory>(data); | ||
|
||
// Get size in bytes of the converted tensor data, u8 for integral and fp16 for floating point | ||
const size_t sConvertedData = std::is_integral<_Ty>::value ? tensor.size() : 2 * tensor.size(); | ||
//const size_t sConvertedData = std::is_integral<_Ty>::value ? tensor.size() : 2 * tensor.size(); | ||
size_t sConvertedData = tensor.size(); | ||
switch(dataType){ | ||
case dai::TensorInfo::DataType::FP64: | ||
sConvertedData *= 8; | ||
break; | ||
case dai::TensorInfo::DataType::FP32: | ||
case dai::TensorInfo::DataType::INT: | ||
sConvertedData *= 4; | ||
break; | ||
case dai::TensorInfo::DataType::FP16: | ||
sConvertedData *= 2; | ||
break; | ||
case dai::TensorInfo::DataType::U8F: | ||
case dai::TensorInfo::DataType::I8: | ||
break; | ||
} | ||
|
||
// Append bytes so that each new tensor is DATA_ALIGNMENT aligned | ||
size_t remainder = std::distance(vecData->begin(), vecData->end()) % DATA_ALIGNMENT; | ||
|
@@ -291,22 +369,38 @@ class NNData : public Buffer { | |
// Reserve space | ||
vecData->resize(offset + sConvertedData); | ||
|
||
// Convert data to u8 or fp16 and write to data | ||
if(std::is_integral<_Ty>::value) { | ||
// Convert data to appropriate data type and write to data | ||
if(dataType == dai::TensorInfo::DataType::I8) { | ||
for(uint32_t i = 0; i < tensor.size(); i++) { | ||
vecData->data()[i + offset] = (uint8_t)tensor.data()[i]; | ||
vecData->data()[i + offset] = (int8_t)tensor.data()[i]; | ||
} | ||
} else { | ||
} else if(dataType == dai::TensorInfo::DataType::FP16) { | ||
for(uint32_t i = 0; i < tensor.size(); i++) { | ||
*(uint16_t*)(&vecData->data()[2 * i + offset]) = fp32_to_fp16(tensor.data()[i]); | ||
} | ||
} else if(dataType == dai::TensorInfo::DataType::FP32){ | ||
for(uint32_t i = 0; i < tensor.size(); i++) { | ||
*(float*)(&vecData->data()[4 * i + offset]) = tensor.data()[i]; | ||
} | ||
} else if(dataType == dai::TensorInfo::DataType::INT){ | ||
for(uint32_t i = 0; i < tensor.size(); i++) { | ||
*(int32_t*)(&vecData->data()[4 * i + offset]) = tensor.data()[i]; | ||
} | ||
} else if(dataType == dai::TensorInfo::DataType::U8F) { | ||
for(uint32_t i = 0; i < tensor.size(); i++) { | ||
vecData->data()[i + offset] = (uint8_t)tensor.data()[i]; | ||
} | ||
}else if(dataType == dai::TensorInfo::DataType::FP64){ | ||
for(uint32_t i = 0; i < tensor.size(); i++) { | ||
*(double*)(&vecData->data()[8 * i + offset]) = tensor.data()[i]; | ||
} | ||
} | ||
|
||
// Add entry in tensors | ||
TensorInfo info; | ||
info.name = name; | ||
info.offset = static_cast<unsigned int>(offset); | ||
info.dataType = std::is_integral<_Ty>::value ? TensorInfo::DataType::U8F : TensorInfo::DataType::FP16; | ||
info.dataType = dataType; | ||
info.numDimensions = tensor.dimension(); | ||
info.order = order; | ||
for(uint32_t i = 0; i < tensor.dimension(); i++) { | ||
|
@@ -374,6 +468,11 @@ class NNData : public Buffer { | |
tensor.data()[i] = reinterpret_cast<float_t*>(data->getData().data())[it->offset / sizeof(float_t) + i]; | ||
} | ||
break; | ||
case TensorInfo::DataType::FP64: | ||
for(uint32_t i = 0; i < tensor.size(); i++) { | ||
tensor.data()[i] = reinterpret_cast<double_t*>(data->getData().data())[it->offset / sizeof(double_t) + i]; | ||
} | ||
break; | ||
} | ||
if(dequantize) { | ||
if(it->quantization) { | ||
|
@@ -518,7 +617,7 @@ class NNData : public Buffer { | |
|
||
return {}; | ||
} | ||
#endif | ||
//#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ;) |
||
void serialize(std::vector<std::uint8_t>& metadata, DatatypeEnum& datatype) const override { | ||
metadata = utility::serialize(*this); | ||
datatype = DatatypeEnum::NNData; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is still needed?