--- /dev/null
+/**
+ * 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
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})
#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"
using namespace std;
using namespace TrainingEngineInterface::Common;
+using namespace Mediavision::MachineLearning::Exception;
FaceRecognition::FaceRecognition() :
_initialized(), _internal(), _backbone(), _face_net_info(), _training_model(), _label_manager()
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)
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)
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;
}
}
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;
}
}
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;
_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;
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;
}
}
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;
_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;
{
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;
#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)
{
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.
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);
* 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)
{
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 };
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)
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);
#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;
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)
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.");
}
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})
* 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 },
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()
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;
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();
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;
}
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);
}
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})
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);
#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)
{
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);
#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)
{
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;
}
readFile.close();
- throw std::string("Label index not found.");
+ throw InvalidOperation("Label index not found.");
}
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;
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;
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;
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());
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;
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;
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
#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)
{
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;
}
}
_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()
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;
}
}