Direct Graphical Models
v.1.7.0
|
Sparse Dictionary Learning class. More...
#include <SparseDictionary.h>
Public Member Functions | |
CSparseDictionary (void) | |
virtual | ~CSparseDictionary (void) |
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 \(D\). More... | |
void | save (const std::string &fileName) const |
Saves dictionary \(D\) into a binary file. More... | |
void | load (const std::string &fileName) |
Loads dictionary \(D\) from the file. More... | |
bool | empty (void) const |
Checks whether the dictionary has been trained or loaded. More... | |
Mat | getDictionary (void) const |
Returns dictionary \(D\). More... | |
int | getBlockSize (void) const |
Returns size of the block, i.e. \(\sqrt{sampleLen}\). More... | |
word | getNumWords (void) const |
Returns the number words in dictionary \(D\). More... | |
Static Public Member Functions | |
static Mat | getDictionary (const std::string &fileName) |
Returns dictionary \(D\) from file. More... | |
static Mat | img2data (const Mat &img, int blockSize, float varianceThreshold=0.0f) |
Converts image into data \(X\). More... | |
static Mat | data2img (const Mat &X, cv::Size imgSize) |
Converts data \(X\) into an image. More... | |
Protected Types | |
enum | grad_type { GRAD_D, GRAD_W } |
Static Protected Member Functions | |
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 \(W\). More... | |
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 \(D\). More... | |
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 \(\frac{\partial J(D, W)}{\partial D}\) and \(\frac{\partial J(D, W)}{\partial W}\). More... | |
static float | calculateCost (const Mat &X, const Mat &D, const Mat &W, float lambda, float epsilon, float gamma) |
Calculates the value of \(J(D, W)\) function. More... | |
Private Attributes | |
Mat | m_D |
The dictionary \(D\): Mat(size: nWords x sampleLen; type: CV_32FC1);. More... | |
Sparse Dictionary Learning class.
This class performs the Sparse dictionary learning, i.e. estimation of dictionary words (bases) \(\vec{d}_i\in D\), such that arbitrary data vector \(\vec{x}\) could be represended via a linear combination:
\[ \begin{align} \sum^{nWords}_{i=1} w_{i,j} \cdot \vec{d}_i = \vec{x}_j, & & \forall j \in [0; nSamples), & \text{ or} \\ \vec{w}_j \times D = \vec{x}_j, & & \forall j \in [0; nSamples), & \end{align} \]
where \(D\) is the dictionary.
The implementation is based on Sparse Coding: Autoencoder Interpretation article, where the task of the dictionary learning turns into the following minimization problem:
\[ \text{arg}\,\min\limits_{D,W} J(D, W) = \left\| W \times D - X \right\|^{2}_{2} + \lambda\left\|W\right\|_1 + \gamma\left\|D\right\|^{2}_{2}, \]
where \(D\in\mathbb{R}^{sampleLen \times nWords}\), \(W\in\mathbb{R}^{nWords \times nSamples}\) is the matrix, containing weighting coefficients for every word and every sample and \(X\in\mathbb{R}^{sampleLen \times nSamples}\) contains the training data as row-vectors samples.
In order to minimize \(J(D, W)\) we use the Gradient Descent algorithm.
We also use \(\sum_{i,j}\sqrt{w^{2}_{i,j} + \epsilon}\) in place of \(\left\|W\right\|_1\) to make \(J(D, W)\) differentiable at \(W = 0\).
In order to train the dictionary, one may use the code:
Definition at line 47 of file SparseDictionary.h.
|
protected |
Enumerator | |
---|---|
GRAD_D | |
GRAD_W |
Definition at line 194 of file SparseDictionary.h.
|
inline |
Definition at line 50 of file SparseDictionary.h.
|
inlinevirtual |
Definition at line 51 of file SparseDictionary.h.
|
staticprotected |
Evaluates dictionary \(D\).
Finds the \(D\), that minimizes \(J(D, W)\) for the given \(W\):
\[ \text{arg}\,\min\limits_{D} J(D, W) = \left\| W \times D - X \right\|^{2}_{2} + \gamma\left\|D\right\|^{2}_{2}, \]
[in] | X | Training data \(X\): Mat(size nSamples x sampleLen; type CV_32FC1) |
[in,out] | D | Dictionary \(D\): Mat(size nWords x sampleLen; type CV_32FC1) |
[in] | W | Weighting coefficients \(W\): Mat(size nSamples x nWords; type CV_32FC1) |
[in] | gamma | Regularisation parameter: \(\gamma\) |
[in] | nIt | Number of iterations |
[in] | lRate | Learning rate parameter, which is charged with the speed of convergence |
Definition at line 235 of file SparseDictionary.cpp.
|
staticprotected |
Evaluates weighting coefficients matrix \(W\).
Finds the \(W\), that minimizes \(J(D, W)\) for the given \(D\):
\[ \text{arg}\,\min\limits_{W} J(D, W) = \left\| W \times D - X \right\|^{2}_{2} + \lambda\sum_{i,j}{\sqrt{w^{2}_{i,j} + \epsilon}} \]
[in] | X | Training data \(X\): Mat(size nSamples x sampleLen; type CV_32FC1) |
[in] | D | Dictionary \(D\): Mat(size nWords x sampleLen; type CV_32FC1) |
[in,out] | W | Weighting coefficients \(W\): Mat(size nSamples x nWords; type CV_32FC1) |
[in] | lambda | Regularisation parameter \(\lambda\) |
[in] | epsilon | L1-regularisation parameter: \(\epsilon\) |
[in] | nIt | Number of iterations |
[in] | lRate | Learning rate parameter, which is charged with the speed of convergence |
Definition at line 220 of file SparseDictionary.cpp.
|
staticprotected |
Calculates the value of \(J(D, W)\) function.
X | Training data \(X\): Mat(size nSamples x sampleLen; type CV_32FC1) |
D | Dictionary \(D\): Mat(size nWords x sampleLen; type CV_32FC1) |
W | Weighting coefficients \(W\): Mat(size nSamples x nWords; type CV_32FC1) |
lambda | Regularisation parameter \(\lambda\) |
epsilon | L1-regularisation parameter: \(\epsilon\) |
gamma | Regularisation parameter: \(\gamma\) |
Definition at line 273 of file SparseDictionary.cpp.
|
staticprotected |
Calculates the gradient matrices \(\frac{\partial J(D, W)}{\partial D}\) and \(\frac{\partial J(D, W)}{\partial W}\).
This function calculates the gradient matrices:
\[\begin{align} \frac{\partial J(D, W)}{\partial D} &= \frac{2}{nSapmles} \cdot W^\top\times [W \times D - X] + 2\cdot\gamma\cdot D, &\text{ if gType = GRAD_D} \\ \frac{\partial J(D, W)}{\partial W} &= \frac{2}{nSapmles} \cdot [W \times D - X] \times D^\top + \lambda\frac{W}{\sqrt{W^2 + \epsilon}}, &\text{ if gType = GRAD_W} \end{align}\]
gType | Gradient type |
X | Training data \(X\): Mat(size nSamples x sampleLen; type CV_32FC1) |
D | Dictionary \(D\): Mat(size nWords x sampleLen; type CV_32FC1) |
W | Weighting coefficients \(W\): Mat(size nSamples x nWords; type CV_32FC1) |
lambda | Regularisation parameter \(\lambda\) |
epsilon | L1-regularisation parameter: \(\epsilon\) |
gamma | Regularisation parameter: \(\gamma\) |
Definition at line 249 of file SparseDictionary.cpp.
|
static |
Converts data \(X\) into an image.
This function performs reverse transformation of img2data() function, thus the code
gives res identical to img.
X | The input data \(X\) (depth: 8 or 16 bit) |
imgSize | The size of the image to return |
Definition at line 192 of file SparseDictionary.cpp.
|
inline |
Checks whether the dictionary has been trained or loaded.
true | if the dictionary has been trained or loaded |
false | otherwise |
Definition at line 82 of file SparseDictionary.h.
|
inline |
Returns size of the block, i.e. \(\sqrt{sampleLen}\).
Definition at line 103 of file SparseDictionary.h.
|
inline |
Returns dictionary \(D\).
Definition at line 87 of file SparseDictionary.h.
|
inlinestatic |
Returns dictionary \(D\) from file.
fileName | Full file name |
Definition at line 93 of file SparseDictionary.h.
|
inline |
Returns the number words in dictionary \(D\).
Definition at line 108 of file SparseDictionary.h.
|
static |
Converts image into data \(X\).
This functions generates a set of data samples (blockSize x blockSize patches) from a single image. The extracted pathces are overlapping, thus the maximal number of data samples is: nMaxSamples = (img.width - blockSize + 1) x (img.height - blockSize + 1)
It is recommended to suffle the samples with parallel::shuffleRows() function before training dictionary with train()
img | The input image (1 or 3 channels, 8 or 16 bit image) |
blockSize | Size of the quadratic patch
|
varianceThreshold | The extracted samples with variance greater or equal to varianceThreshold will be included to \(X\).
|
Definition at line 161 of file SparseDictionary.cpp.
void DirectGraphicalModels::fex::CSparseDictionary::load | ( | const std::string & | fileName | ) |
Loads dictionary \(D\) from the file.
fileName | Full file name |
Definition at line 87 of file SparseDictionary.cpp.
void DirectGraphicalModels::fex::CSparseDictionary::save | ( | const std::string & | fileName | ) | const |
Saves dictionary \(D\) into a binary file.
fileName | Full file name |
Definition at line 78 of file SparseDictionary.cpp.
void DirectGraphicalModels::fex::CSparseDictionary::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 \(D\).
This function creates and trains new dictionary \(D\) on data \(X\)
X | Training data \(X\): Mat(size nSamples x sampleLen; type CV_8UC1 or CV_16UC1)
|
nWords | Length of the dictionary (number of words) |
batch | The number of randomly chosen samples from X to be used in every distinct iteration of training
|
nIt | Number of iterations |
lRate | Learning rate parameter, which is charged with the speed of convergence |
fileName | Path and file name to store intermediate dictionaries \(D\) (every 5 iterations). If specified the resulting file name will be the follows: fileName<it/5>.dic |
Definition at line 9 of file SparseDictionary.cpp.
|
private |
The dictionary \(D\): Mat(size: nWords x sampleLen; type: CV_32FC1);.
Definition at line 190 of file SparseDictionary.h.