24 #ifndef SRC_importers_MNISTMATRIXIMPORTER_HPP_
25 #define SRC_importers_MNISTMATRIXIMPORTER_HPP_
34 template<
typename T=
float>
43 MNISTMatrixImporter(std::string node_name_ =
"mnist_matrix_importer", std::string data_filename_ =
"", std::string labels_filename_ =
"") :
44 Importer< mic::types::Matrix<T>, unsigned int > (node_name_),
86 int label_offset_bytes = 8;
87 int data_offset_bytes = 16;
91 LOG(LSTATUS) <<
"Opening file containing MNIST labels: " <<
labels_filename;
92 std::ifstream labels_file(
labels_filename, std::ios::in | std::ios::binary);
93 if (!labels_file.is_open()) {
99 LOG(LSTATUS) <<
"Opening file containing MNIST images: " <<
data_filename;
100 std::ifstream data_file(
data_filename, std::ios::in | std::ios::binary);
101 if (!data_file.is_open()) {
102 LOG(LFATAL) <<
"Oops! Couldn't find file: " <<
data_filename;
107 LOG(LSTATUS) <<
"Importing MNIST digits. This might take a while...";
110 labels_file.seekg (label_offset_bytes, std::ios::beg);
112 data_file.seekg (data_offset_bytes , std::ios::beg);
117 labels_file.read(buffer, 1);
119 if (labels_file.eof())
122 unsigned int temp_label = (
unsigned int)buffer[0];
135 for (
size_t i = 0; i < (size_t)(
image_width*image_height); i++) {
138 (*image_ptr)(row, col) = (T)((uint8_t)buffer[i])/(T)255.0f;
142 LOG(LDEBUG) <<
"Loading MNIST sample: " << sample;
145 sample_labels.push_back(std::make_shared <unsigned int> (temp_label) );
153 LOG(LINFO) <<
"Imported " <<
sample_labels.size() <<
" patches";
167 LOG(LINFO) <<
"Data import finished";
void setLabelsFilename(std::string labels_filename_)
virtual ~MNISTMatrixImporter()
mic::configuration::Property< std::string > data_filename
std::vector< size_t > sample_indices
Stores sample indices (sample "positions" in original dataset).
MNISTMatrixImporter(std::string node_name_="mnist_matrix_importer", std::string data_filename_="", std::string labels_filename_="")
Parent class for all data importers.
std::vector< std::shared_ptr< mic::types::Matrix< T > > > sample_data
Stores the data.
virtual void initializePropertyDependentVariables()
Contains declaration (and definition) of base template class of all data importers.
mic::configuration::Property< std::string > labels_filename
typename std::shared_ptr< mic::types::Matrix< T > > MatrixPtr
Typedef for a shared pointer to template-typed dynamic matrices.
void setDataFilename(std::string data_filename_)
std::vector< std::shared_ptr< unsigned int > > sample_labels
Stores labels.
mic::configuration::Property< int > samples_limit
Template-typed Matrix of dynamic size. Uses OpenBLAS if found by CMAKE - overloaded, specializations of * operator for types: float, double.