MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
STL10MatrixImporter.cpp
Go to the documentation of this file.
1 
25 
26 #include <fstream>
27 
28 namespace mic {
29 namespace importers {
30 
31 STL10MatrixImporter::STL10MatrixImporter(std::string node_name_, std::string data_filename_, std::string labels_filename_) : Importer (node_name_),
32  data_filename("data_filename",data_filename_),
33  labels_filename("labels_filename", labels_filename_),
34  samples_limit("samples_limit",-1)
35 {
36  // Register properties - so their values can be overridden (read from the configuration file).
37  registerProperty(data_filename);
38  registerProperty(labels_filename);
39  registerProperty(samples_limit);
40 
41  // Set image properties.
42  image_width = 96;
43  image_height = 96;
44 }
45 
46 void STL10MatrixImporter::setDataFilename(std::string data_filename_) {
47  data_filename = data_filename_;
48 }
49 
50 void STL10MatrixImporter::setLabelsFilename(std::string labels_filename_) {
51  labels_filename = labels_filename_;
52 }
53 
54 
56 
57  char buffer[96*96*3];
58  size_t sample = 0;
59 
60  // RGB to grayascale conversion coefficents, using CCIR 601 norm
61  float rgb_grayscale_coeffs[] = {0.299, 0.587, 0.114};
62 
63  // set true if providing labels
64  labeled = labels_filename != "";
65 
66  std::ifstream labels_file(labels_filename, std::ios::in | std::ios::binary);
67  if(labeled) {
68  // Try to open file with labels.
69  LOG(LSTATUS) << "Opening file containing STL-10 labels: " << labels_filename;
70  if (!labels_file.is_open()) {
71  LOG(LFATAL) << "Oops! Couldn't find file: " << labels_filename;
72  return false;
73  }//: else
74  }
75 
76  // Read file containing images (binary format).
77  LOG(LSTATUS) << "Opening file containing STL-10 images: " << data_filename;
78  std::ifstream data_file(data_filename, std::ios::in | std::ios::binary);
79  if (!data_file.is_open()) {
80  LOG(LFATAL) << "Oops! Couldn't find file: " << data_filename;
81  return false;
82  }
83 
84  // Label and image files ok - import digits.
85  LOG(LSTATUS) << "Importing STL-10 images. This might take a while...";
86 
87 
88  // Import loop.
89  while(true) {
90 
91  unsigned int temp_label;
92  if(labeled) {
93  // Try to read the label.
94  labels_file.read(buffer, 1);
95  // If reached the EOF.
96  if (labels_file.eof())
97  break;
98  // Else: get the label.
99  temp_label = (unsigned int)buffer[0];
100  }
101  else {
102  temp_label = 0;
103  }
104 
105  // Try to read the image into buffer.
106  data_file.read(buffer, image_width*image_height*3);
107  // If reached the EOF.
108  if (data_file.eof())
109  break;
110  // Else: get image.
111 
112  // Create new matrix of STL-10 image size.
113  mic::types::MatrixXfPtr image_ptr (new mic::types::MatrixXf(image_height, image_width));
114  image_ptr->zeros();
115 
116  // Parse and set image data.
117  for (size_t j = 0 ; j < 3 ; j++) {
118  for (size_t i = 0 ; i < (size_t)(image_width*image_height) ; i++) {
119  unsigned col = i / image_width;
120  unsigned row = i % image_height;
121  //ch(row, col) = (uint8_t)buffer[i];
122  (*image_ptr)(row, col) += rgb_grayscale_coeffs[j] * (float)((uint8_t)buffer[i + ((image_height*image_width)*j)])/255.0f;
123  }//: for
124  }
125 
126  // Got the image and label.
127  LOG(LDEBUG) << "Loading STL-10 sample: " << sample;
128 
129  sample_data.push_back(image_ptr);
130  sample_labels.push_back(std::make_shared <unsigned int> (temp_label) );
131 
132  sample++;
133  // Check limit.
134  if ((samples_limit > 0) && (sample >= (size_t)samples_limit))
135  break;
136  }//: while !eof
137 
138  LOG(LINFO) << "Imported " << sample_labels.size() << " patches";
139 
140  // Close files
141  labels_file.close();
142  data_file.close();
143 
144  // Fill the indices table(!)
145  for (size_t i=0; i < sample_data.size(); i++ )
146  sample_indices.push_back(i);
147 
148  // Count the classes.
149  //countClasses();
150  number_of_classes = 10;
151 
152  LOG(LINFO) << "Data import finished";
153  return true;
154 }
155 
156 
157 
158 
159 } /* namespace importers */
160 } /* namespace mic */
std::shared_ptr< mic::types::MatrixXf > MatrixXfPtr
Shared pointer to matrix with single precision floats (of dynamic size).
std::vector< size_t > sample_indices
Stores sample indices (sample "positions" in original dataset).
Definition: Batch.hpp:460
mic::configuration::Property< std::string > data_filename
void setDataFilename(std::string data_filename_)
Parent class for all data importers.
Definition: Importer.hpp:51
std::vector< std::shared_ptr< mic::types::MatrixXf > > sample_data
Stores the data.
Definition: Batch.hpp:454
void setLabelsFilename(std::string labels_filename_)
STL10MatrixImporter(std::string node_name_="stl10_matrix_importer", std::string data_filename_="", std::string labels_filename_="")
mic::configuration::Property< int > samples_limit
mic::configuration::Property< std::string > labels_filename
std::vector< std::shared_ptr< unsigned int > > sample_labels
Stores labels.
Definition: Batch.hpp:457
Template-typed Matrix of dynamic size. Uses OpenBLAS if found by CMAKE - overloaded, specializations of * operator for types: float, double.
Definition: Matrix.hpp:64