mv_machine_learning: use carnel notation
[platform/core/api/mediavision.git] / mv_machine_learning / training / src / training_model.cpp
1 /**
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <string.h>
18 #include <fstream>
19 #include <istream>
20 #include <tuple>
21 #include <map>
22 #include <algorithm>
23
24 #include <sys/stat.h>
25
26 #include <dlog.h>
27 #include <mv_private.h>
28
29 #include "machine_learning_exception.h"
30 #include "training_model.h"
31
32 using namespace std;
33 using namespace TrainingEngineInterface::Common;
34 using namespace mediavision::machine_learning::exception;
35
36 TrainingModel::TrainingModel(const training_backend_type_e backend_type, const training_target_type_e target_type,
37                                                          vector<size_t> input_tensor_shape, const string internal_model_file)
38 {
39         _internal_model_file = internal_model_file;
40         _training = make_unique<TrainingEngineInterface::Common::TrainingEngineCommon>();
41
42         training_engine_config config = { "", backend_type, target_type };
43         int ret = _training->BindBackend(&config);
44         if (ret != TRAINING_ENGINE_ERROR_NONE)
45                 throw InvalidOperation("Fail to bind backend engine.");
46
47         training_engine_capacity capacity = { TRAINING_TENSOR_SHAPE_MIN };
48         ret = _training->GetBackendCapacity(capacity);
49         if (ret != TRAINING_ENGINE_ERROR_NONE)
50                 throw InvalidOperation("Fail to get backend capacity.");
51 }
52
53 TrainingModel::~TrainingModel()
54 {
55         if (_training)
56                 _training->UnbindBackend();
57 }
58
59 void TrainingModel::applyDataSet(unique_ptr<DataSetManager> &data_set)
60 {
61         auto &values = data_set->getData();
62         auto &labels = data_set->getLabel();
63
64         LOGD("Generating feature vectors for training");
65
66         _data_set = _training->CreateDataset();
67         if (!_data_set)
68                 throw InvalidOperation("Fail to create a dataset.");
69
70         int ret;
71
72         for (size_t idx = 0; idx < values.size(); ++idx) {
73                 ret = _training->AddDataToDataset(_data_set.get(), values[idx], labels[idx], TRAINING_DATASET_TYPE_TRAIN);
74                 if (ret != TRAINING_ENGINE_ERROR_NONE)
75                         throw InvalidOperation("Fail to add data to dataset.", ret);
76         }
77
78         ret = _training->SetDataset(_model.get(), _data_set.get());
79         if (ret != TRAINING_ENGINE_ERROR_NONE)
80                 throw InvalidOperation("Fail to set dataset to model.", ret);
81 }
82
83 void TrainingModel::clearDataSet(unique_ptr<DataSetManager> &data_set)
84 {
85         data_set->clear();
86         _training->DestroyDataset(_data_set.get());
87 }
88
89 void TrainingModel::compile()
90 {
91         auto optimizer = _training->CreateOptimizer(TRAINING_OPTIMIZER_TYPE_SGD);
92         if (!optimizer)
93                 throw InvalidOperation("Fail to create a optimizer.");
94
95         int ret = _training->SetOptimizerProperty(optimizer.get(), getTrainingEngineInfo().optimizer_property);
96         if (ret != TRAINING_ENGINE_ERROR_NONE)
97                 throw InvalidOperation("Fail to set optimizer property.", ret);
98
99         ret = _training->AddOptimizer(_model.get(), optimizer.get());
100         if (ret != TRAINING_ENGINE_ERROR_NONE)
101                 throw InvalidOperation("Fail to add optimizer to model.", ret);
102
103         ret = _training->CompileModel(_model.get(), getTrainingEngineInfo().compile_property);
104         if (ret != TRAINING_ENGINE_ERROR_NONE)
105                 throw InvalidOperation("Fail to compile model.", ret);
106 }
107
108 void TrainingModel::train()
109 {
110         training_engine_model_property model_property;
111         int ret = _training->TrainModel(_model.get(), model_property);
112         if (ret != TRAINING_ENGINE_ERROR_NONE)
113                 throw InvalidOperation("Fail to train model.", ret);
114
115         // Save model file.
116         saveModel(_internal_model_file);
117 }
118
119 void TrainingModel::getWeights(float **weights, size_t *size, std::string name)
120 {
121         // TODO.
122 }
123
124 void TrainingModel::removeModel()
125 {
126         removeModel(_internal_model_file);
127 }