mv_machine_learning: use MachineLearningConfig class for other task groups
authorInki Dae <inki.dae@samsung.com>
Fri, 10 Nov 2023 04:03:59 +0000 (13:03 +0900)
committerKwanghoon Son <k.son@samsung.com>
Tue, 14 Nov 2023 07:52:55 +0000 (16:52 +0900)
[Issue type] : code refactoring

Use MachineLearningConfig class for other task groups instead of internal ones.

We have introduced a new common config class[1] and object detection and
object detection 3d task groups use already it. So this patch makes
other task groups - which not use the common config class - to use the
common class instead of internal ones, and drop the internal config class.

In addition, this patch applies confidence threshold config key for
landmark detection task group to MachineLearningConfig class.

[1] https://review.tizen.org/gerrit/#/c/platform/core/api/mediavision/+/301043/

Change-Id: I6e77ce33349bbdcb22f64102a7ec3f8808249b1c
Signed-off-by: Inki Dae <inki.dae@samsung.com>
29 files changed:
mv_machine_learning/common/include/machine_learning_key.h
mv_machine_learning/common/src/machine_learning_config.cpp
mv_machine_learning/image_classification/CMakeLists.txt
mv_machine_learning/image_classification/include/image_classification.h
mv_machine_learning/image_classification/include/image_classification_adapter.h
mv_machine_learning/image_classification/include/image_classification_config.h [deleted file]
mv_machine_learning/image_classification/include/image_classification_default.h
mv_machine_learning/image_classification/meta/image_classification.json
mv_machine_learning/image_classification/src/image_classification.cpp
mv_machine_learning/image_classification/src/image_classification_adapter.cpp
mv_machine_learning/image_classification/src/image_classification_config.cpp [deleted file]
mv_machine_learning/image_classification/src/image_classification_default.cpp
mv_machine_learning/landmark_detection/CMakeLists.txt
mv_machine_learning/landmark_detection/include/facial_landmark_adapter.h
mv_machine_learning/landmark_detection/include/fld_tweak_cnn.h
mv_machine_learning/landmark_detection/include/landmark_detection.h
mv_machine_learning/landmark_detection/include/landmark_detection_config.h [deleted file]
mv_machine_learning/landmark_detection/include/pld_cpm.h
mv_machine_learning/landmark_detection/include/pose_landmark_adapter.h
mv_machine_learning/landmark_detection/meta/facial_landmark.json
mv_machine_learning/landmark_detection/meta/pose_landmark.json
mv_machine_learning/landmark_detection/src/facial_landmark_adapter.cpp
mv_machine_learning/landmark_detection/src/fld_tweak_cnn.cpp
mv_machine_learning/landmark_detection/src/landmark_detection.cpp
mv_machine_learning/landmark_detection/src/landmark_detection_config.cpp [deleted file]
mv_machine_learning/landmark_detection/src/pld_cpm.cpp
mv_machine_learning/landmark_detection/src/pose_landmark_adapter.cpp
test/testsuites/machine_learning/image_classification/CMakeLists.txt
test/testsuites/machine_learning/landmark_detection/CMakeLists.txt

index a3b5f6f..6add66c 100644 (file)
 #define MV_MODEL_LABEL_FILE_NAME "MODEL_LABEL_FILE_NAME"
 
 /**
+ * @brief Defines #MV_MODEL_CONFIDENCE_THRESHOLD to set inference
+ *        models's confidence threshold value of the engine configuration.
+ * @details The file includes inference model's confidence threshold value.
+ *
+ * @since_tizen 8.0
+ */
+#define MV_MODEL_CONFIDENCE_THRESHOLD "CONFIDENCE_THRESHOLD"
+
+/**
  * @brief Defines #MV_BACKEND_TYPE
  *        to set inference backend engine type. In default, tensorflow lite is used.
  *
index 48ac7fc..50906ee 100644 (file)
@@ -143,6 +143,10 @@ void MachineLearningConfig::parseConfigFile(const std::string &configFilePath)
                throw InvalidOperation("Fail to get model label file path");
 
        _modelLabelFilePath = _modelDefaultPath + _modelLabelFilePath;
+
+       ret = config->getDoubleAttribute(MV_MODEL_CONFIDENCE_THRESHOLD, &_confidence_threshold);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               LOGW("threshold value doesn't exist.");
 }
 
 void MachineLearningConfig::loadMetaFile(unique_ptr<MetaParser> parser)
@@ -152,4 +156,4 @@ void MachineLearningConfig::loadMetaFile(unique_ptr<MetaParser> parser)
 }
 
 }
-}
\ No newline at end of file
+}
index 3844240..c03d0d7 100644 (file)
@@ -11,7 +11,7 @@ if(NOT OpenCV_FOUND)
 endif()
 
 add_library(${PROJECT_NAME} SHARED ${MV_IMAGE_CLASSIFICATION_SOURCE_LIST})
-target_link_libraries(${PROJECT_NAME} ${MV_COMMON_LIB_NAME} ${OpenCV_LIBS} ${${PROJECT_NAME}_DEP_LIBRARIES} mv_inference)
+target_link_libraries(${PROJECT_NAME} ${MV_COMMON_LIB_NAME} ${OpenCV_LIBS} ${${PROJECT_NAME}_DEP_LIBRARIES} mv_ml_common mv_inference)
 target_include_directories(${PROJECT_NAME} PRIVATE include ../inference/include ../common/include ../meta/include)
 install(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR})
 install(
index cdf2872..f8ee160 100644 (file)
@@ -26,8 +26,8 @@
 #include "Inference.h"
 #include "image_classification_type.h"
 #include "MetaParser.h"
+#include "machine_learning_config.h"
 #include "ImageClassificationParser.h"
-#include "image_classification_config.h"
 #include "machine_learning_preprocess.h"
 #include "async_manager.h"
 
@@ -52,7 +52,7 @@ private:
 
 protected:
        std::unique_ptr<mediavision::inference::Inference> _inference;
-       std::shared_ptr<ImageClassificationConfig> _config;
+       std::shared_ptr<MachineLearningConfig> _config;
        Preprocess _preprocess;
        std::vector<std::string> _labels;
        std::vector<std::string> _valid_backends;
@@ -64,7 +64,7 @@ protected:
        virtual ImageClassificationResult &result() = 0;
 
 public:
-       explicit ImageClassification(std::shared_ptr<ImageClassificationConfig> config);
+       explicit ImageClassification(std::shared_ptr<MachineLearningConfig> config);
        virtual ~ImageClassification() = default;
 
        void preDestroy();
index 6766a1c..e625efe 100644 (file)
@@ -21,7 +21,7 @@
 
 #include "EngineConfig.h"
 #include "itask.h"
-#include "image_classification_config.h"
+#include "machine_learning_config.h"
 #include "image_classification_default.h"
 
 namespace mediavision
@@ -32,7 +32,7 @@ template<typename T, typename V> class ImageClassificationAdapter : public media
 {
 private:
        std::unique_ptr<ImageClassification> _image_classification;
-       std::shared_ptr<ImageClassificationConfig> _config;
+       std::shared_ptr<MachineLearningConfig> _config;
        T _source;
        const std::string _config_file_name = "image_classification.json";
 
diff --git a/mv_machine_learning/image_classification/include/image_classification_config.h b/mv_machine_learning/image_classification/include/image_classification_config.h
deleted file mode 100644 (file)
index dbab988..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __IMAGE_CLASSIFICATION_CONFIG_H__
-#define __IMAGE_CLASSIFICATION_CONFIG_H__
-
-#include <mv_common.h>
-#include "mv_private.h"
-#include "EngineConfig.h"
-
-#include "MetaParser.h"
-#include "image_classification_type.h"
-
-namespace mediavision
-{
-namespace machine_learning
-{
-class ImageClassificationConfig
-{
-private:
-       std::unique_ptr<MetaParser> _parser;
-       std::string _modelFilePath;
-       std::string _modelMetaFilePath;
-       std::string _modelDefaultPath;
-       std::string _modelLabelFilePath;
-       int _backendType {};
-       int _targetDeviceType {};
-
-public:
-       ImageClassificationConfig();
-       virtual ~ImageClassificationConfig() = default;
-
-       void setUserModel(const std::string &model_file, const std::string &meta_file, const std::string &label_file);
-       void parseConfigFile(const std::string &configFilePath);
-       void parseMetaFile();
-       void setBackendType(int backend_type);
-       void setTargetDeviceType(int device_type);
-       const std::string &getModelFilePath() const;
-       const std::string &getLabelFilePath() const;
-       MetaMap &getInputMetaMap() const;
-       MetaMap &getOutputMetaMap() const;
-       int getBackendType() const;
-       int getTargetDeviceType() const;
-       void loadMetaFile();
-};
-
-} // machine_learning
-} // mediavision
-
-#endif
\ No newline at end of file
index 981b15e..870b4eb 100644 (file)
@@ -35,7 +35,7 @@ private:
        ImageClassificationResult _result;
 
 public:
-       ImageClassificationDefault(std::shared_ptr<ImageClassificationConfig> config);
+       ImageClassificationDefault(std::shared_ptr<MachineLearningConfig> config);
        ~ImageClassificationDefault();
 
        ImageClassificationResult &result() override;
index 89714af..c67e5e0 100644 (file)
             "value" : "image-classification-001.tflite"
         },
         {
-            "name"  : "LABEL_FILE_NAME",
+            "name"  : "DEFAULT_MODEL_NAME",
+            "type"  : "string",
+            "value" : ""
+        },
+        {
+            "name"  : "MODEL_LABEL_FILE_NAME",
             "type"  : "string",
             "value" : "image-classification-label.txt"
         },
         {
-            "name"  : "META_FILE_NAME",
+            "name"  : "MODEL_META_FILE_NAME",
             "type"  : "string",
             "value" : "image-classification-001.json"
         },
index 1fe19e2..369cd23 100644 (file)
@@ -35,10 +35,9 @@ namespace mediavision
 {
 namespace machine_learning
 {
-ImageClassification::ImageClassification(shared_ptr<ImageClassificationConfig> config) : _config(config)
+ImageClassification::ImageClassification(std::shared_ptr<MachineLearningConfig> config) : _config(config)
 {
        _inference = make_unique<Inference>();
-       loadLabel();
 }
 
 void ImageClassification::preDestroy()
@@ -51,6 +50,9 @@ void ImageClassification::preDestroy()
 
 void ImageClassification::configure()
 {
+       _config->loadMetaFile(make_unique<ImageClassificationParser>());
+       loadLabel();
+
        int ret = _inference->bind(_config->getBackendType(), _config->getTargetDeviceType());
        if (ret != MEDIA_VISION_ERROR_NONE)
                throw InvalidOperation("Fail to bind a backend engine.");
index 7629e92..17b5a1b 100644 (file)
@@ -31,7 +31,7 @@ namespace machine_learning
 {
 template<typename T, typename V> ImageClassificationAdapter<T, V>::ImageClassificationAdapter() : _source()
 {
-       _config = make_shared<ImageClassificationConfig>();
+       _config = make_shared<MachineLearningConfig>();
        _config->parseConfigFile(_config_file_name);
 
        create();
@@ -44,9 +44,6 @@ template<typename T, typename V> ImageClassificationAdapter<T, V>::~ImageClassif
 
 template<typename T, typename V> void ImageClassificationAdapter<T, V>::create()
 {
-       // reallocate the parser and load the meta file
-       _config->loadMetaFile();
-
        _image_classification = make_unique<ImageClassificationDefault>(_config);
 }
 
diff --git a/mv_machine_learning/image_classification/src/image_classification_config.cpp b/mv_machine_learning/image_classification/src/image_classification_config.cpp
deleted file mode 100644 (file)
index 2ed568a..0000000
+++ /dev/null
@@ -1,152 +0,0 @@
-/**
- * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "machine_learning_exception.h"
-#include "mv_image_classification_config.h"
-#include "ImageClassificationParser.h"
-#include "image_classification_config.h"
-
-using namespace std;
-using namespace MediaVision::Common;
-using namespace mediavision::machine_learning;
-using namespace mediavision::machine_learning::exception;
-
-namespace mediavision
-{
-namespace machine_learning
-{
-ImageClassificationConfig::ImageClassificationConfig()
-{}
-
-void ImageClassificationConfig::setBackendType(int backend_type)
-{
-       _backendType = backend_type;
-}
-
-void ImageClassificationConfig::setTargetDeviceType(int device_type)
-{
-       _targetDeviceType = device_type;
-}
-
-const std::string &ImageClassificationConfig::getModelFilePath() const
-{
-       return _modelFilePath;
-}
-
-const std::string &ImageClassificationConfig::getLabelFilePath() const
-{
-       return _modelLabelFilePath;
-}
-
-MetaMap &ImageClassificationConfig::getInputMetaMap() const
-{
-       return _parser->getInputMetaMap();
-}
-
-MetaMap &ImageClassificationConfig::getOutputMetaMap() const
-{
-       return _parser->getOutputMetaMap();
-}
-
-int ImageClassificationConfig::getBackendType() const
-{
-       return _backendType;
-}
-
-int ImageClassificationConfig::getTargetDeviceType() const
-{
-       return _targetDeviceType;
-}
-
-void ImageClassificationConfig::setUserModel(const string &model_file, const string &meta_file,
-                                                                                        const string &label_file)
-{
-       if (!model_file.empty())
-               _modelFilePath = _modelDefaultPath + model_file;
-       if (!meta_file.empty())
-               _modelMetaFilePath = _modelDefaultPath + meta_file;
-       if (!label_file.empty())
-               _modelLabelFilePath = _modelDefaultPath + label_file;
-}
-
-static bool IsJsonFile(const string &fileName)
-{
-       return (!fileName.substr(fileName.find_last_of(".") + 1).compare("json"));
-}
-
-void ImageClassificationConfig::parseConfigFile(const std::string &configFilePath)
-{
-       auto config = make_unique<EngineConfig>(MV_CONFIG_PATH + configFilePath);
-
-       int ret = MEDIA_VISION_ERROR_NONE;
-
-       if (_backendType == MV_INFERENCE_BACKEND_NONE) {
-               ret = config->getIntegerAttribute(MV_IMAGE_CLASSIFICATION_BACKEND_TYPE, &_backendType);
-               if (ret != MEDIA_VISION_ERROR_NONE)
-                       throw InvalidOperation("Fail to get backend engine type.");
-       }
-
-       if (_targetDeviceType == MV_INFERENCE_TARGET_DEVICE_NONE) {
-               ret = config->getIntegerAttribute(MV_IMAGE_CLASSIFICATION_TARGET_DEVICE_TYPE, &_targetDeviceType);
-               if (ret != MEDIA_VISION_ERROR_NONE)
-                       throw InvalidOperation("Fail to get target device type.");
-       }
-
-       ret = config->getStringAttribute(MV_IMAGE_CLASSIFICATION_MODEL_DEFAULT_PATH, &_modelDefaultPath);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               throw InvalidOperation("Fail to get model default path");
-
-       ret = config->getStringAttribute(MV_IMAGE_CLASSIFICATION_MODEL_FILE_NAME, &_modelFilePath);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               throw InvalidOperation("Fail to get model file path");
-
-       _modelFilePath = _modelDefaultPath + _modelFilePath;
-       LOGI("model file path = %s", _modelFilePath.c_str());
-
-       ret = config->getStringAttribute(MV_IMAGE_CLASSIFICATION_MODEL_META_FILE_NAME, &_modelMetaFilePath);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               throw InvalidOperation("Fail to get model meta file path");
-
-       if (_modelMetaFilePath.empty())
-               throw InvalidOperation("Model meta file doesn't exist.");
-
-       if (!IsJsonFile(_modelMetaFilePath))
-               throw InvalidOperation("Model meta file should be json");
-
-       _modelMetaFilePath = _modelDefaultPath + _modelMetaFilePath;
-       LOGI("meta file path = %s", _modelMetaFilePath.c_str());
-
-       ret = config->getStringAttribute(MV_IMAGE_CLASSIFICATION_MODEL_LABEL_FILE_NAME, &_modelLabelFilePath);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               throw InvalidOperation("Fail to get label file path");
-
-       if (_modelLabelFilePath.empty()) {
-               LOGW("Label doesn't exist.");
-               return;
-       }
-
-       _modelLabelFilePath = _modelDefaultPath + _modelLabelFilePath;
-       LOGI("label file path = %s", _modelLabelFilePath.c_str());
-}
-
-void ImageClassificationConfig::loadMetaFile()
-{
-       _parser = make_unique<ImageClassificationParser>();
-       _parser->load(_modelMetaFilePath);
-}
-
-}
-}
index 3b74c71..37a1873 100644 (file)
@@ -30,7 +30,7 @@ namespace mediavision
 {
 namespace machine_learning
 {
-ImageClassificationDefault::ImageClassificationDefault(shared_ptr<ImageClassificationConfig> config)
+ImageClassificationDefault::ImageClassificationDefault(shared_ptr<MachineLearningConfig> config)
                : ImageClassification(config), _result()
 {}
 
index 5831adb..54f16a5 100644 (file)
@@ -11,7 +11,7 @@ if(NOT OpenCV_FOUND)
 endif()
 
 add_library(${PROJECT_NAME} SHARED ${MV_LANDMARK_DETECTION_SOURCE_LIST})
-target_link_libraries(${PROJECT_NAME} ${MV_COMMON_LIB_NAME} ${OpenCV_LIBS} ${${PROJECT_NAME}_DEP_LIBRARIES} mv_inference)
+target_link_libraries(${PROJECT_NAME} ${MV_COMMON_LIB_NAME} ${OpenCV_LIBS} ${${PROJECT_NAME}_DEP_LIBRARIES} mv_ml_common mv_inference)
 target_include_directories(${PROJECT_NAME} PRIVATE include ../inference/include ../common/include ../meta/include)
 install(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR})
 install(
index 967bf9f..98daae4 100644 (file)
@@ -21,7 +21,7 @@
 
 #include "EngineConfig.h"
 #include "itask.h"
-#include "landmark_detection_config.h"
+#include "machine_learning_config.h"
 #include "fld_tweak_cnn.h"
 
 namespace mediavision
@@ -32,7 +32,7 @@ template<typename T, typename V> class FacialLandmarkAdapter : public mediavisio
 {
 private:
        std::unique_ptr<LandmarkDetection> _landmark_detection;
-       std::shared_ptr<LandmarkDetectionConfig> _config;
+       std::shared_ptr<MachineLearningConfig> _config;
        T _source;
        const std::string _config_file_name = "facial_landmark.json";
 
index 9587633..42dc2c1 100644 (file)
@@ -35,7 +35,7 @@ private:
        LandmarkDetectionResult _result;
 
 public:
-       FldTweakCnn(LandmarkDetectionTaskType task_type, std::shared_ptr<LandmarkDetectionConfig> config);
+       FldTweakCnn(LandmarkDetectionTaskType task_type, std::shared_ptr<MachineLearningConfig> config);
        ~FldTweakCnn();
 
        LandmarkDetectionResult &result() override;
index ff2084f..1556d94 100644 (file)
@@ -27,7 +27,7 @@
 #include "landmark_detection_type.h"
 #include "MetaParser.h"
 #include "LandmarkDetectionParser.h"
-#include "landmark_detection_config.h"
+#include "machine_learning_config.h"
 #include "machine_learning_preprocess.h"
 #include "async_manager.h"
 
@@ -54,7 +54,7 @@ private:
 
 protected:
        std::unique_ptr<mediavision::inference::Inference> _inference;
-       std::shared_ptr<LandmarkDetectionConfig> _config;
+       std::shared_ptr<MachineLearningConfig> _config;
        std::vector<std::string> _labels;
        std::vector<std::string> _valid_backends;
        std::vector<std::string> _valid_devices;
@@ -66,7 +66,7 @@ protected:
        virtual LandmarkDetectionResult &result() = 0;
 
 public:
-       LandmarkDetection(LandmarkDetectionTaskType task_type, std::shared_ptr<LandmarkDetectionConfig> config);
+       LandmarkDetection(LandmarkDetectionTaskType task_type, std::shared_ptr<MachineLearningConfig> config);
        virtual ~LandmarkDetection() = default;
        void preDestroy();
        LandmarkDetectionTaskType getTaskType();
diff --git a/mv_machine_learning/landmark_detection/include/landmark_detection_config.h b/mv_machine_learning/landmark_detection/include/landmark_detection_config.h
deleted file mode 100644 (file)
index 5e1f143..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/**
- * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __LANDMARK_DETECTION_CONFIG_H__
-#define __LANDMARK_DETECTION_CONFIG_H__
-
-#include <mv_common.h>
-#include "mv_private.h"
-#include "EngineConfig.h"
-
-#include "MetaParser.h"
-#include "landmark_detection_type.h"
-
-namespace mediavision
-{
-namespace machine_learning
-{
-class LandmarkDetectionConfig
-{
-private:
-       std::unique_ptr<MetaParser> _parser;
-       std::string _defaultModelName;
-       std::string _modelFilePath;
-       std::string _modelMetaFilePath;
-       std::string _modelDefaultPath;
-       std::string _modelLabelFilePath;
-       int _backendType {};
-       int _targetDeviceType {};
-       double _confidence_threshold {};
-
-public:
-       LandmarkDetectionConfig();
-       virtual ~LandmarkDetectionConfig() = default;
-
-       void setUserModel(const std::string &model_file, const std::string &meta_file, const std::string &label_file);
-       void parseConfigFile(const std::string &configFilePath);
-       void parseMetaFile();
-       void setBackendType(int backend_type);
-       void setTargetDeviceType(int device_type);
-       const std::string &getDefaultModelName() const;
-       const std::string &getModelFilePath() const;
-       const std::string &getLabelFilePath() const;
-       MetaMap &getInputMetaMap() const;
-       MetaMap &getOutputMetaMap() const;
-       double getConfidenceThreshold() const;
-       int getBackendType() const;
-       int getTargetDeviceType() const;
-       void loadMetaFile();
-};
-
-} // machine_learning
-} // mediavision
-
-#endif
\ No newline at end of file
index 5a058ef..7ce71b6 100644 (file)
@@ -35,7 +35,7 @@ private:
        LandmarkDetectionResult _result;
 
 public:
-       PldCpm(LandmarkDetectionTaskType task_type, std::shared_ptr<LandmarkDetectionConfig> config);
+       PldCpm(LandmarkDetectionTaskType task_type, std::shared_ptr<MachineLearningConfig> config);
        ~PldCpm();
 
        LandmarkDetectionResult &result() override;
index 767e488..4d21c3f 100644 (file)
@@ -21,7 +21,7 @@
 
 #include "EngineConfig.h"
 #include "itask.h"
-#include "landmark_detection_config.h"
+#include "machine_learning_config.h"
 #include "pld_cpm.h"
 
 namespace mediavision
@@ -32,7 +32,7 @@ template<typename T, typename V> class PoseLandmarkAdapter : public mediavision:
 {
 private:
        std::unique_ptr<LandmarkDetection> _landmark_detection;
-       std::shared_ptr<LandmarkDetectionConfig> _config;
+       std::shared_ptr<MachineLearningConfig> _config;
        T _source;
        const std::string _config_file_name = "pose_landmark.json";
 
index ad7f7b8..49c1951 100644 (file)
             "value" : "FLD_TWEAK_CNN"
         },
         {
-            "name"  : "META_FILE_NAME",
+            "name"  : "MODEL_META_FILE_NAME",
             "type"  : "string",
             "value" : "fld_tweakcnn_128x128.json"
         },
         {
-            "name"  : "LABEL_FILE_NAME",
+            "name"  : "MODEL_LABEL_FILE_NAME",
             "type"  : "string",
             "value" : ""
         },
index a8637f8..69068da 100644 (file)
             "value" : "PLD_CPM"
         },
         {
-            "name"  : "META_FILE_NAME",
+            "name"  : "MODEL_META_FILE_NAME",
             "type"  : "string",
             "value" : "pld_cpm_192x192.json"
         },
         {
-            "name"  : "LABEL_FILE_NAME",
+            "name"  : "MODEL_LABEL_FILE_NAME",
             "type"  : "string",
             "value" : ""
         },
index c81ff52..370375c 100644 (file)
@@ -29,7 +29,7 @@ namespace machine_learning
 {
 template<typename T, typename V> FacialLandmarkAdapter<T, V>::FacialLandmarkAdapter() : _source()
 {
-       _config = make_shared<LandmarkDetectionConfig>();
+       _config = make_shared<MachineLearningConfig>();
        _config->parseConfigFile(_config_file_name);
 
        LandmarkDetectionTaskType model_type = convertToTaskType(_config->getDefaultModelName());
@@ -49,9 +49,6 @@ template<typename T, typename V> void FacialLandmarkAdapter<T, V>::create(Landma
                        return;
        }
 
-       // if model name is changed by user then reallocate the parser and reload the meta file corresponding to the model name.
-       _config->loadMetaFile();
-
        if (task_type == LandmarkDetectionTaskType::FLD_TWEAK_CNN)
                _landmark_detection = make_unique<FldTweakCnn>(task_type, _config);
 }
index 71c451e..a4b922f 100644 (file)
@@ -30,7 +30,7 @@ namespace mediavision
 {
 namespace machine_learning
 {
-FldTweakCnn::FldTweakCnn(LandmarkDetectionTaskType task_type, std::shared_ptr<LandmarkDetectionConfig> config)
+FldTweakCnn::FldTweakCnn(LandmarkDetectionTaskType task_type, std::shared_ptr<MachineLearningConfig> config)
                : LandmarkDetection(task_type, config), _result()
 {}
 
index 6b6af1e..119e439 100644 (file)
@@ -34,11 +34,10 @@ namespace mediavision
 {
 namespace machine_learning
 {
-LandmarkDetection::LandmarkDetection(LandmarkDetectionTaskType task_type, shared_ptr<LandmarkDetectionConfig> config)
+LandmarkDetection::LandmarkDetection(LandmarkDetectionTaskType task_type, shared_ptr<MachineLearningConfig> config)
                : _task_type(task_type), _config(config)
 {
        _inference = make_unique<Inference>();
-       loadLabel();
 }
 
 void LandmarkDetection::preDestroy()
@@ -176,6 +175,9 @@ void LandmarkDetection::loadLabel()
 
 void LandmarkDetection::configure()
 {
+       _config->loadMetaFile(make_unique<LandmarkDetectionParser>(static_cast<int>(_task_type)));
+       loadLabel();
+
        int ret = _inference->bind(_config->getBackendType(), _config->getTargetDeviceType());
        if (ret != MEDIA_VISION_ERROR_NONE)
                throw InvalidOperation("Fail to bind a backend engine.");
diff --git a/mv_machine_learning/landmark_detection/src/landmark_detection_config.cpp b/mv_machine_learning/landmark_detection/src/landmark_detection_config.cpp
deleted file mode 100644 (file)
index 0221fba..0000000
+++ /dev/null
@@ -1,167 +0,0 @@
-/**
- * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "machine_learning_exception.h"
-#include "mv_landmark_detection_config.h"
-#include "LandmarkDetectionParser.h"
-#include "landmark_detection_config.h"
-
-using namespace std;
-using namespace MediaVision::Common;
-using namespace mediavision::machine_learning;
-using namespace mediavision::machine_learning::exception;
-
-namespace mediavision
-{
-namespace machine_learning
-{
-LandmarkDetectionConfig::LandmarkDetectionConfig()
-{}
-
-void LandmarkDetectionConfig::setBackendType(int backend_type)
-{
-       _backendType = backend_type;
-}
-
-void LandmarkDetectionConfig::setTargetDeviceType(int device_type)
-{
-       _targetDeviceType = device_type;
-}
-
-const std::string &LandmarkDetectionConfig::getDefaultModelName() const
-{
-       return _defaultModelName;
-}
-
-const std::string &LandmarkDetectionConfig::getModelFilePath() const
-{
-       return _modelFilePath;
-}
-
-const std::string &LandmarkDetectionConfig::getLabelFilePath() const
-{
-       return _modelLabelFilePath;
-}
-
-MetaMap &LandmarkDetectionConfig::getInputMetaMap() const
-{
-       return _parser->getInputMetaMap();
-}
-
-MetaMap &LandmarkDetectionConfig::getOutputMetaMap() const
-{
-       return _parser->getOutputMetaMap();
-}
-
-double LandmarkDetectionConfig::getConfidenceThreshold() const
-{
-       return _confidence_threshold;
-}
-
-int LandmarkDetectionConfig::getBackendType() const
-{
-       return _backendType;
-}
-
-int LandmarkDetectionConfig::getTargetDeviceType() const
-{
-       return _targetDeviceType;
-}
-
-void LandmarkDetectionConfig::setUserModel(const string &model_file, const string &meta_file, const string &label_file)
-{
-       if (!model_file.empty())
-               _modelFilePath = _modelDefaultPath + model_file;
-       if (!meta_file.empty())
-               _modelMetaFilePath = _modelDefaultPath + meta_file;
-       if (!label_file.empty())
-               _modelLabelFilePath = _modelDefaultPath + label_file;
-}
-
-static bool IsJsonFile(const string &fileName)
-{
-       return (!fileName.substr(fileName.find_last_of(".") + 1).compare("json"));
-}
-
-void LandmarkDetectionConfig::parseConfigFile(const std::string &configFilePath)
-{
-       auto config = make_unique<EngineConfig>(MV_CONFIG_PATH + configFilePath);
-
-       int ret = config->getStringAttribute(MV_LANDMARK_DETECTION_DEFAULT_MODEL_NAME, &_defaultModelName);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               throw InvalidOperation("Fail to get default model name.");
-
-       if (_backendType == MV_INFERENCE_BACKEND_NONE) {
-               ret = config->getIntegerAttribute(MV_LANDMARK_DETECTION_BACKEND_TYPE, &_backendType);
-               if (ret != MEDIA_VISION_ERROR_NONE)
-                       throw InvalidOperation("Fail to get backend engine type.");
-       }
-
-       if (_targetDeviceType == MV_INFERENCE_TARGET_DEVICE_NONE) {
-               ret = config->getIntegerAttribute(MV_LANDMARK_DETECTION_TARGET_DEVICE_TYPE, &_targetDeviceType);
-               if (ret != MEDIA_VISION_ERROR_NONE)
-                       throw InvalidOperation("Fail to get target device type.");
-       }
-
-       ret = config->getStringAttribute(MV_LANDMARK_DETECTION_MODEL_DEFAULT_PATH, &_modelDefaultPath);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               throw InvalidOperation("Fail to get model default path");
-
-       ret = config->getStringAttribute(MV_LANDMARK_DETECTION_MODEL_FILE_PATH, &_modelFilePath);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               throw InvalidOperation("Fail to get model file path");
-
-       _modelFilePath = _modelDefaultPath + _modelFilePath;
-       LOGI("model file path = %s", _modelFilePath.c_str());
-
-       ret = config->getStringAttribute(MV_LANDMARK_DETECTION_MODEL_META_FILE_PATH, &_modelMetaFilePath);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               throw InvalidOperation("Fail to get model meta file path");
-
-       if (_modelMetaFilePath.empty())
-               throw InvalidOperation("Model meta file doesn't exist.");
-
-       if (!IsJsonFile(_modelMetaFilePath))
-               throw InvalidOperation("Model meta file should be json");
-
-       _modelMetaFilePath = _modelDefaultPath + _modelMetaFilePath;
-       LOGI("meta file path = %s", _modelMetaFilePath.c_str());
-
-       ret = config->getDoubleAttribute(MV_LANDMARK_DETECTION_CONFIDENCE_THRESHOLD, &_confidence_threshold);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               LOGW("threshold value doesn't exist.");
-
-       ret = config->getStringAttribute(MV_LANDMARK_DETECTION_LABEL_FILE_NAME, &_modelLabelFilePath);
-       if (ret != MEDIA_VISION_ERROR_NONE)
-               throw InvalidOperation("Fail to get label file path");
-
-       if (_modelLabelFilePath.empty()) {
-               LOGW("Label doesn't exist.");
-               return;
-       }
-
-       _modelLabelFilePath = _modelDefaultPath + _modelLabelFilePath;
-       LOGI("label file path = %s", _modelLabelFilePath.c_str());
-}
-
-void LandmarkDetectionConfig::loadMetaFile()
-{
-       _parser = make_unique<LandmarkDetectionParser>();
-       _parser->load(_modelMetaFilePath);
-}
-
-}
-}
\ No newline at end of file
index 91f3aeb..d89fcec 100644 (file)
@@ -31,7 +31,7 @@ namespace mediavision
 {
 namespace machine_learning
 {
-PldCpm::PldCpm(LandmarkDetectionTaskType task_type, std::shared_ptr<LandmarkDetectionConfig> config)
+PldCpm::PldCpm(LandmarkDetectionTaskType task_type, std::shared_ptr<MachineLearningConfig> config)
                : LandmarkDetection(task_type, config), _result()
 {}
 
index 4703fbf..bc43978 100644 (file)
@@ -29,7 +29,7 @@ namespace machine_learning
 {
 template<typename T, typename V> PoseLandmarkAdapter<T, V>::PoseLandmarkAdapter() : _source()
 {
-       _config = make_shared<LandmarkDetectionConfig>();
+       _config = make_shared<MachineLearningConfig>();
        _config->parseConfigFile(_config_file_name);
 
        LandmarkDetectionTaskType model_type = convertToTaskType(_config->getDefaultModelName());
@@ -51,9 +51,6 @@ template<typename T, typename V> void PoseLandmarkAdapter<T, V>::create(Landmark
                        return;
        }
 
-       // if model name is changed by user then reallocate the parser and reload the meta file corresponding to the model name.
-       _config->loadMetaFile();
-
        if (task_type == LandmarkDetectionTaskType::PLD_CPM)
                _landmark_detection = make_unique<PldCpm>(task_type, _config);
 }
index 03580d7..e92889a 100644 (file)
@@ -9,6 +9,7 @@ add_executable(${TEST_IMAGE_CLASSIFICATION_ASYNC} test_image_classification_asyn
 
 target_compile_definitions(${TEST_IMAGE_CLASSIFICATION} PRIVATE -DTEST_RES_PATH="${TEST_RES_PATH}")
 target_link_libraries(${TEST_IMAGE_CLASSIFICATION} gtest gtest_main
+                      mv_ml_common
                       mv_inference
                       mv_image_classification
                       mv_image_helper
@@ -16,6 +17,7 @@ target_link_libraries(${TEST_IMAGE_CLASSIFICATION} gtest gtest_main
 
 target_compile_definitions(${TEST_IMAGE_CLASSIFICATION_ASYNC} PRIVATE -DTEST_RES_PATH="${TEST_RES_PATH}")
 target_link_libraries(${TEST_IMAGE_CLASSIFICATION_ASYNC} gtest gtest_main pthread
+                      mv_ml_common
                       mv_inference
                       mv_image_classification
                       mv_image_helper
index 718c65e..47dfade 100644 (file)
@@ -8,12 +8,14 @@ add_executable(${TEST_LANDMARK_DETECTION} test_landmark_detection.cpp)
 add_executable(${TEST_LANDMARK_DETECTION_ASYNC} test_landmark_detection_async.cpp)
 
 target_link_libraries(${TEST_LANDMARK_DETECTION} gtest gtest_main
+                      mv_ml_common
                       mv_inference
                       mv_landmark_detection
                       mv_image_helper
 )
 
 target_link_libraries(${TEST_LANDMARK_DETECTION_ASYNC} gtest gtest_main pthread
+                      mv_ml_common
                       mv_inference
                       mv_landmark_detection
                       mv_image_helper