[capi/util] Change framework auto-detection
authorgichan-jang <gichan2.jang@samsung.com>
Thu, 2 Apr 2020 04:06:08 +0000 (13:06 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Tue, 7 Apr 2020 02:33:46 +0000 (11:33 +0900)
Integrate duplicated code for framework auto detection.

Signed-off-by: gichan-jang <gichan2.jang@samsung.com>
api/capi/src/nnstreamer-capi-util.c
meson.build
meson_options.txt
nnstreamer.ini.in

index 945a333..4ed2867 100644 (file)
@@ -27,6 +27,7 @@
 #include "nnstreamer_plugin_api.h"
 #include "nnstreamer_plugin_api_filter.h"
 #include "nnstreamer_conf.h"
+#include <tensor_filter/tensor_filter_common.h>
 
 /**
  * @brief Allocates a tensors information handle with default value.
@@ -909,6 +910,7 @@ ml_validate_model_file (char **model, unsigned int num_models,
   /** @todo This is a hotfix. Make a general mechanism instead! (n paths) */
   gchar *path_down, *path_down_2 = NULL;
   int status = ML_ERROR_NONE;
+  gchar *detected_fw = NULL;
 
   if (!model) {
     ml_loge ("The required param, model is not provided (null).");
@@ -933,46 +935,36 @@ ml_validate_model_file (char **model, unsigned int num_models,
 
   switch (*nnfw) {
     case ML_NNFW_TYPE_ANY:
-      /** @todo Make sure num_models is correct */
-      if (g_str_has_suffix (path_down, ".tflite")) {
-        /**
-         * .tflite is supported by both tensorflow and nnfw.
-         * Priority decided with ini file.
-         */
-        gboolean nnfw_runtime_priority =
-            nnsconf_get_custom_value_bool ("nnfw-runtime",
-            "prioritize_tflite_ext", FALSE);
-        gboolean available_nnfw =
-            ml_nnfw_is_available (ML_NNFW_TYPE_NNFW, ML_NNFW_HW_ANY);
-        gboolean available_tflite =
-            ml_nnfw_is_available (ML_NNFW_TYPE_TENSORFLOW_LITE, ML_NNFW_HW_ANY);
-
-        if ((nnfw_runtime_priority && available_nnfw) ||
-            (!nnfw_runtime_priority && !available_tflite)) {
-          ml_logi ("The given model [%s] is supposed a nnfw model.", model[0]);
-          *nnfw = ML_NNFW_TYPE_NNFW;
-        } else {
-          ml_logi ("The given model [%s] is supposed a tensorflow-lite model.",
-              model[0]);
-          *nnfw = ML_NNFW_TYPE_TENSORFLOW_LITE;
-        }
-      } else if ((g_str_has_suffix (path_down, ".so") &&
-              g_str_has_suffix (path_down_2, ".nb")) ||
-          (g_str_has_suffix (path_down, ".nb") &&
-              g_str_has_suffix (path_down_2, ".so"))) {
+      detected_fw =
+          gst_tensor_filter_framework_auto_detection ((const gchar **) model,
+          num_models);
+
+      if (detected_fw == NULL) {
+        ml_loge ("The given model [%s] has unknown or not supported extension.",
+            model[0]);
+        status = ML_ERROR_INVALID_PARAMETER;
+      } else if (g_ascii_strcasecmp (detected_fw, "nnfw") == 0) {
+        ml_logi ("The given model [%s] is supposed a nnfw model.", model[0]);
+        *nnfw = ML_NNFW_TYPE_NNFW;
+      } else if (g_ascii_strcasecmp (detected_fw, "tensorflow-lite") == 0) {
+        ml_logi ("The given model [%s] is supposed a tensorflow-lite model.",
+            model[0]);
+        *nnfw = ML_NNFW_TYPE_TENSORFLOW_LITE;
+      } else if (g_ascii_strcasecmp (detected_fw, "vivante") == 0) {
         ml_logi ("The given model [%s,%s] looks like a Vivante model.",
             model[0], model[1]);
         *nnfw = ML_NNFW_TYPE_VIVANTE;
-      } else if (g_str_has_suffix (path_down, ".pb")) {
+      } else if (g_ascii_strcasecmp (detected_fw, "tensorflow") == 0) {
         ml_logi ("The given model [%s] is supposed a tensorflow model.",
             model[0]);
         *nnfw = ML_NNFW_TYPE_TENSORFLOW;
-      } else if (g_str_has_suffix (path_down, NNSTREAMER_SO_FILE_EXTENSION)) {
+      } else if (g_ascii_strcasecmp (detected_fw, "custom") == 0) {
         ml_logi ("The given model [%s] is supposed a custom filter model.",
             model[0]);
         *nnfw = ML_NNFW_TYPE_CUSTOM_FILTER;
       } else {
-        ml_loge ("The given model [%s] has unknown extension.", model[0]);
+        ml_loge ("The given model [%s] has unknown or not supported extension.",
+            model[0]);
         status = ML_ERROR_INVALID_PARAMETER;
       }
 
@@ -981,6 +973,9 @@ ml_validate_model_file (char **model, unsigned int num_models,
           status = ML_ERROR_NOT_SUPPORTED;
       }
 
+      if (detected_fw != NULL)
+        g_free (detected_fw);
+
       break;
     case ML_NNFW_TYPE_CUSTOM_FILTER:
       /** @todo Make sure num_models is correct */
index 0850212..21e06bb 100644 (file)
@@ -298,7 +298,6 @@ nnstreamer_install_conf.merge_from(nnstreamer_conf)
 nnstreamer_install_conf.set('ENABLE_ENV_VAR', get_option('enable-env-var'))
 nnstreamer_install_conf.set('ENABLE_SYMBOLIC_LINK', get_option('enable-symbolic-link'))
 nnstreamer_install_conf.set('TORCH_USE_GPU', get_option('enable-pytorch-use-gpu'))
-nnstreamer_install_conf.set('NNFW_RUNTIME_PRIORITIZE', get_option('nnfw-runtime-prioritize'))
 
 # Element restriction
 restriction_config = ''
@@ -349,7 +348,6 @@ if get_option('enable-test')
   nnstreamer_test_conf.set('ENABLE_SYMBOLIC_LINK', false)
   nnstreamer_test_conf.set('TORCH_USE_GPU', false)
   nnstreamer_test_conf.set('ELEMENT_RESTRICTION_CONFIG', '')
-  nnstreamer_test_conf.set('NNFW_RUNTIME_PRIORITIZE', false)
 
   configure_file(input: 'nnstreamer.ini.in', output: 'nnstreamer-test.ini',
     install: get_option('install-test'),
index e5f5c85..13a732b 100644 (file)
@@ -19,7 +19,6 @@ option('enable-element-restriction', type: 'boolean', value: false) # true to re
 option('restricted-elements', type: 'string', value: '')
 option('enable-tflite-nnapi-delegation', type: 'boolean', value: false) # true to enable tensorflow-lite to delegate nnapi interpretation to nnfw backend in tizen
 option('enable-nnfw-runtime', type: 'boolean', value: false) # true to enable nnfw tensor filter element
-option('nnfw-runtime-prioritize', type: 'boolean', value: false) # true to set higher priority for nnfw for tflite extension files in auto mode
 option('enable-cppfilter', type: 'boolean', value: true) # Allows C++ custom filters
 option('enable-filter-cpp-class', type: 'boolean', value: false) # Allows to accept C++ classes as filter subplugin implementation.
 option('enable-tizen-sensor', type: 'boolean', value: false)
index 6b50eb6..70ded91 100644 (file)
@@ -26,8 +26,4 @@ enable_nnapi=False
 [pytorch]
 enable_use_gpu=@TORCH_USE_GPU@
 
-# Set 1 or True if you want to prioritize nnfw over tensorflow lite for .tflite extension model files when automatically selecting framework for tensor filter.
-[nnfw-runtime]
-prioritize_tflite_ext=@NNFW_RUNTIME_PRIORITIZE@
-
 @ELEMENT_RESTRICTION_CONFIG@