pdfbthmt.hh
上传用户:l56789
上传日期:2022-02-25
资源大小:2422k
文件大小:5k
源码类别:

图形图像处理

开发平台:

Matlab

  1. //-----------------------------------------------------------------------------
  2. // pdfbthmt.hh  -  Tying Hidden Markov Tree Models For PDFB
  3. //-----------------------------------------------------------------------------
  4. // 
  5. // Original by F. Worm, Jan-1999
  6. //
  7. // Modified by M. Do, Feb-1999
  8. //
  9. // Modified by D. Po, June-2002
  10. //
  11. // Description: Tying of HMT models parameters within each scale
  12. //
  13. // example modelfile:
  14. //
  15. // nStates: 2 (number of states)
  16. // nLevels: 4 (number of tree levels)
  17. // zeroMean: no (yes if all states at all levels are zero mean, no otherwise)
  18. //         (empty line must be present)
  19. // 0.6 0.4 (probability of first level to be in each state)
  20. //         (empty line must be present)
  21. // 0.8 0.2 (state transition probability, row number is the parent state,) 
  22. // 0.2 0.8 (column number is the child state, first level to second)
  23. //
  24. // 0.7 0.3 (state transition prob, second level to third)
  25. // 0.3 0.7
  26. //
  27. // 0.7 0.3 (state transition prob, second level to third)
  28. // 0.3 0.7
  29. //
  30. // 0.7 0.3 (state transition prob, third to fourth)
  31. // 0.3 0.7
  32. //         (two empty lines must be present here to separate prob and mean/var)
  33. //
  34. // 0.0 0.0 (means, rows correspond to levels, columns correspond to states)
  35. // 0.0 0.0
  36. // 0.0 0.0 
  37. // 0.0 0.0
  38. //
  39. // 40.0 160.0 (variances, rows correspond to levels, columns correspond to states)
  40. // 20.0 80.0
  41. // 10.0 40.0
  42. // 05.0 20.0
  43. //-----------------------------------------------------------------------------
  44. #ifndef THMT_H
  45. #define THMT_H
  46. #include <vector>
  47. #include "matrix.hh"                         
  48. #include "tree.hh"
  49. #include "mex.h"
  50. // #define DEBUG
  51. using std::vector;
  52. const int    MAX_ITR = 1000; // max number of iterations
  53. class THMT {
  54.     public:
  55. THMT(int ns, int nl, int* levndir, bool zm = false); // random model
  56. THMT(const mxArray* initmodel, int* levndir); // model from file
  57.         THMT(char *filename, int* levndir, long& startpos);
  58. // Batch train HMT model using data from a tree or file
  59. void batch_train(tree<double> *trainTree, 
  60.  double min_delta = 0.001);
  61. // Batch test against HMT model using data from a tree or file
  62. double batch_test(char *filename);
  63. double batch_test(tree<double> *testTree);
  64. // Generate random data based on the model
  65. void generate_one(tree<double> &aTree, int &idum);
  66. void generate_one(tree<double> &aTree, int &idum, double
  67.         initval);
  68. // Generate n trees
  69. void generate_data(tree<double>& obs,int n);
  70. void generate_data(char *filename, int n); 
  71.         // Denoise an image
  72.         tree<double>* denoise(double nvar, tree<double>* source,
  73.      const mxArray* stateprob);
  74.   
  75. // Save the model to file
  76. void dump_model(char *filename);
  77.         // Save model into a struct
  78.         void dump_model_struct(const mxArray* model);
  79.         // Save State Prob for denoising
  80.         void dump_state_prob(const mxArray* stateprob);
  81. // Estimate Kullback-Leibler distance using Monte-Carlo method
  82. friend double KLD_est(THMT model1, THMT model2, int nObservations);
  83. // Compute an upper bound of the Kullback-Leibler distance
  84. friend double KLD_upb(THMT model1, THMT model2);
  85.     private:
  86. int M; // number of states
  87.         int nCh;                        // number of children
  88. int nLev; // number of levels
  89. int nObs; // number of observations
  90. bool zeromean;
  91. // model parameters (same for a certain scale - tying)
  92.         vector< vector< matrix<double> > > model_trans;
  93.         vector< vector< vector<double> > > model_mean;
  94.         vector< vector< vector<double> > > model_stdv;
  95. // training data
  96. tree<double> *obs; // pointer to the tree
  97. // variables for training & testing
  98. tree< vector<double> > alpha;
  99. tree< vector<double> > beta;
  100. tree< vector<double> > beta_par;
  101.         tree< vector<double> > state_prob;  //this is used for denoising
  102. vector< vector < matrix<double> > > sum_trans;
  103.         vector< vector< vector<double> > > sum_prob;
  104.         vector< vector< vector<double> > > sum_mean;
  105.         vector< vector< vector<double> > > sum_stdv;
  106. tree<double> scaling; // scaling factor at each node
  107.         tree<int> subbandtree;              // indicates which subbandtree 
  108.                                         // coefficient belongs to
  109. // E-step: upward-downward for a single tree
  110. void compute_beta(int ob_ind); // compute beta up-tree
  111. void compute_alpha(); // compute alpha down-tree
  112. // Rescale beta
  113. void rescale(int J); // rescale beta at the given level
  114. // Compute log-likehood (using beta and scaling)
  115. double compute_likelihood();
  116. // M-step for multiple tress.  Return total adj. & likelihood
  117. void update_model(double& delta, double &avf); 
  118.         // reorder the model so that the first state always has the largest
  119.         // standard deviation
  120.         void reorder_model();
  121. // Train HMT model from assigned data
  122. void train_all(double min_delta);
  123. // Compute average log-likelihood
  124. double test_all();
  125. // Random initializes model parameters
  126. void rnd_init_model();
  127.   
  128. // Allocate spaces
  129. void allocate_training();
  130. void allocate_testing();
  131. };
  132. #endif // THMT_H