mv_machine_learning: drop template from MachineLearningConfig class
authorVibhav Aggarwal <v.aggarwal@samsung.com>
Fri, 10 Nov 2023 03:45:39 +0000 (12:45 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 14 Nov 2023 07:41:11 +0000 (16:41 +0900)
[Issue type] code refactoring

MachineLearningConfig class introduced in [1] required the
ParserType template to initialize the _parser. This resulted in
the inclusion of parser header files of each task group in the
higher level MachineLearningConfig class which led to code smell.

This patch drops the use of template and as a first step,
modifies the object detection 3d task group to work with this
new version.

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

Change-Id: I90971294c89615d85ae61e789fa6200e53d367c9
Signed-off-by: Vibhav Aggarwal <v.aggarwal@samsung.com>
mv_machine_learning/common/CMakeLists.txt
mv_machine_learning/common/include/machine_learning_config.h
mv_machine_learning/common/src/machine_learning_config.cpp
mv_machine_learning/object_detection_3d/include/object_detection_3d.h
mv_machine_learning/object_detection_3d/include/object_detection_3d_adapter.h
mv_machine_learning/object_detection_3d/include/objectron.h
mv_machine_learning/object_detection_3d/src/object_detection_3d.cpp
mv_machine_learning/object_detection_3d/src/object_detection_3d_adapter.cpp
mv_machine_learning/object_detection_3d/src/objectron.cpp

index 933ebb0..eff7e5a 100644 (file)
@@ -5,7 +5,7 @@ file(GLOB MV_ML_SOURCE_LIST  "${PROJECT_SOURCE_DIR}/src/*.c" "${PROJECT_SOURCE_D
 
 add_library(${PROJECT_NAME} SHARED ${MV_ML_SOURCE_LIST})
 target_link_libraries(${PROJECT_NAME} ${MV_COMMON_LIB_NAME} ${${PROJECT_NAME}_DEP_LIBRARIES})
-target_include_directories(${PROJECT_NAME} PRIVATE include ../meta/include ../object_detection_3d/include ../../include)
+target_include_directories(${PROJECT_NAME} PRIVATE include ../meta/include ../../include)
 install(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR})
 install(
     DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include/media
index ba39029..c10d46e 100644 (file)
@@ -27,7 +27,7 @@ namespace mediavision
 {
 namespace machine_learning
 {
-template<typename ParserType> class MachineLearningConfig
+class MachineLearningConfig
 {
 private:
        std::unique_ptr<MetaParser> _parser;
@@ -57,7 +57,7 @@ public:
        double getConfidenceThreshold() const;
        int getBackendType() const;
        int getTargetDeviceType() const;
-       void loadMetaFile(int task_type = 0);
+       void loadMetaFile(std::unique_ptr<MetaParser> parser);
 };
 
 } // machine_learning
index 08d6be4..5fbdebf 100644 (file)
@@ -18,8 +18,6 @@
 #include "machine_learning_config.h"
 #include "machine_learning_key.h"
 
-#include "ObjectDetection3dParser.h"
-
 using namespace std;
 using namespace MediaVision::Common;
 using namespace mediavision::machine_learning;
@@ -29,62 +27,60 @@ namespace mediavision
 {
 namespace machine_learning
 {
-template<typename ParserType> MachineLearningConfig<ParserType>::MachineLearningConfig()
+MachineLearningConfig::MachineLearningConfig()
 {}
 
-template<typename ParserType> void MachineLearningConfig<ParserType>::setBackendType(int backend_type)
+void MachineLearningConfig::setBackendType(int backend_type)
 {
        _backendType = backend_type;
 }
 
-template<typename ParserType> void MachineLearningConfig<ParserType>::setTargetDeviceType(int device_type)
+void MachineLearningConfig::setTargetDeviceType(int device_type)
 {
        _targetDeviceType = device_type;
 }
 
-template<typename ParserType> const std::string &MachineLearningConfig<ParserType>::getDefaultModelName() const
+const std::string &MachineLearningConfig::getDefaultModelName() const
 {
        return _defaultModelName;
 }
 
-template<typename ParserType> const std::string &MachineLearningConfig<ParserType>::getModelFilePath() const
+const std::string &MachineLearningConfig::getModelFilePath() const
 {
        return _modelFilePath;
 }
 
-template<typename ParserType> const std::string &MachineLearningConfig<ParserType>::getLabelFilePath() const
+const std::string &MachineLearningConfig::getLabelFilePath() const
 {
        return _modelLabelFilePath;
 }
 
-template<typename ParserType> MetaMap &MachineLearningConfig<ParserType>::getInputMetaMap() const
+MetaMap &MachineLearningConfig::getInputMetaMap() const
 {
        return _parser->getInputMetaMap();
 }
 
-template<typename ParserType> MetaMap &MachineLearningConfig<ParserType>::getOutputMetaMap() const
+MetaMap &MachineLearningConfig::getOutputMetaMap() const
 {
        return _parser->getOutputMetaMap();
 }
 
-template<typename ParserType> double MachineLearningConfig<ParserType>::getConfidenceThreshold() const
+double MachineLearningConfig::getConfidenceThreshold() const
 {
        return _confidence_threshold;
 }
 
-template<typename ParserType> int MachineLearningConfig<ParserType>::getBackendType() const
+int MachineLearningConfig::getBackendType() const
 {
        return _backendType;
 }
 
-template<typename ParserType> int MachineLearningConfig<ParserType>::getTargetDeviceType() const
+int MachineLearningConfig::getTargetDeviceType() const
 {
        return _targetDeviceType;
 }
 
-template<typename ParserType>
-void MachineLearningConfig<ParserType>::setUserModel(const string &model_file, const string &meta_file,
-                                                                                                        const string &label_file)
+void MachineLearningConfig::setUserModel(const string &model_file, const string &meta_file, const string &label_file)
 {
        if (!model_file.empty())
                _modelFilePath = _modelDefaultPath + model_file;
@@ -99,7 +95,7 @@ static bool IsJsonFile(const string &fileName)
        return (!fileName.substr(fileName.find_last_of(".") + 1).compare("json"));
 }
 
-template<typename ParserType> void MachineLearningConfig<ParserType>::parseConfigFile(const std::string &configFilePath)
+void MachineLearningConfig::parseConfigFile(const std::string &configFilePath)
 {
        auto config = make_unique<EngineConfig>(MV_CONFIG_PATH + configFilePath);
 
@@ -145,13 +141,11 @@ template<typename ParserType> void MachineLearningConfig<ParserType>::parseConfi
        LOGI("meta file path = %s", _modelMetaFilePath.c_str());
 }
 
-template<typename ParserType> void MachineLearningConfig<ParserType>::loadMetaFile(int task_type)
+void MachineLearningConfig::loadMetaFile(unique_ptr<MetaParser> parser)
 {
-       _parser = make_unique<ParserType>(task_type);
+       _parser = move(parser);
        _parser->load(_modelMetaFilePath);
 }
 
-template class MachineLearningConfig<ObjectDetection3dParser>;
-
 }
 }
\ No newline at end of file
index 310fd14..370e54a 100644 (file)
@@ -45,7 +45,7 @@ private:
 
 protected:
        std::unique_ptr<mediavision::inference::Inference> _inference;
-       std::shared_ptr<MachineLearningConfig<ObjectDetection3dParser> > _config;
+       std::shared_ptr<MachineLearningConfig> _config;
        std::vector<std::string> _labels;
        std::vector<std::string> _valid_backends;
        std::vector<std::string> _valid_devices;
@@ -58,8 +58,7 @@ protected:
        template<typename T> void inference(std::vector<std::vector<T> > &inputVectors);
 
 public:
-       ObjectDetection3d(ObjectDetection3dTaskType task_type,
-                                         std::shared_ptr<MachineLearningConfig<ObjectDetection3dParser> > config);
+       ObjectDetection3d(ObjectDetection3dTaskType task_type, std::shared_ptr<MachineLearningConfig> config);
        virtual ~ObjectDetection3d() = default;
 
        ObjectDetection3dTaskType getTaskType();
index 702cdf1..4b0b266 100644 (file)
@@ -32,7 +32,7 @@ template<typename T, typename V> class ObjectDetection3dAdapter : public mediavi
 {
 private:
        std::unique_ptr<ObjectDetection3d> _object_detection_3d;
-       std::shared_ptr<MachineLearningConfig<ObjectDetection3dParser> > _config;
+       std::shared_ptr<MachineLearningConfig> _config;
        T _source;
        const std::string _config_file_name = "object_detection_3d.json";
 
index 3129b4f..d86fb63 100644 (file)
@@ -35,8 +35,7 @@ private:
        ObjectDetection3dResult _result;
 
 public:
-       Objectron(ObjectDetection3dTaskType task_type,
-                         std::shared_ptr<MachineLearningConfig<ObjectDetection3dParser> > config);
+       Objectron(ObjectDetection3dTaskType task_type, std::shared_ptr<MachineLearningConfig> config);
        ~Objectron();
 
        ObjectDetection3dResult &result() override;
index 686e7b3..5997a5f 100644 (file)
@@ -34,8 +34,7 @@ namespace mediavision
 {
 namespace machine_learning
 {
-ObjectDetection3d::ObjectDetection3d(ObjectDetection3dTaskType task_type,
-                                                                        std::shared_ptr<MachineLearningConfig<ObjectDetection3dParser> > config)
+ObjectDetection3d::ObjectDetection3d(ObjectDetection3dTaskType task_type, std::shared_ptr<MachineLearningConfig> config)
                : _task_type(task_type), _config(config)
 {
        _inference = make_unique<Inference>();
index 19c8fe1..07df946 100644 (file)
@@ -28,7 +28,7 @@ namespace machine_learning
 {
 template<typename T, typename V> ObjectDetection3dAdapter<T, V>::ObjectDetection3dAdapter() : _source()
 {
-       _config = make_shared<MachineLearningConfig<ObjectDetection3dParser> >();
+       _config = make_shared<MachineLearningConfig>();
        _config->parseConfigFile(_config_file_name);
        create(convertToTaskType(_config->getDefaultModelName()));
 }
@@ -45,7 +45,7 @@ template<typename T, typename V> void ObjectDetection3dAdapter<T, V>::create(Obj
        }
 
        // if model name is changed by user then reallocate the parser and reload the meta file corresponding to the model name.
-       _config->loadMetaFile();
+       _config->loadMetaFile(make_unique<ObjectDetection3dParser>(static_cast<int>(task_type)));
 
        if (task_type == ObjectDetection3dTaskType::OBJECTRON)
                _object_detection_3d = make_unique<Objectron>(task_type, _config);
index 003e5d9..6cb13b1 100644 (file)
@@ -30,8 +30,7 @@ namespace mediavision
 {
 namespace machine_learning
 {
-Objectron::Objectron(ObjectDetection3dTaskType task_type,
-                                        std::shared_ptr<MachineLearningConfig<ObjectDetection3dParser> > config)
+Objectron::Objectron(ObjectDetection3dTaskType task_type, std::shared_ptr<MachineLearningConfig> config)
                : ObjectDetection3d(task_type, config), _result()
 {}