[nnfw] update nnfw model dir path to model file path
authorParichay Kapoor <pk.kapoor@samsung.com>
Fri, 8 Nov 2019 07:26:01 +0000 (16:26 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Thu, 12 Dec 2019 06:18:50 +0000 (15:18 +0900)
Update the nnfw framework input model format to work with file path
nnfw framework takes in model directory path
Directory path is extracted from file path
Metadata is verified in the extracted directory path

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
ext/nnstreamer/tensor_filter/tensor_filter_nnfw.c
tests/tizen_nnfw_runtime/unittest_tizen_nnfw_runtime_raw.cpp

index 4a5ee34..1bae681 100644 (file)
@@ -55,7 +55,7 @@ typedef struct
   nnfw_tensorinfo i_in;
   nnfw_tensorinfo i_out;
   nnfw_session *session;
-  gchar *model_path;
+  gchar *model_file;
 } nnfw_pdata;
 
 static void nnfw_close (const GstTensorFilterProperties * prop,
@@ -70,10 +70,12 @@ nnfw_open (const GstTensorFilterProperties * prop, void **private_data)
   NNFW_STATUS status;
   int err = 0;
   nnfw_pdata *pdata;
+  char *model_path = NULL;
+  char *meta_file = NULL;
 
   if (*private_data != NULL) {
     pdata = *private_data;
-    if (g_strcmp0 (prop->model_files[0], pdata->model_path) != 0) {
+    if (g_strcmp0 (prop->model_files[0], pdata->model_file) != 0) {
       nnfw_close (prop, private_data);  /* "reopen" */
     } else {
       return 1;
@@ -93,7 +95,20 @@ nnfw_open (const GstTensorFilterProperties * prop, void **private_data)
     goto unalloc_exit;
   }
 
-  status = nnfw_load_model_from_file (pdata->session, prop->model_files[0]);
+  /** @note nnfw opens the first model listed in the MANIFEST file */
+  model_path = g_path_get_dirname (prop->model_files[0]);
+  meta_file = g_build_filename (model_path, "metadata", "MANIFEST", NULL);
+
+  if (!g_file_test (prop->model_files[0], G_FILE_TEST_IS_REGULAR) ||
+      !g_file_test (meta_file, G_FILE_TEST_IS_REGULAR)) {
+    err = -EINVAL;
+    g_printerr ("Model file (%s) or its metadata is not valid (not regular).",
+        prop->model_files[0]);
+    goto session_exit;
+  }
+
+  /* @todo open using model_file once nnfw works with it */
+  status = nnfw_load_model_from_file (pdata->session, model_path);
   if (status != NNFW_STATUS_NO_ERROR) {
     err = -EINVAL;
     g_printerr ("Cannot load the model file: %s", prop->model_files[0]);
@@ -108,13 +123,18 @@ nnfw_open (const GstTensorFilterProperties * prop, void **private_data)
     goto session_exit;
   }
 
-  pdata->model_path = g_strdup (prop->model_files[0]);
+  pdata->model_file = g_strdup (prop->model_files[0]);
+  g_free (meta_file);
+  g_free (model_path);
+
   return 0;
 
 session_exit:
   status = nnfw_close_session(pdata->session);
   if (status != NNFW_STATUS_NO_ERROR)
     g_printerr ("Closing the session just opened by %s has failed", __func__);
+  g_free (meta_file);
+  g_free (model_path);
 unalloc_exit:
   g_free (pdata);
   *private_data = NULL;
@@ -136,7 +156,7 @@ nnfw_close (const GstTensorFilterProperties * prop, void **private_data)
 
     if (status != NNFW_STATUS_NO_ERROR) {
       g_printerr ("cannot close nnfw-runtime session for %s",
-          pdata->model_path);
+          pdata->model_file);
     }
   } else {
     g_printerr ("nnfw_close called without proper nnfw_open");
@@ -145,8 +165,8 @@ nnfw_close (const GstTensorFilterProperties * prop, void **private_data)
   }
   pdata->session = NULL;
 
-  g_free (pdata->model_path);
-  pdata->model_path = NULL;
+  g_free (pdata->model_file);
+  pdata->model_file = NULL;
 
   g_free (pdata);
   *private_data = NULL;
index 7579201..3549919 100644 (file)
@@ -45,39 +45,50 @@ TEST (nnstreamer_nnfw_runtime_raw_functions, open_close_00_n)
   EXPECT_NE (ret, 0);
 }
 
-/**
- * @brief Test nnfw subplugin with successful open/close
- */
-TEST (nnstreamer_nnfw_runtime_raw_functions, open_close_01_n)
+static
+gboolean gen_tf_prop (GstTensorFilterProperties *prop)
 {
-  int ret;
-  void *data = NULL;
-  gchar *test_model;
+  gchar *model_file;
+  gchar *meta_file;
   gchar *model_path;
   const gchar *root_path = g_getenv ("NNSTREAMER_BUILD_ROOT_PATH");
 
-  ASSERT_NE (root_path, nullptr);
+  g_return_val_if_fail (root_path != nullptr, FALSE);
 
   /** nnfw needs a directory with model file and metadata in that directory */
   model_path = g_build_filename (root_path, "tests", "test_models", "models",
       NULL);
+
+  meta_file = g_build_filename (model_path, "metadata", "MANIFEST", NULL);
+  g_return_val_if_fail (g_file_test (meta_file, G_FILE_TEST_EXISTS), FALSE);
+  g_free (meta_file);
+
+  model_file = g_build_filename (model_path, "add.tflite", NULL);
+  g_return_val_if_fail (g_file_test (model_file, G_FILE_TEST_EXISTS), FALSE);
+  g_free (model_path);
+
+  prop->fwname = "nnfw";
+  prop->fw_opened = 0;
+
   const gchar *model_files[] = {
     model_path, NULL,
   };
-  GstTensorFilterProperties prop = {
-    .fwname = "nnfw",
-    .fw_opened = 0,
-    .model_files = model_files,
-    .num_models = 1,
-  };
+  prop->model_files = model_files;
+  prop->num_models = 1;
+
+  return TRUE;
+}
 
-  test_model = g_build_filename (model_path, "add.tflite", NULL);
-  ASSERT_TRUE (g_file_test (test_model, G_FILE_TEST_EXISTS));
-  g_free (test_model);
+/**
+ * @brief Test nnfw subplugin with successful open/close
+ */
+TEST (nnstreamer_nnfw_runtime_raw_functions, open_close_01_n)
+{
+  int ret;
+  void *data = NULL;
+  GstTensorFilterProperties prop;
 
-  test_model = g_build_filename (model_path, "metadata", "MANIFEST", NULL);
-  ASSERT_TRUE (g_file_test (test_model, G_FILE_TEST_EXISTS));
-  g_free (test_model);
+  ASSERT_TRUE (gen_tf_prop (&prop));
 
   const GstTensorFilterFramework *sp = nnstreamer_filter_find ("nnfw");
   EXPECT_NE (sp, (void *) NULL);
@@ -101,33 +112,10 @@ TEST (nnstreamer_nnfw_runtime_raw_functions, get_dimension)
 {
   int ret;
   void *data = NULL;
-  gchar *test_model;
-  gchar *model_path;
-  const gchar *root_path = g_getenv ("NNSTREAMER_BUILD_ROOT_PATH");
   GstTensorsInfo info, res;
+  GstTensorFilterProperties prop;
 
-  ASSERT_NE (root_path, nullptr);
-
-  /** nnfw needs a directory with model file and metadata in that directory */
-  model_path = g_build_filename (root_path, "tests", "test_models", "models",
-      NULL);
-  const gchar *model_files[] = {
-    model_path, NULL,
-  };
-  GstTensorFilterProperties prop = {
-    .fwname = "nnfw",
-    .fw_opened = 0,
-    .model_files = model_files,
-    .num_models = 1,
-  };
-
-  test_model = g_build_filename (model_path, "add.tflite", NULL);
-  ASSERT_TRUE (g_file_test (test_model, G_FILE_TEST_EXISTS));
-  g_free (test_model);
-
-  test_model = g_build_filename (model_path, "metadata", "MANIFEST", NULL);
-  ASSERT_TRUE (g_file_test (test_model, G_FILE_TEST_EXISTS));
-  g_free (test_model);
+  ASSERT_TRUE (gen_tf_prop (&prop));
 
   const GstTensorFilterFramework *sp = nnstreamer_filter_find ("nnfw");
   EXPECT_NE (sp, (void *) NULL);
@@ -183,34 +171,11 @@ TEST (nnstreamer_nnfw_runtime_raw_functions, invoke)
 {
   int ret;
   void *data = NULL;
-  gchar *test_model;
-  gchar *model_path;
-  const gchar *root_path = g_getenv ("NNSTREAMER_BUILD_ROOT_PATH");
   GstTensorMemory input, output;
 
-  ASSERT_NE (root_path, nullptr);
-
-  /** nnfw needs a directory with model file and metadata in that directory */
-  model_path = g_build_filename (root_path, "tests", "test_models", "models",
-      NULL);
-  const gchar *model_files[] = {
-    model_path, NULL,
-  };
-  GstTensorFilterProperties prop = {
-    .fwname = "nnfw",
-    .fw_opened = 0,
-    .model_files = model_files,
-    .num_models = 1,
-  };
-
-  /** this model adds 2 to the input data passed in float format */
-  test_model = g_build_filename (model_path, "add.tflite", NULL);
-  ASSERT_TRUE (g_file_test (test_model, G_FILE_TEST_EXISTS));
-  g_free (test_model);
+  GstTensorFilterProperties prop;
 
-  test_model = g_build_filename (model_path, "metadata", "MANIFEST", NULL);
-  ASSERT_TRUE (g_file_test (test_model, G_FILE_TEST_EXISTS));
-  g_free (test_model);
+  ASSERT_TRUE (gen_tf_prop (&prop));
 
   const GstTensorFilterFramework *sp = nnstreamer_filter_find ("nnfw");
   EXPECT_NE (sp, (void *) NULL);