mv_machine_learning: use DEFAULT_MODEL_NAME for face detection task
authorInki Dae <inki.dae@samsung.com>
Wed, 25 Oct 2023 06:48:25 +0000 (15:48 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 14 Nov 2023 07:38:37 +0000 (16:38 +0900)
[Issue type] : new feature

Use the meta file for face detection task to set the default model

With this patch, a proper concrete class object will be created
at the constructor of FaceDetectionAdapter class according to the default
model name which is parsed from face_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, face_detection.json.

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

index b6f427d..c6f8dc9 100644 (file)
@@ -36,8 +36,10 @@ private:
        std::string _model_file;
        std::string _meta_file;
        std::string _label_file;
+       const std::string _config_file_name = "face_detection.json";
 
        void create(ObjectDetectionTaskType task_type);
+       ObjectDetectionTaskType convertToTaskType(std::string model_name);
 
 public:
        FaceDetectionAdapter();
index 973634f..f447418 100644 (file)
             "value" : "fd_mobilenet_v1_ssd_postop_300x300.tflite"
         },
         {
+            "name"  : "DEFAULT_MODEL_NAME",
+            "type"  : "string",
+            "value" : "FD_MOBILENET_V1_SSD"
+        },
+        {
             "name"  : "META_FILE_NAME",
             "type"  : "string",
             "value" : "fd_mobilenet_v1_ssd_postop_300x300.json"
index 1e1473d..1efa783 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "machine_learning_exception.h"
 #include "face_detection_adapter.h"
+#include "mv_object_detection_config.h"
 
 using namespace std;
 using namespace MediaVision::Common;
@@ -28,10 +29,14 @@ namespace machine_learning
 {
 template<typename T, typename V> FaceDetectionAdapter<T, V>::FaceDetectionAdapter() : _source()
 {
-       // In default, FD 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::FD_MOBILENET_V1_SSD);
+       auto config = make_unique<EngineConfig>(string(MV_CONFIG_PATH) + _config_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> FaceDetectionAdapter<T, V>::~FaceDetectionAdapter()
@@ -41,11 +46,15 @@ template<typename T, typename V> FaceDetectionAdapter<T, V>::~FaceDetectionAdapt
 
 template<typename T, typename V> void FaceDetectionAdapter<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::FD_MOBILENET_V1_SSD)
                _object_detection = make_unique<MobilenetV1Ssd>(task_type);
@@ -53,31 +62,43 @@ template<typename T, typename V> void FaceDetectionAdapter<T, V>::create(ObjectD
 }
 
 template<typename T, typename V>
-void FaceDetectionAdapter<T, V>::setModelInfo(const char *model_file, const char *meta_file, const char *label_file,
-                                                                                         const char *model_name)
+ObjectDetectionTaskType FaceDetectionAdapter<T, V>::convertToTaskType(string model_name)
 {
-       string model_name_str(model_name);
+       if (model_name.empty())
+               throw InvalidParameter("model name is empty.");
+
+       transform(model_name.begin(), model_name.end(), model_name.begin(), ::toupper);
 
-       if (!model_name_str.empty()) {
-               transform(model_name_str.begin(), model_name_str.end(), model_name_str.begin(), ::toupper);
+       ObjectDetectionTaskType task_type = ObjectDetectionTaskType::OBJECT_DETECTION_TASK_NONE;
 
-               ObjectDetectionTaskType task_type = ObjectDetectionTaskType::OBJECT_DETECTION_TASK_NONE;
+       if (model_name == "FD_MOBILENET_V1_SSD")
+               return ObjectDetectionTaskType::FD_MOBILENET_V1_SSD;
+       // TODO.
 
-               if (model_name_str == string("FD_MOBILENET_V1_SSD"))
-                       task_type = ObjectDetectionTaskType::FD_MOBILENET_V1_SSD;
-               // TODO.
-               else
-                       throw InvalidParameter("Invalid face detection model name.");
+       throw InvalidParameter("Invalid face detection model name.");
+}
 
-               create(task_type);
+template<typename T, typename V>
+void FaceDetectionAdapter<T, V>::setModelInfo(const char *model_file, const char *meta_file, const char *label_file,
+                                                                                         const char *model_name)
+{
+       try {
+               create(convertToTaskType(model_name));
+       } catch (const BaseException &e) {
+               LOGW("A given model name is invalid so default task type will be used.");
        }
 
-       _model_file = string(model_file);
-       _meta_file = string(meta_file);
-       _label_file = string(label_file);
+       if (model_file)
+               _model_file = model_file;
+       if (meta_file)
+               _meta_file = meta_file;
+       if (label_file)
+               _label_file = label_file;
 
-       if (_model_file.empty() && _meta_file.empty() && _label_file.empty())
+       if (_model_file.empty() && _meta_file.empty() && _label_file.empty()) {
+               LOGW("Given model info is invalid so default model info will be used instead.");
                return;
+       }
 
        _object_detection->setUserModel(_model_file, _meta_file, _label_file);
 }
@@ -90,7 +111,7 @@ void FaceDetectionAdapter<T, V>::setEngineInfo(const char *engine_type, const ch
 
 template<typename T, typename V> void FaceDetectionAdapter<T, V>::configure()
 {
-       _object_detection->configure("face_detection.json");
+       _object_detection->configure(_config_file_name);
 }
 
 template<typename T, typename V> void FaceDetectionAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)