From 267fd40092bece18ee51337645c5ca1d2b972dbd Mon Sep 17 00:00:00 2001 From: Hyoung Joo Ahn Date: Thu, 24 Jan 2019 21:39:51 +0900 Subject: [PATCH] [Filter/NNFW] clean code of tensorflow & tf-lite adjust the style and remove useless keywords Signed-off-by: Hyoung Joo Ahn --- .../tensor_filter/tensor_filter_tensorflow_core.cc | 129 +++++++++++---------- .../tensor_filter/tensor_filter_tensorflow_core.h | 24 ++-- .../tensor_filter_tensorflow_lite_core.cc | 20 ++-- .../tensor_filter_tensorflow_lite_core.h | 24 ++-- 4 files changed, 99 insertions(+), 98 deletions(-) diff --git a/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.cc b/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.cc index 36e76fc..8af38b0 100644 --- a/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.cc +++ b/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.cc @@ -33,7 +33,7 @@ #define DBG FALSE #endif -std::map TFCore::outputTensorMap; +std::map TFCore::outputTensorMap; /** * @brief TFCore creator @@ -41,7 +41,7 @@ std::map TFCore::outputTensorMap; * @note the model of _model_path will be loaded simultaneously * @return Nothing */ -TFCore::TFCore (const char *_model_path) +TFCore::TFCore (const char * _model_path) { model_path = _model_path; @@ -113,34 +113,34 @@ TFCore::loadModel () #endif GraphDef graph_def; Status load_graph_status = - ReadBinaryProto(Env::Default(), model_path, &graph_def); + ReadBinaryProto (Env::Default (), model_path, &graph_def); if (!load_graph_status.ok()) { GST_ERROR ("Failed to load compute graph at '%s'", model_path); return -1; } /* get input tensor */ - std::vector placeholders; - for (const NodeDef& node : graph_def.node()) { - if (node.op() == "Placeholder") { - placeholders.push_back(&node); + std::vector placeholders; + for (const NodeDef& node : graph_def.node ()) { + if (node.op () == "Placeholder") { + placeholders.push_back (&node); } } - if (placeholders.empty()) { + if (placeholders.empty ()) { GST_WARNING ("No inputs spotted."); } else { - GST_INFO ("Found possible inputs: %ld", placeholders.size()); - if (inputTensorValidation(placeholders)) { + GST_INFO ("Found possible inputs: %ld", placeholders.size ()); + if (inputTensorValidation (placeholders)) { GST_ERROR ("Input Tensor Information is not valid"); return -2; } } /* get session */ - Status new_session_status = NewSession(SessionOptions(), &session); - Status session_create_status = session->Create(graph_def); - if (!new_session_status.ok() || !session_create_status.ok()) { + Status new_session_status = NewSession (SessionOptions (), &session); + Status session_create_status = session->Create (graph_def); + if (!new_session_status.ok () || !session_create_status.ok ()) { GST_ERROR ("Create Tensorflow Session was Failed"); return -3; } @@ -235,67 +235,67 @@ TFCore::getTensorTypeToTF (tensor_type tType) * -5 if the rank of input tensors exceeds our capacity NNS_TENSOR_RANK_LIMIT. */ int -TFCore::inputTensorValidation (const std::vector &placeholders) +TFCore::inputTensorValidation (const std::vector &placeholders) { - int length = placeholders.size(); + int length = placeholders.size (); - if (inputTensorMeta.num_tensors != length){ + if (inputTensorMeta.num_tensors != length) { GST_ERROR ("Input Tensor is not valid: the number of input tensor is different\n"); return -1; } - for (int i = 0; i < length; i++) { + for (int i = 0; i < length; ++i) { const NodeDef* node = placeholders[i]; string shape_description = "None"; - if (node->attr().count("shape")) { - TensorShapeProto shape_proto = node->attr().at("shape").shape(); - Status shape_status = PartialTensorShape::IsValidShape(shape_proto); - if (shape_status.ok()) { - shape_description = PartialTensorShape(shape_proto).DebugString(); + if (node->attr ().count ("shape")) { + TensorShapeProto shape_proto = node->attr ().at ("shape").shape (); + Status shape_status = PartialTensorShape::IsValidShape (shape_proto); + if (shape_status.ok ()) { + shape_description = PartialTensorShape (shape_proto).DebugString (); } else { - shape_description = shape_status.error_message(); + shape_description = shape_status.error_message (); } } char chars[] = "[]"; - for (unsigned int i = 0; i < strlen(chars); ++i) + for (unsigned int i = 0; i < strlen (chars); ++i) { shape_description.erase ( - std::remove( - shape_description.begin(), - shape_description.end(), + std::remove ( + shape_description.begin (), + shape_description.end (), chars[i] ), - shape_description.end() + shape_description.end () ); } DataType dtype = DT_INVALID; - if (node->attr().count("dtype")) { - dtype = node->attr().at("dtype").type(); + if (node->attr ().count ("dtype")) { + dtype = node->attr ().at ("dtype").type (); } - if (strcmp (inputTensorMeta.info[i].name, node->name().c_str())){ + if (strcmp (inputTensorMeta.info[i].name, node->name ().c_str ())) { GST_ERROR ("Input Tensor is not valid: the name of input tensor is different\n"); return -2; } - if (inputTensorMeta.info[i].type != getTensorTypeFromTF(dtype)){ + if (inputTensorMeta.info[i].type != getTensorTypeFromTF (dtype)) { GST_ERROR ("Input Tensor is not valid: the type of input tensor is different\n"); return -3; } gchar **str_dims; - str_dims = g_strsplit (shape_description.c_str(), ",", -1); + str_dims = g_strsplit (shape_description.c_str (), ",", -1); inputTensorRank[i] = g_strv_length (str_dims); if (inputTensorRank[i] > NNS_TENSOR_RANK_LIMIT) { GST_ERROR ("The Rank of Input Tensor is not affordable. It's over our capacity.\n"); g_strfreev (str_dims); return -5; } - for (int j = 0; j < inputTensorRank[i]; j++) { + for (int j = 0; j < inputTensorRank[i]; ++j) { if (!strcmp (str_dims[j], "?")) continue; - if (inputTensorMeta.info[i].dimension[inputTensorRank[i] - j - 1] != atoi (str_dims[j])){ + if (inputTensorMeta.info[i].dimension[inputTensorRank[i] - j - 1] != atoi (str_dims[j])) { GST_ERROR ("Input Tensor is not valid: the dim of input tensor is different\n"); g_strfreev (str_dims); return -4; @@ -370,11 +370,11 @@ TFCore::getOutputTensorDim (GstTensorsInfo * info) #define copyInputWithType(type) do { \ for (int idx = 0; idx < array_len; ++idx) \ - inputTensor.flat()(idx) = ((type*)input[i].data)[idx]; \ + inputTensor.flat ()(idx) = ((type*) input[i].data)[idx]; \ } while (0) #define copyOutputWithType(type) do { \ - output[i].data = outputs[i].flat().data(); \ + output[i].data = outputs[i].flat ().data (); \ outputTensorMap.insert (std::make_pair (output[i].data, outputs[i])); \ } while (0) @@ -388,18 +388,18 @@ TFCore::getOutputTensorDim (GstTensorsInfo * info) int TFCore::run (const GstTensorMemory * input, GstTensorMemory * output) { - std::vector> input_feeds; - std::vector output_tensor_names; - std::vector outputs; + std::vector > input_feeds; + std::vector output_tensor_names; + std::vector outputs; int array_len; - for (int i = 0; i < inputTensorMeta.num_tensors; i++) { - TensorShape ts = TensorShape({}); - for (int j = inputTensorRank[i] - 1; j >= 0; j--){ - ts.AddDim(inputTensorMeta.info[i].dimension[j]); + for (int i = 0; i < inputTensorMeta.num_tensors; ++i) { + TensorShape ts = TensorShape ({}); + for (int j = inputTensorRank[i] - 1; j >= 0; j--) { + ts.AddDim (inputTensorMeta.info[i].dimension[j]); } - Tensor inputTensor( - getTensorTypeToTF(input[i].type), + Tensor inputTensor ( + getTensorTypeToTF (input[i].type), ts ); @@ -441,25 +441,25 @@ TFCore::run (const GstTensorMemory * input, GstTensorMemory * output) break; } - input_feeds.push_back({inputTensorMeta.info[i].name, inputTensor}); + input_feeds.push_back ({inputTensorMeta.info[i].name, inputTensor}); } - for (int i = 0; i < outputTensorMeta.num_tensors; i++) { - output_tensor_names.push_back(outputTensorMeta.info[i].name); + for (int i = 0; i < outputTensorMeta.num_tensors; ++i) { + output_tensor_names.push_back (outputTensorMeta.info[i].name); } Status run_status = - session->Run(input_feeds, output_tensor_names, {}, &outputs); + session->Run (input_feeds, output_tensor_names, {}, &outputs); - if (run_status != Status::OK ()){ + if (run_status != Status::OK ()) { GST_ERROR ("Failed to run model: %s\n", (run_status.ToString ()).c_str ()); return -1; } - for (int i = 0; i < outputTensorMeta.num_tensors; i++) { + for (int i = 0; i < outputTensorMeta.num_tensors; ++i) { output[i].type = getTensorTypeFromTF (outputs[i].dtype()); output[i].size = tensor_element_size[output[i].type]; - for (int j = 0; j < NNS_TENSOR_RANK_LIMIT; j++) + for (int j = 0; j < NNS_TENSOR_RANK_LIMIT; ++j) output[i].size *= outputTensorMeta.info[i].dimension[j]; array_len = output[i].size / tensor_element_size[output[i].type]; @@ -504,8 +504,8 @@ TFCore::run (const GstTensorMemory * input, GstTensorMemory * output) return 0; } -extern void * -tf_core_new (const char *_model_path) +void * +tf_core_new (const char * _model_path) { return new TFCore (_model_path); } @@ -515,8 +515,8 @@ tf_core_new (const char *_model_path) * @param tf : the class object * @return Nothing */ -extern void -tf_core_delete (void *tf) +void +tf_core_delete (void * tf) { TFCore *c = (TFCore *) tf; delete c; @@ -528,7 +528,7 @@ tf_core_delete (void *tf) * @return 0 if OK. non-zero if error. */ int -tf_core_init (void *tf, const GstTensorFilterProperties * prop) +tf_core_init (void * tf, const GstTensorFilterProperties * prop) { TFCore *c = (TFCore *) tf; int ret = c->init (prop); @@ -540,8 +540,8 @@ tf_core_init (void *tf, const GstTensorFilterProperties * prop) * @param tf : the class object * @return model path */ -extern const char * -tf_core_getModelPath (void *tf) +const char * +tf_core_getModelPath (void * tf) { TFCore *c = (TFCore *) tf; return c->getModelPath (); @@ -554,7 +554,7 @@ tf_core_getModelPath (void *tf) * @return 0 if OK. non-zero if error. */ int -tf_core_getInputDim (void *tf, GstTensorsInfo * info) +tf_core_getInputDim (void * tf, GstTensorsInfo * info) { TFCore *c = (TFCore *) tf; return c->getInputTensorDim (info); @@ -567,7 +567,7 @@ tf_core_getInputDim (void *tf, GstTensorsInfo * info) * @return 0 if OK. non-zero if error. */ int -tf_core_getOutputDim (void *tf, GstTensorsInfo * info) +tf_core_getOutputDim (void * tf, GstTensorsInfo * info) { TFCore *c = (TFCore *) tf; return c->getOutputTensorDim (info); @@ -581,7 +581,7 @@ tf_core_getOutputDim (void *tf, GstTensorsInfo * info) * @return 0 if OK. non-zero if error. */ int -tf_core_run (void *tf, const GstTensorMemory * input, GstTensorMemory * output) +tf_core_run (void * tf, const GstTensorMemory * input, GstTensorMemory * output) { TFCore *c = (TFCore *) tf; return c->run (input, output); @@ -592,6 +592,7 @@ tf_core_run (void *tf, const GstTensorMemory * input, GstTensorMemory * output) * @param[in] data : the data element destroyed at the pipeline */ void -tf_core_destroyNotify (void * data){ +tf_core_destroyNotify (void * data) +{ TFCore::outputTensorMap.erase (data); } diff --git a/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.h b/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.h index 34435fc..835f27c 100644 --- a/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.h +++ b/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.h @@ -61,7 +61,7 @@ public: /** * member functions. */ - TFCore (const char *_model_path); + TFCore (const char * _model_path); ~TFCore (); int init(const GstTensorFilterProperties * prop); @@ -76,7 +76,7 @@ public: int getOutputTensorDim (GstTensorsInfo * info); int run (const GstTensorMemory * input, GstTensorMemory * output); - static std::map outputTensorMap; + static std::map outputTensorMap; private: @@ -88,12 +88,12 @@ private: int inputTensorRank[NNS_TENSOR_SIZE_LIMIT]; int outputTensorRank[NNS_TENSOR_SIZE_LIMIT]; - Session * session; + Session *session; tensor_type getTensorTypeFromTF (DataType tfType); DataType getTensorTypeToTF (tensor_type tType); int setTensorProp (GstTensorsInfo * dest, const GstTensorsInfo * src); - int inputTensorValidation (const std::vector &placeholders); + int inputTensorValidation (const std::vector &placeholders); }; /** @@ -103,15 +103,15 @@ extern "C" { #endif - extern void *tf_core_new (const char *_model_path); - extern void tf_core_delete (void *tf); - extern int tf_core_init (void *tf, const GstTensorFilterProperties * prop); - extern const char *tf_core_getModelPath (void *tf); - extern int tf_core_getInputDim (void *tf, GstTensorsInfo * info); - extern int tf_core_getOutputDim (void *tf, GstTensorsInfo * info); - extern int tf_core_run (void *tf, const GstTensorMemory * input, + void *tf_core_new (const char *_model_path); + void tf_core_delete (void * tf); + int tf_core_init (void * tf, const GstTensorFilterProperties * prop); + const char *tf_core_getModelPath (void * tf); + int tf_core_getInputDim (void * tf, GstTensorsInfo * info); + int tf_core_getOutputDim (void * tf, GstTensorsInfo * info); + int tf_core_run (void * tf, const GstTensorMemory * input, GstTensorMemory * output); - extern void tf_core_destroyNotify (void * data); + void tf_core_destroyNotify (void * data); #ifdef __cplusplus } diff --git a/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_lite_core.cc b/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_lite_core.cc index 5253178..3208433 100644 --- a/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_lite_core.cc +++ b/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_lite_core.cc @@ -39,7 +39,7 @@ * @note the model of _model_path will be loaded simultaneously * @return Nothing */ -TFLiteCore::TFLiteCore (const char *_model_path) +TFLiteCore::TFLiteCore (const char * _model_path) { model_path = _model_path; @@ -104,7 +104,7 @@ TFLiteCore::loadModel () if (!interpreter) { model = - std::unique_ptr < tflite::FlatBufferModel > + std::unique_ptr (tflite::FlatBufferModel::BuildFromFile (model_path)); if (!model) { GST_ERROR ("Failed to mmap model\n"); @@ -318,7 +318,7 @@ TFLiteCore::invoke (const GstTensorMemory * input, GstTensorMemory * output) gint64 start_time = g_get_real_time (); #endif - std::vector tensors_idx; + std::vector tensors_idx; int tensor_idx; TfLiteTensor *tensor_ptr; @@ -366,7 +366,7 @@ TFLiteCore::invoke (const GstTensorMemory * input, GstTensorMemory * output) * @return TFLiteCore class */ void * -tflite_core_new (const char *_model_path) +tflite_core_new (const char * _model_path) { return new TFLiteCore (_model_path); } @@ -377,7 +377,7 @@ tflite_core_new (const char *_model_path) * @return Nothing */ void -tflite_core_delete (void *tflite) +tflite_core_delete (void * tflite) { TFLiteCore *c = (TFLiteCore *) tflite; delete c; @@ -389,7 +389,7 @@ tflite_core_delete (void *tflite) * @return 0 if OK. non-zero if error. */ int -tflite_core_init (void *tflite) +tflite_core_init (void * tflite) { TFLiteCore *c = (TFLiteCore *) tflite; int ret = c->init (); @@ -402,7 +402,7 @@ tflite_core_init (void *tflite) * @return the model path. */ const char * -tflite_core_getModelPath (void *tflite) +tflite_core_getModelPath (void * tflite) { TFLiteCore *c = (TFLiteCore *) tflite; return c->getModelPath (); @@ -415,7 +415,7 @@ tflite_core_getModelPath (void *tflite) * @return 0 if OK. non-zero if error. */ int -tflite_core_getInputDim (void *tflite, GstTensorsInfo * info) +tflite_core_getInputDim (void * tflite, GstTensorsInfo * info) { TFLiteCore *c = (TFLiteCore *) tflite; int ret = c->getInputTensorDim (info); @@ -429,7 +429,7 @@ tflite_core_getInputDim (void *tflite, GstTensorsInfo * info) * @return 0 if OK. non-zero if error. */ int -tflite_core_getOutputDim (void *tflite, GstTensorsInfo * info) +tflite_core_getOutputDim (void * tflite, GstTensorsInfo * info) { TFLiteCore *c = (TFLiteCore *) tflite; int ret = c->getOutputTensorDim (info); @@ -444,7 +444,7 @@ tflite_core_getOutputDim (void *tflite, GstTensorsInfo * info) * @return 0 if OK. non-zero if error. */ int -tflite_core_invoke (void *tflite, const GstTensorMemory * input, +tflite_core_invoke (void * tflite, const GstTensorMemory * input, GstTensorMemory * output) { TFLiteCore *c = (TFLiteCore *) tflite; diff --git a/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_lite_core.h b/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_lite_core.h index 9d7386c..b102f5b 100644 --- a/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_lite_core.h +++ b/gst/nnstreamer/tensor_filter/tensor_filter_tensorflow_lite_core.h @@ -41,12 +41,12 @@ class TFLiteCore { public: - TFLiteCore (const char *_model_path); + TFLiteCore (const char * _model_path); ~TFLiteCore (); - int init(); + int init (); int loadModel (); - const char* getModelPath(); + const char* getModelPath (); int setInputTensorProp (); int setOutputTensorProp (); int getInputTensorDim (GstTensorsInfo * info); @@ -60,8 +60,8 @@ private: GstTensorsInfo inputTensorMeta; /**< The tensor info of input tensors */ GstTensorsInfo outputTensorMeta; /**< The tensor info of output tensors */ - std::unique_ptr < tflite::Interpreter > interpreter; - std::unique_ptr < tflite::FlatBufferModel > model; + std::unique_ptr interpreter; + std::unique_ptr model; int getInputTensorSize (); int getOutputTensorSize (); @@ -76,13 +76,13 @@ extern "C" { #endif - 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); - extern int tflite_core_invoke (void *tflite, const GstTensorMemory * input, + void *tflite_core_new (const char *_model_path); + void tflite_core_delete (void * tflite); + int tflite_core_init (void * tflite); + const char *tflite_core_getModelPath (void * tflite); + int tflite_core_getInputDim (void * tflite, GstTensorsInfo * info); + int tflite_core_getOutputDim (void * tflite, GstTensorsInfo * info); + int tflite_core_invoke (void * tflite, const GstTensorMemory * input, GstTensorMemory * output); #ifdef __cplusplus -- 2.7.4