X-Boost  2.3.8
PrecomputedPatternResponseLoad.h
Go to the documentation of this file.
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 _PRECOMPUTED_PATTERN_RESPONSE_LOAD_H
22 #define _PRECOMPUTED_PATTERN_RESPONSE_LOAD_H
23 
25 #include <fstream>
26 #include <sstream>
27 #include <map>
28 
37 template<class ScalarType>
38 void LoadData(PrecomputedPatternResponse<ScalarType> & out, const char *filename)
39 {
40  std::ifstream in(filename);
41 
42  int n_samples, n_features;
43  in >> n_samples >> n_features;
44 
45  out.allocate(n_samples, n_features);
46 
47  for(int i =0; i<n_samples; ++i)
48  {
49  in >> out.category[i];
50  for(int j=0; j<n_features; ++j)
51  in >> out.response[i + j * n_samples];
52  }
53 }
54 
56 template<class ScalarType>
57 void LoadSVMData(PrecomputedPatternResponse<ScalarType> & out, const char *filename)
58 {
59  std::ifstream in(filename);
60 
61  std::vector< std::map<int, ScalarType> > m;
62  std::vector< int> cat;
63  int n_features = 0;
64 
65  std::string data;
66 
67  while(std::getline(in, data))
68  {
69  std::istringstream src(data);
70  int c;
71  src >> c;
72  cat.push_back(c);
73 
74  std::map<int, ScalarType> f;
75 
76  while(src)
77  {
78  int index;
79  char sep;
80  ScalarType value;
81  src>>index >> sep >> value;
82 
83  f[index] = value;
84  if(index >= n_features)
85  n_features = index+1;
86  }
87 
88  m.push_back(f);
89  }
90 
91  int n_samples = m.size();
92  out.allocate(n_samples, n_features);
93 
94  for(int i =0; i<n_samples; ++i)
95  {
96  out.category[i] = cat[i];
97  for(int j=0; j<n_features; ++j)
98  {
99  out.response[i + j * n_samples] = m[i][j];
100  }
101  }
102 
103 }
104 
105 #endif
int * category
category list
Definition: PrecomputedPatternResponse.h:48
void LoadSVMData(PrecomputedPatternResponse< ScalarType > &out, const char *filename)
Load SVM Light file.
Definition: PrecomputedPatternResponseLoad.h:57
this header contains PrecomputedPatternResponse class, used to store hash information provided by a m...
Definition: PrecomputedPatternResponse.h:41
ResponseType * response
response matrix. For performance reason it each row is a feature and columns are samples ...
Definition: PrecomputedPatternResponse.h:52
void LoadData(PrecomputedPatternResponse< ScalarType > &out, const char *filename)
Definition: PrecomputedPatternResponseLoad.h:38
void allocate(int samples, int feats)
allocate the response matrix
Definition: PrecomputedPatternResponse.h:117