mv_machine_learning: face recognition: allow 5 set per a label 04/286304/2
authorInki Dae <inki.dae@samsung.com>
Tue, 3 Jan 2023 09:43:01 +0000 (18:43 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 3 Jan 2023 10:16:40 +0000 (19:16 +0900)
[Issue type] New feature

Added a new feature that it allows maximum 5 set of feature vector
per a label. In case of few-shot leanring based on KNN algorthm,
Feature vector set more than 5 aren't needed.

This is a temporary solution so I will introduce a feature vector priority
based solution if NNTrainer is ready for this later.

Change-Id: I57d24f36101b354ea2e0fed8134f99a31e32307e
Signed-off-by: Inki Dae <inki.dae@samsung.com>
mv_machine_learning/face_recognition/include/nntrainer_dsm.h
mv_machine_learning/face_recognition/src/face_recognition.cpp
mv_machine_learning/face_recognition/src/nntrainer_dsm.cpp
mv_machine_learning/training/include/data_set_manager.h
mv_machine_learning/training/src/data_set_manager.cpp

index 20eac475751d0181090ef50c732c00188830e34d..f988ea023be3da8a08ba129fe4d77b3aef141f01 100644 (file)
@@ -31,6 +31,7 @@ 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;
index 5ec22451ccf4349e196bdba78b9cd309d836c957..54b3557216ed0a0f8311a325c6218dd779b3dc06 100644 (file)
@@ -254,8 +254,11 @@ int FaceRecognition::RegisterNewFace(std::vector<float> &input_vec, string label
                        data_set->LoadDataSet(_config.feature_vector_file_path, label_cnt);
                }
 
-               // Add new feature vectors.
-               data_set->AddDataSet(input_vec, label_idx, label_cnt);
+               // Add new feature vector only in case that feature vector count of given label_idx is less then 5.
+               // It means that only 5 set of feature vector per a label is valid.
+               // TODO. According to feature vector priority, new feature vector should be added.
+               if (data_set->IsFeatureVectorAllowed(label_idx))
+                       data_set->AddDataSet(input_vec, label_idx, label_cnt);
 
                _training_model->ApplyDataSet(data_set);
                _training_model->Compile();
index 4c7d484451f0d5452a8671b6aa7afb7dff5ac94d..f25abd011674db9d81dc3c3e3ae79ec1240277b5 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "machine_learning_exception.h"
 #include "nntrainer_dsm.h"
+#define MAX_FEATURE_VECTOR_CNT 5
 
 using namespace std;
 using namespace mediavision::machine_learning::exception;
@@ -35,11 +36,17 @@ void NNTrainerDSM::PrintHeader(FeaVecHeader &fvh)
 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;
 
@@ -109,5 +116,7 @@ void NNTrainerDSM::LoadDataSet(const string file_name, unsigned int new_label_cn
 
                _labels.push_back(label);
                _label_index.push_back(label_idx);
+
+               _fv_cnt_per_label[label_idx]++;
        }
 }
\ No newline at end of file
index 65e4728fb14f22f59fd9dd69735a044284b9b5f2..4d770ba022aba2a4e3ba35fae642273d03fe720f 100644 (file)
 
 #include <fstream>
 #include <vector>
+#include <map>
 
 #include "feature_vector_manager.h"
 
 class DataSetManager
 {
 protected:
+       std::map<unsigned int, unsigned int> _fv_cnt_per_label;
        std::vector<std::vector<float> > _data;
        std::vector<std::vector<float> > _labels;
        std::vector<unsigned int> _label_index;
@@ -42,6 +44,7 @@ public:
        size_t GetFeaVecSize(void);
        std::vector<unsigned int> &GetLabelIdx(void);
 
+       virtual bool IsFeatureVectorAllowed(unsigned int label_idx) = 0;
        virtual void LoadDataSet(const std::string file_name, unsigned int new_label_cnt) = 0;
        virtual void AddDataSet(std::vector<float> &feature_vec, const unsigned int label_idx,
                                                        const unsigned int label_cnt) = 0;
index 6756ddf10a154c9f29b1337bd1ef1787d864d897..079eb0d0eecd4cb63dcea551bafdb6147e7ae114 100644 (file)
@@ -38,6 +38,7 @@ void DataSetManager::Clear()
 
        _labels.clear();
        _label_index.clear();
+       _fv_cnt_per_label.clear();
 }
 
 bool DataSetManager::IsFeatureVectorDuplicated(const vector<float> &vec)