mv_machine_learning: introduce DEFAULT_MODEL_NAME for object detection
authorInki Dae <inki.dae@samsung.com>
Wed, 9 Aug 2023 04:04:47 +0000 (13:04 +0900)
committerKwanghoon Son <k.son@samsung.com>
Mon, 4 Sep 2023 04:57:02 +0000 (13:57 +0900)
[Issue type] : new feature

Introduce DEFAULT_MODEL_NAME for object detection task group, which is a new
key for object detection meta file.

With this patch, a proper concreate class object will be created
at the constructor of ObjectDetectionAdapter class according to the default
model name which is parsed from object_detection.json file.

And also this patch calls the member function, create, to create the concrete
class object corresponding to the default model name instead of creating
a default concrete class object directly.

Finally, we can create user-desired concrete class object without rebuilding
by simply modifying the meta file, object_detection.json.

Change-Id: Ie159991985c4ed9a28abc5a377037110fe319be6
Signed-off-by: Inki Dae <inki.dae@samsung.com>
mv_machine_learning/object_detection/include/mv_object_detection_config.h
mv_machine_learning/object_detection/include/object_detection_adapter.h
mv_machine_learning/object_detection/meta/object_detection.json
mv_machine_learning/object_detection/src/object_detection_adapter.cpp

index 32d527d..7dbf27f 100644 (file)
 #define MV_OBJECT_DETECTION_MODEL_FILE_PATH "MODEL_FILE_NAME"
 
 /**
+ * @brief Defines #MV_OBJECT_DETECTION_DEFAULT_MODEL_NAME
+ *        to set the object detection default model name.
+ *
+ * @since_tizen 8.0
+ */
+#define MV_OBJECT_DETECTION_DEFAULT_MODEL_NAME "DEFAULT_MODEL_NAME"
+
+/**
  * @brief Defines #MV_OBJECT_DETECTION_3D_MODEL_META_FILE_PATH to set inference
  *        models's metadata file attribute of the engine configuration.
  * @details The file includes inference model's metadata such as input and output
index 45aea8b..1650726 100644 (file)
@@ -37,6 +37,7 @@ private:
        std::string _model_file;
        std::string _meta_file;
        std::string _label_file;
+       const std::string _meta_file_name = "object_detection.json";
 
        void create(ObjectDetectionTaskType task_type);
        ObjectDetectionTaskType convertToTaskType(std::string model_name);
index ed06d7a..d1bdbf3 100644 (file)
             "value" : "od_mobilenet_v1_ssd_postop_300x300.tflite"
         },
         {
+            "name"  : "DEFAULT_MODEL_NAME",
+            "type"  : "string",
+            "value" : "MOBILENET_V1_SSD"
+        },
+        {
             "name"  : "META_FILE_NAME",
             "type"  : "string",
             "value" : "od_mobilenet_v1_ssd_postop_300x300.json"
index de9ccbd..4cc86b7 100644 (file)
@@ -17,6 +17,7 @@
 #include "machine_learning_exception.h"
 #include "object_detection_adapter.h"
 #include "object_detection_external.h"
+#include "mv_object_detection_config.h"
 
 using namespace std;
 using namespace MediaVision::Common;
@@ -29,10 +30,15 @@ namespace machine_learning
 {
 template<typename T, typename V> ObjectDetectionAdapter<T, V>::ObjectDetectionAdapter() : _source()
 {
-       // In default, Mobilenet v1 ssd model will be used.
-       // If other model is set by user then strategy pattern will be used
-       // to create its corresponding concrete class by calling create().
-       _object_detection = make_unique<MobilenetV1Ssd>(ObjectDetectionTaskType::MOBILENET_V1_SSD);
+       auto config = make_unique<EngineConfig>(string(MV_CONFIG_PATH) + _meta_file_name);
+
+       string defaultModelName;
+
+       int ret = config->getStringAttribute(MV_OBJECT_DETECTION_DEFAULT_MODEL_NAME, &defaultModelName);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               throw InvalidOperation("Fail to get default model name.");
+
+       create(convertToTaskType(defaultModelName.c_str()));
 }
 
 template<typename T, typename V> ObjectDetectionAdapter<T, V>::~ObjectDetectionAdapter()
@@ -42,11 +48,15 @@ template<typename T, typename V> ObjectDetectionAdapter<T, V>::~ObjectDetectionA
 
 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::create(ObjectDetectionTaskType task_type)
 {
-       // If default task type is same as a given one then skip.
-       if (_object_detection->getTaskType() == task_type)
-               return;
+       // If a concrete class object created already exists, reset the object
+       // so that other concrete class object can be created again according to a given task_type.
+       if (_object_detection) {
+               // If default task type is same as a given one then skip.
+               if (_object_detection->getTaskType() == task_type)
+                       return;
 
-       _object_detection.reset();
+               _object_detection.reset();
+       }
 
        if (task_type == ObjectDetectionTaskType::MOBILENET_V1_SSD)
                _object_detection = make_unique<MobilenetV1Ssd>(task_type);
@@ -113,7 +123,7 @@ void ObjectDetectionAdapter<T, V>::setEngineInfo(const char *engine_type, const
 
 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::configure()
 {
-       _object_detection->configure("object_detection.json");
+       _object_detection->configure(_meta_file_name);
 }
 
 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)