25 #ifndef BMPIMPORTER_HPP_
26 #define BMPIMPORTER_HPP_
40 template <
typename eT>
48 BMPImporter(std::string node_name_ =
"bmp_importer", std::string data_filename_ =
"")
78 std::vector<std::string> names_array;
79 std::size_t pos = 0, found;
80 while((found = std::string(
data_filename).find_first_of(
';', pos)) != std::string::npos) {
82 names_array.push_back(std::string(
data_filename).substr(pos, found - pos));
87 names_array.push_back(std::string(
data_filename).substr(pos));
90 for (
size_t fi = 0; fi < names_array.size(); ++fi) {
93 LOG(LSTATUS) <<
"Opening file: " << names_array[fi];
94 std::ifstream file(names_array[fi], std::ios::in | std::ios::binary);
95 if (!file.is_open()) {
96 LOG(LFATAL) <<
"Oops! Couldn't find file: " << names_array[fi];
102 LOG(LDEBUG) <<
"Loading sample: " << names_array[fi];
113 LOG(LINFO) <<
"Imported " <<
sample_data.size() <<
" samples";
118 sample_labels.push_back(std::make_shared <unsigned int> (i) );
125 LOG(LINFO) <<
"Data import finished";
132 static constexpr
size_t HEADER_SIZE = 54;
135 std::array<char, HEADER_SIZE> header;
136 file_.read(header.data(), header.size());
141 uint32_t bfSize = *
reinterpret_cast<uint32_t *
>(&header[2]);
142 uint32_t bfOffBits = *
reinterpret_cast<uint32_t *
>(&header[10]);
144 uint32_t biWidth = *
reinterpret_cast<uint32_t *
>(&header[18]);
145 uint32_t biHeight = *
reinterpret_cast<uint32_t *
>(&header[22]);
147 uint16_t biBitCount = *
reinterpret_cast<uint16_t *
>(&header[28]);
149 LOG(LDEBUG) <<
"fileSize: " << bfSize;
150 LOG(LDEBUG) <<
"dataOffset: " << bfOffBits;
151 LOG(LDEBUG) <<
"width: " << biWidth;
152 LOG(LDEBUG) <<
"height: " << biHeight;
153 LOG(LDEBUG) <<
"depth: " << biBitCount <<
"-bit";
157 size_t img_channels = biBitCount/8;
158 size_t roff, goff, boff;
159 if (biBitCount == 24) {
163 }
else if (biBitCount == 32) {
168 LOG(LERROR) <<
"Unhandel number of bits per pixel: " << biBitCount <<
"-bit";
172 std::vector<char> rest(bfOffBits - HEADER_SIZE);
173 file_.read(rest.data(), rest.size());
176 std::vector<char> img(bfSize - bfOffBits);
177 file_.read(img.data(), img.size());
181 size_t padWidth= (size_t)(img_channels*biWidth);
184 while(padWidth%4!=0) {
187 LOG(LDEBUG) <<
"padWidth: " << padWidth;
193 float* data_ptr = ptr->data();
196 for (
size_t h =0; h < biHeight; h++){
198 for (
size_t w =0; w < padWidth; w+=img_channels) {
200 if (w >= img_channels*biWidth) {
203 size_t i = h*padWidth + w;
210 data_ptr[(biHeight-1 - h)*biWidth + w/img_channels] = (
int(img[i + roff] & 0xff)) / (255.0);
213 data_ptr[(biHeight-1 - h)*biWidth + w/img_channels + 1*biWidth*biHeight] = (
int(img[i + goff] & 0xff)) / (255.0);
216 data_ptr[(biHeight-1 - h)*biWidth + w/img_channels + 2*biWidth*biHeight] = (
int(img[i + boff] & 0xff)) / (255.0);
mic::configuration::Property< std::string > data_filename
mic::types::TensorPtr< float > loadBMP(std::ifstream &file_)
std::vector< size_t > sample_indices
Stores sample indices (sample "positions" in original dataset).
Parent class for all data importers.
std::vector< std::shared_ptr< mic::types::Tensor< eT > > > sample_data
Stores the data.
#define MAKE_TENSOR_PTR(eT,...)
Macro for initialization of tensor pointer.
Contains declaration (and definition) of base template class of all data importers.
void setDataFilename(std::string data_filename_)
typename std::shared_ptr< mic::types::Tensor< eT > > TensorPtr
Typedef for a shared pointer to template-typed dynamic matrices.
std::vector< std::shared_ptr< unsigned int > > sample_labels
Stores labels.
virtual void initializePropertyDependentVariables()
Class responsible for importing CIFAR images. Returns a batch of Tensors. Assumes that every image ca...
BMPImporter(std::string node_name_="bmp_importer", std::string data_filename_="")
Contains declaration of tensor types.