tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
cifar10_parser.h
1/*
2 Copyright (c) 2013, Taiga Nomi
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27#pragma once
28#include "tiny_dnn/util/util.h"
29#include <fstream>
30#include <cstdint>
31#include <algorithm>
32
33#define CIFAR10_IMAGE_DEPTH (3)
34#define CIFAR10_IMAGE_WIDTH (32)
35#define CIFAR10_IMAGE_HEIGHT (32)
36#define CIFAR10_IMAGE_AREA (CIFAR10_IMAGE_WIDTH*CIFAR10_IMAGE_HEIGHT)
37#define CIFAR10_IMAGE_SIZE (CIFAR10_IMAGE_AREA*CIFAR10_IMAGE_DEPTH)
38
39
40namespace tiny_dnn {
41
53inline void parse_cifar10(const std::string& filename,
54 std::vector<vec_t> *train_images,
55 std::vector<label_t> *train_labels,
56 float_t scale_min,
57 float_t scale_max,
58 int x_padding,
59 int y_padding)
60{
61 if (x_padding < 0 || y_padding < 0)
62 throw nn_error("padding size must not be negative");
63 if (scale_min >= scale_max)
64 throw nn_error("scale_max must be greater than scale_min");
65
66 std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
67 if (ifs.fail() || ifs.bad())
68 throw nn_error("failed to open file:" + filename);
69
70 uint8_t label;
71 std::vector<unsigned char> buf(CIFAR10_IMAGE_SIZE);
72
73 while (ifs.read((char*) &label, 1)) {
74 vec_t img;
75
76 if (!ifs.read((char*) &buf[0], CIFAR10_IMAGE_SIZE)) break;
77
78 if (x_padding || y_padding)
79 {
80 int w = CIFAR10_IMAGE_WIDTH + 2 * x_padding;
81 int h = CIFAR10_IMAGE_HEIGHT + 2 * y_padding;
82
83 img.resize(w * h * CIFAR10_IMAGE_DEPTH, scale_min);
84
85 for (int c = 0; c < CIFAR10_IMAGE_DEPTH; c++) {
86 for (int y = 0; y < CIFAR10_IMAGE_HEIGHT; y++) {
87 for (int x = 0; x < CIFAR10_IMAGE_WIDTH; x++) {
88 img[c * w * h + (y + y_padding) * w + x + x_padding]
89 = scale_min + (scale_max - scale_min) * buf[c * CIFAR10_IMAGE_AREA + y * CIFAR10_IMAGE_WIDTH + x] / 255;
90 }
91 }
92 }
93 }
94 else
95 {
96 std::transform(buf.begin(), buf.end(), std::back_inserter(img),
97 [=](unsigned char c) { return scale_min + (scale_max - scale_min) * c / 255; });
98 }
99
100 train_images->push_back(img);
101 train_labels->push_back(label);
102 }
103}
104
105} // namespace tiny_dnn