X-Boost  2.3.8
ImageClassifierOperator.h
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _IMAGE_CLASSIFIER_OPERATOR_H
22 #define _IMAGE_CLASSIFIER_OPERATOR_H
23 
24 #include <vector>
26 
28 
29  ImageClassifier *c;
30  // TODO: devlop directly the ROC
31  std::vector<int> cat;
32  std::vector<float> resp;
33 public:
35  {
36  }
38  {
39  }
40 
41  bool operator()(const std::string & filename, const ImageHandle & img, int category)
42  {
43  c->setImage(img);
44  float v = (*c)(0,0);
45  c->Release();
46 
47  cat.push_back(category);
48  resp.push_back(v);
49 
50  return true;
51  }
52 
53 };
54 
57  int TP, TN, FP, FN;
58  double mpos, mneg;
59  double pavg, navg;
60  double pavg2, navg2;
61  ImageClassifier *c;
62 
63  int verbose;
64  double threshold;
65 
66  // TODO: create ROC/PRC
67 // std::map<float, int> m_pos, m_neg;
68 
69 public:
71  {
72  pavg = navg = 0.0;
73  pavg2 = navg2 = 0.0;
74  mpos = 100000000.0;
75  mneg = -100000000.0;
76 
77  TP=TN=FP=FN = 0;
78 
79  verbose = 0;
80  threshold = 0.0;
81  }
82 
83  void setVerbosity(int l) {
84  verbose = l;
85  }
86  void setThreshold(double th) {
87  threshold = th;
88  }
89 
90  bool operator()(const std::string & filename, const ImageHandle & img, int category)
91  {
92 // int w = img.width;
93 // int h = img.height;
94  c->setImage(img);
95  double v = (*c)(0,0);
96  c->Release();
97 
98  if(category == 1)
99  {
100 // m_pos[v]++;
101  pavg += v;
102  pavg2 += v*v;
103  if(verbose==1)
104  {
105  std::cout << v << ' ';
106  if(v<threshold)
107  std::cout << "FN";
108  else
109  std::cout << "TP";
110  std::cout << std::endl;
111  }
112  else if(verbose==2)
113  {
114  std::cout << filename << ' ' << v << ' ';
115  if(v<threshold)
116  std::cout << "FN";
117  else
118  std::cout << "TP";
119  std::cout << std::endl;
120  }
121  else if(verbose==3)
122  {
123  std::cout << v << ' ' << filename << ' ';
124  if(v<threshold)
125  std::cout << "FN";
126  else
127  std::cout << "TP";
128  std::cout << std::endl;
129  }
130 
131  if(v<mpos) mpos = v;
132 
133  if(v<threshold)
134  FN++;
135  else
136  TP++;
137  }
138  if(category == -1)
139  {
140 // m_neg[v]++;
141  navg += v;
142  navg2 += v*v;
143 
144  if(verbose==1)
145  {
146  std::cout << v << ' ';
147  if(v>=threshold)
148  std::cout << "FP";
149  else
150  std::cout << "TN";
151  std::cout << std::endl;
152  }
153  else if(verbose==2)
154  {
155  std::cout << filename << ' ' << v << ' ';
156  if(v>=threshold)
157  std::cout << "FP";
158  else
159  std::cout << "TN";
160  std::cout << std::endl;
161  }
162  else if(verbose==3)
163  {
164  std::cout << v << ' ' << filename << ' ';
165  if(v>=threshold)
166  std::cout << "FP";
167  else
168  std::cout << "TN";
169  std::cout << std::endl;
170  }
171 
172  if(v>mneg) mneg = v;
173 
174  if(v>=threshold)
175  FP++;
176  else
177  TN++;
178 
179  }
180 
181  return true;
182  }
183 
184 // void PrintCurve()
185 // {
186 // float min_th = std::min( m_pos.begin()->first, m_neg.begin()->first);
187 // float max_th = std::min( m_pos.rbegin()->first, m_neg.rbegin()->first);
188 //
189 // float accum[50];
190 // for(int i = 0;i<50;++i) accum[i] = 0.0f;
191 // }
192 
193  // TODO: set a ostream
194  void Done()
195  {
196  pavg /= (double) (FN+TP);
197  navg /= (double) (FP+TN);
198 
199  std::cout << "Margin: " << mpos - mneg << "(" << mneg << ";" << mpos << ")\n";
200  std::cout << "Average: " << pavg << "(+) " << navg << "(-)\n";
201  std::cout << "Sqm: " << std::sqrt((pavg2/(double) (FN+TP)) - pavg*pavg) << "(+) " << std::sqrt((navg2/(double) (FP+TN)) - navg*navg) << "(-)\n";
202  std::cout << "Statistics with threshold = " << threshold << '\n';
203  std::cout << "TP:" << TP << ", TN:" << TN << ", FN:" << FN <<", FP:" << FP << '\n';
204  std::cout << "Correct Detection: " << TP+TN << " (" << (100*(TP+TN))/(TP+TN+FN+FP) <<"%), Errors: " << FN+FP << '\n';
205  std::cout << "Accuracy: " << (float)(TP+TN)/(float)(TP+TN+FN+FP)
206  << " Error Rate: " << (float)(FN+FP)/(float)(TP+TN+FN+FP)
207  << " Precision: " << (float)(TP)/(float)(FP+TP)
208  << " Recall: " << (float)(TP)/(float)(FN+TP) << std::endl;
209 
210  }
211 
212 };
213 
214 #endif
"Binary" Classifier, virtual class
Definition: ImageClassifier.h:37
Definition: Image.h:35
Definition: ImageClassifierOperator.h:27
an operator used to collect pattern from the ProcessSet operator
Definition: ImageClassifierOperator.h:56
virtual void Release()=0
virtual classes to work on classifier. ImageClassifier is the simplest one, computing the response im...
virtual void setImage(const unsigned char *ptr, long stride, unsigned int width, unsigned int height)=0