clean exception code sandbox/inki.dae/face_recognition
authorInki Dae <inki.dae@samsung.com>
Fri, 11 Mar 2022 02:43:20 +0000 (11:43 +0900)
committerInki Dae <inki.dae@samsung.com>
Fri, 11 Mar 2022 02:43:20 +0000 (11:43 +0900)
Change-Id: Iae5569ba916dce82b2844625235048495de308f3
Signed-off-by: Inki Dae <inki.dae@samsung.com>
13 files changed:
mv_machine_learning/common/include/machine_learning_exception.h [new file with mode: 0644]
mv_machine_learning/face_recognition/CMakeLists.txt
mv_machine_learning/face_recognition/src/face_recognition.cpp
mv_machine_learning/face_recognition/src/nntrainer_dsm.cpp
mv_machine_learning/face_recognition/src/nntrainer_fvm.cpp
mv_machine_learning/face_recognition/src/simple_shot.cpp
mv_machine_learning/inference/CMakeLists.txt
mv_machine_learning/inference/src/inference_engine_helper.cpp
mv_machine_learning/training/CMakeLists.txt
mv_machine_learning/training/include/label_manager.h
mv_machine_learning/training/src/feature_vector_manager.cpp
mv_machine_learning/training/src/label_manager.cpp
mv_machine_learning/training/src/training_model.cpp

diff --git a/mv_machine_learning/common/include/machine_learning_exception.h b/mv_machine_learning/common/include/machine_learning_exception.h
new file mode 100644 (file)
index 0000000..4524210
--- /dev/null
@@ -0,0 +1,89 @@
+/**
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef MACHINE_LEARNING_EXCEPTION_H
+#define MACHINE_LEARNING_EXCEPTION_H
+
+#include <exception>
+#include <string>
+
+#include <mv_common.h>
+
+namespace Mediavision {
+namespace MachineLearning {
+namespace Exception {
+
+class FileNotFound : public std::exception {
+private:
+       std::string _msg;
+       mv_error_e _errorType;
+
+public:
+       FileNotFound(std::string msg, mv_error_e errorType = MEDIA_VISION_ERROR_INVALID_OPERATION) :
+               _msg("File not found : " + msg), _errorType(errorType) {}
+       const char *what() const throw() {
+               return _msg.c_str();
+       }
+       mv_error_e getError() { return _errorType; }
+};
+
+class InvalidParameter : public std::exception {
+private:
+       std::string _msg;
+       mv_error_e _errorType;  
+
+public:
+       InvalidParameter(std::string msg, mv_error_e errorType = MEDIA_VISION_ERROR_INVALID_OPERATION) :
+               _msg("Invalid parameter : " + msg), _errorType(errorType) {}
+       const char *what() const throw() {
+               return _msg.c_str();
+       }
+       mv_error_e getError() { return _errorType; }
+};
+
+class InvalidOperation : public std::exception {
+private:
+       std::string _msg;
+       mv_error_e _errorType;  
+
+public:
+       InvalidOperation(std::string msg, mv_error_e errorType = MEDIA_VISION_ERROR_INVALID_OPERATION) :
+               _msg("Invalid operation : " + msg), _errorType(errorType) {}
+       const char *what() const throw() {
+               return _msg.c_str();
+       }
+       mv_error_e getError() { return _errorType; }
+};
+
+class OutOfMemory : public std::exception {
+private:
+       std::string _msg;
+       mv_error_e _errorType;  
+
+public:
+       OutOfMemory(std::string msg, mv_error_e errorType = MEDIA_VISION_ERROR_INVALID_OPERATION) :
+               _msg("Out of memory : " + msg), _errorType(errorType) {}
+       const char *what() const throw() {
+               return _msg.c_str();
+       }
+       mv_error_e getError() { return _errorType; }
+};
+
+};
+};
+};
+
+#endif
\ No newline at end of file
index 6fcb15c..2806b8a 100644 (file)
@@ -17,5 +17,5 @@ else()
 endif()
 
 target_link_libraries(${PROJECT_NAME} ${MV_COMMON_LIB_NAME} ${OpenCV_LIBS} ${${PROJECT_NAME}_DEP_LIBRARIES} mv_inference mv_training)
-target_include_directories(${PROJECT_NAME} PRIVATE include ../inference/include ../training/include)
+target_include_directories(${PROJECT_NAME} PRIVATE include ../inference/include ../training/include ../common/include)
 install(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR})
index 93338cc..cf09240 100644 (file)
@@ -27,6 +27,7 @@
 #include <mv_common.h>
 #include <mv_private.h>
 
+#include "machine_learning_exception.h"
 #include "face_recognition.h"
 #include "nntrainer_fvm.h"
 #include "nntrainer_dsm.h"
@@ -34,6 +35,7 @@
 
 using namespace std;
 using namespace TrainingEngineInterface::Common;
+using namespace Mediavision::MachineLearning::Exception;
 
 FaceRecognition::FaceRecognition() :
                _initialized(), _internal(), _backbone(), _face_net_info(), _training_model(), _label_manager()
@@ -66,10 +68,10 @@ unique_ptr<DataSetManager> FaceRecognition::CreateDSM(const training_engine_back
        case TRAINING_ENGINE_BACKEND_NNTRAINER:
                return make_unique<NNTrainerDSM>();
        default:
-               throw string("Invalid training engine backend type.");
+               throw InvalidParameter("Invalid training engine backend type.", MEDIA_VISION_ERROR_INVALID_PARAMETER);
        }
 
-       throw string("Invalid training engine backend type.");
+       throw InvalidParameter("Invalid training engine backend type.", MEDIA_VISION_ERROR_INVALID_PARAMETER);
 }
 
 unique_ptr<FeatureVectorManager> FaceRecognition::CreateFVM(const training_engine_backend_type_e backend_type, std::string file_name)
@@ -78,10 +80,10 @@ unique_ptr<FeatureVectorManager> FaceRecognition::CreateFVM(const training_engin
        case TRAINING_ENGINE_BACKEND_NNTRAINER:
                return make_unique<NNTrainerFVM>(file_name);
        default:
-               throw string("Invalid training engine backend type.");
+               throw InvalidParameter("Invalid training engine backend type.", MEDIA_VISION_ERROR_INVALID_PARAMETER);
        }
 
-       throw string("Invalid training engine backend type.");
+       throw InvalidParameter("Invalid training engine backend type.", MEDIA_VISION_ERROR_INVALID_PARAMETER);
 }
 
 void FaceRecognition::UpdateDataSet(unique_ptr<DataSetManager>& data_set, std::vector<float>& feature_vec, const int label_idx, const int label_cnt)
@@ -135,9 +137,11 @@ void FaceRecognition::UpdateDataSet(unique_ptr<DataSetManager>& data_set, std::v
 
                data_set->Clear();
                data_set->LoadDataSet(fvm->GetFileName());
-       } catch (const string& exception) {
-               throw exception;
-       } catch (const exception& e) {
+       } catch (const InvalidOperation& e) {
+               throw e;
+       } catch (const InvalidParameter & e) {
+               throw e;
+       } catch (const OutOfMemory& e) {
                throw e;
        }
 }
@@ -150,12 +154,14 @@ void FaceRecognition::UpdateDataSet(unique_ptr<DataSetManager>& data_set)
                auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path);
 
                if (FaceRecogUtil::IsFileExist(fvm->GetFileName()) == false)
-                       throw string("Feature vector file not found.");
+                       throw FileNotFound("Feature vector file not found.", MEDIA_VISION_ERROR_INVALID_OPERATION);
 
                data_set->LoadDataSet(fvm->GetFileName());
-       } catch (const string& exception) {
-               throw exception;
-       } catch (const exception& e) {
+       } catch (const InvalidOperation& e) {
+               throw e;
+       } catch (const InvalidParameter & e) {
+               throw e;
+       } catch (const OutOfMemory& e) {
                throw e;
        }
 }
@@ -206,12 +212,15 @@ int FaceRecognition::Prepare()
                TrainingEngineBackendInfo engine_info = _training_model->GetTrainingEngineInfo();
 
                _internal = make_unique<InferenceEngineHelper>(engine_info.backend_name, engine_info.target_device);
-       } catch (const string& exception) {
-               LOGE("%s", exception.c_str());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
-       } catch (const std::exception& e) {
+       } catch (InvalidOperation& e) {
                LOGE("%s", e.what());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+               return e.getError();
+       } catch (InvalidParameter & e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       } catch (OutOfMemory& e) {
+               LOGE("%s", e.what());
+               return e.getError();
        }
 
        _initialized = true;
@@ -281,12 +290,15 @@ int FaceRecognition::RegisterNewFace(vector<float>& vec, string label_name)
                _training_model->ApplyDataSet(data_set);
                _training_model->Compile();
                _training_model->Training();
-       } catch (string& exception) {
-               LOGE("%s", exception.c_str());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
-       } catch (const std::exception& e) {
+       } catch (InvalidOperation& e) {
                LOGE("%s", e.what());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+               return e.getError();
+       } catch (InvalidParameter & e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       } catch (OutOfMemory& e) {
+               LOGE("%s", e.what());
+               return e.getError();
        }
 
        return MEDIA_VISION_ERROR_NONE;
@@ -298,32 +310,35 @@ int FaceRecognition::GetAnswer(vector<float>& result_tensor, unsigned int *out_i
 
        string result_str;
 
-       for (auto& r : result_tensor)
-               result_str += to_string(r) + " ";
+       try {
+               for (auto& r : result_tensor)
+                       result_str += to_string(r) + " ";
 
-       LOGD("raw data = %s", result_str.c_str());
+               LOGD("raw data = %s", result_str.c_str());
 
-       answer_idx = max_element(result_tensor.begin(), result_tensor.end()) - result_tensor.begin();
+               answer_idx = max_element(result_tensor.begin(), result_tensor.end()) - result_tensor.begin();
 
-       // Check decision threshold.
-       if (result_tensor[answer_idx] < _label_manager->GetDecisionThreshold()) {
-               return MEDIA_VISION_ERROR_NO_DATA;
-       }
+               // Check decision threshold.
+               if (result_tensor[answer_idx] < _label_manager->GetDecisionThreshold()) {
+                       throw InvalidOperation("Not meet decision threshold.", MEDIA_VISION_ERROR_NO_DATA);
+               }
 
-#if 0
-       float weighted = result_tensor[answer_idx] * _label_manager->GetDecisionWeight();
+               float weighted = result_tensor[answer_idx] * _label_manager->GetDecisionWeight();
 
-       // Check weighted value.
-       for (auto& r : result_tensor) {
-               if (result_tensor[answer_idx] == r)
-                       continue;
+               // Check decision weight threshold.
+               for (auto& r : result_tensor) {
+                       if (result_tensor[answer_idx] == r)
+                               continue;
 
-               if (weighted < r)
-                       return MEDIA_VISION_ERROR_NO_DATA;
-       }
-#endif
+                       if (weighted < r)
+                               throw InvalidOperation("Not meet decision weight threshold.", MEDIA_VISION_ERROR_NO_DATA);
+               }
 
-       *out_idx = answer_idx;
+               *out_idx = answer_idx;
+       } catch(InvalidOperation& e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       }
 
        return MEDIA_VISION_ERROR_NONE;
 }
@@ -402,12 +417,15 @@ int FaceRecognition::RecognizeFace(vector<float>& vec, unsigned int *out_idx)
                }
 
                return GetAnswer(result_tensor, out_idx);
-       } catch (const string& exception) {
-               LOGE("%s", exception.c_str());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
-       } catch (const exception& e) {
+       } catch (InvalidParameter& e) {
                LOGE("%s", e.what());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+               return e.getError();
+       } catch (InvalidOperation& e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       } catch (OutOfMemory& e) {
+               LOGE("%s", e.what());
+               return e.getError();
        }
 
        return MEDIA_VISION_ERROR_INVALID_OPERATION;
@@ -489,12 +507,15 @@ int FaceRecognition::DeleteLabel(std::string label_name)
                _training_model->ApplyDataSet(new_data_set);
                _training_model->Compile();
                _training_model->Training();
-       } catch (const string& exception) {
-               LOGE("%s", exception.c_str());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
-       } catch (const exception& e) {
+       } catch (InvalidOperation& e) {
                LOGE("%s", e.what());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+               return e.getError();
+       } catch (InvalidParameter& e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       } catch (OutOfMemory& e) {
+               LOGE("%s", e.what());
+               return e.getError();
        }
 
        return MEDIA_VISION_ERROR_NONE;
@@ -504,12 +525,9 @@ int FaceRecognition::GetLabelWithIndex(string& out_label, unsigned int label_idx
 {
        try {
                _label_manager->GetLabelString(out_label, label_idx);
-       } catch (const string& exception) {
-               LOGE("%s", exception.c_str());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
-       } catch (const exception& e) {
+       } catch (InvalidOperation& e) {
                LOGE("%s", e.what());
-               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+               return e.getError();
        }
 
        return MEDIA_VISION_ERROR_NONE;
index 34a5bb8..3ef31aa 100644 (file)
 #include <dlog.h>
 #include <mv_private.h>
 
+#include "machine_learning_exception.h"
 #include "nntrainer_dsm.h"
 
 using namespace std;
+using namespace Mediavision::MachineLearning::Exception;
 
 void NNTrainerDSM::PrintHeader(FeaVecHeader& fvh)
 {
@@ -44,7 +46,7 @@ void NNTrainerDSM::LoadDataSet(const std::string file_name)
        std::ifstream openFile(file_name);
 
        if (!openFile.is_open())
-               throw std::string("fail to open a file.");
+               throw InvalidOperation("fail to open a file.");
 
        // Feature vector file header is written at the end of the data file
        // So read feature vector header from the end of the file.
@@ -65,7 +67,7 @@ void NNTrainerDSM::LoadDataSet(const std::string file_name)
 
        float *line_data = new(std::nothrow) float[line_size_in_bytes];
        if (!line_data)
-               throw std::string("fail to allocate line_data.");
+               throw OutOfMemory("fail to allocate line_data.");
 
        for (size_t data_set_idx = 0; data_set_idx < fvh.data_set_cnt; ++data_set_idx) {
                openFile.read((char *)line_data, line_size_in_bytes);
index 68df940..75ea6cf 100644 (file)
  * limitations under the License.
  */
 
+#include "machine_learning_exception.h"
 #include "nntrainer_fvm.h"
 
 using namespace std;
+using namespace Mediavision::MachineLearning::Exception;
 
 NNTrainerFVM::NNTrainerFVM(const std::string feature_vector_file) : FeatureVectorManager(feature_vector_file)
 {
@@ -34,7 +36,7 @@ void NNTrainerFVM::WriteHeader(size_t feature_size, size_t one_hot_table_size, u
 
        writeFile.open(_feature_vector_file.c_str(), std::ios::out | std::ios::binary | std::ios::app);
        if (!writeFile.is_open())
-               throw std::string("fail to open a file");
+               throw InvalidOperation("fail to open a file");
 
        FeaVecHeader header = { feature_vector_signagure, feature_size, one_hot_table_size, data_set_cnt };
 
@@ -48,18 +50,18 @@ void NNTrainerFVM::ReadHeader(FeaVecHeader& header)
        std::ifstream openFile(_feature_vector_file.c_str(), std::ios::in | std::ios::binary);
 
        if (!openFile.is_open())
-               throw std::string("fail to open a file.");
+               throw InvalidOperation("fail to open a file.");
 
        openFile.seekg(static_cast<int>(sizeof(FeaVecHeader) * -1), std::ios::end);
 
        openFile.read((char *)&header, sizeof(FeaVecHeader));
        if (!openFile)
-               throw std::string("fail to read feature vector file.");
+               throw InvalidOperation("fail to read feature vector file.");
 
        openFile.close();
 
        if (header.signature != feature_vector_signagure)
-               throw std::string("wrong feature vector file header.");
+               throw InvalidParameter("wrong feature vector file header.", MEDIA_VISION_ERROR_INVALID_PARAMETER);
 }
 
 void NNTrainerFVM::WriteFeatureVec(std::vector<float>& feature_vec, const int max_label, const int label_index)
@@ -69,12 +71,12 @@ void NNTrainerFVM::WriteFeatureVec(std::vector<float>& feature_vec, const int ma
        writeFile.open(_feature_vector_file.c_str(), std::ios::out | std::ios::binary | std::ios::app);
 
        if (!writeFile.is_open())
-               throw std::string("fail to open a file.");
+               throw InvalidOperation("fail to open a file.");
 
        size_t img_size = feature_vec.size() * 4;
        std::unique_ptr<char[]> pBuffer = std::make_unique<char[]>(img_size);
        if (!pBuffer)
-               throw std::string("fail to allocate buffer.");
+               throw OutOfMemory("fail to allocate buffer.", MEDIA_VISION_ERROR_OUT_OF_MEMORY);
 
        memcpy(pBuffer.get(), feature_vec.data(), img_size);
        writeFile.write(pBuffer.get(), img_size);
index cfbae23..15910d2 100644 (file)
 
 #include <sys/stat.h>
 
+#include "machine_learning_exception.h"
 #include "simple_shot.h"
 #include "data_set_manager.h"
 #include "feature_vector_manager.h"
 
 using namespace std;
 using namespace TrainingEngineInterface::Common;
+using namespace Mediavision::MachineLearning::Exception;
 
 SimpleShot::SimpleShot(const training_engine_backend_type_e backend_type, const std::string internal_model_file) :
                                                TrainingModel(backend_type, internal_model_file)
 {
        map<int, string>::iterator item = _backend_lookup.find(backend_type);
        if (item == _backend_lookup.end())
-               throw string("Invalid training engine backend type.");
+               throw InvalidParameter("Invalid training engine backend type.", MEDIA_VISION_ERROR_INVALID_PARAMETER);
 
        _engine_info.backend_name = item->second;
        _engine_info.target_device = INFERENCE_TARGET_CPU;
@@ -79,46 +81,46 @@ void SimpleShot::ConfigureModel(int num_of_class)
        training_engine_config config = { _engine_info.backend_name };
        int ret = _training->BindBackend(config);
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw std::string("Fail to bind backend engine.");
+               throw InvalidOperation("Fail to bind backend engine.");
 
        training_engine_capacity capacity = { TRAINING_TENSOR_SHAPE_MIN };
        ret = _training->GetBackendCapacity(capacity);
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw std::string("Fail to get backend capacity.");
+               throw InvalidOperation("Fail to get backend capacity.");
 
        _model = _training->CreateModel();
        if (!_model)
-               throw std::string("Fail to create a model.");
+               throw InvalidOperation("Fail to create a model.");
 
        auto l2norm = _training->CreateLayer(TRAINING_LAYER_TYPE_L2NORM);
        if (!l2norm)
-               throw std::string("Fail to create l2norm layer.");
+               throw InvalidOperation("Fail to create l2norm layer.");
 
        auto knn = _training->CreateLayer(TRAINING_LAYER_TYPE_CENTROID_KNN);
        if (!knn)
-               throw std::string("Fail to create knn layer.");
+               throw InvalidOperation("Fail to create knn layer.");
 
        // Ps. In case of the first layer, input_shape property is mandatorily required.
        // 1:192 is a shape of backbone model output tensor.
        training_engine_layer_property l2norm_property = { .options = { "input_shape=1:192", "trainable=false" } };
        ret = _training->SetLayerProperty(l2norm.get(), l2norm_property);
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw std::string("Fail to set layer propery.");
+               throw InvalidOperation("Fail to set layer propery.");
 
        const std::string num_class_prop = "num_class=" + std::to_string(num_of_class);
        training_engine_layer_property knn_property = { .options = { num_class_prop, "trainable=false" } };
 
        ret = _training->SetLayerProperty(knn.get(), knn_property);
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw std::string("Fail to set layer property.");
+               throw InvalidOperation("Fail to set layer property.");
 
        ret = _training->AddLayer(_model.get(), l2norm.get());
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw std::string("Fail to add l2norm layer.");
+               throw InvalidOperation("Fail to add l2norm layer.");
 
        ret = _training->AddLayer(_model.get(), knn.get());
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw std::string("Fail to add knn layer.");
+               throw InvalidOperation("Fail to add knn layer.");
 }
 
 void SimpleShot::SaveModel(const std::string file_path)
@@ -135,5 +137,5 @@ void SimpleShot::SaveModel(const std::string file_path)
 
     ret = _training->SaveModel(_model.get(), file_path);
     if (ret != TRAINING_ENGINE_ERROR_NONE)
-        throw std::string("Fail to save a model.");
+        throw InvalidOperation("Fail to save a model.");
 }
index f9eae6d..34ee18b 100644 (file)
@@ -22,5 +22,5 @@ IF (${ENABLE_INFERENCE_PROFILER})
 ENDIF()
 
 target_link_libraries(${PROJECT_NAME} ${MV_COMMON_LIB_NAME} ${OpenCV_LIBS} ${${PROJECT_NAME}_DEP_LIBRARIES})
-target_include_directories(${PROJECT_NAME} PRIVATE include)
+target_include_directories(${PROJECT_NAME} PRIVATE include ../common/include)
 install(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR})
index f356f3b..6f1ed61 100644 (file)
  * limitations under the License.
  */
 
+#include "machine_learning_exception.h"
 #include "inference_engine_helper.h"
 
 using namespace std;
 using namespace InferenceEngineInterface::Common;
+using namespace Mediavision::MachineLearning::Exception;
 
 static std::map<std::string, int> Model_Formats = {
        { "caffemodel", INFERENCE_MODEL_CAFFE }, { "pb", INFERENCE_MODEL_TF },
@@ -34,17 +36,17 @@ InferenceEngineHelper::InferenceEngineHelper(std::string backend_name, int targe
 
        int ret = _engine->BindBackend(&config);
        if (ret != INFERENCE_ENGINE_ERROR_NONE)
-               throw std::string("fail to bind backend engine");
+               throw InvalidOperation("fail to bind backend engine");
 
        inference_engine_capacity capacity;
 
        ret = _engine->GetBackendCapacity(&capacity);
        if (ret != INFERENCE_ENGINE_ERROR_NONE)
-               throw std::string("fail to get backend engine capacity.");
+               throw InvalidOperation("fail to get backend engine capacity.");
 
        ret = _engine->SetTargetDevices(config.target_devices);
        if (ret != INFERENCE_ENGINE_ERROR_NONE)
-               throw std::string("fail to set target device.");
+               throw InvalidOperation("fail to set target device.");
 }
 
 InferenceEngineHelper::~InferenceEngineHelper()
@@ -214,13 +216,13 @@ int InferenceEngineHelper::Load(std::string backbone_path)
        ret = _engine->Load(models, (inference_model_format_e) model_type);
        if (ret != INFERENCE_ENGINE_ERROR_NONE) {
                if (model_type != INFERENCE_MODEL_NNTRAINER)
-                       throw std::string("fail to load model file.");
+                       throw InvalidOperation("fail to load model file.");
        }
 
        ret = PrepareTensorBuffers(_engine.get(), _inputs, _outputs);
        if (ret != INFERENCE_ENGINE_ERROR_NONE) {
                if (model_type != INFERENCE_MODEL_NNTRAINER)
-                       throw std::string("fail to prepare tensor buffers.");
+                       throw InvalidOperation("fail to prepare tensor buffers.");
        }
 
        return ret;
@@ -237,14 +239,14 @@ int InferenceEngineHelper::UpdateLayerInfo(const std::vector<std::string>& input
 
        if (input_layers.size() != input_tensor_info.size() ||
                output_layers.size() != output_tensor_info.size())
-               throw std::string("layer name and tensor info size are different.");
+               throw InvalidParameter("layer name and tensor info size are different.", MEDIA_VISION_ERROR_INVALID_PARAMETER);
 
        for (size_t idx = 0; idx < input_layers.size(); ++idx)
                _input_property.layers.insert(std::make_pair(input_layers[idx], input_tensor_info[idx]));
 
        ret = _engine->SetInputLayerProperty(_input_property);
        if (ret != INFERENCE_ENGINE_ERROR_NONE)
-               throw std::string("fail to set input layer property.");
+               throw InvalidOperation("fail to set input layer property.");
 
        _output_property.layers.clear();
 
@@ -253,7 +255,7 @@ int InferenceEngineHelper::UpdateLayerInfo(const std::vector<std::string>& input
 
        ret = _engine->SetOutputLayerProperty(_output_property);
        if (ret != INFERENCE_ENGINE_ERROR_NONE)
-               throw std::string("fail to set output layer property.");
+               throw InvalidOperation("fail to set output layer property.");
 
        return ret;
 }
@@ -261,12 +263,12 @@ int InferenceEngineHelper::UpdateLayerInfo(const std::vector<std::string>& input
 void InferenceEngineHelper::UpdateInputData(std::string layer_name, std::vector<float>& data, size_t size)
 {
        if (data.size() * 4 != size)
-               throw std::string("number of bytes of data should be same as size");
+               throw InvalidParameter("number of bytes of data should be same as size", MEDIA_VISION_ERROR_INVALID_PARAMETER);
 
        auto it = _inputs.find(layer_name);
 
        if (it == _inputs.end())
-               throw std::string("Invalid layer name.");
+               throw InvalidParameter("Invalid layer name.", MEDIA_VISION_ERROR_INVALID_PARAMETER);
 
        memcpy(it->second.buffer, data.data(), size);
 }
index 6ecd656..097dd0d 100644 (file)
@@ -17,5 +17,5 @@ else()
 endif()
 
 target_link_libraries(${PROJECT_NAME} ${MV_COMMON_LIB_NAME} ${OpenCV_LIBS} ${${PROJECT_NAME}_DEP_LIBRARIES})
-target_include_directories(${PROJECT_NAME} PRIVATE include)
+target_include_directories(${PROJECT_NAME} PRIVATE include ../common/include)
 install(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR})
index 952ecc2..ef90c3e 100644 (file)
@@ -32,7 +32,7 @@ private:
        std::map<std::string, std::string> _labels_and_files;
        std::string _label_file;
        float _decision_threshold;
-       static constexpr float _decision_weight = 1.2;
+       static constexpr float _decision_weight = 0.01;
 
 public:
        LabelManager(std::string label_file, double decision_threshold);
index 9b61dc9..fcb7fab 100644 (file)
 #include <opencv2/opencv.hpp>
 #include <opencv2/imgproc/imgproc.hpp>
 
+#include "machine_learning_exception.h"
 #include "feature_vector_manager.h"
 
 using namespace std;
+using namespace Mediavision::MachineLearning::Exception;
 
 FeatureVectorManager::FeatureVectorManager(const std::string feature_vector_file) : _feature_vector_file(feature_vector_file)
 {
@@ -40,12 +42,12 @@ void FeatureVectorManager::GetVecFromImg(const std::string image_file, std::vect
        cv::Mat src, dst;
 
        if (!FaceRecogUtil::IsImageFile(image_file))
-               throw std::string("Invalid image file.");
+               throw InvalidOperation("Invalid image file.");
 
        src = cv::imread(image_file);
 
        if (!src.data)
-               throw std::string("Invalid image data.");
+               throw InvalidOperation("Invalid image data.");
 
        cv::cvtColor(src, src, cv::COLOR_BGR2RGB);
 
index a92c768..437a645 100644 (file)
 #include <dlog.h>
 #include <mv_private.h>
 
+#include "machine_learning_exception.h"
 #include "label_manager.h"
 
 using namespace std;
+using namespace Mediavision::MachineLearning::Exception;
 
 LabelManager::LabelManager(std::string label_file, double decision_threshold) : _labels_and_files(), _label_file(label_file)
 {
@@ -52,7 +54,7 @@ unsigned int LabelManager::GetLabelIndex(const std::string given_label)
        int label_index = -1;
 
        if (readFile.fail())
-               throw std::string("Fail to open " + _label_file + " file.");
+               throw InvalidOperation("Fail to open " + _label_file + " file.");
 
        std::string line;
 
@@ -67,7 +69,7 @@ unsigned int LabelManager::GetLabelIndex(const std::string given_label)
        }
 
        readFile.close();
-       throw std::string("Label index not found.");
+       throw InvalidOperation("Label index not found.");
 }
 
 bool LabelManager::IsExist(const std::string given_label)
@@ -79,7 +81,7 @@ bool LabelManager::IsExist(const std::string given_label)
        int label_index = -1;
 
        if (readFile.fail())
-               throw std::string("Fail to open " + _label_file + " file.");
+               throw InvalidOperation("Fail to open " + _label_file + " file.");
 
        std::string line;
 
@@ -113,7 +115,7 @@ unsigned int LabelManager::RemoveLabel(const std::string given_label)
        int label_index = 0;
 
        if (readFile.fail() || !writeFile.is_open())
-               throw std::string("Fail to open " + _label_file + "or " + new_label_file + " file.");
+               throw InvalidOperation("Fail to open " + _label_file + "or " + new_label_file + " file.");
 
        std::string line;
 
@@ -149,7 +151,7 @@ int LabelManager::GetLabelString(std::string& label, const int idx)
        int ret = 0;
 
        if (readFile.fail())
-               throw std::string("Fail to open " + _label_file + " file.");
+               throw InvalidOperation("Fail to open " + _label_file + " file.");
 
        std::string line;
 
@@ -175,7 +177,7 @@ unsigned int LabelManager::AddLabelToFile(std::string given_label)
        writeFile.open(_label_file.c_str(), std::ios::out | std::ios::app);
 
        if (!writeFile.is_open())
-               throw std::string("Fail to open " + _label_file + " file.");
+               throw InvalidOperation("Fail to open " + _label_file + " file.");
 
        given_label += "\n";
        writeFile.write(given_label.c_str(), given_label.size());
@@ -197,7 +199,7 @@ int LabelManager::ImportLabel(void)
        int label_cnt = 0;
 
        if (readFile.fail())
-               throw std::string("Fail to open " + _label_file + " file.");
+               throw InvalidOperation("Fail to open " + _label_file + " file.");
 
        std::string line;
 
@@ -243,7 +245,7 @@ int LabelManager::GetMaxLabel(const std::string label_file)
        int label_cnt = 0;
 
        if (readFile.fail())
-               throw std::string("Fail to open " + label_file + " file.");
+               throw InvalidOperation("Fail to open " + label_file + " file.");
 
        std::string line;
 
@@ -263,17 +265,17 @@ int LabelManager::GetMaxLabel()
 std::string LabelManager::GetLabelFromAnswer(const std::vector<float>& result)
 {
        if (result.empty())
-               throw std::string("result vector is empty.");
+               throw InvalidParameter("result vector is empty.", MEDIA_VISION_ERROR_INVALID_PARAMETER);
 
        int answer_idx = max_element(result.begin(), result.end()) - result.begin();
        if (result[answer_idx] < _decision_threshold)
-               return std::string("Not recognized.");
+               throw InvalidOperation("Not recognized.");
 
        std::string answer_label;
 
        int ret = GetLabelString(answer_label, answer_idx);
        if (ret)
-               throw std::string("answer label not found.");
+               throw InvalidOperation("answer label not found.");
 
        return answer_label;
 }
\ No newline at end of file
index c362338..1820f8a 100644 (file)
 #include <dlog.h>
 #include <mv_private.h>
 
+#include "machine_learning_exception.h"
 #include "training_model.h"
 
 using namespace std;
 using namespace TrainingEngineInterface::Common;
+using namespace Mediavision::MachineLearning::Exception;
 
 TrainingModel::TrainingModel(const training_engine_backend_type_e backend_type, const std::string internal_model_file)
 {
@@ -39,11 +41,8 @@ TrainingModel::TrainingModel(const training_engine_backend_type_e backend_type,
 
        try {
                _training = std::make_unique<TrainingEngineInterface::Common::TrainingEngineCommon>();
-       } catch (std::string& exception) {
-               std::cerr << exception << std::endl;
-               return;
        } catch (const std::exception& e) {
-               std::cerr << e.what() << std::endl;
+               LOGE("%s", e.what());
                return;
        }
 }
@@ -62,40 +61,40 @@ void TrainingModel::ApplyDataSet(unique_ptr<DataSetManager>& data_set)
 
        _data_set = _training->CreateDataset();
        if (!_data_set)
-               throw std::string("Fail to create a dataset.");
+               throw InvalidOperation("Fail to create a dataset.");
 
        int ret;
 
        for (size_t idx = 0; idx < values.size(); ++idx) {
                ret = _training->AddDataToDataset(_data_set.get(), values[idx], labels[idx], TRAINING_DATASET_TYPE_TRAIN);
                if (ret != TRAINING_ENGINE_ERROR_NONE)
-                       throw string("Fail to add data to dataset.");
+                       throw InvalidOperation("Fail to add data to dataset.");
        }
 
        data_set->Clear();
 
        ret = _training->SetDataset(_model.get(), _data_set.get());
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw string("Fail to set dataset to model.");
+               throw InvalidOperation("Fail to set dataset to model.");
 }
 
 void TrainingModel::Compile()
 {
        auto optimizer = _training->CreateOptimizer(TRAINING_OPTIMIZER_TYPE_SGD);
        if (!optimizer)
-               throw string("Fail to create a optimizer.");
+               throw InvalidOperation("Fail to create a optimizer.");
 
        int ret = _training->SetOptimizerProperty(optimizer.get(), GetTrainingEngineInfo().optimizer_property);
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw string("Fail to set optimizer property.");
+               throw InvalidOperation("Fail to set optimizer property.");
 
        ret = _training->AddOptimizer(_model.get(), optimizer.get());
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw string("Fail to add optimizer to model.");
+               throw InvalidOperation("Fail to add optimizer to model.");
 
        ret = _training->CompileModel(_model.get(), GetTrainingEngineInfo().compile_property);
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw string("Fail to compile model.");
+               throw InvalidOperation("Fail to compile model.");
 }
 
 void TrainingModel::Training()
@@ -103,14 +102,12 @@ void TrainingModel::Training()
        training_engine_model_property model_property;
        int ret = _training->TrainModel(_model.get(), model_property);
        if (ret != TRAINING_ENGINE_ERROR_NONE)
-               throw string("Fail to train model.");
+               throw InvalidOperation("Fail to train model.");
 
        try {
                // Save model file.
                SaveModel(_internal_model_file);
-       } catch (const string& exception) {
-               throw exception;
-       } catch (const exception& e) {
+       } catch (const InvalidOperation& e) {
                throw e;
        }
 }