}
// Get input tensor buffers from a backend engine if the backend engine allocated.
- auto& inputTensorBuffers = mInputTensorBuffers.getAllTensorBuffer();
+ auto& inputTensorBuffers = mInputTensorBuffers.getIETensorBuffer();
int ret = mBackend->GetInputTensorBuffers(inputTensorBuffers);
if (ret != INFERENCE_ENGINE_ERROR_NONE) {
LOGE("Fail to get input tensor buffers from backend engine.");
LOGI("Input tensor buffer count is %zu", mInputTensorBuffers.size());
// Get output tensor buffers from a backend engine if the backend engine allocated.
- auto& outputTensorBuffers = mOutputTensorBuffers.getAllTensorBuffer();
+ auto& outputTensorBuffers = mOutputTensorBuffers.getIETensorBuffer();
ret = mBackend->GetOutputTensorBuffers(outputTensorBuffers);
if (ret != INFERENCE_ENGINE_ERROR_NONE) {
LOGE("Fail to get output tensor buffers from backend engine.");
return MEDIA_VISION_ERROR_NONE;
}
- int Inference::FillOutputResult(tensor_t &outputData)
+ int Inference::ConvertOutputDataTypeToFloat()
{
- for (auto& layer : mOutputLayerProperty.layers) {
- const inference_engine_tensor_info& tensor_info = layer.second;
+ IETensorBuffer& ieTensorBuffers = mOutputTensorBuffers.getIETensorBuffer();
- std::vector<int> tmpDimInfo;
- for (auto& dim : tensor_info.shape) {
- tmpDimInfo.push_back(dim);
- }
-
- outputData.dimInfo.push_back(tmpDimInfo);
-
- inference_engine_tensor_buffer* tensorBuffer =
- mOutputTensorBuffers.getTensorBuffer(layer.first);
- if (tensorBuffer == NULL) {
- LOGE("Fail to getTensorBuffer with name %s", layer.first.c_str());
- return MEDIA_VISION_ERROR_INVALID_OPERATION;
- }
+ for (auto& ieTensorBuffer : ieTensorBuffers) {
+ auto& tensorBuffer = ieTensorBuffer.second;
// Normalize output tensor data converting it to float type in case of quantized model.
- if (tensor_info.data_type == INFERENCE_TENSOR_DATA_TYPE_UINT8) {
- int ret = mOutputTensorBuffers.convertToFloat<unsigned char>(tensorBuffer, tensor_info);
+ if (tensorBuffer.data_type == INFERENCE_TENSOR_DATA_TYPE_UINT8) {
+ int ret = mOutputTensorBuffers.convertToFloat<unsigned char>(&tensorBuffer);
if (ret != MEDIA_VISION_ERROR_NONE) {
LOGE("Fail to convert tensor data to float type.");
return ret;
}
}
- if (tensor_info.data_type == INFERENCE_TENSOR_DATA_TYPE_UINT16) {
- int ret = mOutputTensorBuffers.convertToFloat<unsigned short>(tensorBuffer, tensor_info);
+ if (tensorBuffer.data_type == INFERENCE_TENSOR_DATA_TYPE_UINT16) {
+ int ret = mOutputTensorBuffers.convertToFloat<unsigned short>(&tensorBuffer);
if (ret != MEDIA_VISION_ERROR_NONE) {
LOGE("Fail to convert tensor data to float type.");
return ret;
}
}
-
- outputData.data.push_back(static_cast<void *>(tensorBuffer->buffer));
}
return MEDIA_VISION_ERROR_NONE;
}
if (mMetadata.GetInputMeta().IsParsed()) {
- for (auto& buffer : mInputTensorBuffers.getAllTensorBuffer()) {
+ for (auto& buffer : mInputTensorBuffers.getIETensorBuffer()) {
inference_engine_tensor_buffer& tensor_buffer = buffer.second;
const LayerInfo& layerInfo = mMetadata.GetInputMeta().GetLayer().at(buffer.first);
const Options& opt = mMetadata.GetInputMeta().GetOption().empty() ? Options() : mMetadata.GetInputMeta().GetOption().at(buffer.first);
}
}
} else {
- for (auto& buffer : mInputTensorBuffers.getAllTensorBuffer()) {
+ for (auto& buffer : mInputTensorBuffers.getIETensorBuffer()) {
inference_engine_tensor_buffer& tensor_buffer = buffer.second;
int data_type = ConvertToCv(tensor_buffer.data_type);
}
}
- ret = mBackend->Run(mInputTensorBuffers.getAllTensorBuffer(),
- mOutputTensorBuffers.getAllTensorBuffer());
+ ret = mBackend->Run(mInputTensorBuffers.getIETensorBuffer(),
+ mOutputTensorBuffers.getIETensorBuffer());
+ if (ret != INFERENCE_ENGINE_ERROR_NONE)
+ return ret;
- return ConvertEngineErrorToVisionError(ret);
+ return ConvertOutputDataTypeToFloat();
}
int Inference::Run(std::vector<void *>& buffer_objs)
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
- if (mInputTensorBuffers.getAllTensorBuffer().size() != buffer_objs.size()) {
+ if (mInputTensorBuffers.getIETensorBuffer().size() != buffer_objs.size()) {
LOGE("Raw source count is not invalid.");
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
unsigned int buffer_idx = 0;
- for (auto& buffer : mInputTensorBuffers.getAllTensorBuffer()) {
+ for (auto& buffer : mInputTensorBuffers.getIETensorBuffer()) {
inference_engine_tensor_buffer& tensor_buffer = buffer.second;
inference_engine_tensor_buffer *buffer_obj = static_cast<inference_engine_tensor_buffer *>(buffer_objs[buffer_idx]);
buffer_idx++;
}
- ret = mBackend->Run(mInputTensorBuffers.getAllTensorBuffer(),
- mOutputTensorBuffers.getAllTensorBuffer());
+ ret = mBackend->Run(mInputTensorBuffers.getIETensorBuffer(),
+ mOutputTensorBuffers.getIETensorBuffer());
+ if (ret != INFERENCE_ENGINE_ERROR_NONE)
+ return ret;
- return ConvertEngineErrorToVisionError(ret);
+ return ConvertOutputDataTypeToFloat();
}
std::pair<std::string, bool>
std::vector<std::pair<float, int>> topScore;
auto threadHold = mConfig.mConfidenceThresHold;
constexpr unsigned int default_top_number = 5;
- tensor_t outputData;
+ tensor_t outputTensorInfo;
- // Get inference result and contain it to outputData.
- int ret = FillOutputResult(outputData);
+ // Get inference result and contain it to outputTensorInfo.
+ int ret = mOutputTensorBuffers.GetTensorInfo(mOutputLayerProperty, outputTensorInfo);
if (ret != MEDIA_VISION_ERROR_NONE) {
LOGE("Fail to get output result.");
return ret;
}
PostProcess postProc;
- unsigned int classes = outputData.dimInfo[0][1];
+ unsigned int classes = outputTensorInfo.dimInfo[0][1];
unsigned int top_number = default_top_number;
if (mMetadata.GetOutputMeta().IsParsed()) {
postProc.ScoreClear(top_number);
- auto *prediction = reinterpret_cast<float *>(outputData.data[0]);
+ auto *prediction = reinterpret_cast<float *>(outputTensorInfo.data[0]);
LOGI("class count: %d", classes);
LOGI("Inference: GetObjectDetectionResults: %d\n", results->number_of_objects);
} else {
- tensor_t outputData;
+ tensor_t outputTensorInfo;
- // Get inference result and contain it to outputData.
- int ret = FillOutputResult(outputData);
+ // Get inference result and contain it to outputTensorInfo.
+ int ret = mOutputTensorBuffers.GetTensorInfo(mOutputLayerProperty, outputTensorInfo);
if (ret != MEDIA_VISION_ERROR_NONE) {
LOGE("Fail to get output result.");
return ret;
float *scores = nullptr;
int number_of_detections = 0;
- if (outputData.dimInfo.size() == 1) {
+ if (outputTensorInfo.dimInfo.size() == 1) {
// there is no way to know how many objects are detect unless the number of objects aren't
// provided. In the case, each backend should provide the number of results manually.
// For example, in OpenCV, MobilenetV1-SSD doesn't provide it so the number of objects are
- // written to the 1st element i.e., outputData.data[0] (the shape is 1x1xNx7 and the 1st of 7
+ // written to the 1st element i.e., outputTensorInfo.data[0] (the shape is 1x1xNx7 and the 1st of 7
// indicates the image id. But it is useless if a batch mode isn't supported.
// So, use the 1st of 7.
number_of_detections = static_cast<int>(
- *reinterpret_cast<float *>(outputData.data[0]));
- cv::Mat cvOutputData(number_of_detections, outputData.dimInfo[0][3],
- CV_32F, outputData.data[0]);
+ *reinterpret_cast<float *>(outputTensorInfo.data[0]));
+ cv::Mat cvOutputData(number_of_detections, outputTensorInfo.dimInfo[0][3],
+ CV_32F, outputTensorInfo.data[0]);
// boxes
cv::Mat cvLeft = cvOutputData.col(3).clone();
classes = cvClasses.ptr<float>(0);
scores = cvScores.ptr<float>(0);
} else {
- boxes = reinterpret_cast<float *>(outputData.data[0]);
- classes = reinterpret_cast<float *>(outputData.data[1]);
- scores = reinterpret_cast<float *>(outputData.data[2]);
- number_of_detections = (int) (*reinterpret_cast<float *>(outputData.data[3]));
+ boxes = reinterpret_cast<float *>(outputTensorInfo.data[0]);
+ classes = reinterpret_cast<float *>(outputTensorInfo.data[1]);
+ scores = reinterpret_cast<float *>(outputTensorInfo.data[2]);
+ number_of_detections = (int) (*reinterpret_cast<float *>(outputTensorInfo.data[3]));
}
LOGI("number_of_detections = %d", number_of_detections);
LOGE("Inference: GetFaceDetectionResults: %d\n", results->number_of_faces);
} else {
- tensor_t outputData;
+ tensor_t outputTensorInfo;
- // Get inference result and contain it to outputData.
- int ret = FillOutputResult(outputData);
+ // Get inference result and contain it to outputTensorInfo.
+ int ret = mOutputTensorBuffers.GetTensorInfo(mOutputLayerProperty, outputTensorInfo);
if (ret != MEDIA_VISION_ERROR_NONE) {
LOGE("Fail to get output result.");
return ret;
int number_of_detections = 0;
cv::Mat cvScores, cvClasses, cvBoxes;
- if (outputData.dimInfo.size() == 1) {
+ if (outputTensorInfo.dimInfo.size() == 1) {
// there is no way to know how many objects are detect unless the number of objects aren't
// provided. In the case, each backend should provide the number of results manually.
// For example, in OpenCV, MobilenetV1-SSD doesn't provide it so the number of objects are
- // written to the 1st element i.e., outputData.data[0] (the shape is 1x1xNx7 and the 1st of 7
+ // written to the 1st element i.e., outputTensorInfo.data[0] (the shape is 1x1xNx7 and the 1st of 7
// indicates the image id. But it is useless if a batch mode isn't supported.
// So, use the 1st of 7.
- number_of_detections = static_cast<int>(*reinterpret_cast<float *>(outputData.data[0]));
- cv::Mat cvOutputData(number_of_detections, outputData.dimInfo[0][3], CV_32F, outputData.data[0]);
+ number_of_detections = static_cast<int>(*reinterpret_cast<float *>(outputTensorInfo.data[0]));
+ cv::Mat cvOutputData(number_of_detections, outputTensorInfo.dimInfo[0][3], CV_32F, outputTensorInfo.data[0]);
// boxes
cv::Mat cvLeft = cvOutputData.col(3).clone();
classes = cvClasses.ptr<float>(0);
scores = cvScores.ptr<float>(0);
} else {
- boxes = reinterpret_cast<float *>(outputData.data[0]);
- classes = reinterpret_cast<float *>(outputData.data[1]);
- scores = reinterpret_cast<float *>(outputData.data[2]);
- number_of_detections = static_cast<int>(*reinterpret_cast<float *>(outputData.data[3]));
+ boxes = reinterpret_cast<float *>(outputTensorInfo.data[0]);
+ classes = reinterpret_cast<float *>(outputTensorInfo.data[1]);
+ scores = reinterpret_cast<float *>(outputTensorInfo.data[2]);
+ number_of_detections = static_cast<int>(*reinterpret_cast<float *>(outputTensorInfo.data[3]));
}
results->number_of_faces = 0;
results->number_of_landmarks = results->locations.size();
} else {
- tensor_t outputData;
+ tensor_t outputTensorInfo;
- // Get inference result and contain it to outputData.
- int ret = FillOutputResult(outputData);
+ // Get inference result and contain it to outputTensorInfo.
+ int ret = mOutputTensorBuffers.GetTensorInfo(mOutputLayerProperty, outputTensorInfo);
if (ret != MEDIA_VISION_ERROR_NONE) {
LOGE("Fail to get output result.");
return ret;
}
- int number_of_detections = outputData.dimInfo[0][1] >> 1;
+ int number_of_detections = outputTensorInfo.dimInfo[0][1] >> 1;
results->number_of_landmarks = number_of_detections;
results->locations.resize(number_of_detections);
LOGI("imgW:%d, imgH:%d", mSourceSize.width, mSourceSize.height);
- float *loc = reinterpret_cast<float *>(outputData.data[0]);
+ float *loc = reinterpret_cast<float *>(outputTensorInfo.data[0]);
for (auto& point : results->locations) {
point.x = static_cast<int>(*loc++ * mSourceSize.width);
detectionResults = std::move(poseResult);
} else {
- tensor_t outputData;
+ tensor_t outputTensorInfo;
- // Get inference result and contain it to outputData.
- int ret = FillOutputResult(outputData);
+ // Get inference result and contain it to outputTensorInfo.
+ int ret = mOutputTensorBuffers.GetTensorInfo(mOutputLayerProperty, outputTensorInfo);
if (ret != MEDIA_VISION_ERROR_NONE) {
LOGE("Fail to get output result.");
return ret;
}
- cv::Mat reShapeTest(cv::Size(outputData.dimInfo[0][2], outputData.dimInfo[0][1]),
- CV_32FC(outputData.dimInfo[0][3]), outputData.data[0]);
- cv::Mat multiChannels[outputData.dimInfo[0][3]];
+ cv::Mat reShapeTest(cv::Size(outputTensorInfo.dimInfo[0][2], outputTensorInfo.dimInfo[0][1]),
+ CV_32FC(outputTensorInfo.dimInfo[0][3]), outputTensorInfo.data[0]);
+ cv::Mat multiChannels[outputTensorInfo.dimInfo[0][3]];
split(reShapeTest, multiChannels);
- float ratioX = static_cast<float>(outputData.dimInfo[0][2]);
- float ratioY = static_cast<float>(outputData.dimInfo[0][1]);
+ float ratioX = static_cast<float>(outputTensorInfo.dimInfo[0][2]);
+ float ratioY = static_cast<float>(outputTensorInfo.dimInfo[0][1]);
poseResult->number_of_poses = 1;
- poseResult->number_of_landmarks_per_pose = outputData.dimInfo[0][3];
+ poseResult->number_of_landmarks_per_pose = outputTensorInfo.dimInfo[0][3];
if (poseResult->number_of_landmarks_per_pose >= MAX_NUMBER_OF_LANDMARKS_PER_POSE) {
LOGE("Exeeded maxinum number of landmarks per pose(%d >= %d).",
bool TensorBuffer::empty()
{
- return mTensorBuffer.empty();
+ return _tensorBuffer.empty();
}
bool TensorBuffer::exist(std::string name)
void TensorBuffer::release()
{
- for (auto& tensorBuffer : mTensorBuffer) {
+ for (auto& tensorBuffer : _tensorBuffer) {
auto& tBuffer = tensorBuffer.second;
if (tBuffer.owner_is_backend) {
continue;
}
}
- LOGI("Tensor(%zu) have been released.", mTensorBuffer.size());
- IETensorBuffer().swap(mTensorBuffer);
+ LOGI("Tensor(%zu) have been released.", _tensorBuffer.size());
+ IETensorBuffer().swap(_tensorBuffer);
}
size_t TensorBuffer::size()
{
- return mTensorBuffer.size();
+ return _tensorBuffer.size();
}
- IETensorBuffer& TensorBuffer::getAllTensorBuffer()
+ IETensorBuffer& TensorBuffer::getIETensorBuffer()
{
- return mTensorBuffer;
+ return _tensorBuffer;
}
inference_engine_tensor_buffer* TensorBuffer::getTensorBuffer(std::string name)
{
- if (mTensorBuffer.find(name) == mTensorBuffer.end()){
+ if (_tensorBuffer.find(name) == _tensorBuffer.end()){
return nullptr;
}
- return &mTensorBuffer[name];
+ return &_tensorBuffer[name];
}
bool TensorBuffer::addTensorBuffer(std::string name, inference_engine_tensor_buffer& buffer)
return false;
}
- auto ret = mTensorBuffer.insert(std::make_pair(name, buffer));
+ auto ret = _tensorBuffer.insert(std::make_pair(name, buffer));
if (ret.second == false) {
LOGE("Fail to insert %s with buffer %p", name.c_str(), buffer.buffer);
return false;
return true;
}
+ int TensorBuffer::GetTensorInfo(inference_engine_layer_property& layerProperty, tensor_t& outputTensorInfo)
+ {
+ for (auto& layer : layerProperty.layers) {
+ const inference_engine_tensor_info& tensor_info = layer.second;
+
+ std::vector<int> dimInfo;
+
+ for (auto& dim : tensor_info.shape) {
+ dimInfo.push_back(dim);
+ }
+
+ outputTensorInfo.dimInfo.push_back(dimInfo);
+
+ inference_engine_tensor_buffer* tensorBuffer = getTensorBuffer(layer.first);
+ if (tensorBuffer == NULL) {
+ LOGE("Fail to getTensorBuffer with name %s", layer.first.c_str());
+ return MEDIA_VISION_ERROR_INVALID_OPERATION;
+ }
+
+ outputTensorInfo.data.push_back(static_cast<void *>(tensorBuffer->buffer));
+ }
+
+ return MEDIA_VISION_ERROR_NONE;
+ }
+
template <typename T>
- int TensorBuffer::convertToFloat(inference_engine_tensor_buffer *tensorBuffer, const inference_engine_tensor_info& tensor_info)
+ int TensorBuffer::convertToFloat(inference_engine_tensor_buffer *tensorBuffer)
{
- float *new_buf = new(std::nothrow) float[tensor_info.size];
+ float *new_buf = new(std::nothrow) float[tensorBuffer->size];
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 < tensor_info.size; idx++)
+ for (size_t idx = 0; idx < tensorBuffer->size; idx++)
new_buf[idx] = static_cast<float>(ori_buf[idx]) / 255.0f;
// replace original buffer with new one, and release origin one.
template float TensorBuffer::getValue<float>(std::string, int);
template int TensorBuffer::getValue<int>(std::string, int);
- template int TensorBuffer::convertToFloat<unsigned char>(_inference_engine_tensor_buffer*, _inference_engine_tensor_info const&);
- template int TensorBuffer::convertToFloat<unsigned short>(_inference_engine_tensor_buffer*, _inference_engine_tensor_info const&);
+ template int TensorBuffer::convertToFloat<unsigned char>(_inference_engine_tensor_buffer*);
+ template int TensorBuffer::convertToFloat<unsigned short>(_inference_engine_tensor_buffer*);
} /* Inference */
} /* MediaVision */