iRoCS Toolbox  1.1.0
Model_MC.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: base class for all multi class models
26 ** $RCSfile$
27 ** $Revision: 2824 $$Name$
28 ** $Date: 2009-09-14 09:30:46 +0200 (Mon, 14 Sep 2009) $
29 ** Copyright: GPL $Author: ronneber $
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/24 12:57:50 fehr
41 ** some more 64-bit netcdf bugfixing
42 **
43 ** Revision 1.1 2004/08/26 08:36:59 ronneber
44 ** initital import
45 **
46 **
47 **
48 **************************************************************************/
49 
50 #ifndef MODEL_MC_HH
51 #define MODEL_MC_HH
52 
53 #ifdef HAVE_CONFIG_H
54 #include <config.hh>
55 #endif
56 
57 #include "SVMError.hh"
58 
60 
61 namespace svt
62 {
63 
64 template< typename TCModel>
65 class Model_MC
66 {
68 
69 public:
70 
71  typedef typename TCModel::FV_type FV;
72 
73 
75  :_owningSupportVectors(false),
76  _maxUniqueID( 0)
77  {}
78 
80  {
82  }
83 
85  :_collectedSupportVectors( orig._collectedSupportVectors),
86  _classIndexToLabel( orig._classIndexToLabel),
87  _owningSupportVectors( false),
88  _maxUniqueID( orig._maxUniqueID)
89  {
90  }
91 
93  {
94  _collectedSupportVectors = orig._collectedSupportVectors;
95  _classIndexToLabel = orig._classIndexToLabel;
96  _owningSupportVectors = false;
97  _maxUniqueID = orig._maxUniqueID;
98  }
99 
100 
101 
102  void resizeCollectedSupportVectors( unsigned int size)
103  {
105  if( size != _collectedSupportVectors.size())
106  {
107  _collectedSupportVectors.resize( size);
108  }
109 
110  }
111 
112 
114  {
115  if( _owningSupportVectors)
116  {
117  for( unsigned int i = 0; i < _collectedSupportVectors.size(); ++i)
118  {
119  delete _collectedSupportVectors[i];
120  _collectedSupportVectors[i] = 0;
121  }
122  }
123  _owningSupportVectors = false;
124  }
125 
126 
127 
128  const std::vector<FV*>& collectedSupportVectors() const
129  {
130  return _collectedSupportVectors;
131  }
132 
133 
134 
135  unsigned int maxUniqueID() const
136  {
137  return _maxUniqueID;
138  }
139 
140  void setClassIndizesToLabels( const std::vector<double>& v)
141  {
142  _classIndexToLabel = v;
143  }
144 
145  double classIndexToLabel( unsigned int classIndex) const
146  {
147  SVM_ASSERT( classIndex < _classIndexToLabel.size());
148  return _classIndexToLabel[classIndex];
149  }
150 
151  /*======================================================================*/
163  /*======================================================================*/
165  const std::vector<char>& leaveOutFlagsByUID) const
166  {
167  for( unsigned int i = 0; i < _collectedSupportVectors.size(); ++i)
168  {
169  unsigned int uid = _collectedSupportVectors[i]->uniqueID();
170  SVM_ASSERT( uid < leaveOutFlagsByUID.size());
171 
172  if( leaveOutFlagsByUID[uid] == 1) return true;
173  }
174  return false;
175  }
176 
177 
178  std::vector<FV*> getSupportVectors() const
179  {
180  return _collectedSupportVectors;
181  }
182 
183 protected:
184  template< typename ForwardIter>
185  void collectSupportVectorsFromTCModels( const ForwardIter& modelsBegin,
186  const ForwardIter& modelsEnd);
187 
188 
189  template<typename STDATA>
190  void saveSupportVectorsAndClassLabels( STDATA& stData) const;
191 
192  template<typename STDATA>
193  void loadSupportVectorsAndClassLabels( STDATA& stData);
194 
195  template< typename ForwardIter, typename STDATA>
197  const ForwardIter& modelsBegin, const ForwardIter& modelsEnd,
198  STDATA& statistics);
199 
200 
201 
202 
203 private:
204  std::vector<FV*> _collectedSupportVectors;
205  std::vector<double> _classIndexToLabel;
206  bool _owningSupportVectors;
207  unsigned int _maxUniqueID;
208 
209 };
210 
211 
212 }
213 
214 #include "Model_MC.icc"
215 #endif
TCModel::FV_type FV
Definition: Model_MC.hh:71
unsigned int maxUniqueID() const
Definition: Model_MC.hh:135
void freeCollectedSupportVectors()
Definition: Model_MC.hh:113
const std::vector< FV * > & collectedSupportVectors() const
Definition: Model_MC.hh:128
#define SVM_ASSERT(condition)
Definition: SVMError.hh:176
Ensure that TCMODEL provides minimal functions to be used With Multiclass-Algortithms.
double classIndexToLabel(unsigned int classIndex) const
Definition: Model_MC.hh:145
void collectSupportVectorsFromTCModels(const ForwardIter &modelsBegin, const ForwardIter &modelsEnd)
#define CHECK_CLASS_TEMPLATE1(c)
void setClassIndizesToLabels(const std::vector< double > &v)
Definition: Model_MC.hh:140
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_MC.hh:164
std::vector< FV * > getSupportVectors() const
Definition: Model_MC.hh:178
void saveSupportVectorsAndClassLabels(STDATA &stData) const
Model_MC< TCModel > & operator=(const Model_MC< TCModel > &orig)
Definition: Model_MC.hh:92
Model_MC(const Model_MC< TCModel > &orig)
Definition: Model_MC.hh:84
void resizeCollectedSupportVectors(unsigned int size)
Definition: Model_MC.hh:102
void loadSupportVectorsAndClassLabels(STDATA &stData)
void calcTrainingStatisticsFromTCModels(const ForwardIter &modelsBegin, const ForwardIter &modelsEnd, STDATA &statistics)