mv_inference: Introduce FillOutputResult function
authorInki Dae <inki.dae@samsung.com>
Mon, 24 Feb 2020 05:36:44 +0000 (14:36 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 14 Apr 2020 00:40:31 +0000 (09:40 +0900)
This patch introduces FillOutResult function which gets result from
output tensor buffers. This function is used commonly for all other
scenarios - image classification, object detection, face detection,
and facial landmark detection.

Change-Id: I494f2924094d409939053753324a6a8485673664
Signed-off-by: Inki Dae <inki.dae@samsung.com>
mv_inference/inference/include/Inference.h
mv_inference/inference/src/Inference.cpp

index c2e953bf5bd8a4422cd6a8d428b6c660e4a599e2..8f877b2384574e63fc1cf5e8fcff2ae98311df18 100755 (executable)
@@ -325,6 +325,7 @@ private:
        int PrepareTenosrBuffers(void);
        void CleanupTensorBuffers(void);
        int SetUserFile(std::string filename);
+       int FillOutputResult(tensor_t &outputData);
 };
 
 } /* Inference */
index 8522c28b8fc68e58b1d8e7266ebde7d4b1dcc876..8b184879a5b49fba8ee5db9a8f66db176d2fa8fe 100755 (executable)
@@ -495,6 +495,42 @@ int Inference::PrepareTenosrBuffers(void)
        return MEDIA_VISION_ERROR_NONE;
 }
 
+int Inference::FillOutputResult(tensor_t &outputData)
+{
+       for (int i = 0; i < mOutputLayerProperty.tensor_infos.size(); ++i) {
+               inference_engine_tensor_info tensor_info = mOutputLayerProperty.tensor_infos[i];
+
+               std::vector<int> tmpDimInfo;
+               for (int i = 0; i < (int)tensor_info.shape.size(); i++) {
+                       tmpDimInfo.push_back(tensor_info.shape[i]);
+               }
+
+               outputData.dimInfo.push_back(tmpDimInfo);
+
+               // Normalize output tensor data converting it to float type in case of quantized model.
+               if (tensor_info.data_type == TENSOR_DATA_TYPE_UINT8) {
+                       unsigned char *ori_buf = (unsigned char *)mOutputTensorBuffers[i].buffer;
+                       float *new_buf = new float[tensor_info.size];
+                       if (new_buf == NULL) {
+                               LOGE("Fail to allocate a new output tensor buffer.");
+                               return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
+                       }
+
+                       for (int j = 0; j < tensor_info.size; j++) {
+                               new_buf[j] = (float)ori_buf[j] / 255.0f;
+                       }
+
+                       // replace original buffer with new one, and release origin one.
+                       mOutputTensorBuffers[i].buffer = new_buf;
+                       delete[] ori_buf;
+               }
+
+               outputData.data.push_back((void *)mOutputTensorBuffers[i].buffer);
+       }
+
+       return MEDIA_VISION_ERROR_NONE;
+}
+
 int Inference::Bind(void)
 {
        LOGI("ENTER");
@@ -761,35 +797,12 @@ std::pair<std::string, bool> Inference::GetSupportedInferenceBackend(int backend
 int Inference::GetClassficationResults(ImageClassificationResults *classificationResults)
 {
        tensor_t outputData;
-       for (int i = 0; i < mOutputLayerProperty.tensor_infos.size(); ++i) {
-               inference_engine_tensor_info tensor_info = mOutputLayerProperty.tensor_infos[i];
-
-               std::vector<int> tmpDimInfo;
-               for (int i = 0; i < (int)tensor_info.shape.size(); i++) {
-                       tmpDimInfo.push_back(tensor_info.shape[i]);
-               }
-
-               outputData.dimInfo.push_back(tmpDimInfo);
-
-               // Normalize output tensor data converting it to float type in case of quantized model.
-               if (tensor_info.data_type == TENSOR_DATA_TYPE_UINT8) {
-                       unsigned char *ori_buf = (unsigned char *)mOutputTensorBuffers[i].buffer;
-                       float *new_buf = new float[tensor_info.size];
-                       if (new_buf == NULL) {
-                               LOGE("Fail to allocate a new output tensor buffer.");
-                               return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
-                       }
-
-                       for (int j = 0; j < tensor_info.size; j++) {
-                               new_buf[j] = (float)ori_buf[j] / 255.0f;
-                       }
-
-                       // replace original buffer with new one, and release origin one.
-                       mOutputTensorBuffers[i].buffer = new_buf;
-                       delete[] ori_buf;
-               }
 
-               outputData.data.push_back((void *)mOutputTensorBuffers[i].buffer);
+       // Get inference result and contain it to outputData.
+       int ret = FillOutputResult(outputData);
+       if (ret != MEDIA_VISION_ERROR_NONE) {
+               LOGE("Fail to get output result.");
+               return ret;
        }
 
        // Will contain top N results in ascending order.
@@ -851,20 +864,23 @@ int Inference::GetClassficationResults(ImageClassificationResults *classificatio
 int Inference::GetObjectDetectionResults(ObjectDetectionResults *detectionResults)
 {
        tensor_t outputData;
-       int ret = mBackend->GetInferenceResult(outputData);
-       if (ret != INFERENCE_ENGINE_ERROR_NONE) {
-               LOGE("Fail to GetObjectDetectionResults");
-               return ConvertEngineErrorToVisionError(ret);
+
+       // Get inference result and contain it to outputData.
+       int ret = FillOutputResult(outputData);
+       if (ret != MEDIA_VISION_ERROR_NONE) {
+               LOGE("Fail to get output result.");
+               return ret;
        }
 
        std::vector<std::vector<int>> inferDimInfo(outputData.dimInfo);
        std::vector<void*> inferResults(outputData.data.begin(), outputData.data.end());
-
        float* boxes = reinterpret_cast<float*>(inferResults[0]);
        float* classes = reinterpret_cast<float*>(inferResults[1]);
        float* scores = reinterpret_cast<float*>(inferResults[2]);
        int number_of_detections = (int)(*reinterpret_cast<float*>(inferResults[3]));
 
+       LOGI("number_of_detections = %d", number_of_detections);
+
        int left, top, right, bottom;
        cv::Rect loc;
 
@@ -903,10 +919,12 @@ int Inference::GetObjectDetectionResults(ObjectDetectionResults *detectionResult
 int Inference::GetFaceDetectionResults(FaceDetectionResults *detectionResults)
 {
        tensor_t outputData;
-       int ret = mBackend->GetInferenceResult(outputData);
-       if (ret != INFERENCE_ENGINE_ERROR_NONE) {
-               LOGE("Fail to GetFaceDetectionResults");
-               return ConvertEngineErrorToVisionError(ret);
+
+       // Get inference result and contain it to outputData.
+       int ret = FillOutputResult(outputData);
+       if (ret != MEDIA_VISION_ERROR_NONE) {
+               LOGE("Fail to get output result.");
+               return ret;
        }
 
        std::vector<std::vector<int>> inferDimInfo(outputData.dimInfo);
@@ -954,10 +972,12 @@ int Inference::GetFaceDetectionResults(FaceDetectionResults *detectionResults)
 int Inference::GetFacialLandMarkDetectionResults(FacialLandMarkDetectionResults *detectionResults)
 {
        tensor_t outputData;
-       int ret = mBackend->GetInferenceResult(outputData);
-       if (ret != INFERENCE_ENGINE_ERROR_NONE) {
-               LOGE("Fail to GetFacialLandMarkDetectionResults");
-               return ConvertEngineErrorToVisionError(ret);
+
+       // Get inference result and contain it to outputData.
+       int ret = FillOutputResult(outputData);
+       if (ret != MEDIA_VISION_ERROR_NONE) {
+               LOGE("Fail to get output result.");
+               return ret;
        }
 
        std::vector<std::vector<int>> inferDimInfo(outputData.dimInfo);