mv_machine_learning: drop Mediavision dependency from object detection adapter
authorInki Dae <inki.dae@samsung.com>
Fri, 9 Jun 2023 00:59:11 +0000 (09:59 +0900)
committerKwanghoon Son <k.son@samsung.com>
Tue, 4 Jul 2023 05:08:39 +0000 (14:08 +0900)
[Issue type] code refactoring

Drop Mediavision dependency from adapter class of object detection task
group. There is a use case that pre-trained model file is used in private.
Therefore, the model relevant code cannot be opened. And even such users want
to configure the inference or training engines in their way.

In this case, we need to manage it properly by providing plugin approach of
behavior class. And this patch is a first step for supporting plugin based
behavior class which can be delivered as separate package.

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

index 78cb090462be4b501248605c0021213cda090475..561a42f9be3faa24f4b33666b4e1a34478e9cebd 100644 (file)
@@ -38,16 +38,6 @@ namespace machine_learning
 class ObjectDetection
 {
 private:
-       void loadLabel();
-       void getEngineList();
-       void getDeviceList(const char *engine_type);
-       void updateResult(ObjectDetectionResult &result);
-       template<typename T>
-       void preprocess(mv_source_h &mv_src, std::shared_ptr<MetaInfo> metaInfo, std::vector<T> &inputVector);
-       template<typename T> void pushToInput(ObjectDetectionQueue<T> &input);
-       ObjectDetectionResult popFromOutput();
-       bool isOutputQueueEmpty();
-
        ObjectDetectionTaskType _task_type;
        template<typename T> std::queue<ObjectDetectionQueue<T> > static _incoming_queue;
        std::queue<ObjectDetectionResult> _outgoing_queue;
@@ -59,6 +49,23 @@ private:
        ObjectDetectionResult _current_result {};
        unsigned long _input_index {};
 
+       void loadLabel();
+       void getEngineList();
+       void getDeviceList(const char *engine_type);
+       void updateResult(ObjectDetectionResult &result);
+       template<typename T>
+       void preprocess(mv_source_h &mv_src, std::shared_ptr<MetaInfo> metaInfo, std::vector<T> &inputVector);
+       template<typename T> void pushToInput(ObjectDetectionQueue<T> &input);
+       ObjectDetectionResult popFromOutput();
+       bool isOutputQueueEmpty();
+       template<typename T> ObjectDetectionQueue<T> popFromInput();
+       template<typename T> bool isInputQueueEmpty();
+       void pushToOutput(ObjectDetectionResult &output);
+       std::shared_ptr<MetaInfo> getInputMetaInfo();
+       template<typename T> void perform(mv_source_h &mv_src, std::shared_ptr<MetaInfo> metaInfo);
+       template<typename T> void performAsync(ObjectDetectionInput &input, std::shared_ptr<MetaInfo> metaInfo);
+       bool exitThread();
+
 protected:
        std::unique_ptr<mediavision::inference::Inference> _inference;
        std::unique_ptr<MediaVision::Common::EngineConfig> _config;
@@ -76,6 +83,10 @@ protected:
 
        void getOutputNames(std::vector<std::string> &names);
        void getOutputTensor(std::string target_name, std::vector<float> &tensor);
+       void parseMetaFile(std::string meta_file_name);
+       template<typename T> void inference(std::vector<std::vector<T> > &inputVectors);
+       virtual ObjectDetectionResult &result() = 0;
+       template<typename T> friend void inferenceThreadLoop(ObjectDetection *object);
 
 public:
        ObjectDetection(ObjectDetectionTaskType task_type);
@@ -88,19 +99,11 @@ public:
        void getEngineType(unsigned int engine_index, char **engine_type);
        void getNumberOfDevices(const char *engine_type, unsigned int *number_of_devices);
        void getDeviceType(const char *engine_type, const unsigned int device_index, char **device_type);
-       std::shared_ptr<MetaInfo> getInputMetaInfo();
-       bool exitThread();
-       void parseMetaFile(const char *meta_file_name);
-       void configure();
+       void configure(std::string configFile);
        void prepare();
+       void perform(mv_source_h &mv_src);
+       void performAsync(ObjectDetectionInput &input);
        template<typename V> V &getOutput();
-       template<typename T> void perform(mv_source_h &mv_src, std::shared_ptr<MetaInfo> metaInfo);
-       template<typename T> void performAsync(ObjectDetectionInput &input, std::shared_ptr<MetaInfo> metaInfo);
-       template<typename T> void inference(std::vector<std::vector<T> > &inputVectors);
-       template<typename T> ObjectDetectionQueue<T> popFromInput();
-       template<typename T> bool isInputQueueEmpty();
-       void pushToOutput(ObjectDetectionResult &output);
-       virtual ObjectDetectionResult &result() = 0;
 };
 
 } // machine_learning
index 31bd39b3a90e1d8f4d01718a4eb34eea5559588d..530cfc97e50e470a037a5341b05c20d4ff731b3b 100644 (file)
@@ -92,8 +92,7 @@ void FaceDetectionAdapter<T, V>::setEngineInfo(const char *engine_type, const ch
 
 template<typename T, typename V> void FaceDetectionAdapter<T, V>::configure()
 {
-       _object_detection->parseMetaFile("face_detection.json");
-       _object_detection->configure();
+       _object_detection->configure("face_detection.json");
 }
 
 template<typename T, typename V> void FaceDetectionAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)
@@ -131,27 +130,12 @@ template<typename T, typename V> void FaceDetectionAdapter<T, V>::setInput(T &t)
 
 template<typename T, typename V> void FaceDetectionAdapter<T, V>::perform()
 {
-       shared_ptr<MetaInfo> metaInfo = _object_detection->getInputMetaInfo();
-       if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8)
-               _object_detection->perform<unsigned char>(_source.inference_src, metaInfo);
-       else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32)
-               _object_detection->perform<float>(_source.inference_src, metaInfo);
-       else
-               throw InvalidOperation("Invalid model data type.");
+       _object_detection->perform(_source.inference_src);
 }
 
 template<typename T, typename V> void FaceDetectionAdapter<T, V>::performAsync(T &t)
 {
-       shared_ptr<MetaInfo> metaInfo = _object_detection->getInputMetaInfo();
-
-       if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8) {
-               _object_detection->performAsync<unsigned char>(t, metaInfo);
-       } else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32) {
-               _object_detection->performAsync<float>(t, metaInfo);
-               // TODO
-       } else {
-               throw InvalidOperation("Invalid model data type.");
-       }
+       _object_detection->performAsync(t);
 }
 
 template<typename T, typename V> V &FaceDetectionAdapter<T, V>::getOutput()
index f1fcabf0627e70039f7b2c1493809b6efae9c556..6dc943367d507f43c78b211082f6828139df7f60 100644 (file)
@@ -187,9 +187,9 @@ void ObjectDetection::loadLabel()
        readFile.close();
 }
 
-void ObjectDetection::parseMetaFile(const char *meta_file_name)
+void ObjectDetection::parseMetaFile(string meta_file_name)
 {
-       _config = make_unique<EngineConfig>(string(MV_CONFIG_PATH) + string(meta_file_name));
+       _config = make_unique<EngineConfig>(string(MV_CONFIG_PATH) + meta_file_name);
 
        int ret = _config->getIntegerAttribute(string(MV_OBJECT_DETECTION_BACKEND_TYPE), &_backendType);
        if (ret != MEDIA_VISION_ERROR_NONE)
@@ -245,8 +245,10 @@ void ObjectDetection::parseMetaFile(const char *meta_file_name)
        loadLabel();
 }
 
-void ObjectDetection::configure()
+void ObjectDetection::configure(string configFile)
 {
+       parseMetaFile(configFile);
+
        int ret = _inference->bind(_backendType, _targetDeviceType);
        if (ret != MEDIA_VISION_ERROR_NONE)
                throw InvalidOperation("Fail to bind a backend engine.");
@@ -317,6 +319,17 @@ template<typename T> void ObjectDetection::perform(mv_source_h &mv_src, shared_p
        inference<T>(inputVectors);
 }
 
+void ObjectDetection::perform(mv_source_h &mv_src)
+{
+       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
+       if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8)
+               perform<unsigned char>(mv_src, metaInfo);
+       else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32)
+               perform<float>(mv_src, metaInfo);
+       else
+               throw InvalidOperation("Invalid model data type.");
+}
+
 template<typename T> void inferenceThreadLoop(ObjectDetection *object)
 {
        // If user called destroy API then this thread loop will be terminated.
@@ -362,6 +375,20 @@ template<typename T> void ObjectDetection::performAsync(ObjectDetectionInput &in
                _thread_handle = make_unique<thread>(&inferenceThreadLoop<T>, this);
 }
 
+void ObjectDetection::performAsync(ObjectDetectionInput &input)
+{
+       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
+
+       if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8) {
+               performAsync<unsigned char>(input, metaInfo);
+       } else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32) {
+               performAsync<float>(input, metaInfo);
+               // TODO
+       } else {
+               throw InvalidOperation("Invalid model data type.");
+       }
+}
+
 void ObjectDetection::updateResult(ObjectDetectionResult &result)
 {
        _current_result = result;
index c4f7fffbc79da4cec61d0967bc2a6738254d199b..ca285c02477c3d860f563d5fa8add97d3c46aba7 100644 (file)
@@ -98,8 +98,7 @@ void ObjectDetectionAdapter<T, V>::setEngineInfo(const char *engine_type, const
 
 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::configure()
 {
-       _object_detection->parseMetaFile("object_detection.json");
-       _object_detection->configure();
+       _object_detection->configure("object_detection.json");
 }
 
 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)
@@ -137,13 +136,7 @@ template<typename T, typename V> void ObjectDetectionAdapter<T, V>::setInput(T &
 
 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::perform()
 {
-       shared_ptr<MetaInfo> metaInfo = _object_detection->getInputMetaInfo();
-       if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8)
-               _object_detection->perform<unsigned char>(_source.inference_src, metaInfo);
-       else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32)
-               _object_detection->perform<float>(_source.inference_src, metaInfo);
-       else
-               throw InvalidOperation("Invalid model data type.");
+       _object_detection->perform(_source.inference_src);
 }
 
 template<typename T, typename V> V &ObjectDetectionAdapter<T, V>::getOutput()
@@ -153,16 +146,7 @@ template<typename T, typename V> V &ObjectDetectionAdapter<T, V>::getOutput()
 
 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::performAsync(T &t)
 {
-       shared_ptr<MetaInfo> metaInfo = _object_detection->getInputMetaInfo();
-
-       if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8) {
-               _object_detection->performAsync<unsigned char>(t, metaInfo);
-       } else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32) {
-               _object_detection->performAsync<float>(t, metaInfo);
-               // TODO
-       } else {
-               throw InvalidOperation("Invalid model data type.");
-       }
+       _object_detection->performAsync(t);
 }
 
 template class ObjectDetectionAdapter<ObjectDetectionInput, ObjectDetectionResult>;