mv_machine_learning: face recognition: drop nntrainer dependency 68/286368/3
authorInki Dae <inki.dae@samsung.com>
Thu, 5 Jan 2023 00:37:18 +0000 (09:37 +0900)
committerInki Dae <inki.dae@samsung.com>
Thu, 5 Jan 2023 07:51:38 +0000 (16:51 +0900)
[Issue type] code refactoring

Did code refactoring to face recognition by dropping NNTrainer dependency.
With this patch, Face recognition framework uses only NNTrainer backend
type for inference and training. And also did code cleanup by
dropping redundant code.

Change-Id: Idb1ec527f9f08b5f18a6c647325a99092d0ed4ff
Signed-off-by: Inki Dae <inki.dae@samsung.com>
13 files changed:
mv_machine_learning/face_recognition/include/face_recognition.h
mv_machine_learning/face_recognition/include/face_recognition_dsm.h [new file with mode: 0644]
mv_machine_learning/face_recognition/include/face_recognition_fvm.h [new file with mode: 0644]
mv_machine_learning/face_recognition/include/nntrainer_dsm.h [deleted file]
mv_machine_learning/face_recognition/include/nntrainer_fvm.h [deleted file]
mv_machine_learning/face_recognition/src/face_recognition.cpp
mv_machine_learning/face_recognition/src/face_recognition_dsm.cpp [new file with mode: 0644]
mv_machine_learning/face_recognition/src/face_recognition_fvm.cpp [new file with mode: 0644]
mv_machine_learning/face_recognition/src/nntrainer_dsm.cpp [deleted file]
mv_machine_learning/face_recognition/src/nntrainer_fvm.cpp [deleted file]
mv_machine_learning/face_recognition/src/simple_shot.cpp
mv_machine_learning/training/include/feature_vector_manager.h
mv_machine_learning/training/include/training_model.h

index f927031c21e152b98c9a52e29ef6423ba3824927..bcc46891e605b8c52b79cc120b2934080f32c560 100644 (file)
 #include "Inference.h"
 #include "label_manager.h"
 #include "backbone_model_info.h"
+#include "feature_vector_manager.h"
+#include "data_set_manager.h"
 #include "simple_shot.h"
-#include "data_augment_default.h"
-#include "data_augment_flip.h"
-#include "data_augment_rotate.h"
 
 namespace mediavision
 {
@@ -96,14 +95,10 @@ private:
        std::unique_ptr<LabelManager> _label_manager;
        FaceRecognitionConfig _config;
        mv_face_recognition_result_s _result;
-       std::vector<std::unique_ptr<DataAugment> > _data_augments;
 
        // FYI. This function should be called every time a new face is registered.
        void ImportLabel();
-       void CheckFeatureVectorFile(std::unique_ptr<FeatureVectorManager> &old_fvm,
-                                                               std::unique_ptr<FeatureVectorManager> &new_fvm);
-       std::unique_ptr<DataSetManager> CreateDSM(const training_backend_type_e backend_type);
-       std::unique_ptr<FeatureVectorManager> CreateFVM(const training_backend_type_e backend_type, std::string file_name);
+       void CheckFeatureVectorFile(std::string fv_file_name, std::string new_fv_file_name);
        void StoreDataSet(std::unique_ptr<DataSetManager> &data_set, unsigned int label_cnt);
        int GetAnswer();
        std::vector<model_layer_info> &GetBackboneInputLayerInfo();
diff --git a/mv_machine_learning/face_recognition/include/face_recognition_dsm.h b/mv_machine_learning/face_recognition/include/face_recognition_dsm.h
new file mode 100644 (file)
index 0000000..ace108a
--- /dev/null
@@ -0,0 +1,40 @@
+/**
+ * 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 __FACE_RECOGNITION_DSM_H__
+#define __FACE_RECOGNITION_DSM_H__
+
+#include <string>
+
+#include "feature_vector_manager.h"
+#include "data_set_manager.h"
+
+class FaceRecognitionDSM : public DataSetManager
+{
+private:
+       void PrintHeader(FeaVecHeader &fvh);
+
+public:
+       FaceRecognitionDSM();
+       ~FaceRecognitionDSM() = default;
+
+       bool IsFeatureVectorAllowed(unsigned int label_idx) override;
+       void LoadDataSet(const std::string file_name, unsigned int new_label_cnt) override;
+       void AddDataSet(std::vector<float> &feature_vec, const unsigned int label_idx,
+                                       const unsigned int label_cnt) override;
+};
+
+#endif
\ No newline at end of file
diff --git a/mv_machine_learning/face_recognition/include/face_recognition_fvm.h b/mv_machine_learning/face_recognition/include/face_recognition_fvm.h
new file mode 100644 (file)
index 0000000..e0b4241
--- /dev/null
@@ -0,0 +1,37 @@
+/**
+ * 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 __FACE_RECOGNITION_FVM_H__
+#define __FACE_RECOGNITION_FVM_H__
+
+#include <string.h>
+#include <vector>
+
+#include "feature_vector_manager.h"
+#include "file_util.h"
+
+class FaceRecognitionFVM : public FeatureVectorManager
+{
+public:
+       FaceRecognitionFVM(const std::string feature_vector_file = "feature_vector_file.dat");
+       ~FaceRecognitionFVM() = default;
+
+       void WriteHeader(size_t feature_size, size_t label_cnt, unsigned int data_set_cnt) override;
+       void StoreData(std::vector<std::vector<float> > &features_vec, std::vector<unsigned int> &label_index) override;
+       void Remove() override;
+};
+
+#endif
\ No newline at end of file
diff --git a/mv_machine_learning/face_recognition/include/nntrainer_dsm.h b/mv_machine_learning/face_recognition/include/nntrainer_dsm.h
deleted file mode 100644 (file)
index f988ea0..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/**
- * 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 __NNTRAINER_DSM_H__
-#define __NNTRAINER_DSM_H__
-
-#include <string>
-
-#include "feature_vector_manager.h"
-#include "data_set_manager.h"
-
-class NNTrainerDSM : public DataSetManager
-{
-private:
-       void PrintHeader(FeaVecHeader &fvh);
-
-public:
-       NNTrainerDSM();
-       ~NNTrainerDSM() = default;
-
-       bool IsFeatureVectorAllowed(unsigned int label_idx) override;
-       void LoadDataSet(const std::string file_name, unsigned int new_label_cnt) override;
-       void AddDataSet(std::vector<float> &feature_vec, const unsigned int label_idx,
-                                       const unsigned int label_cnt) override;
-};
-
-#endif
\ No newline at end of file
diff --git a/mv_machine_learning/face_recognition/include/nntrainer_fvm.h b/mv_machine_learning/face_recognition/include/nntrainer_fvm.h
deleted file mode 100644 (file)
index 8782c01..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-/**
- * 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 __NNTRAINER_FVM_H__
-#define __NNTRAINER_FVM_H__
-
-#include <string.h>
-#include <vector>
-
-#include "feature_vector_manager.h"
-#include "file_util.h"
-
-class NNTrainerFVM : public FeatureVectorManager
-{
-public:
-       NNTrainerFVM(const std::string feature_vector_file = "feature_vector_file.dat");
-       ~NNTrainerFVM() = default;
-
-       void WriteHeader(size_t feature_size, size_t label_cnt, unsigned int data_set_cnt) override;
-       void ReadHeader(FeaVecHeader &header) override;
-       void StoreData(std::vector<std::vector<float> > &features_vec, std::vector<unsigned int> &label_index) override;
-       void Remove() override;
-};
-
-#endif
\ No newline at end of file
index 54b3557216ed0a0f8311a325c6218dd779b3dc06..6ffc4d70d44741548d7eb2dea5f7f3a8bfc8c68a 100644 (file)
@@ -30,8 +30,8 @@
 #include "machine_learning_exception.h"
 #include "TensorBuffer.h"
 #include "face_recognition.h"
-#include "nntrainer_fvm.h"
-#include "nntrainer_dsm.h"
+#include "face_recognition_fvm.h"
+#include "face_recognition_dsm.h"
 #include "file_util.h"
 
 using namespace std;
@@ -53,10 +53,7 @@ FaceRecognition::FaceRecognition()
                , _label_manager()
                , _config()
                , _result()
-{
-       _data_augments.push_back(std::make_unique<DataAugmentDefault>());
-       /* Add other data argument classes. */
-}
+{}
 
 FaceRecognition::~FaceRecognition()
 {
@@ -64,13 +61,12 @@ FaceRecognition::~FaceRecognition()
                _label_manager->Clear();
 }
 
-void FaceRecognition::CheckFeatureVectorFile(unique_ptr<FeatureVectorManager> &old_fvm,
-                                                                                        unique_ptr<FeatureVectorManager> &new_fvm)
+void FaceRecognition::CheckFeatureVectorFile(string fv_file_name, string new_fv_file_name)
 {
        // Change new feature vector file to existing one in case that current process is terminated just after removing existing feature vector file but
        // new feature vector file isn't changed to existing one yet.
-       if (FaceRecogUtil::IsFileExist(new_fvm->GetFileName()) && !FaceRecogUtil::IsFileExist(old_fvm->GetFileName())) {
-               int ret = ::rename(new_fvm->GetFileName().c_str(), old_fvm->GetFileName().c_str());
+       if (FaceRecogUtil::IsFileExist(new_fv_file_name) && !FaceRecogUtil::IsFileExist(fv_file_name)) {
+               int ret = ::rename(new_fv_file_name.c_str(), fv_file_name.c_str());
                if (ret)
                        throw InvalidOperation("Fail to rename new feature vector file to original one.");
 
@@ -79,46 +75,22 @@ void FaceRecognition::CheckFeatureVectorFile(unique_ptr<FeatureVectorManager> &o
 
        // Make sure to remove a temp file in case that current process is terminated just after generating new feature vector file
        // which is not correct file but existing one isn't removed. In this cae, existing file is used again.
-       if (FaceRecogUtil::IsFileExist(new_fvm->GetFileName())) {
-               int ret = ::remove(new_fvm->GetFileName().c_str());
+       if (FaceRecogUtil::IsFileExist(new_fv_file_name)) {
+               int ret = ::remove(new_fv_file_name.c_str());
                if (ret)
                        throw InvalidOperation("Fail to remove new feature vector file.");
        }
 }
 
-unique_ptr<DataSetManager> FaceRecognition::CreateDSM(const training_backend_type_e backend_type)
-{
-       switch (backend_type) {
-       case TRAINING_BACKEND_NNTRAINER:
-               return make_unique<NNTrainerDSM>();
-       default:
-               break;
-       }
-
-       throw InvalidParameter("Invalid training engine backend type.");
-}
-
-unique_ptr<FeatureVectorManager> FaceRecognition::CreateFVM(const training_backend_type_e backend_type,
-                                                                                                                       string file_name)
-{
-       switch (backend_type) {
-       case TRAINING_BACKEND_NNTRAINER:
-               return make_unique<NNTrainerFVM>(file_name);
-       default:
-               break;
-       }
-
-       throw InvalidParameter("Invalid training engine backend type.");
-}
-
 void FaceRecognition::StoreDataSet(unique_ptr<DataSetManager> &data_set, unsigned int label_cnt)
 {
        try {
-               auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path);
-               auto fvm_new = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path + ".new");
+               unique_ptr<FeatureVectorManager> fvm = make_unique<FaceRecognitionFVM>(_config.feature_vector_file_path);
+               unique_ptr<FeatureVectorManager> fvm_new =
+                               make_unique<FaceRecognitionFVM>(_config.feature_vector_file_path + ".new");
 
                // Make sure feature vector file.
-               CheckFeatureVectorFile(fvm, fvm_new);
+               CheckFeatureVectorFile(fvm->GetFileName(), fvm_new->GetFileName());
 
                // 1. Write feature vector and it's label index.
                fvm_new->StoreData(data_set->GetData(), data_set->GetLabelIdx());
@@ -244,7 +216,7 @@ int FaceRecognition::RegisterNewFace(std::vector<float> &input_vec, string label
 
                _training_model->ConfigureModel(label_cnt);
 
-               unique_ptr<DataSetManager> data_set = CreateDSM(_config.training_engine_backend_type);
+               unique_ptr<DataSetManager> data_set = make_unique<FaceRecognitionDSM>();
 
                data_set->Clear();
 
@@ -265,7 +237,7 @@ int FaceRecognition::RegisterNewFace(std::vector<float> &input_vec, string label
                _training_model->Train();
 
                // TODO. apply feature vector priority policy here.
-               // We can get weight trained from NNTrainer.
+               // We can get weight trained from training engine.
                // _training_model->getWeights(&weights, &size, "centroid_knn1");
 
                // Store dataset to feature vector file.
@@ -429,13 +401,14 @@ int FaceRecognition::DeleteLabel(string label_name)
                _label_manager->RemoveLabel(label_name);
 
                auto label_cnt = _label_manager->GetMaxLabel();
-               auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path);
-               auto fvm_new = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path + ".new");
+               unique_ptr<FeatureVectorManager> fvm = make_unique<FaceRecognitionFVM>(_config.feature_vector_file_path);
+               unique_ptr<FeatureVectorManager> fvm_new =
+                               make_unique<FaceRecognitionFVM>(_config.feature_vector_file_path + ".new");
 
                // Make sure feature vector file.
-               CheckFeatureVectorFile(fvm, fvm_new);
+               CheckFeatureVectorFile(fvm->GetFileName(), fvm_new->GetFileName());
 
-               auto data_set = CreateDSM(_config.training_engine_backend_type);
+               unique_ptr<DataSetManager> data_set = make_unique<FaceRecognitionDSM>();
 
                // feature vectors corresponding to given label aren't removed yet from feature vector file.
                // So label_cnt_ori is needed.
@@ -496,7 +469,7 @@ int FaceRecognition::DeleteLabel(string label_name)
                                throw InvalidOperation("Fail to rename new feature vector file to original one.");
 
                        _training_model->ConfigureModel(label_cnt);
-                       unique_ptr<DataSetManager> new_data_set = CreateDSM(_config.training_engine_backend_type);
+                       unique_ptr<DataSetManager> new_data_set = make_unique<FaceRecognitionDSM>();
                        new_data_set->Clear();
 
                        // TODO. Remove existing internal model file.
diff --git a/mv_machine_learning/face_recognition/src/face_recognition_dsm.cpp b/mv_machine_learning/face_recognition/src/face_recognition_dsm.cpp
new file mode 100644 (file)
index 0000000..5611daf
--- /dev/null
@@ -0,0 +1,122 @@
+/**
+ * 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.
+ */
+
+#include <dlog.h>
+#include <mv_private.h>
+#include <algorithm>
+
+#include "machine_learning_exception.h"
+#include "face_recognition_dsm.h"
+#define MAX_FEATURE_VECTOR_CNT 5
+
+using namespace std;
+using namespace mediavision::machine_learning::exception;
+
+void FaceRecognitionDSM::PrintHeader(FeaVecHeader &fvh)
+{
+       LOGD("signature = %u", fvh.signature);
+       LOGD("feature vector size = %zu", fvh.feature_size);
+       LOGD("label count = %zu", fvh.label_cnt);
+       LOGD("data set count = %u", fvh.data_set_cnt);
+}
+
+FaceRecognitionDSM::FaceRecognitionDSM() : DataSetManager()
+{}
+
+bool FaceRecognitionDSM::IsFeatureVectorAllowed(unsigned int label_idx)
+{
+       return (_fv_cnt_per_label[label_idx] < MAX_FEATURE_VECTOR_CNT);
+}
+
+void FaceRecognitionDSM::AddDataSet(std::vector<float> &feature_vec, const unsigned int label_idx,
+                                                                       const unsigned int label_cnt)
+{
+       _data.push_back(feature_vec);
+       _label_index.push_back(label_idx);
+       _fv_cnt_per_label[label_idx]++;
+
+       vector<float> oneHotEncoding;
+
+       for (size_t num = 0; num < label_cnt; ++num)
+               oneHotEncoding.push_back(label_idx == num ? 1.0f : 0.0f);
+
+       _labels.push_back(oneHotEncoding);
+       _feature_vector_size = feature_vec.size();
+       _label_count = label_cnt;
+}
+
+void FaceRecognitionDSM::LoadDataSet(const string file_name, unsigned int new_label_cnt)
+{
+       std::ifstream inFile(file_name);
+
+       if (!inFile.is_open())
+               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.
+       inFile.seekg(static_cast<int>(sizeof(FeaVecHeader)) * -1, ios::end);
+
+       FeaVecHeader fvh;
+
+       inFile.read((char *) &fvh, sizeof(FeaVecHeader));
+       if (inFile.gcount() != sizeof(FeaVecHeader))
+               throw InvalidOperation("Invalid feature vector file.");
+
+       inFile.seekg(0, ios::beg);
+
+       PrintHeader(fvh);
+
+       if (FeatureVectorManager::feature_vector_signature != fvh.signature)
+               throw InvalidOperation("Wrong feature vector header.");
+
+       /*
+       * stride line format is as follows
+       * ********************************
+       * ____________________________
+       * |feature vector|label index|
+       * ----------------------------
+       */
+       size_t line_size_in_bytes = fvh.feature_size * sizeof(float) + sizeof(unsigned int);
+
+       _feature_vector_size = fvh.feature_size;
+       _label_count = fvh.label_cnt;
+
+       vector<float> line_data(fvh.feature_size + 1);
+
+       for (size_t idx = 0; idx < fvh.data_set_cnt; ++idx) {
+               inFile.read(reinterpret_cast<char *>(line_data.data()), line_size_in_bytes);
+
+               vector<float> data;
+
+               copy_n(line_data.begin(), _feature_vector_size, back_inserter(data));
+               _data.push_back(data);
+
+               unsigned int label_idx;
+
+               memcpy(&label_idx, reinterpret_cast<void *>(line_data.data() + _feature_vector_size), sizeof(unsigned int));
+
+               vector<float> label;
+
+               // max label count may be changed so update one hot encoding table.
+               for (size_t num = 0; num < new_label_cnt; ++num)
+                       label.push_back(label_idx == num ? 1.0f : 0.0f);
+
+               _labels.push_back(label);
+               _label_index.push_back(label_idx);
+
+               _fv_cnt_per_label[label_idx]++;
+       }
+}
\ No newline at end of file
diff --git a/mv_machine_learning/face_recognition/src/face_recognition_fvm.cpp b/mv_machine_learning/face_recognition/src/face_recognition_fvm.cpp
new file mode 100644 (file)
index 0000000..7111cde
--- /dev/null
@@ -0,0 +1,59 @@
+/**
+ * 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.
+ */
+
+#include <fstream>
+
+#include "machine_learning_exception.h"
+#include "face_recognition_fvm.h"
+
+using namespace std;
+using namespace mediavision::machine_learning::exception;
+
+FaceRecognitionFVM::FaceRecognitionFVM(const string feature_vector_file) : FeatureVectorManager(feature_vector_file)
+{}
+
+void FaceRecognitionFVM::WriteHeader(size_t feature_size, size_t label_cnt, unsigned int data_set_cnt)
+{
+       ofstream outFile { _feature_vector_file, ios::out | ios::binary | ios::app };
+
+       if (!outFile.is_open())
+               throw InvalidOperation("fail to open a file");
+
+       FeaVecHeader fvHeader { FeatureVectorManager::feature_vector_signature, feature_size, label_cnt, data_set_cnt };
+
+       outFile.write((char *) &fvHeader, sizeof(FeaVecHeader));
+}
+
+void FaceRecognitionFVM::StoreData(vector<vector<float> > &features_vec, vector<unsigned int> &label_index)
+{
+       ofstream outFile { _feature_vector_file, ios::out | ios::binary | ios::app };
+
+       if (!outFile.is_open())
+               throw InvalidOperation("fail to open a file.");
+
+       for (size_t idx = 0; idx < features_vec.size(); ++idx) {
+               outFile.write(reinterpret_cast<char *>(features_vec[idx].data()), features_vec[idx].size() * sizeof(float));
+               outFile.write(reinterpret_cast<char *>(&label_index[idx]), sizeof(unsigned int));
+       }
+}
+
+void FaceRecognitionFVM::Remove()
+{
+       // Remove existing file forcely.
+       int ret = ::remove(_feature_vector_file.c_str());
+       if (ret)
+               throw InvalidOperation("Fail to remove feature vector file.");
+}
\ No newline at end of file
diff --git a/mv_machine_learning/face_recognition/src/nntrainer_dsm.cpp b/mv_machine_learning/face_recognition/src/nntrainer_dsm.cpp
deleted file mode 100644 (file)
index f25abd0..0000000
+++ /dev/null
@@ -1,122 +0,0 @@
-/**
- * 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.
- */
-
-#include <dlog.h>
-#include <mv_private.h>
-#include <algorithm>
-
-#include "machine_learning_exception.h"
-#include "nntrainer_dsm.h"
-#define MAX_FEATURE_VECTOR_CNT 5
-
-using namespace std;
-using namespace mediavision::machine_learning::exception;
-
-void NNTrainerDSM::PrintHeader(FeaVecHeader &fvh)
-{
-       LOGD("signature = %u", fvh.signature);
-       LOGD("feature vector size = %zu", fvh.feature_size);
-       LOGD("label count = %zu", fvh.label_cnt);
-       LOGD("data set count = %u", fvh.data_set_cnt);
-}
-
-NNTrainerDSM::NNTrainerDSM() : DataSetManager()
-{}
-
-bool NNTrainerDSM::IsFeatureVectorAllowed(unsigned int label_idx)
-{
-       return (_fv_cnt_per_label[label_idx] < MAX_FEATURE_VECTOR_CNT);
-}
-
-void NNTrainerDSM::AddDataSet(std::vector<float> &feature_vec, const unsigned int label_idx,
-                                                         const unsigned int label_cnt)
-{
-       _data.push_back(feature_vec);
-       _label_index.push_back(label_idx);
-       _fv_cnt_per_label[label_idx]++;
-
-       vector<float> oneHotEncoding;
-
-       for (size_t num = 0; num < label_cnt; ++num)
-               oneHotEncoding.push_back(label_idx == num ? 1.0f : 0.0f);
-
-       _labels.push_back(oneHotEncoding);
-       _feature_vector_size = feature_vec.size();
-       _label_count = label_cnt;
-}
-
-void NNTrainerDSM::LoadDataSet(const string file_name, unsigned int new_label_cnt)
-{
-       std::ifstream inFile(file_name);
-
-       if (!inFile.is_open())
-               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.
-       inFile.seekg(static_cast<int>(sizeof(FeaVecHeader)) * -1, ios::end);
-
-       FeaVecHeader fvh;
-
-       inFile.read((char *) &fvh, sizeof(FeaVecHeader));
-       if (inFile.gcount() != sizeof(FeaVecHeader))
-               throw InvalidOperation("Invalid feature vector file.");
-
-       inFile.seekg(0, ios::beg);
-
-       PrintHeader(fvh);
-
-       if (FeatureVectorManager::feature_vector_signature != fvh.signature)
-               throw InvalidOperation("Wrong feature vector header.");
-
-       /*
-       * stride line format is as follows
-       * ********************************
-       * ____________________________
-       * |feature vector|label index|
-       * ----------------------------
-       */
-       size_t line_size_in_bytes = fvh.feature_size * sizeof(float) + sizeof(unsigned int);
-
-       _feature_vector_size = fvh.feature_size;
-       _label_count = fvh.label_cnt;
-
-       vector<float> line_data(fvh.feature_size + 1);
-
-       for (size_t idx = 0; idx < fvh.data_set_cnt; ++idx) {
-               inFile.read(reinterpret_cast<char *>(line_data.data()), line_size_in_bytes);
-
-               vector<float> data;
-
-               copy_n(line_data.begin(), _feature_vector_size, back_inserter(data));
-               _data.push_back(data);
-
-               unsigned int label_idx;
-
-               memcpy(&label_idx, reinterpret_cast<void *>(line_data.data() + _feature_vector_size), sizeof(unsigned int));
-
-               vector<float> label;
-
-               // max label count may be changed so update one hot encoding table.
-               for (size_t num = 0; num < new_label_cnt; ++num)
-                       label.push_back(label_idx == num ? 1.0f : 0.0f);
-
-               _labels.push_back(label);
-               _label_index.push_back(label_idx);
-
-               _fv_cnt_per_label[label_idx]++;
-       }
-}
\ No newline at end of file
diff --git a/mv_machine_learning/face_recognition/src/nntrainer_fvm.cpp b/mv_machine_learning/face_recognition/src/nntrainer_fvm.cpp
deleted file mode 100644 (file)
index bc872be..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-/**
- * 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.
- */
-
-#include <fstream>
-
-#include "machine_learning_exception.h"
-#include "nntrainer_fvm.h"
-
-using namespace std;
-using namespace mediavision::machine_learning::exception;
-
-NNTrainerFVM::NNTrainerFVM(const string feature_vector_file) : FeatureVectorManager(feature_vector_file)
-{}
-
-void NNTrainerFVM::WriteHeader(size_t feature_size, size_t label_cnt, unsigned int data_set_cnt)
-{
-       ofstream outFile { _feature_vector_file, ios::out | ios::binary | ios::app };
-
-       if (!outFile.is_open())
-               throw InvalidOperation("fail to open a file");
-
-       FeaVecHeader fvHeader { FeatureVectorManager::feature_vector_signature, feature_size, label_cnt, data_set_cnt };
-
-       outFile.write((char *) &fvHeader, sizeof(FeaVecHeader));
-}
-
-void NNTrainerFVM::ReadHeader(FeaVecHeader &header)
-{
-       ifstream inFile { _feature_vector_file, ios::in | ios::binary };
-
-       if (!inFile.is_open())
-               throw InvalidOperation("fail to open a file.");
-
-       inFile.seekg(static_cast<int>(sizeof(FeaVecHeader) * -1), ios::end);
-
-       inFile.read((char *) &header, sizeof(FeaVecHeader));
-       if (inFile.gcount() != sizeof(FeaVecHeader))
-               throw InvalidOperation("Invalid feature vector file.");
-
-       if (header.signature != FeatureVectorManager::feature_vector_signature)
-               throw InvalidParameter("wrong feature vector file header.");
-}
-
-void NNTrainerFVM::StoreData(vector<vector<float> > &features_vec, vector<unsigned int> &label_index)
-{
-       ofstream outFile { _feature_vector_file, ios::out | ios::binary | ios::app };
-
-       if (!outFile.is_open())
-               throw InvalidOperation("fail to open a file.");
-
-       for (size_t idx = 0; idx < features_vec.size(); ++idx) {
-               outFile.write(reinterpret_cast<char *>(features_vec[idx].data()), features_vec[idx].size() * sizeof(float));
-               outFile.write(reinterpret_cast<char *>(&label_index[idx]), sizeof(unsigned int));
-       }
-}
-
-void NNTrainerFVM::Remove()
-{
-       // Remove existing file forcely.
-       int ret = ::remove(_feature_vector_file.c_str());
-       if (ret)
-               throw InvalidOperation("Fail to remove feature vector file.");
-}
\ No newline at end of file
index 8b787a2e094ed8c7dd1cd250bfe1b1e2e37dada2..2f49344aa741c8baf9ffd9ad2ee40e8a9b2b2715 100644 (file)
@@ -37,28 +37,21 @@ SimpleShot::SimpleShot(const training_backend_type_e backend_type, const trainin
                                           const string internal_model_file)
                : TrainingModel(backend_type, target_type, internal_model_file)
 {
-       _engine_info.backend_type = backend_type;
-       // TODO. training engine interface has no target type attribute yet.
-       //       Add target type to training_engine_config and consider setting this type
-       //       to a given training engine backend.
-       _engine_info.target_device = target_type;
-
-       const inference_engine_tensor_info nntrainer_input_tensor_info = {
+       const inference_engine_tensor_info input_tensor_info = {
                { 512, 1, 1, 1 }, INFERENCE_TENSOR_SHAPE_NCHW, INFERENCE_TENSOR_DATA_TYPE_FLOAT32, (size_t)(512 * 1 * 1 * 1)
        };
 
        _engine_info.input_layer_names.push_back("preprocess_l2norm0");
-       _engine_info.input_tensor_info.push_back(nntrainer_input_tensor_info);
+       _engine_info.input_tensor_info.push_back(input_tensor_info);
 
        // size of output tensor will be updated by RecognizeFace function
        // because the size should be changed according to maximum label count
        // so it has 1 in default.
-       inference_engine_tensor_info nntrainer_output_tensor_info = { vector<size_t> { 1, 1, 1, 1 },
-                                                                                                                                 INFERENCE_TENSOR_SHAPE_NCHW,
-                                                                                                                                 INFERENCE_TENSOR_DATA_TYPE_FLOAT32, 1 };
+       inference_engine_tensor_info output_tensor_info = { vector<size_t> { 1, 1, 1, 1 }, INFERENCE_TENSOR_SHAPE_NCHW,
+                                                                                                               INFERENCE_TENSOR_DATA_TYPE_FLOAT32, 1 };
 
        _engine_info.output_layer_names.push_back("centroid_knn1");
-       _engine_info.output_tensor_info.push_back(nntrainer_output_tensor_info);
+       _engine_info.output_tensor_info.push_back(output_tensor_info);
 
        _engine_info.optimizer_property = { .options = { "learning_rate=0.1" } };
        _engine_info.compile_property = { .options = { "batch_size=1" } };
index 45938067ee7d342a658d9a369e68b19fd37c3920..f63bade1952cfa2ac861cd3e2f1444cb753e6e95 100644 (file)
 
 #include "file_util.h"
 
-typedef struct {
+struct FeaVecHeader {
        unsigned int signature;
        size_t feature_size;
        size_t label_cnt;
        unsigned int data_set_cnt;
-} FeaVecHeader;
+};
 
 class FeatureVectorManager
 {
@@ -51,7 +51,6 @@ public:
                                                           unsigned int in_height, unsigned int re_width, unsigned int re_height);
 
        virtual void WriteHeader(size_t feature_size, size_t label_cnt, unsigned int data_set_cnt) = 0;
-       virtual void ReadHeader(FeaVecHeader &header) = 0;
        virtual void StoreData(std::vector<std::vector<float> > &features_vec, std::vector<unsigned int> &label_index) = 0;
        virtual void Remove() = 0;
 
index b2702631cb6dbb19274321b818b6cd88e838e86b..ba99ae1c5ad4edbe6d893d93c415e1de625616b5 100644 (file)
@@ -29,8 +29,6 @@
 #include "feature_vector_manager.h"
 
 typedef struct {
-       int backend_type;
-       unsigned int target_device;
        std::vector<std::string> input_layer_names;
        std::vector<std::string> output_layer_names;
        std::vector<inference_engine_tensor_info> input_tensor_info;