/*! * \file FeatureEvaluator.h * \date 2019/11/12 * * \author Lin, Chi * Contact: lin.chi@hzleaper.com * * * \note */ #ifndef __FeatureEvaluator_h_ #define __FeatureEvaluator_h_ #include "CyclopsCommon.h" #include "CyclopsModules.h" #include "CyclopsEnums.h" #include "CyclopsFeature.h" #include "StdUtils.h" #include "CVUtils.h" #include "SampleDBManager.h" /*! \brief Feature evaluator, for feature extraction and selection * Should be used together with LLClassifier */ class FeatureEvaluator : public ICyclopsModuleInstance { public: FeatureEvaluator() : mDirtyBit(false) {} virtual ~FeatureEvaluator() {} //! Smart pointer to hold an instance of FeatureEvaluator typedef std::shared_ptr Ptr; DECL_GET_INSTANCE(FeatureEvaluator::Ptr) /*! \fn serializeToMemory * Serialize information of the feature evaluator into a in-memory string, see also deserializeFromMemory() * @param str used to take the output serialization result * @return true for succeed, false for fail * \fn serializeToFile * Serialize information of the feature evaluator into a text file, see also deserializeFromFile() * @param filename file name (full path) where we will write the data * @return true for succeed, false for fail * \fn deserializeFromMemory * Deserialize the feature evaluator from in-memory string, see also serializeToMemory() * @param str in-memory string * @return true for succeed, false for fail * \fn deserializeFromFile * Deserialize the feature evaluator from a text file, see also serializeToFile() * @param filename file name (full path) where we will read the data * @return true for succeed, false for fail */ DECL_SERIALIZE_FUNCS /*! Add a new feature of given type */ virtual CyclopsFeature::Ptr add(FeatureType ftype); /*! Total count of features defined in this evaluator */ int size() const { return mFeatures.size(); } /*! Get feature by index */ CyclopsFeature::Ptr get(int index) { if (index < 0 || index >= size()) return nullptr; return mFeatures[index]; } /*! Remove feature by index */ virtual bool remove(int index); /*! Remove all features */ virtual bool removeAll(); /*! Query feature index by its name */ virtual int indexByName(const std::string& name); /*! Total count of all enabled features */ virtual int enabledSize() const; /*! Enable or disable all features */ virtual void enableAll(bool val); /*! Check whether we need to fix some features of bad parameter configuration * @param sampleSize uniformed sample size * @param doFix true if we should also do the real fix * @return true if fix is needed */ virtual bool needFix(const Size& sampleSize, bool doFix = false); /*! Check whether we need to re-train some features if parameter changed * @param sampleSize uniformed sample size * @return true if re-train is needed */ virtual bool needTrain(const Size& sampleSize); /*! Train features with provided samples */ virtual bool train(std::vector& samples, const Size& sampleSize); /** @overload Only train feature with given index */ virtual bool train(int index, std::vector& samples, const Size& sampleSize); /*! Evaluate current parameter configuration with provided samples * @param index feature index * @param samples samples used for training * @param sampleSize uniformed sample size * @param score output evaluation score, higher is better * @param time output computation time of the feature * @return true if everything is fine */ virtual bool evaluate(int index, std::vector& samples, const Size& sampleSize, double& score, double& time); /*! Evaluate whether it is hard to distinguish the provided sample collections */ virtual bool confuse(std::vector& samples, const Size& sampleSize, Mat& confuseMat); /*! Total length of feature vector contains all */ virtual int featureVecSize(const Size& sampleSize) const; /*! Get range of features in feature vector */ virtual vector featureVecRanges(const Size& sampleSize) const; /*! Compute feature vector for given image */ virtual void compute(const SampleInstPtr& sample, Mat& result); /*! True if any feature's any parameter is changed */ virtual bool isDirty() const; /*! Clear trained cache of all features */ virtual void cleanAll(); private: virtual bool serialize(FileStorage& fs); virtual bool deserialize(const FileNode& fs); void silhouetteAnalysis(int classCount, int totalSampleCount, const std::vector& featureMats, const Mat& labelMat, const Mat& weightMat, vector& shMeans); private: std::vector mFeatures; bool mDirtyBit; }; #endif // FeatureEvaluator_h_