[Filter/TFLite] fix bug
authorHyoungjooAhn <hello.ahnn@gmail.com>
Fri, 16 Nov 2018 02:19:59 +0000 (11:19 +0900)
committerMyungJoo Ham <myungjoo.ham@gmail.com>
Mon, 19 Nov 2018 06:10:08 +0000 (06:10 +0000)
fix NULL ptr error by adding initialize function

Signed-off-by: HyoungjooAhn <hello.ahnn@gmail.com>
gst/tensor_filter/tensor_filter_tensorflow_lite.c
gst/tensor_filter/tensor_filter_tensorflow_lite_core.cc
gst/tensor_filter/tensor_filter_tensorflow_lite_core.h

index 571be2b..e5d93c6 100644 (file)
@@ -43,7 +43,9 @@ typedef struct _Tflite_data tflite_data;
  * @brief Load tensorflow lite modelfile
  * @param filter : tensor_filter instance
  * @param private_data : tensorflow lite plugin's private data
- * @return 0 if successfully loaded. 1 if skipped (already loaded). -1 if error
+ * @return 0 if successfully loaded. 1 if skipped (already loaded).
+ *        -1 if the object construction is failed.
+ *        -2 if the object initialization if failed
  */
 static int
 tflite_loadModelFile (const GstTensorFilter * filter, void **private_data)
@@ -57,6 +59,8 @@ tflite_loadModelFile (const GstTensorFilter * filter, void **private_data)
   *private_data = tf;
   tf->tflite_private_data = tflite_core_new (filter->prop.model_file);
   if (tf->tflite_private_data) {
+    if (tflite_core_init (tf->tflite_private_data))
+      return -2;
     return 0;
   } else {
     return -1;
index 79ea3a4..67dd44f 100644 (file)
@@ -48,9 +48,6 @@
 TFLiteCore::TFLiteCore (const char *_model_path)
 {
   model_path = _model_path;
-  loadModel ();
-  setInputTensorProp ();
-  setOutputTensorProp ();
 }
 
 /**
@@ -62,6 +59,34 @@ TFLiteCore::~TFLiteCore ()
 }
 
 /**
+ * @brief      initialize the object with tflite model
+ * @return 0 if OK. non-zero if error.
+ *        -1 if the model is not loaded.
+ *        -2 if the initialization of input tensor is failed.
+ *        -3 if the initialization of output tensor is failed.
+ */
+int
+TFLiteCore::init()
+{
+  if(loadModel ())
+  {
+    _print_log ("Failed to load model\n");
+    return -1;
+  }
+  if(setInputTensorProp ())
+  {
+    _print_log ("Failed to initialize input tensor\n");
+    return -2;
+  }
+  if(setOutputTensorProp ())
+  {
+    _print_log ("Failed to initialize output tensor\n");
+    return -3;
+  }
+  return 0;
+}
+
+/**
  * @brief      get millisecond for time profiling.
  * @note       it returns the millisecond.
  * @param t    : the time struct.
@@ -351,7 +376,7 @@ TFLiteCore::invoke (const GstTensorMemory * input, GstTensorMemory * output)
  * @param      _model_path     : the logical path to '{model_name}.tffile' file
  * @return     TFLiteCore class
  */
-extern void *
+void *
 tflite_core_new (const char *_model_path)
 {
   return new TFLiteCore (_model_path);
@@ -362,7 +387,7 @@ tflite_core_new (const char *_model_path)
  * @param      tflite  : the class object
  * @return     Nothing
  */
-extern void
+void
 tflite_core_delete (void *tflite)
 {
   TFLiteCore *c = (TFLiteCore *) tflite;
@@ -370,6 +395,18 @@ tflite_core_delete (void *tflite)
 }
 
 /**
+ * @brief      initialize the object with tflite model
+ * @return 0 if OK. non-zero if error.
+ */
+int
+tflite_core_init (void *tflite)
+{
+  TFLiteCore *c = (TFLiteCore *) tflite;
+  int ret = c->init ();
+  return ret;
+}
+
+/**
  * @brief      get the Dimension of Input Tensor of model
  * @param      tflite  : the class object
  * @param[out] info Structure for tensor info.
index ed3fa7a..a4dd6f1 100644 (file)
@@ -44,6 +44,7 @@ public:
   TFLiteCore (const char *_model_path);
   ~TFLiteCore ();
 
+  int init();
   int loadModel ();
   int setInputTensorProp ();
   int setOutputTensorProp ();
@@ -77,6 +78,7 @@ extern "C"
 
   extern void *tflite_core_new (const char *_model_path);
   extern void tflite_core_delete (void *tflite);
+  extern int tflite_core_init (void *tflite);
   extern const char *tflite_core_getModelPath (void *tflite);
   extern int tflite_core_getInputDim (void *tflite, GstTensorsInfo * info);
   extern int tflite_core_getOutputDim (void *tflite, GstTensorsInfo * info);