Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnPixelSegModel.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnPixelSegModel.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <cstdlib>
16 #include <cassert>
17 #include <iostream>
18 #include <fstream>
19 #include <iomanip>
20 #include <vector>
21 
22 #include "Eigen/Core"
23 
24 #include "cv.h"
25 #include "highgui.h"
26 
27 #include "drwnBase.h"
28 #include "drwnIO.h"
29 #include "drwnML.h"
30 #include "drwnPGM.h"
31 #include "drwnVision.h"
32 
33 using namespace std;
34 
35 // drwnPixelSegModel class ------------------------------------------------
47 
49  protected:
52 
54  vector<double> _classTrainingWeights;
55 
59  vector<drwnBoostedClassifier *> _pixelClassModels;
60 
65  //drwnTMultiClassLogistic<drwnSquareJointFeatureMap> _pixelUnaryModel;
66  //drwnTMultiClassLogistic<drwnQuadraticJointFeatureMap> _pixelUnaryModel;
67 
70 
77 
80 
81  public:
86  virtual ~drwnPixelSegModel();
87 
88  // i/o
89  const char *type() const { return "pixelSegModel"; }
91  virtual void clear();
92  virtual bool save(drwnXMLNode& xml) const;
93  virtual bool load(drwnXMLNode& xml);
94 
96  int numLabels() const { return _pixelUnaryModel.numClasses(); }
97 
98  // feature generation
100  void setFeatureGenerator(const drwnSegImagePixelFeatures& featureGenerator);
103  return *_featureGenerator;
104  }
105 
106  // learning
108  void learnTrainingClassWeights(const vector<string>& baseNames);
110  void learnBoostedPixelModels(const vector<string>& baseNames, int subSample = 0);
112  void learnPixelUnaryModel(const vector<string>& baseNames, int subSample = 0);
114  void learnPixelContrastWeight(const vector<string>& baseNames);
116  void learnPixelContrastWeight(double weight);
118  void learnLongRangePairwiseWeight(const vector<string>& baseNames, double threshold, unsigned radius = 4);
120  void learnLongRangePairwiseWeight(double weight, double threshold, unsigned radius = 4);
122  void learnRobustPottsWeight(const vector<string>& baseNames);
124  void learnRobustPottsWeight(double weight);
126  void learnPixelContrastAndRobustPottsWeights(const vector<string>& baseNames);
128  void learnPixelContrastAndLongRangeWeights(const vector<string>& baseNames, double threshold, unsigned radius = 4);
129 
131  double getPairwiseContrastWeight() const { return _pixelContrastWeight; }
133  double getLongRangePairwiseWeight() const { return _longRangeEdgeWeight; }
135  double getRobustPottsWeight() const { return _robustPottsWeight; }
136 
137  // inference
139  void cacheUnaryPotentials(drwnSegImageInstance *instance) const;
141  void cacheLongRangeEdges(drwnSegImageInstance *instance) const;
144  double inferPixelLabels(drwnSegImageInstance *instance) const;
147  virtual double inferPixelLabels(const vector<string>& baseNames,
148  vector<MatrixXi>& predictedLabels) const;
150  virtual double energy(drwnSegImageInstance *instance) const;
151 
152  protected:
154  void computeBoostedResponses(const vector<double>& x, vector<double>& y) const;
156  void cacheBoostedPixelResponses(drwnSegImageInstance &instance) const;
157 
159  void buildSampledTrainingSet(const vector<string>& baseNames, const char *labelExt,
160  int nLabels, vector<vector<double> >& featureVectors, vector<int>& featureLabels,
161  int subSample, bool bRawFeatures) const;
162 
164  void crossValidateWeights(const vector<string>& baseNames,
165  const vector<double>& pairwiseContrastValues,
166  const vector<double>& robustPottsValues, const vector<double>& longRangeValues);
167 };
168 
double _pixelContrastWeight
weight for pairwise constrast-dependent smoothness term
Definition: drwnPixelSegModel.h:69
Encapsulates a single instance of an image for multi-class pixel labeling problems (i...
Definition: drwnSegImageInstance.h:47
double getPairwiseContrastWeight() const
get the weight of the pairwise contrast term
Definition: drwnPixelSegModel.h:131
double _longRangeEdgeThreshold
percentage of long range edges to include
Definition: drwnPixelSegModel.h:76
drwnTMultiClassLogistic< drwnBiasJointFeatureMap > _pixelUnaryModel
pixelwise unary model
Definition: drwnPixelSegModel.h:64
interface for objects that can serialize and de-serialize themselves
Definition: drwnInterfaces.h:48
Whitens (zero mean, unit variance) feature vector (see also drwnPCA).
Definition: drwnFeatureWhitener.h:55
double _robustPottsWeight
weight for robust potts consistency term
Definition: drwnPixelSegModel.h:79
vector< double > _classTrainingWeights
class training weights (normalized to sum to one)
Definition: drwnPixelSegModel.h:54
Implements a pixel-level CRF model for multi-class image segmentation (pixel labeling).
Definition: drwnPixelSegModel.h:48
Interface for generating per-pixel features for a drwnSegImageInstance object.
Definition: drwnSegImagePixelFeatures.h:49
double getRobustPottsWeight() const
get the weight of the robust potts term
Definition: drwnPixelSegModel.h:135
int numLabels() const
return the number of labels that this model has been trained to recognize
Definition: drwnPixelSegModel.h:96
double getLongRangePairwiseWeight() const
get the weight of the long range pairwise term
Definition: drwnPixelSegModel.h:133
double _longRangeEdgeWeight
weight for (long range) auxiliary edges
Definition: drwnPixelSegModel.h:72
const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnPixelSegModel.h:89
vector< drwnBoostedClassifier * > _pixelClassModels
boosted pixel classifiers for generating pixel features (if boosted pixel classifiers are not learned...
Definition: drwnPixelSegModel.h:59
drwnFeatureWhitener _pixelFeatureWhitener
feature whitener for unary model
Definition: drwnPixelSegModel.h:62
drwnSegImagePixelFeatures * _featureGenerator
pixel feature generator
Definition: drwnPixelSegModel.h:51
const drwnSegImagePixelFeatures & getFeatureGenerator() const
return the object used to generate pixel features from the image instance
Definition: drwnPixelSegModel.h:102
unsigned _longRangeMatchRadius
radius for long range edge match
Definition: drwnPixelSegModel.h:74