mv_machine_learning: code cleanup to label manager
authorInki Dae <inki.dae@samsung.com>
Tue, 4 Jul 2023 06:04:47 +0000 (15:04 +0900)
committerKwanghoon Son <k.son@samsung.com>
Wed, 12 Jul 2023 09:45:35 +0000 (18:45 +0900)
[Issue type] : code cleanup

Clean up the label manager of face recognition task group by doing,
  - drop redundant code.
  - change _labels_and_files to _labels. Aand we don't need to use
    map data structure so use just vector type instead.
  - change return type of importLabel function - int to size_t.
  - iterate _labels vector instead of the label file.

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

index 15598cb479b21bf13b590e7934e6fedbd374ce3e..92a4850a20500422180643b9469643877d0ba531 100644 (file)
@@ -96,7 +96,7 @@ private:
 
 public:
        FaceRecognition();
-       ~FaceRecognition();
+       ~FaceRecognition() = default;
 
        int initialize();
        void setConfig(FaceRecognitionConfig &config);
index 8193533958c6cfd7df3d2b99e4347b9e7f7b243c..2019e4bc23f3692b9a892af152c776697e91a001 100644 (file)
@@ -54,12 +54,6 @@ FaceRecognition::FaceRecognition()
                , _result()
 {}
 
-FaceRecognition::~FaceRecognition()
-{
-       if (_label_manager)
-               _label_manager->clear();
-}
-
 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
@@ -165,9 +159,8 @@ int FaceRecognition::registerNewFace(std::vector<float> &input_vec, string label
                // Import label data from a label file.
                importLabel();
 
-               // 1. Store only label names to label file, which aren't duplicated.
-               bool duplicated = _label_manager->addLabelToMap(label_name, label_name);
-               if (!duplicated) {
+               // 1. Store a new label name to label file if the given label doesn't exist.
+               if (!_label_manager->isExist(label_name)) {
                        int ret = _label_manager->addLabelToFile(label_name);
                        if (ret == 0)
                                return MEDIA_VISION_ERROR_INVALID_OPERATION;
@@ -351,7 +344,7 @@ int FaceRecognition::deleteLabel(string label_name)
                // Import label data from a label file.
                importLabel();
 
-               if (_label_manager->isExist(label_name) == false) {
+               if (!_label_manager->isExist(label_name)) {
                        LOGE("%s doesn't exist in label file.", label_name.c_str());
                        return MEDIA_VISION_ERROR_INVALID_OPERATION;
                }
index c7cbeccc9dd884f446173ee1ba426c72a8d59359..7fcf7b44b6d0a6722b3a62c84fda263b0af93955 100644 (file)
@@ -30,7 +30,7 @@
 class LabelManager
 {
 private:
-       std::map<std::string, std::string> _labels_and_files;
+       std::vector<std::string> _labels;
        std::string _label_file;
        float _decision_threshold;
        static constexpr float _decision_weight = 0.01;
@@ -38,19 +38,16 @@ private:
 public:
        LabelManager(std::string label_file, double decision_threshold);
        ~LabelManager();
-       void clear();
        float getDecisionThreshold();
        float getDecisionWeight();
-       unsigned int getLabelIndex(const std::string given_label);
+       unsigned int getLabelIndex(const std::string &given_label);
        bool isExist(const std::string given_label);
        unsigned int removeLabel(const std::string given_label);
-       int getLabelString(std::string &label, const int idx);
+       void getLabelString(std::string &label, const int idx);
        unsigned int addLabelToFile(std::string given_label);
-       int importLabel(void);
-       bool addLabelToMap(const std::string given_label, const std::string image_file);
-       size_t getMaxLabel(const std::string label_file);
+       size_t importLabel(void);
+       std::vector<std::string> &getLabels(void);
        size_t getMaxLabel();
-       std::string getLabelFromAnswer(const std::vector<float> &result);
        void removeFile();
 };
 
index bb222202d35cd8007f70d9c5919aa67165d1657a..7a685af664d37514ade64feb92d5ca974ae2f33c 100644 (file)
@@ -23,7 +23,7 @@
 using namespace std;
 using namespace mediavision::machine_learning::exception;
 
-LabelManager::LabelManager(string label_file, double decision_threshold) : _labels_and_files(), _label_file(label_file)
+LabelManager::LabelManager(string label_file, double decision_threshold) : _labels(), _label_file(label_file)
 {
        _decision_threshold = decision_threshold;
 
@@ -34,11 +34,6 @@ LabelManager::LabelManager(string label_file, double decision_threshold) : _labe
 LabelManager::~LabelManager()
 {}
 
-void LabelManager::clear()
-{
-       _labels_and_files.clear();
-}
-
 float LabelManager::getDecisionThreshold()
 {
        return _decision_threshold;
@@ -49,57 +44,15 @@ float LabelManager::getDecisionWeight()
        return _decision_weight;
 }
 
-unsigned int LabelManager::getLabelIndex(const string given_label)
+unsigned int LabelManager::getLabelIndex(const string &given_label)
 {
-       ifstream readFile;
-
-       readFile.open(_label_file.c_str());
+       importLabel();
 
-       int label_index = -1;
-
-       if (readFile.fail())
-               throw InvalidOperation("Fail to open " + _label_file + " file.");
-
-       string line;
+       auto iter = find(_labels.begin(), _labels.end(), given_label);
+       if (iter == _labels.end())
+               throw InvalidOperation("Label index not found.");
 
-       while (getline(readFile, line)) {
-               label_index++;
-
-               if (line.compare(given_label) == 0) {
-                       readFile.close();
-
-                       return label_index;
-               }
-       }
-
-       readFile.close();
-       throw InvalidOperation("Label index not found.");
-}
-
-bool LabelManager::isExist(const string given_label)
-{
-       ifstream readFile;
-
-       readFile.open(_label_file.c_str());
-
-       int label_index = -1;
-
-       if (readFile.fail())
-               throw InvalidOperation("Fail to open " + _label_file + " file.");
-
-       string line;
-
-       while (getline(readFile, line)) {
-               label_index++;
-
-               if (line.compare(given_label) == 0) {
-                       readFile.close();
-                       return true;
-               }
-       }
-
-       readFile.close();
-       return false;
+       return distance(_labels.begin(), iter);
 }
 
 unsigned int LabelManager::removeLabel(const string given_label)
@@ -152,33 +105,14 @@ unsigned int LabelManager::removeLabel(const string given_label)
        return label_index;
 }
 
-int LabelManager::getLabelString(string &label, const int idx)
+void LabelManager::getLabelString(string &label, const int idx)
 {
-       ifstream readFile;
-
-       readFile.open(_label_file.c_str());
+       importLabel();
 
-       int label_index = -1;
-       int ret = 0;
+       if (idx < 0 || _labels.size() <= idx)
+               throw InvalidOperation("A given label index is invalid.");
 
-       if (readFile.fail())
-               throw InvalidOperation("Fail to open " + _label_file + " file.");
-
-       string line;
-
-       while (getline(readFile, line)) {
-               label_index++;
-               line += "\n";
-
-               if (idx == label_index) {
-                       label = line;
-                       label.erase(remove(label.begin(), label.end(), '\n'), label.end());
-                       break;
-               }
-       }
-
-       readFile.close();
-       return ret;
+       label = _labels[idx];
 }
 
 unsigned int LabelManager::addLabelToFile(string given_label)
@@ -194,10 +128,10 @@ unsigned int LabelManager::addLabelToFile(string given_label)
        writeFile.write(given_label.c_str(), static_cast<streamsize>(given_label.size()));
        writeFile.close();
 
-       return getMaxLabel(_label_file);
+       return getMaxLabel();
 }
 
-int LabelManager::importLabel(void)
+size_t LabelManager::importLabel(void)
 {
        // label count is 0 if lael file doesn't exist.
        if (!FaceRecogUtil::isFileExist(_label_file))
@@ -207,86 +141,36 @@ int LabelManager::importLabel(void)
 
        readFile.open(_label_file);
 
-       int label_cnt = 0;
-
        if (readFile.fail())
                throw InvalidOperation("Fail to open " + _label_file + " file.");
 
        string line;
 
+       _labels.clear();
+
        while (getline(readFile, line)) {
-               bool duplicated = addLabelToMap(line, line);
-               if (duplicated)
+               // If a given label alreadys in the label file then just skip.
+               if (isExist(line))
                        continue;
 
-               label_cnt++;
+               _labels.push_back(line);
        }
 
        readFile.close();
 
-       return label_cnt;
+       return _labels.size();
 }
 
-bool LabelManager::addLabelToMap(const string given_label, const string image_file)
+bool LabelManager::isExist(const string given_label)
 {
-       // Find same one if not empty. If same one exists in the map then skip.
-       if (!_labels_and_files.empty()) {
-               auto item = _labels_and_files.find(given_label);
-               if (item != _labels_and_files.end())
-                       return true;
-       }
-
-       _labels_and_files.insert(pair<string, string>(given_label, image_file));
-
-       return false;
-}
-
-size_t LabelManager::getMaxLabel(const string label_file)
-{
-       // label count is 0 if lael file doesn't exist.
-       if (!FaceRecogUtil::isFileExist(label_file))
-               return 0;
-
-       ifstream readFile;
-
-       readFile.open(label_file.c_str());
-
-       size_t label_cnt = 0;
-
-       if (readFile.fail())
-               throw InvalidOperation("Fail to open " + label_file + " file.");
-
-       string line;
-
-       while (getline(readFile, line))
-               label_cnt++;
-
-       readFile.close();
-
-       return label_cnt;
+       return (find(_labels.begin(), _labels.end(), given_label) != _labels.end());
 }
 
 size_t LabelManager::getMaxLabel()
 {
-       return getMaxLabel(_label_file);
-}
-
-string LabelManager::getLabelFromAnswer(const vector<float> &result)
-{
-       if (result.empty())
-               throw InvalidParameter("result vector is empty.");
-
-       int answer_idx = max_element(result.begin(), result.end()) - result.begin();
-       if (result[answer_idx] < _decision_threshold)
-               throw InvalidOperation("Not recognized.");
-
-       string answer_label;
-
-       int ret = getLabelString(answer_label, answer_idx);
-       if (ret)
-               throw InvalidOperation("answer label not found.");
+       importLabel();
 
-       return answer_label;
+       return _labels.size();
 }
 
 void LabelManager::removeFile()