return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
}
- tensor_buffer.size = tensor_info.size;
-
- LOGI("Allocated tensor buffer(size = %zu, data type = %d)", tensor_info.size, tensor_info.data_type);
+ tensor_buffer.size = tensor_info.size * getSizeOf(tensor_info.data_type);
tensor_buffer.owner_is_backend = 0;
tensor_buffer.data_type = tensor_info.data_type;
+ LOGI("Allocated tensor buffer(size = %zu, data type = %d)", tensor_buffer.size, tensor_buffer.data_type);
+
return MEDIA_VISION_ERROR_NONE;
}
IETensorBuffer().swap(_tensorBuffer);
}
+size_t TensorBuffer::getSizeOf(inference_tensor_data_type_e data_type)
+{
+ switch (data_type) {
+ case INFERENCE_TENSOR_DATA_TYPE_FLOAT32:
+ return sizeof(float);
+ case INFERENCE_TENSOR_DATA_TYPE_INT64:
+ case INFERENCE_TENSOR_DATA_TYPE_UINT64:
+ return sizeof(int64_t);
+ case INFERENCE_TENSOR_DATA_TYPE_UINT32:
+ return sizeof(uint32_t);
+ case INFERENCE_TENSOR_DATA_TYPE_UINT16:
+ return sizeof(u_int16_t);
+ default:
+ return 1;
+ }
+}
+
size_t TensorBuffer::size()
{
return _tensorBuffer.size();
template<typename T> int TensorBuffer::convertToFloat(inference_engine_tensor_buffer *tensorBuffer)
{
- float *new_buf = new (std::nothrow) float[tensorBuffer->size];
+ size_t element_cnt = tensorBuffer->size / getSizeOf(tensorBuffer->data_type);
+ float *new_buf = new (std::nothrow) float[element_cnt];
if (new_buf == NULL) {
LOGE("Fail to allocate a new output tensor buffer.");
return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
auto ori_buf = static_cast<T *>(tensorBuffer->buffer);
- for (size_t idx = 0; idx < tensorBuffer->size; idx++)
+ for (size_t idx = 0; idx < element_cnt; idx++)
new_buf[idx] = static_cast<float>(ori_buf[idx]) / 255.0f;
// replace original buffer with new one, and release origin one.
tensorBuffer->data_type = INFERENCE_TENSOR_DATA_TYPE_FLOAT32;
tensorBuffer->owner_is_backend = false;
+ // tensorBuffer->buffer has been changed to new one which is converted to float data type.
+ // And its size is tensor element count * sizeof(float) so the size should be calculated again like below,
+ tensorBuffer->size = element_cnt * sizeof(float);
+
return MEDIA_VISION_ERROR_NONE;
}