[Filter/TF-Lite] add a variable to store in/out tensor rank
authorHyoungjooAhn <hello.ahn@samsung.com>
Thu, 16 Aug 2018 05:33:48 +0000 (14:33 +0900)
committer함명주/동작제어Lab(SR)/Principal Engineer/삼성전자 <myungjoo.ham@samsung.com>
Fri, 17 Aug 2018 06:34:27 +0000 (15:34 +0900)
the TfLiteCore class will hold the rank information of each tensors

Signed-off-by: HyoungjooAhn <hello.ahn@samsung.com>
common/tensor_common.c
gst/tensor_filter/tensor_filter_tensorflow_lite_core.cc
include/tensor_filter_tensorflow_lite_core.h
include/tensor_typedef.h

index 705864f..29f5a30 100644 (file)
@@ -972,9 +972,6 @@ get_tensors_from_structure (const GstStructure * str,
     return 0;
 
   meta->num_tensors = num;
-  meta->dims = g_new (tensor_dim, num);
-  meta->types = g_new (tensor_type, num);
-  meta->ranks = (unsigned int *) g_malloc (sizeof (gint) * num);
 
   if (gst_structure_get_int (str, "rank", (int *) &rank)) {
     if (rank != NNS_TENSOR_RANK_LIMIT) {
@@ -983,7 +980,7 @@ get_tensors_from_structure (const GstStructure * str,
     }
   }
   if (0 == rank)
-    goto err_alloc;
+    return 0;
 
   if (gst_structure_get_fraction (str, "framerate", &fn, &fd)) {
     if (framerate_num)
@@ -1001,17 +998,17 @@ get_tensors_from_structure (const GstStructure * str,
     if (counter >= num) {
       err_print
           ("The number of dimensions does not match the number of tensors.\n");
-      goto err_alloc;
+      return 0;
     }
     ret = get_tensor_dimension (ftrim (strv[counter]), meta->dims[counter]);
     if (ret > NNS_TENSOR_RANK_LIMIT || ret < 1)
-      goto err_alloc;
+      return 0;
     counter++;
   }
   if (counter != num) {
     err_print
         ("The number of dimensions does not match the number of tensors.\n");
-    goto err_alloc;
+    return 0;
   }
   g_strfreev (strv);
 
@@ -1021,29 +1018,19 @@ get_tensors_from_structure (const GstStructure * str,
   while (strv[counter]) {
     if (counter >= num) {
       err_print ("The number of types does not match the number of tensors.\n");
-      goto err_alloc;
+      return 0;
     }
     meta->types[counter] = get_tensor_type (ftrim (strv[counter]));
     if (meta->types[counter] >= _NNS_END)
-      goto err_alloc;
+      return 0;
     counter++;
   }
   if (counter != num) {
     err_print ("The number of types does not match the number of tensors.\n");
-    goto err_alloc;
+    return 0;
   }
   g_strfreev (strv);
   return num;
-
-err_alloc:
-  meta->num_tensors = 0;
-  g_free (meta->dims);
-  meta->dims = NULL;
-  g_free (meta->types);
-  meta->types = NULL;
-  g_free (meta->ranks);
-  meta->ranks = NULL;
-  return 0;
 }
 
 /**
index 1520c9e..e036dbd 100644 (file)
@@ -151,7 +151,10 @@ TFLiteCore::setInputTensorProp ()
   inputTensorSize = input_idx_list.size ();
 
   for (int i = 0; i < inputTensorSize; i++) {
-    if (getTensorDim (input_idx_list[i], inputDimension[i])) {
+    inputTensorRank[i] = NNS_TENSOR_RANK_LIMIT;
+
+    if (getTensorDim (input_idx_list[i], inputDimension[i],
+            &inputTensorRank[i])) {
       return -1;
     }
     inputType[i] =
@@ -179,7 +182,10 @@ TFLiteCore::setOutputTensorProp ()
   outputTensorSize = output_idx_list.size ();
 
   for (int i = 0; i < outputTensorSize; i++) {
-    if (getTensorDim (output_idx_list[i], outputDimension[i])) {
+    outputTensorRank[i] = NNS_TENSOR_RANK_LIMIT;
+
+    if (getTensorDim (output_idx_list[i], outputDimension[i],
+            &outputTensorRank[i])) {
       return -1;
     }
     outputType[i] =
@@ -200,12 +206,14 @@ TFLiteCore::setOutputTensorProp ()
  * @brief      return the Dimension of Tensor.
  * @param tensor_idx   : the real index of model of the tensor
  * @param[out] dim     : the array of the tensor
+ * @param[out] rank    : the rank of the tensor
  * @return 0 if OK. non-zero if error.
  */
 int
-TFLiteCore::getTensorDim (int tensor_idx, tensor_dim dim)
+TFLiteCore::getTensorDim (int tensor_idx, tensor_dim dim, unsigned int *rank)
 {
   int len = interpreter->tensor (tensor_idx)->dims->size;
+  *rank = len;
   g_assert (len <= NNS_TENSOR_RANK_LIMIT);
 
   /* the order of dimension is reversed at CAPS negotiation */
@@ -252,6 +260,9 @@ TFLiteCore::getInputTensorDim (tensor_dim dim, tensor_type * type)
 {
   memcpy (dim, inputDimension[0], sizeof (tensor_dim));
   *type = inputType[0];
+  printf ("[IN]\nDim: %d %d %d %d \nType: %d \nRank: %u\n",
+      inputDimension[0][0], inputDimension[0][1], inputDimension[0][2],
+      inputDimension[0][3], inputType[0], inputTensorRank[0]);
   return inputTensorSize;
 }
 
@@ -267,6 +278,9 @@ TFLiteCore::getOutputTensorDim (tensor_dim dim, tensor_type * type)
 {
   memcpy (dim, outputDimension[0], sizeof (tensor_dim));
   *type = outputType[0];
+  printf ("[OUT]\nDim: %d %d %d %d \nType: %d \nRank: %u\n",
+      outputDimension[0][0], outputDimension[0][1], outputDimension[0][2],
+      outputDimension[0][3], outputType[0], outputTensorRank[0]);
   return outputTensorSize;
 }
 
index aa16297..758f921 100644 (file)
@@ -69,12 +69,15 @@ private:
   int inputTensorSize; /**< The number of input tensors */
   int outputTensorSize; /**< The number of output tensors */
 
+  unsigned int inputTensorRank[NNS_TENSOR_SIZE_LIMIT]; /**< The rank of input tensors */
+  unsigned int outputTensorRank[NNS_TENSOR_SIZE_LIMIT]; /**< The rank of output tensors */
+
   std::unique_ptr < tflite::Interpreter > interpreter;
   std::unique_ptr < tflite::FlatBufferModel > model;
 
   double get_ms (struct timeval t);
   _nns_tensor_type getTensorType (TfLiteType tfType);
-  int getTensorDim (int tensor_idx, tensor_dim dim);
+  int getTensorDim (int tensor_idx, tensor_dim dim, unsigned int *rank);
 };
 
 /**
index 0e02cb4..e1cc9b3 100644 (file)
@@ -101,9 +101,9 @@ typedef uint8_t *tensors[NNS_TENSOR_SIZE_LIMIT];     /**< Array of tensors */
  */
 typedef struct {
   unsigned int num_tensors;    /**< Number of tensors in each frame */
-  tensor_dim *dims;     /**< Array of tensor_dim, [num_tensors] */
-  tensor_type *types;   /**< Array of tensor_type, [num_tensors] */
-  unsigned int *ranks;          /**< Array of rank, [num_tensors] */
+  tensor_dim dims[NNS_TENSOR_SIZE_LIMIT];     /**< Array of tensor_dim, [num_tensors] */
+  tensor_type types[NNS_TENSOR_SIZE_LIMIT];   /**< Array of tensor_type, [num_tensors] */
+  unsigned int ranks[NNS_TENSOR_SIZE_LIMIT];          /**< Array of rank, [num_tensors] */
 } GstTensor_TensorsMeta;
 
 /**