iRoCS Toolbox  1.1.0
Model.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: Model
26 ** $RCSfile$
27 ** $Revision: 5097 $$Name$
28 ** $Date: 2013-12-05 16:20:39 +0100 (Thu, 05 Dec 2013) $
29 ** Copyright: LGPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.3 2005/02/24 13:45:49 fehr
38 ** enable SV access through SVMAdapter
39 **
40 ** Revision 1.2 2005/02/01 12:34:45 ronneber
41 ** - fixed comment
42 **
43 ** Revision 1.1 2004/08/26 08:36:59 ronneber
44 ** initital import
45 **
46 ** Revision 1.9 2003/10/01 09:25:59 ronneber
47 ** - cosmetic changes
48 **
49 ** Revision 1.8 2003/05/19 10:57:23 ronneber
50 ** - now stores SolutionInfo in Model to have later access to several
51 ** training parameters
52 **
53 ** Revision 1.7 2002/10/28 16:00:52 mechnich
54 ** fixed compatibility problems with gcc3
55 **
56 ** Revision 1.6 2002/06/07 11:37:46 ronneber
57 ** - added detachFromTrainingDataSet()
58 **
59 ** Revision 1.5 2002/06/07 07:34:19 ronneber
60 ** - added public typedef for Support Vector List type, to make
61 ** classifier more flexible (e.g. when using a slightly different but
62 ** compatible Feature Vector class)
63 **
64 ** Revision 1.4 2002/05/21 18:31:11 ronneber
65 ** - SupportVector.hh must not be included in angle brackets!
66 **
67 ** Revision 1.3 2002/05/13 16:20:40 ronneber
68 ** - moved SupportVector class to SupportVector.hh
69 ** - made all attributes private and added accessing methods
70 ** - removed coutPlus and countMinus -- they could easily be calculated
71 ** later by the library-user
72 **
73 ** Revision 1.2 2002/05/06 12:22:52 ronneber
74 ** - removed Parameter struct
75 **
76 ** Revision 1.1 2002/03/26 12:44:02 ronneber
77 ** restructured for autoconf
78 **
79 ** Revision 1.4 2002/03/08 11:42:38 pigorsch
80 ** * introduced new Model and SupportVector classes
81 **
82 ** Revision 1.3 2002/01/21 13:47:15 pigorsch
83 ** * child class Parameters of class Model is now separate class
84 ** * removed parameter "params" from constructor of CachedKernel
85 **
86 ** Revision 1.2 2001/12/17 13:18:30 pigorsch
87 ** *** empty log message ***
88 **
89 ** Revision 1.1 2001/12/11 11:03:00 pigorsch
90 ** Initial Revision
91 **
92 **
93 **
94 **************************************************************************/
95 
96 #ifndef MODEL_HH
97 #define MODEL_HH
98 
99 #ifdef HAVE_CONFIG_H
100 #include <config.hh>
101 #endif
102 
103 #include <vector>
104 #include "StDataASCII.hh"
105 
106 // requirements of template parameters
110 
111 // Disable Warnings about usage of unsafe functions
112 #ifdef _WIN32
113 #pragma warning(disable : 4996)
114 #endif
115 
116 namespace svt
117 {
118  template<typename FV>
119  class Model
120  {
121 
122  public:
123 
124  typedef FV FV_type;
125 
127  :_rho(0),
128  _supportVectors(0),
129  _alphas(0),
130  _size(0),
131  _owningSupportVectors(false)
132  {}
133 
134 
135  Model( int nSupportVectors)
136  :_rho(0),
137  _supportVectors(0),
138  _alphas(0),
139  _size(0),
140  _owningSupportVectors(false)
141  {
142  resize( nSupportVectors);
143  }
144 
146  {
147  free_memory();
148  }
149 
150  /*======================================================================*/
156  /*======================================================================*/
157  Model( const Model<FV>& orig)
158  :_rho( 0),
159  _supportVectors( 0),
160  _alphas(0),
161  _size(0),
162  _owningSupportVectors(false)
163  {
164  *this = orig;
165  }
166 
167  /*======================================================================*/
173  /*======================================================================*/
174  Model& operator=( const Model& orig)
175  {
176  _rho = orig._rho;
177  resize( orig._size);
178  _owningSupportVectors = false;
179  std::copy( orig._supportVectors, orig._supportVectors+orig._size,
180  _supportVectors);
181  std::copy( orig._alphas, orig._alphas + orig._size,
182  _alphas);
183  _trainingInfo = orig._trainingInfo;
184  return *this;
185  }
186 
187 
188 
189 
190  void free_memory();
191 
192 
193  void resize( size_t nSupportVectors);
194 
195 
196 
197  size_t size() const
198  { return _size; }
199 
200  void setRho( double rho)
201  { _rho = rho; }
202 
203  double rho() const
204  { return _rho; }
205 
206  void setSupportVector( unsigned int i, FV* fv, double alpha)
207  {
208  _supportVectors[i] = fv;
209  _alphas[i] = alpha;
210  }
211 
212 
213  const FV* supportVector( unsigned int i) const
214  { return _supportVectors[i]; }
215 
216  FV* supportVector( unsigned int i)
217  { return _supportVectors[i]; }
218 
219  double alpha( unsigned int i) const
220  { return _alphas[i]; }
221 
222 
223 
225  { return _supportVectors; }
226 
227  FV* const * allSupportVectors() const
228  { return _supportVectors; }
229 
230 
231 
232  double* allAlphas()
233  { return _alphas; }
234 
235  const double* allAlphas() const
236  { return _alphas; }
237 
238 
240 
242  {
243  if( _owningSupportVectors == true) return;
244 
245  for( unsigned int i = 0; i < _size; ++i)
246  {
247  _supportVectors[i] = new FV( *_supportVectors[i]);
248  }
249  _owningSupportVectors = true;
250 
251  }
252 
253  template<typename T>
254  void setTrainingInfoValue( const std::string& key, const T& value )
255  {
256  _trainingInfo.setValue( key, value);
257  }
258 
259  double getTrainingInfoValue( const std::string& key)
260  {
261  return _trainingInfo.asDouble( key);
262  }
263 
264  std::string trainingInfoPlainText()
265  {
266  std::ostringstream oss;
267  for( std::map<std::string, std::string>::const_iterator
268  p = _trainingInfo.begin(); p != _trainingInfo.end(); ++p)
269  {
270  oss << p->first << ": " << p->second << std::endl;
271  }
272  return oss.str();
273  }
274 
275  template<typename STDATA>
276  void saveTrainingInfo( STDATA& stData,
277  const std::string& prefix = "")
278  {
280  for( std::map<std::string, std::string>::const_iterator
281  p = _trainingInfo.begin(); p != _trainingInfo.end(); ++p)
282  {
283  double val = 0;
284  _trainingInfo.getValue( p->first, val);
285  stData.setValue( prefix + p->first, val);
286  }
287  }
288 
289 
290  /*======================================================================*/
301  /*======================================================================*/
302  template<typename STDATA>
303  void saveParameters( STDATA& stData,
304  const std::string& prefix = "") const;
305 
306 
307  /*======================================================================*/
321  /*======================================================================*/
322  template<typename STDATA>
323  void loadParameters( STDATA& stData,
324  const std::string& prefix = "");
325 
326 
327  /*======================================================================*/
338  /*======================================================================*/
339  template<typename STDATA>
340  void saveParametersWithUIDs( STDATA& stData,
341  const std::string& prefix = "") const;
342 
343 
344  /*======================================================================*/
354  /*======================================================================*/
355  template<typename STDATA>
356  void loadParametersWithUIDs( STDATA& stData,
357  const std::vector<FV*>& fvsByUID,
358  const std::string& prefix = "");
359 
360 
361  /*======================================================================*/
373  /*======================================================================*/
375  const std::vector<char>& leaveOutFlagsByUID) const
376  {
378 
379  for( unsigned int i = 0; i < _size; ++i)
380  {
381  unsigned int uid = _supportVectors[i]->uniqueID();
382  SVM_ASSERT( uid < leaveOutFlagsByUID.size());
383 
384  if( leaveOutFlagsByUID[uid] == 1) return true;
385  }
386  return false;
387  }
388 
389 
390  private:
391  double _rho;
392  FV** _supportVectors; // array of Feature Vector pointers
393  double* _alphas;
394  size_t _size;
395  bool _owningSupportVectors;
396 
397  StDataASCII _trainingInfo;
398 
399 
400  };
401 
402 }
403 
404 #include "Model.icc"
405 
406 #endif
std::map< std::string, std::string >::const_iterator begin() const
const access to internal map
Definition: StDataASCII.hh:392
~Model()
Definition: Model.hh:145
#define CHECK_MEMBER_TEMPLATE(c)
double alpha(unsigned int i) const
Definition: Model.hh:219
Model & operator=(const Model &orig)
operator=
Definition: Model.hh:174
std::string trainingInfoPlainText()
Definition: Model.hh:264
#define SVM_ASSERT(condition)
Definition: SVMError.hh:176
void saveParametersWithUIDs(STDATA &stData, const std::string &prefix="") const
save the model data to string-string map.
void detachFromTrainingDataSet()
Definition: Model.hh:241
FV * supportVector(unsigned int i)
Definition: Model.hh:216
void saveTrainingInfo(STDATA &stData, const std::string &prefix="")
Definition: Model.hh:276
bool isModelAffectedByLeftOutVectors(const std::vector< char > &leaveOutFlagsByUID) const
check, if this model is affected by specified left out vectors, which means, that it needs to be retr...
Definition: Model.hh:374
Model(int nSupportVectors)
Definition: Model.hh:135
FV ** allSupportVectors()
Definition: Model.hh:224
void setSupportVector(unsigned int i, FV *fv, double alpha)
Definition: Model.hh:206
FV *const * allSupportVectors() const
Definition: Model.hh:227
void saveParameters(STDATA &stData, const std::string &prefix="") const
save the model data to given structured data.
void copySVCoefsToSupportVectors()
Model(const Model< FV > &orig)
Copy contructor.
Definition: Model.hh:157
std::map< std::string, std::string >::const_iterator end() const
Definition: StDataASCII.hh:397
double rho() const
Definition: Model.hh:203
void getValue(const std::string &key, T &value) const
get a value (with arbitrary type) from the internal map<string,string> specified by the given key...
void setValue(const std::string &key, const T &value)
setValue writes/adds a value (with arbitrary type) to the internal map<string,string>.
double getTrainingInfoValue(const std::string &key)
Definition: Model.hh:259
void loadParameters(STDATA &stData, const std::string &prefix="")
load the model data from given structured data.
void setTrainingInfoValue(const std::string &key, const T &value)
Definition: Model.hh:254
double asDouble(const std::string &key) const
Definition: StDataASCII.hh:331
double * allAlphas()
Definition: Model.hh:232
const FV * supportVector(unsigned int i) const
Definition: Model.hh:213
void setRho(double rho)
Definition: Model.hh:200
void free_memory()
void resize(size_t nSupportVectors)
void loadParametersWithUIDs(STDATA &stData, const std::vector< FV *> &fvsByUID, const std::string &prefix="")
(description)
size_t size() const
Definition: Model.hh:197
FV FV_type
Definition: Model.hh:124
The StDataASCII class is a container for "structured data", that is kept completly in memory...
Definition: StDataASCII.hh:83
const double * allAlphas() const
Definition: Model.hh:235