Add nnapi property in the tensor_filter element.
Use nnapi=(true|false)(:(cpu|gpu|npu)) option for tensor_filter
element. For example,
gst-launch-1.0 --gst-plugin-path=${PATH_TO_PLUGIN} filesrc
location=${PATH_TO_IMAGE} ! pngdec ! videoscale ! imagefreeze !
videoconvert ! video/x-raw,format=RGB,framerate=0/1 ! tensor_converter
! tensor_filter framework=tensorflow-lite model=${PATH_TO_MODEL} nnapi=true:gpu ! filesink location=tensorfilter.out.log
**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>
#include <string.h>
#include "tensor_filter_tensorflow_lite_core.h"
+#include "tensor_common.h"
+
+/**
+ * @brief nnapi hw type string
+ */
+static const char *nnapi_hw_string[] = {
+ [NNAPI_CPU] = "cpu",
+ [NNAPI_GPU] = "gpu",
+ [NNAPI_NPU] = "npu",
+ [NNAPI_UNKNOWN] = "unknown"
+};
void init_filter_tflite (void) __attribute__ ((constructor));
void fini_filter_tflite (void) __attribute__ ((destructor));
}
/**
+ * @brief return nnapi_hw type
+ */
+static nnapi_hw
+get_nnapi_hw_type (const gchar * str)
+{
+ gint index = 0;
+ index = find_key_strv (nnapi_hw_string, str);
+ return (index < 0) ? NNAPI_UNKNOWN : index;
+}
+
+/**
* @brief Load tensorflow lite modelfile
* @param prop property of tensor_filter instance
* @param private_data : tensorflow lite plugin's private data
void **private_data)
{
tflite_data *tf;
+ nnapi_hw hw = NNAPI_UNKNOWN;
+ if (prop->nnapi) {
+ gchar **strv = NULL;
+ strv = g_strsplit (prop->nnapi, ":", 2);
+ if (!g_strcmp0 (strv[0], "true") && g_strv_length (strv) >= 2) {
+ hw = get_nnapi_hw_type (strv[1]);
+ } else if (!g_strcmp0 (strv[0], "true") && g_strv_length (strv) == 1) {
+ /** defalut hw for nnapi is CPU */
+ hw = NNAPI_CPU;
+ }
+ }
+
if (*private_data != NULL) {
/** @todo : Check the integrity of filter->data and filter->model_file, nnfw */
tf = *private_data;
}
tf = g_new0 (tflite_data, 1); /** initialize tf Fill Zero! */
*private_data = tf;
- tf->tflite_private_data = tflite_core_new (prop->model_file);
+ tf->tflite_private_data = tflite_core_new (prop->model_file, hw);
if (tf->tflite_private_data) {
if (tflite_core_init (tf->tflite_private_data)) {
g_printerr ("failed to initialize the object: Tensorflow-lite");
* @note the model of _model_path will be loaded simultaneously
* @return Nothing
*/
-TFLiteCore::TFLiteCore (const char * _model_path)
+TFLiteCore::TFLiteCore (const char * _model_path, nnapi_hw hw)
{
model_path = _model_path;
- use_nnapi = nnsconf_get_custom_value_bool ("tensorflowlite", "enable_nnapi", FALSE);
+ if(hw == NNAPI_UNKNOWN){
+ use_nnapi = nnsconf_get_custom_value_bool ("tensorflowlite", "enable_nnapi", FALSE);
+ } else {
+ use_nnapi = TRUE;
+ accel = hw;
+ }
gst_tensors_info_init (&inputTensorMeta);
gst_tensors_info_init (&outputTensorMeta);
* @return TFLiteCore class
*/
void *
-tflite_core_new (const char * _model_path)
+tflite_core_new (const char * _model_path, nnapi_hw hw)
{
- return new TFLiteCore (_model_path);
+ return new TFLiteCore (_model_path, hw);
}
/**
class TFLiteCore
{
public:
- TFLiteCore (const char * _model_path);
+ TFLiteCore (const char *_model_path, nnapi_hw hw);
~TFLiteCore ();
int init ();
const char *model_path;
bool use_nnapi;
+ nnapi_hw accel;
GstTensorsInfo inputTensorMeta; /**< The tensor info of input tensors */
GstTensorsInfo outputTensorMeta; /**< The tensor info of output tensors */
{
#endif
- void *tflite_core_new (const char *_model_path);
+ void *tflite_core_new (const char *_model_path, nnapi_hw hw);
void tflite_core_delete (void * tflite);
int tflite_core_init (void * tflite);
const char *tflite_core_getModelPath (void * tflite);
#endif
/**
+ * @brief nnapi hw properties.
+ */
+typedef enum
+{
+ NNAPI_CPU = 0,
+ NNAPI_GPU = 1,
+ NNAPI_NPU = 2,
+
+ NNAPI_UNKNOWN
+} nnapi_hw;
+
+/**
* @brief GstTensorFilter's properties for NN framework (internal data structure)
*
* Because custom filters of tensor_filter may need to access internal data
GstTensorsInfo output_meta; /**< configured output tensor info */
const char *custom_properties; /**< sub-plugin specific custom property values in string */
+ const char *nnapi;
+
} GstTensorFilterProperties;
/**
#define g_free_const(x) g_free((void*)(long)(x))
+#define REGEX_NNAPI_OPTION "(^(true|false)$|^(true|false)([:]?(cpu|gpu|npu)))"
+
/**
* @brief Validate filter sub-plugin's data.
*/
PROP_OUTPUTTYPE,
PROP_OUTPUTNAME,
PROP_CUSTOM,
- PROP_SUBPLUGINS
+ PROP_SUBPLUGINS,
+ PROP_NNAPI
};
/**
g_param_spec_string ("sub-plugins", "Sub-plugins",
"Registrable sub-plugins list", "",
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
-
+ g_object_class_install_property (gobject_class, PROP_NNAPI,
+ g_param_spec_string ("nnapi", "NNAPI",
+ "Enable Neural Newtorks API ?", "", G_PARAM_READWRITE));
gst_element_class_set_details_simple (gstelement_class,
"Tensor_Filter",
"Converter/Filter/Tensor",
prop->input_configured = FALSE;
prop->output_configured = FALSE;
prop->model_file = NULL;
+ prop->nnapi = NULL;
prop->custom_properties = NULL;
gst_tensors_info_init (&prop->input_meta);
gst_tensors_info_init (&prop->output_meta);
prop->custom_properties = g_value_dup_string (value);
silent_debug ("Custom Option = %s\n", prop->custom_properties);
break;
+ case PROP_NNAPI:
+ prop->nnapi = g_value_dup_string (value);
+ if (!g_regex_match_simple (REGEX_NNAPI_OPTION, prop->nnapi, 0, 0)) {
+ gchar *name = gst_object_get_name ((GstObject *) object);
+ g_critical
+ ("%s: nnapi: \'%s\' is not valid string: it should be in the form of BOOL:ACCELLERATOR or BOOL with a regex, "
+ REGEX_NNAPI_OPTION "\n", name, prop->nnapi);
+ g_free (name);
+ break;
+ }
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
g_value_take_string (value, g_string_free (subplugins, FALSE));
break;
}
+ case PROP_NNAPI:
+ g_value_set_string (value, prop->nnapi);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
add_project_arguments('-D__TIZEN__=1', language: ['c', 'cpp'])
endif
-# nnfw
+# nnfw ( details in https://review.tizen.org/gerrit/p/platform/core/ml/nnfw )
if get_option('enable-nnfw')
add_project_arguments('-DENABLE_NNFW=1', language: ['c', 'cpp'])
endif