| 13 | namespace impl |
| 14 | { |
| 15 | void load_cifar_10_batch ( |
| 16 | const std::string& folder_name, |
| 17 | const std::string& batch_name, |
| 18 | const size_t first_idx, |
| 19 | const size_t images_per_batch, |
| 20 | std::vector<matrix<rgb_pixel>>& images, |
| 21 | std::vector<unsigned long>& labels |
| 22 | ) |
| 23 | { |
| 24 | std::ifstream fin(folder_name + "/" + batch_name, std::ios::binary); |
| 25 | if (!fin) throw error("Unable to open file " + batch_name); |
| 26 | const long nr = 32; |
| 27 | const long nc = 32; |
| 28 | const long plane_size = nr * nc; |
| 29 | const long image_size = 3 * plane_size; |
| 30 | |
| 31 | for (size_t i = 0; i < images_per_batch; ++i) |
| 32 | { |
| 33 | char l; |
| 34 | fin.read(&l, 1); |
| 35 | labels[first_idx + i] = l; |
| 36 | images[first_idx + i].set_size(nr, nc); |
| 37 | |
| 38 | std::array<unsigned char, image_size> buffer; |
| 39 | fin.read((char*)(buffer.data()), buffer.size()); |
| 40 | for (long k = 0; k < plane_size; ++k) |
| 41 | { |
| 42 | char r = buffer[0 * plane_size + k]; |
| 43 | char g = buffer[1 * plane_size + k]; |
| 44 | char b = buffer[2 * plane_size + k]; |
| 45 | const long row = k / nr; |
| 46 | const long col = k % nr; |
| 47 | images[first_idx + i](row, col) = rgb_pixel(r, g, b); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (!fin) throw error("Unable to read file " + batch_name); |
| 52 | |
| 53 | if (fin.get() != EOF) throw error("Unexpected bytes at end of " + batch_name); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void load_cifar_10_dataset ( |