#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
{
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();
--- /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 __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
--- /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 __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
+++ /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 __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
+++ /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 __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
#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;
, _label_manager()
, _config()
, _result()
-{
- _data_augments.push_back(std::make_unique<DataAugmentDefault>());
- /* Add other data argument classes. */
-}
+{}
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.");
// 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());
_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();
_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.
_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.
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.
--- /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.
+ */
+
+#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
--- /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.
+ */
+
+#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
+++ /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.
- */
-
-#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
+++ /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.
- */
-
-#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
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" } };
#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
{
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;
#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;