provide scale and zero_point values 68/319868/3
authorInki Dae <inki.dae@samsung.com>
Tue, 5 Nov 2024 03:51:24 +0000 (12:51 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 5 Nov 2024 04:07:39 +0000 (13:07 +0900)
[Version] : 0.0.6
[Issue type] : new feature

Provide scale and zero_point values as tensor info if a given
tensor is quanziation type. In addition, this patch drops
unnecessary code.

Change-Id: I1bd22236c57c137d6747652aef18f852ed27e7e2
Signed-off-by: Inki Dae <inki.dae@samsung.com>
packaging/inference-engine-tflite.spec
src/inference_engine_tflite.cpp

index 566eae8062a31f4dd01d0f722e8f18b403be5887..f80e5cbd98fe9a0079229d5230fbba960d43dd52 100644 (file)
@@ -1,6 +1,6 @@
 Name:       inference-engine-tflite
 Summary:    Tensorflow-Lite based implementation of inference-engine-interface
-Version:    0.0.5
+Version:    0.0.6
 Release:    0
 Group:      Multimedia/Libraries
 License:    Apache-2.0
index 3697e71d1110725a23ae6840e5352e3dc14a911f..21a0e05792985c52f7ebe94588bce568cac53e76 100644 (file)
@@ -267,53 +267,7 @@ namespace TFLiteImpl
        {
                LOGI("ENTER");
 
-               mOutputLayers.clear();
-
-               for (auto& layer :mOutputLayerId) {
-                       if (layer.second < 0) {
-                               LOGE("Invalid output layer ID [%d]", layer.second);
-                               return INFERENCE_ENGINE_ERROR_INVALID_OPERATION;
-                       }
-
-                       inference_engine_tensor_info tensor_info;
-
-                       LOGI("mInterpreter->tensor(%d)->dims name[%s] size[%d] type[%d]",
-                                layer.second,
-                                mInterpreter->tensor(layer.second)->name,
-                                mInterpreter->tensor(layer.second)->dims->size,
-                                mInterpreter->tensor(layer.second)->type);
-
-                       std::vector<size_t> shape_nhwc;
-                       for (int idx = 0; idx < mInterpreter->tensor(layer.second)->dims->size; idx++)
-                               shape_nhwc.push_back(mInterpreter->tensor(layer.second)->dims->data[idx]);
-
-                       //tflite only supports NHWC (https://www.tensorflow.org/lite/guide/ops_compatibility).
-                       tensor_info.shape = shape_nhwc;
-                       tensor_info.shape_type = INFERENCE_TENSOR_SHAPE_NHWC;
-                       if (mInterpreter->tensor(layer.second)->type == kTfLiteUInt8) {
-                               LOGI("type is kTfLiteUInt8");
-                               tensor_info.data_type = INFERENCE_TENSOR_DATA_TYPE_UINT8;
-                       } else if (mInterpreter->tensor(layer.second)->type == kTfLiteInt8) {
-                               LOGI("type is kTfLiteInt8");
-                               tensor_info.data_type = INFERENCE_TENSOR_DATA_TYPE_INT8;
-                       } else if (mInterpreter->tensor(layer.second)->type == kTfLiteInt64) {
-                               LOGI("type is kTfLiteInt64");
-                               tensor_info.data_type = INFERENCE_TENSOR_DATA_TYPE_INT64;
-                       } else if (mInterpreter->tensor(layer.second)->type == kTfLiteFloat32) {
-                               LOGI("type is kTfLiteFloat32");
-                               tensor_info.data_type = INFERENCE_TENSOR_DATA_TYPE_FLOAT32;
-                       } else {
-                               LOGE("Not supported");
-                               return INFERENCE_ENGINE_ERROR_NOT_SUPPORTED_FORMAT;
-                       }
-                       tensor_info.size = 1;
-
-                       for (auto & dim : tensor_info.shape)
-                               tensor_info.size *= dim;
-
-                       mOutputLayers.insert(std::make_pair(mInterpreter->tensor(layer.second)->name, tensor_info));
-               }
-
+               SetInterpreterInfo();
                property.layers = mOutputLayers;
 
                LOGI("LEAVE");
@@ -495,19 +449,25 @@ namespace TFLiteImpl
                        std::map<std::string, int>& layerId)
        {
                layers.clear();
-               for (auto& layer : layerId) {
 
+               for (auto& layer : layerId) {
+                       const TfLiteTensor *tensor = mInterpreter->tensor(layer.second);
+                       if (!tensor) {
+                               LOGE("tensor for tensor index(%d) is null", layer.second);
+                               return INFERENCE_ENGINE_ERROR_INVALID_OPERATION;
+                       }
                        std::vector<size_t> shape_nhwc;
 
-                       for (int idx = 0; idx < mInterpreter->tensor(layer.second)->dims->size; idx++)
-                               shape_nhwc.push_back(mInterpreter->tensor(layer.second)->dims->data[idx]);
+                       for (int idx = 0; idx < tensor->dims->size; idx++)
+                               shape_nhwc.push_back(tensor->dims->data[idx]);
 
+                       //tflite only supports NHWC (https://www.tensorflow.org/lite/guide/ops_compatibility).
                        inference_engine_tensor_info tensor_info {
                                shape_nhwc, INFERENCE_TENSOR_SHAPE_NHWC,
-                               INFERENCE_TENSOR_DATA_TYPE_NONE, 1
+                               INFERENCE_TENSOR_DATA_TYPE_NONE, 1, 0.0f, 0
                        };
 
-                       switch (mInterpreter->tensor(layer.second)->type) {
+                       switch (tensor->type) {
                        case kTfLiteUInt8:
                                LOGI("type is kTfLiteUInt8");
                                tensor_info.data_type = INFERENCE_TENSOR_DATA_TYPE_UINT8;
@@ -528,9 +488,23 @@ namespace TFLiteImpl
                        for (auto& dim : tensor_info.shape)
                                tensor_info.size *= dim;
 
-                       layers.insert(std::make_pair(mInterpreter->tensor(layer.second)->name, tensor_info));
+                       if (tensor->quantization.type == kTfLiteAffineQuantization) {
+                               auto *quant_parms = reinterpret_cast<TfLiteAffineQuantization *>(tensor->quantization.params);
+
+                               if (quant_parms) {
+                                       tensor_info.scale =  quant_parms->scale->data[0];
+                                       tensor_info.zero_point = quant_parms->zero_point->data[0];
+
+                                       LOGD("scale of %s : %f", tensor->name, tensor_info.scale);
+                                       LOGD("zero_point of %s : %d", tensor->name, tensor_info.zero_point);
+                               }
+
+                       }
+
+                       layers.insert(std::make_pair(tensor->name, tensor_info));
 
                }
+
                return INFERENCE_ENGINE_ERROR_NONE;
        }