Direct Graphical Models  v.1.7.0
SparseDictionary.h
1 // Sparse Dictionary class interface
2 // Written by Sergey G. Kosov in 2016 for Project X (based on Xingdi (Eric) Yuan implementation)
3 #pragma once
4 
5 #include "types.h"
6 
7 namespace DirectGraphicalModels { namespace fex
8 {
9  const float SC_LRATE_W = 5e-2f;
10  const float SC_LRATE_D = 1e-2f;
11 
12  const float SC_LAMBDA = 5e-5f;
13  const float SC_EPSILON = 1e-5f;
14  const float SC_GAMMA = 1e-2f;
15 
16 
17  // ================================ Sparse Dictionary Class ==============================
48  {
49  public:
50  CSparseDictionary(void) : m_D(Mat()) { }
51  virtual ~CSparseDictionary(void) {}
52 
66  DllExport void train(const Mat &X, word nWords, dword batch = 2000, unsigned int nIt = 1000, float lRate = SC_LRATE_D, const std::string &fileName = std::string());
71  DllExport void save(const std::string &fileName) const;
76  DllExport void load(const std::string &fileName);
82  DllExport bool empty(void) const { return m_D.empty(); }
87  DllExport Mat getDictionary(void) const { return m_D; }
93  DllExport static Mat getDictionary(const std::string &fileName)
94  {
96  me.load(fileName);
97  return me.getDictionary();
98  }
103  DllExport int getBlockSize(void) const { return empty() ? 0 : static_cast<int>(sqrt(m_D.cols)); }
108  DllExport word getNumWords(void) const { return empty() ? 0 : static_cast<word>(m_D.rows); }
109 
110 #ifdef DEBUG_MODE // --- Debugging ---
111 
130  DllExport Mat TEST_decode(const Mat &X, cv::Size imgSize) const;
131 #endif // --- --------- ---
132 
145  DllExport static Mat img2data(const Mat &img, int blockSize, float varianceThreshold = 0.0f);
158  DllExport static Mat data2img(const Mat &X, cv::Size imgSize);
159 
160 
161  protected:
174  DllExport static void calculate_W(const Mat &X, const Mat& D, Mat &W, float lambda, float epsilon, unsigned int nIt = 800, float lRate = SC_LRATE_W);
186  DllExport static void calculate_D(const Mat &X, Mat &D, const Mat &W, float gamma, unsigned int nIt = 800, float lRate = SC_LRATE_D);
187 
188 
189  private:
190  Mat m_D;
191 
192 
193  protected:
211  static Mat calculateGradient(grad_type gType, const Mat &X, const Mat &D, const Mat &W, float lambda, float epsilon, float gamma);
222  static float calculateCost(const Mat &X, const Mat &D, const Mat &W, float lambda, float epsilon, float gamma);
223  };
224 
225 } }
226 
static void calculate_W(const Mat &X, const Mat &D, Mat &W, float lambda, float epsilon, unsigned int nIt=800, float lRate=SC_LRATE_W)
Evaluates weighting coefficients matrix .
const float SC_LRATE_W
Learning rate (speed) for weights .
Sparse Dictionary Learning class.
const float SC_EPSILON
: L1-regularisation epsilon
static Mat calculateGradient(grad_type gType, const Mat &X, const Mat &D, const Mat &W, float lambda, float epsilon, float gamma)
Calculates the gradient matrices and .
static float calculateCost(const Mat &X, const Mat &D, const Mat &W, float lambda, float epsilon, float gamma)
Calculates the value of function.
static Mat data2img(const Mat &X, cv::Size imgSize)
Converts data into an image.
void save(const std::string &fileName) const
Saves dictionary into a binary file.
void load(const std::string &fileName)
Loads dictionary from the file.
static void calculate_D(const Mat &X, Mat &D, const Mat &W, float gamma, unsigned int nIt=800, float lRate=SC_LRATE_D)
Evaluates dictionary .
const float SC_GAMMA
: L2-regularisation parameter (on dictionary words)
word getNumWords(void) const
Returns the number words in dictionary .
static Mat img2data(const Mat &img, int blockSize, float varianceThreshold=0.0f)
Converts image into data .
Mat m_D
The dictionary : Mat(size: nWords x sampleLen; type: CV_32FC1);.
void train(const Mat &X, word nWords, dword batch=2000, unsigned int nIt=1000, float lRate=SC_LRATE_D, const std::string &fileName=std::string())
Trains dictionary .
const float SC_LAMBDA
: L1-regularisation parameter (on features)
bool empty(void) const
Checks whether the dictionary has been trained or loaded.
const float SC_LRATE_D
Learning rate (speed) for dictionary .
static Mat getDictionary(const std::string &fileName)
Returns dictionary from file.
Mat getDictionary(void) const
Returns dictionary .
int getBlockSize(void) const
Returns size of the block, i.e. .