[Filter] Modify property dimension to get tensors format
authorjijoong.moon <jijoong.moon@samsung.com>
Wed, 22 Aug 2018 00:20:59 +0000 (09:20 +0900)
committer안형주/언어이해Lab(SR)/Engineer/삼성전자 <hello.ahn@samsung.com>
Mon, 27 Aug 2018 09:15:37 +0000 (18:15 +0900)
In order to get tensors format, set & get properties in filter should
be changed. It takes "1:640:480:3,1:100:100:3". It counts the rank of
each tensor and number of tensor in tensors as well.

**Self evaluation:**
1. Build test:  [X]Passed [ ]Failed [ ]Skipped
2. Run test:  [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: jijoong.moon <jijoong.moon@samsung.com>
common/tensor_common.c
gst/tensor_filter/tensor_filter.c
include/tensor_common.h
include/tensor_typedef.h
tests/common/unittest_common.cpp

index 376b15b..2146c93 100644 (file)
@@ -990,26 +990,35 @@ find_key_strv (const gchar ** strv, const gchar * key)
  * @param param The parameter string in the format of d1:d2:d3:d4, d1:d2:d3, d1:d2, or d1, where dN is a positive integer and d1 is the innermost dimension; i.e., dim[d4][d3][d2][d1];
  */
 int
-get_tensor_dimension (const gchar * param, uint32_t dim[NNS_TENSOR_RANK_LIMIT])
+get_tensor_dimension (const gchar * param,
+    uint32_t dim[NNS_TENSOR_SIZE_LIMIT][NNS_TENSOR_RANK_LIMIT],
+    int rank[NNS_TENSOR_SIZE_LIMIT])
 {
-  gchar **strv = g_strsplit (param, ":", NNS_TENSOR_RANK_LIMIT);
-  int i, retval = 0;
+  int i, j = 0;
   guint64 val;
+  gchar **_strv = g_strsplit (param, ",./", -1);
+  gint num_tensor = g_strv_length (_strv);
 
-  g_assert (strv != NULL);
+  for (i = 0; i < num_tensor; i++) {
+    gchar **strv = g_strsplit (_strv[i], ":", NNS_TENSOR_RANK_LIMIT);
 
-  for (i = 0; i < NNS_TENSOR_RANK_LIMIT; i++) {
-    if (strv[i] == NULL)
-      break;
-    val = g_ascii_strtoull (strv[i], NULL, 10);
-    dim[i] = val;
-    retval = i + 1;
+    g_assert (strv != NULL);
+
+    for (j = 0; j < NNS_TENSOR_RANK_LIMIT; j++) {
+      if (strv[j] == NULL)
+        break;
+      val = g_ascii_strtoull (strv[j], NULL, 10);
+      dim[i][j] = val;
+      rank[i] = j + 1;
+    }
+    for (; j < NNS_TENSOR_RANK_LIMIT; j++)
+      dim[i][j] = 1;
+    g_strfreev (strv);
   }
-  for (; i < NNS_TENSOR_RANK_LIMIT; i++)
-    dim[i] = 1;
 
-  g_strfreev (strv);
-  return retval;
+  g_strfreev (_strv);
+
+  return num_tensor;
 }
 
 /**
@@ -1094,7 +1103,7 @@ get_tensors_from_structure (const GstStructure * str,
     GstTensor_TensorsMeta * meta, int *framerate_num, int *framerate_denom)
 {
   int num = 0;
-  int rank = 0;
+  int rank[NNS_TENSOR_SIZE_LIMIT];
   const gchar *strval;
   gint fn = 0, fd = 0;
   gchar **strv;
@@ -1112,14 +1121,21 @@ get_tensors_from_structure (const GstStructure * str,
 
   meta->num_tensors = num;
 
-  if (gst_structure_get_int (str, "rank", (int *) &rank)) {
-    if (rank != NNS_TENSOR_RANK_LIMIT) {
+  strval = gst_structure_get_string (str, "rank");
+
+  strv = g_strsplit (strval, ",./", -1);
+  counter = 0;
+  while (strv[counter]) {
+    rank[counter] = g_ascii_strtod (strv[counter], NULL);
+    if (rank[counter] != NNS_TENSOR_RANK_LIMIT) {
       err_print ("rank value of other/tensors incorrect.\n");
-      rank = 0;
+      rank[counter] = 0;
+      return 0;
     }
+    counter++;
   }
-  if (0 == rank)
-    return 0;
+
+  g_strfreev (strv);
 
   if (gst_structure_get_fraction (str, "framerate", &fn, &fd)) {
     if (framerate_num)
@@ -1129,27 +1145,14 @@ get_tensors_from_structure (const GstStructure * str,
   }
 
   strval = gst_structure_get_string (str, "dimensions");
-  strv = g_strsplit (strval, ",", -1);
-  counter = 0;
-  while (strv[counter]) {
-    int ret;
 
-    if (counter >= num) {
-      err_print
-          ("The number of dimensions does not match the number of tensors.\n");
-      return 0;
-    }
-    ret = get_tensor_dimension (ftrim (strv[counter]), meta->dims[counter]);
-    if (ret > NNS_TENSOR_RANK_LIMIT || ret < 1)
-      return 0;
-    counter++;
-  }
+  counter = get_tensor_dimension (strval, meta->dims, rank);
+
   if (counter != num) {
     err_print
         ("The number of dimensions does not match the number of tensors.\n");
     return 0;
   }
-  g_strfreev (strv);
 
   strval = gst_structure_get_string (str, "types");
   strv = g_strsplit (strval, ",", -1);
index ab00b14..5db3fce 100644 (file)
@@ -548,26 +548,38 @@ gst_tensor_filter_set_property (GObject * object, guint prop_id,
       g_assert (!(prop->inputConfigured & _TFC_DIMENSION) && value);
       /* Once configures, it cannot be changed in runtime */
       {
-        int rank = get_tensor_dimension (g_value_get_string (value),
-            prop->inputDimension[0]);
-        g_assert (rank > 0 && rank <= NNS_TENSOR_RANK_LIMIT);
+        int i;
+        prop->inputTensorSize =
+            get_tensor_dimension (g_value_get_string (value),
+            prop->inputDimension, prop->inputTensorRank);
+        for (i = 0; i < prop->inputTensorSize; i++) {
+          g_assert (prop->inputTensorRank[i] > 0
+              && prop->inputTensorRank[i] <= NNS_TENSOR_RANK_LIMIT);
+          silent_debug ("Input Prop: %d:%d:%d:%d Rank %d\n",
+              prop->inputDimension[i][0], prop->inputDimension[i][1],
+              prop->inputDimension[i][2], prop->inputDimension[i][3],
+              prop->inputTensorRank[i]);
+        }
         prop->inputConfigured |= _TFC_DIMENSION;
-        silent_debug ("Input Prop: %d:%d:%d:%d Rank %d\n",
-            prop->inputDimension[0][0], prop->inputDimension[0][1],
-            prop->inputDimension[0][2], prop->inputDimension[0][3], rank);
       }
       break;
     case PROP_OUTPUT:
       g_assert (!(prop->outputConfigured & _TFC_DIMENSION) && value);
       /* Once configures, it cannot be changed in runtime */
       {
-        int rank = get_tensor_dimension (g_value_get_string (value),
-            prop->outputDimension[0]);
-        g_assert (rank > 0 && rank <= NNS_TENSOR_RANK_LIMIT);
+        int i;
+        prop->outputTensorSize =
+            get_tensor_dimension (g_value_get_string (value),
+            prop->outputDimension, prop->outputTensorRank);
+        for (i = 0; i < prop->outputTensorSize; i++) {
+          g_assert (prop->outputTensorRank[i] > 0
+              && prop->outputTensorRank[i] <= NNS_TENSOR_RANK_LIMIT);
+          silent_debug ("Output Prop: %d:%d:%d:%d Rank %d\n",
+              prop->outputDimension[i][0], prop->outputDimension[i][1],
+              prop->outputDimension[i][2], prop->outputDimension[i][3],
+              prop->outputTensorRank[i]);
+        }
         prop->outputConfigured |= _TFC_DIMENSION;
-        silent_debug ("Output Prop: %d:%d:%d:%d Rank %d\n",
-            prop->outputDimension[0][0], prop->outputDimension[0][1],
-            prop->outputDimension[0][2], prop->outputDimension[0][3], rank);
       }
       break;
     case PROP_INPUTTYPE:
index da52ead..0177fc1 100644 (file)
@@ -358,7 +358,9 @@ extern int find_key_strv (const gchar ** strv, const gchar * key);
  * @return The Rank.
  * @param param The parameter string in the format of d1:d2:d3:d4, d1:d2:d3, d1:d2, or d1, where dN is a positive integer and d1 is the innermost dimension; i.e., dim[d4][d3][d2][d1];
  */
-extern int get_tensor_dimension (const gchar * param, tensor_dim dim);
+extern int get_tensor_dimension (const gchar * param,
+    uint32_t dim[NNS_TENSOR_SIZE_LIMIT][NNS_TENSOR_RANK_LIMIT],
+    int rank[NNS_TENSOR_SIZE_LIMIT]);
 
 /**
  * @brief Count the number of elemnts of a tensor
index f1bb452..a5571a8 100644 (file)
@@ -129,11 +129,13 @@ typedef struct _GstTensor_Filter_Properties
 
   tensor_dim inputDimension[NNS_TENSOR_SIZE_LIMIT]; /**< The list of dimensions of each input tensors */
   tensor_type inputType[NNS_TENSOR_SIZE_LIMIT]; /**< The list of types for each input tensors */
+  int inputTensorRank[NNS_TENSOR_SIZE_LIMIT]; /**< The list of types for each input tensors */
   int inputCapNegotiated; /**< @todo check if this is really needed */
   int inputTensorSize; /**< The number of input tensors */
 
   tensor_dim outputDimension[NNS_TENSOR_SIZE_LIMIT]; /**< The list of dimensions of each output tensors */
   tensor_type outputType[NNS_TENSOR_SIZE_LIMIT];  /**< The list of types for each output tensors */
+  int outputTensorRank[NNS_TENSOR_SIZE_LIMIT]; /**< The list of types for each input tensors */
   int outputCapNegotiated; /**< @todo check if this is really needed */
   int outputTensorSize; /**< The number of output tensors */
 
index e965b58..e430141 100644 (file)
@@ -7,7 +7,10 @@
  * @see                https://github.sec.samsung.net/STAR/nnstreamer
  * @bug                No known bugs.
  *
- * Copyright (C) 2018 Samsung Electronics Co., Ltd.
+ *  @brief Unit test module for NNStreamer common library
+ *  @bug       No known bugs except for NYI items
+ *
+ *  Copyright 2018 Samsung Electronics
  *
  */
 
@@ -171,13 +174,15 @@ TEST (common_find_key_strv, key_index)
  */
 TEST (common_get_tensor_dimension, case1)
 {
-  uint32_t dim[NNS_TENSOR_RANK_LIMIT];
-  int rank = get_tensor_dimension ("345:123:433:177", dim);
-  EXPECT_EQ (rank, 4);
-  EXPECT_EQ (dim[0], 345);
-  EXPECT_EQ (dim[1], 123);
-  EXPECT_EQ (dim[2], 433);
-  EXPECT_EQ (dim[3], 177);
+  uint32_t dim[NNS_TENSOR_SIZE_LIMIT][NNS_TENSOR_RANK_LIMIT];
+  int rank[NNS_TENSOR_RANK_LIMIT];
+  int num_tensors = get_tensor_dimension ("345:123:433:177", dim, rank);
+  EXPECT_EQ (num_tensors, 1);
+  EXPECT_EQ (rank[0], 4);
+  EXPECT_EQ (dim[0][0], 345);
+  EXPECT_EQ (dim[0][1], 123);
+  EXPECT_EQ (dim[0][2], 433);
+  EXPECT_EQ (dim[0][3], 177);
 }
 
 /**
@@ -185,13 +190,15 @@ TEST (common_get_tensor_dimension, case1)
  */
 TEST (common_get_tensor_dimension, case2)
 {
-  uint32_t dim[NNS_TENSOR_RANK_LIMIT];
-  int rank = get_tensor_dimension ("345:123:433", dim);
-  EXPECT_EQ (rank, 3);
-  EXPECT_EQ (dim[0], 345);
-  EXPECT_EQ (dim[1], 123);
-  EXPECT_EQ (dim[2], 433);
-  EXPECT_EQ (dim[3], 1);
+  uint32_t dim[NNS_TENSOR_SIZE_LIMIT][NNS_TENSOR_RANK_LIMIT];
+  int rank[NNS_TENSOR_RANK_LIMIT];
+  int num_tensors = get_tensor_dimension ("345:123:433", dim, rank);
+  EXPECT_EQ (num_tensors, 1);
+  EXPECT_EQ (rank[0], 3);
+  EXPECT_EQ (dim[0][0], 345);
+  EXPECT_EQ (dim[0][1], 123);
+  EXPECT_EQ (dim[0][2], 433);
+  EXPECT_EQ (dim[0][3], 1);
 }
 
 /**
@@ -199,13 +206,15 @@ TEST (common_get_tensor_dimension, case2)
  */
 TEST (common_get_tensor_dimension, case3)
 {
-  uint32_t dim[NNS_TENSOR_RANK_LIMIT];
-  int rank = get_tensor_dimension ("345:123", dim);
-  EXPECT_EQ (rank, 2);
-  EXPECT_EQ (dim[0], 345);
-  EXPECT_EQ (dim[1], 123);
-  EXPECT_EQ (dim[2], 1);
-  EXPECT_EQ (dim[3], 1);
+  uint32_t dim[NNS_TENSOR_SIZE_LIMIT][NNS_TENSOR_RANK_LIMIT];
+  int rank[NNS_TENSOR_RANK_LIMIT];
+  int num_tensors = get_tensor_dimension ("345:123", dim, rank);
+  EXPECT_EQ (num_tensors, 1);
+  EXPECT_EQ (rank[0], 2);
+  EXPECT_EQ (dim[0][0], 345);
+  EXPECT_EQ (dim[0][1], 123);
+  EXPECT_EQ (dim[0][2], 1);
+  EXPECT_EQ (dim[0][3], 1);
 }
 
 /**
@@ -213,13 +222,15 @@ TEST (common_get_tensor_dimension, case3)
  */
 TEST (common_get_tensor_dimension, case4)
 {
-  uint32_t dim[NNS_TENSOR_RANK_LIMIT];
-  int rank = get_tensor_dimension ("345", dim);
-  EXPECT_EQ (rank, 1);
-  EXPECT_EQ (dim[0], 345);
-  EXPECT_EQ (dim[1], 1);
-  EXPECT_EQ (dim[2], 1);
-  EXPECT_EQ (dim[3], 1);
+  uint32_t dim[NNS_TENSOR_SIZE_LIMIT][NNS_TENSOR_RANK_LIMIT];
+  int rank[NNS_TENSOR_RANK_LIMIT];
+  int num_tensors = get_tensor_dimension ("345", dim, rank);
+  EXPECT_EQ (num_tensors, 1);
+  EXPECT_EQ (rank[0], 1);
+  EXPECT_EQ (dim[0][0], 345);
+  EXPECT_EQ (dim[0][1], 1);
+  EXPECT_EQ (dim[0][2], 1);
+  EXPECT_EQ (dim[0][3], 1);
 }
 
 /**