mv_machine_learning: move inferenceThreadLoop into class
authorInki Dae <inki.dae@samsung.com>
Wed, 6 Sep 2023 00:49:36 +0000 (09:49 +0900)
committerKwanghoon Son <k.son@samsung.com>
Wed, 25 Oct 2023 01:54:03 +0000 (10:54 +0900)
[Issue type] : code cleanup

Move the thread callback, inferenceThreadLoop(), for performing asynchronous
inference into ObjectDetection class. This is a first step for introducing
asynchronous inference manager which can be used commonly for other
task groups.

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

index ff9c90f..83b328d 100644 (file)
@@ -68,6 +68,7 @@ private:
        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);
+       template<typename T> void inferenceThreadLoop();
 
 protected:
        std::unique_ptr<mediavision::inference::Inference> _inference;
@@ -89,7 +90,6 @@ protected:
        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);
index 5f39359..649e240 100644 (file)
@@ -360,29 +360,29 @@ void ObjectDetection::perform(mv_source_h &mv_src)
                throw InvalidOperation("Invalid model data type.");
 }
 
-template<typename T> void inferenceThreadLoop(ObjectDetection *object)
+template<typename T> void ObjectDetection::inferenceThreadLoop()
 {
        // If user called destroy API then this thread loop will be terminated.
-       while (!object->_exit_thread) {
+       while (!_exit_thread) {
                // If input queue is empty then skip inference request.
-               if (object->isInputQueueEmpty<T>())
+               if (isInputQueueEmpty<T>())
                        continue;
 
-               ObjectDetectionQueue<T> input = object->popFromInput<T>();
+               ObjectDetectionQueue<T> input = popFromInput<T>();
 
                LOGD("Popped : input frame number = %lu", input.frame_number);
 
-               object->inference<T>(input.inputs);
+               inference<T>(input.inputs);
 
-               ObjectDetectionResult &result = object->result();
-               result.frame_number = input.frame_number;
+               ObjectDetectionResult &resultQueue = result();
+               resultQueue.frame_number = input.frame_number;
 
-               object->pushToOutput(result);
+               pushToOutput(resultQueue);
        }
 
        // waitforOutputQueue function could wait for the notify event after while loop is exited.
        // So make sure to call notify_one() here.
-       object->_cv_event.notify_one();
+       _cv_event.notify_one();
 }
 
 template<typename T> void ObjectDetection::performAsync(ObjectDetectionInput &input, shared_ptr<MetaInfo> metaInfo)
@@ -403,7 +403,7 @@ template<typename T> void ObjectDetection::performAsync(ObjectDetectionInput &in
        LOGD("Pushed : input frame number = %lu", in_queue.frame_number);
 
        if (!_thread_handle)
-               _thread_handle = make_unique<thread>(&inferenceThreadLoop<T>, this);
+               _thread_handle = make_unique<thread>(&ObjectDetection::inferenceThreadLoop<T>, this);
 }
 
 void ObjectDetection::performAsync(ObjectDetectionInput &input)
@@ -579,7 +579,6 @@ template void ObjectDetection::perform<unsigned char>(mv_source_h &mv_src, share
 template void ObjectDetection::pushToInput<unsigned char>(ObjectDetectionQueue<unsigned char> &inputQueue);
 template ObjectDetectionQueue<unsigned char> ObjectDetection::popFromInput();
 template bool ObjectDetection::isInputQueueEmpty<unsigned char>();
-
 template void ObjectDetection::performAsync<unsigned char>(ObjectDetectionInput &input, shared_ptr<MetaInfo> metaInfo);
 
 }