mv_machine_learning: optimize preprocess for other task groups 63/305063/1
authorVibhav Aggarwal <v.aggarwal@samsung.com>
Fri, 26 Jan 2024 04:23:21 +0000 (13:23 +0900)
committerVibhav Aggarwal <v.aggarwal@samsung.com>
Fri, 26 Jan 2024 05:17:04 +0000 (14:17 +0900)
[Issue type] code optimization

This patch optimizes the preprocess performance for tasks
other than ImageClassification. This is in continuation
with this patch[1].

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

Change-Id: Ibefd5e4e53bc5d91e3b86358d493dd13841a77b8
Signed-off-by: Vibhav Aggarwal <v.aggarwal@samsung.com>
mv_machine_learning/image_segmentation/include/ImageSegmentation.h
mv_machine_learning/image_segmentation/src/ImageSegmentation.cpp
mv_machine_learning/landmark_detection/include/LandmarkDetection.h
mv_machine_learning/landmark_detection/src/LandmarkDetection.cpp
mv_machine_learning/object_detection/include/ObjectDetection.h
mv_machine_learning/object_detection/src/ObjectDetection.cpp
mv_machine_learning/object_detection_3d/include/ObjectDetection3d.h
mv_machine_learning/object_detection_3d/src/ObjectDetection3d.cpp

index c16fa0d220f41d3d02de4b31ffa8cd7decbb68ab..31095bef319dc06405acfa2a00db68555e6e19e2 100644 (file)
@@ -50,7 +50,7 @@ private:
        void loadLabel();
        void getEngineList();
        void getDeviceList(const std::string &engine_type);
-       void preprocess(mv_source_h &mv_src, std::shared_ptr<MetaInfo> metaInfo, std::vector<T> &inputVector);
+       void configurePreprocess();
        std::shared_ptr<MetaInfo> getInputMetaInfo();
 
 protected:
index 096bc11644e4f2c5a6ffb27357e75d4d650252e3..7fecb325a3f04e0d8451562bb66d12ad41b0c338 100644 (file)
@@ -194,6 +194,8 @@ template<typename T> void ImageSegmentation<T>::prepare()
        ret = _inference->load();
        if (ret != MEDIA_VISION_ERROR_NONE)
                throw InvalidOperation("Fail to load model files.");
+
+       configurePreprocess();
 }
 
 template<typename T> shared_ptr<MetaInfo> ImageSegmentation<T>::getInputMetaInfo()
@@ -211,11 +213,12 @@ template<typename T> shared_ptr<MetaInfo> ImageSegmentation<T>::getInputMetaInfo
        return _config->getInputMetaMap()[tensor_buffer_iter->first];
 }
 
-template<typename T>
-void ImageSegmentation<T>::preprocess(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo, vector<T> &inputVector)
+template<typename T> void ImageSegmentation<T>::configurePreprocess()
 {
        LOGI("ENTER");
 
+       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
+
        PreprocessConfig config = { false,
                                                                metaInfo->colorSpace,
                                                                metaInfo->dataType,
@@ -239,7 +242,6 @@ void ImageSegmentation<T>::preprocess(mv_source_h &mv_src, shared_ptr<MetaInfo>
        }
 
        _preprocess.setConfig(config);
-       _preprocess.run<T>(mv_src, inputVector);
 
        LOGI("LEAVE");
 }
@@ -257,12 +259,10 @@ template<typename T> void ImageSegmentation<T>::inference(vector<vector<T> > &in
 
 template<typename T> void ImageSegmentation<T>::perform(mv_source_h &mv_src)
 {
-       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
-       vector<T> inputVector;
+       vector<vector<T> > inputVectors(1);
 
-       preprocess(mv_src, metaInfo, inputVector);
+       _preprocess.run<T>(mv_src, inputVectors[0]);
 
-       vector<vector<T> > inputVectors = { inputVector };
        inference(inputVectors);
 }
 
@@ -281,12 +281,10 @@ template<typename T> void ImageSegmentation<T>::performAsync(ImageSegmentationIn
                });
        }
 
-       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
-       vector<T> inputVector;
+       vector<vector<T> > inputVectors(1);
 
-       preprocess(input.inference_src, metaInfo, inputVector);
+       _preprocess.run<T>(input.inference_src, inputVectors[0]);
 
-       vector<vector<T> > inputVectors = { inputVector };
        _async_manager->push(inputVectors);
 }
 
index 01d45f0840dee5d1840a5f16f778cf3c14f3b7e2..92e062b609ee8b7cb463bd8ce57758014a234007 100644 (file)
@@ -47,7 +47,7 @@ private:
        void getEngineList();
        void getDeviceList(const std::string &engine_type);
 
-       void preprocess(mv_source_h &mv_src, std::shared_ptr<MetaInfo> metaInfo, std::vector<T> &inputVector);
+       void configurePreprocess();
        std::shared_ptr<MetaInfo> getInputMetaInfo();
 
 protected:
index d4f179e3a196807bf29ed5e9e82e4cda80cac2b9..3f979ccf28e3be2135c8a336d2382171399dcebc 100644 (file)
@@ -196,6 +196,8 @@ template<typename T> void LandmarkDetection<T>::prepare()
        ret = _inference->load();
        if (ret != MEDIA_VISION_ERROR_NONE)
                throw InvalidOperation("Fail to load model files.");
+
+       configurePreprocess();
 }
 
 template<typename T> shared_ptr<MetaInfo> LandmarkDetection<T>::getInputMetaInfo()
@@ -213,11 +215,12 @@ template<typename T> shared_ptr<MetaInfo> LandmarkDetection<T>::getInputMetaInfo
        return _config->getInputMetaMap()[tensor_buffer_iter->first];
 }
 
-template<typename T>
-void LandmarkDetection<T>::preprocess(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo, vector<T> &inputVector)
+template<typename T> void LandmarkDetection<T>::configurePreprocess()
 {
        LOGI("ENTER");
 
+       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
+
        PreprocessConfig config = { false,
                                                                metaInfo->colorSpace,
                                                                metaInfo->dataType,
@@ -241,7 +244,6 @@ void LandmarkDetection<T>::preprocess(mv_source_h &mv_src, shared_ptr<MetaInfo>
        }
 
        _preprocess.setConfig(config);
-       _preprocess.run<T>(mv_src, inputVector);
 
        LOGI("LEAVE");
 }
@@ -259,12 +261,10 @@ template<typename T> void LandmarkDetection<T>::inference(vector<vector<T> > &in
 
 template<typename T> void LandmarkDetection<T>::perform(mv_source_h &mv_src)
 {
-       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
-       vector<T> inputVector;
+       vector<vector<T> > inputVectors(1);
 
-       preprocess(mv_src, metaInfo, inputVector);
+       _preprocess.run<T>(mv_src, inputVectors[0]);
 
-       vector<vector<T> > inputVectors = { inputVector };
        inference(inputVectors);
 }
 
@@ -301,12 +301,10 @@ template<typename T> void LandmarkDetection<T>::performAsync(LandmarkDetectionIn
                });
        }
 
-       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
-       vector<T> inputVector;
+       vector<vector<T> > inputVectors(1);
 
-       preprocess(input.inference_src, metaInfo, inputVector);
+       _preprocess.run<T>(input.inference_src, inputVectors[0]);
 
-       vector<vector<T> > inputVectors = { inputVector };
        _async_manager->push(inputVectors);
 }
 
index 03bce2012f0f082377d5d0876e251b0fa8191e88..ad3b71d88fad925f1e7fb3d4991f38e1fdfc0507 100644 (file)
@@ -51,7 +51,7 @@ private:
        void loadLabel();
        void getEngineList();
        void getDeviceList(const std::string &engine_type);
-       void preprocess(mv_source_h &mv_src, std::shared_ptr<MetaInfo> metaInfo, std::vector<T> &inputVector);
+       void configurePreprocess();
        std::shared_ptr<MetaInfo> getInputMetaInfo();
 
 protected:
index ea31f322856a9bb1f791c8fa8ef0a091515126e5..ddf9ab1bdec1da1ab3dff1018a0f170b745133ad 100644 (file)
@@ -196,6 +196,8 @@ template<typename T> void ObjectDetection<T>::prepare()
        ret = _inference->load();
        if (ret != MEDIA_VISION_ERROR_NONE)
                throw InvalidOperation("Fail to load model files.");
+
+       configurePreprocess();
 }
 
 template<typename T> shared_ptr<MetaInfo> ObjectDetection<T>::getInputMetaInfo()
@@ -213,11 +215,12 @@ template<typename T> shared_ptr<MetaInfo> ObjectDetection<T>::getInputMetaInfo()
        return _config->getInputMetaMap()[tensor_buffer_iter->first];
 }
 
-template<typename T>
-void ObjectDetection<T>::preprocess(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo, vector<T> &inputVector)
+template<typename T> void ObjectDetection<T>::configurePreprocess()
 {
        LOGI("ENTER");
 
+       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
+
        PreprocessConfig config = { false,
                                                                metaInfo->colorSpace,
                                                                metaInfo->dataType,
@@ -241,7 +244,6 @@ void ObjectDetection<T>::preprocess(mv_source_h &mv_src, shared_ptr<MetaInfo> me
        }
 
        _preprocess.setConfig(config);
-       _preprocess.run<T>(mv_src, inputVector);
 
        LOGI("LEAVE");
 }
@@ -259,12 +261,10 @@ template<typename T> void ObjectDetection<T>::inference(vector<vector<T> > &inpu
 
 template<typename T> void ObjectDetection<T>::perform(mv_source_h &mv_src)
 {
-       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
-       vector<T> inputVector;
+       vector<vector<T> > inputVectors(1);
 
-       preprocess(mv_src, metaInfo, inputVector);
+       _preprocess.run<T>(mv_src, inputVectors[0]);
 
-       vector<vector<T> > inputVectors = { inputVector };
        inference(inputVectors);
 }
 
@@ -283,12 +283,10 @@ template<typename T> void ObjectDetection<T>::performAsync(ObjectDetectionInput
                });
        }
 
-       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
-       vector<T> inputVector;
+       vector<vector<T> > inputVectors(1);
 
-       preprocess(input.inference_src, metaInfo, inputVector);
+       _preprocess.run<T>(input.inference_src, inputVectors[0]);
 
-       vector<vector<T> > inputVectors = { inputVector };
        _async_manager->push(inputVectors);
 }
 
index e2b347a501bb4e428768436d2140b3fae4074894..fb6058dcf6b90e400d6166bb831d9e78df55c27c 100644 (file)
@@ -54,7 +54,7 @@ protected:
 
        void getOutputNames(std::vector<std::string> &names);
        void getOutputTensor(std::string &target_name, std::vector<float> &tensor);
-       void preprocess(mv_source_h &mv_src, std::shared_ptr<MetaInfo> metaInfo, std::vector<T> &inputVector);
+       void configurePreprocess();
        void inference(std::vector<std::vector<T> > &inputVectors);
 
 public:
index 120f64762b526525bfbd6c872a553a4e914d1640..51f1b109ff655ce577cf1ee2e3f3e2c958005dbb 100644 (file)
@@ -186,6 +186,8 @@ template<typename T> void ObjectDetection3d<T>::prepare()
        ret = _inference->load();
        if (ret != MEDIA_VISION_ERROR_NONE)
                throw InvalidOperation("Fail to load model files.");
+
+       configurePreprocess();
 }
 
 template<typename T> shared_ptr<MetaInfo> ObjectDetection3d<T>::getInputMetaInfo()
@@ -203,11 +205,12 @@ template<typename T> shared_ptr<MetaInfo> ObjectDetection3d<T>::getInputMetaInfo
        return _config->getInputMetaMap()[tensor_buffer_iter->first];
 }
 
-template<typename T>
-void ObjectDetection3d<T>::preprocess(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo, vector<T> &inputVector)
+template<typename T> void ObjectDetection3d<T>::configurePreprocess()
 {
        LOGI("ENTER");
 
+       shared_ptr<MetaInfo> metaInfo = getInputMetaInfo();
+
        PreprocessConfig config = { false,
                                                                metaInfo->colorSpace,
                                                                metaInfo->dataType,
@@ -231,7 +234,6 @@ void ObjectDetection3d<T>::preprocess(mv_source_h &mv_src, shared_ptr<MetaInfo>
        }
 
        _preprocess.setConfig(config);
-       _preprocess.run<T>(mv_src, inputVector);
 
        LOGI("LEAVE");
 }
@@ -249,11 +251,9 @@ template<typename T> void ObjectDetection3d<T>::inference(vector<vector<T> > &in
 
 template<typename T> void ObjectDetection3d<T>::perform(mv_source_h &mv_src, shared_ptr<MetaInfo> metaInfo)
 {
-       vector<T> inputVector;
-
-       preprocess(mv_src, metaInfo, inputVector);
+       vector<vector<T> > inputVectors(1);
 
-       vector<vector<T> > inputVectors = { inputVector };
+       _preprocess.run<T>(mv_src, inputVectors[0]);
 
        inference(inputVectors);
 }