2 * mlp-mnist is an implementation of a multi-layer neural network.
4 * Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
5 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
7 * This file is part of mlp-mnist.
9 * mlp-mnist is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 3 as
11 * published by the Free Software Foundation.
13 * mlp-mnist is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with mlp-mnist. If not, see <http://www.gnu.org/licenses/>.
26 PixelMaps::PixelMaps(int size) : _nb_ref(0),_core(new unsigned char[size]) {}
27 PixelMaps::~PixelMaps() { delete[] _core; }
28 PixelMaps *PixelMaps::add_ref() { _nb_ref++; return this; }
29 void PixelMaps::del_ref() { _nb_ref--; if(_nb_ref == 0) delete this; }
31 const unsigned int mnist_pictures_magic = 0x00000803;
32 const unsigned int mnist_labels_magic = 0x00000801;
34 inline unsigned int read_high_endian_int(istream &is) {
36 char *s = (char *) &result;
38 is.read(s, sizeof(result));
39 c = s[0]; s[0] = s[3]; s[3] = c;
40 c = s[1]; s[1] = s[2]; s[2] = c;
44 ImageSet::ImageSet() : _nb_pics(-1), _nb_obj(0), _width(-1), _height(-1),
45 _pixel_maps(0), _pixels(0), _labels(0), _used_picture(0) { }
47 ImageSet::~ImageSet() {
48 if(_pixel_maps) _pixel_maps->del_ref();
51 delete[] _used_picture;
54 void ImageSet::reset_used_pictures() {
55 for(int p = 0; p < _nb_pics; p++) _used_picture[p] = false;
58 int ImageSet::nb_unused_pictures() {
60 for(int p = 0; p < _nb_pics; p++) if(!_used_picture[p]) n++;
64 int ImageSet::pick_unused_picture() {
66 do { m = int(drand48() * _nb_pics); } while(_used_picture[m]);
67 _used_picture[m] = true;
71 void ImageSet::extract_unused_pictures(ImageSet &is, int nb) {
72 if(nb > is.nb_unused_pictures()) {
73 cerr << "Trying to extract " << nb << " pictures from a set of " << is.nb_unused_pictures() << "\n";
81 _pixel_maps = is._pixel_maps->add_ref();
82 _pixels = new unsigned char *[_nb_pics];
83 _labels = new unsigned char[_nb_pics];
84 _used_picture = new bool[_nb_pics];
85 for(int n = 0; n < _nb_pics; n++) {
86 int m = is.pick_unused_picture();
87 _pixels[n] = is._pixels[m];
88 _labels[n] = is._labels[m];
91 reset_used_pictures();
94 void ImageSet::load_mnist_format(char *picture_file_name, char *label_file_name) {
97 ifstream picture_is(picture_file_name);
99 if(picture_is.fail()) {
100 cerr << "Can not open file [" << picture_file_name << "].\n";
104 magic = read_high_endian_int(picture_is);
105 if(magic != mnist_pictures_magic) {
106 cerr << "Invalid magic for picture, file [" << picture_file_name << "] number [" << magic << "]\n";
110 _nb_pics = read_high_endian_int(picture_is);
111 _width = read_high_endian_int(picture_is);
112 _height = read_high_endian_int(picture_is);
114 ifstream label_is(label_file_name);
115 if(label_is.fail()) {
116 cerr << "Can not open file [" << label_file_name << "].\n";
120 magic = read_high_endian_int(label_is);
121 if(magic != mnist_labels_magic) {
122 cerr << "Invalid magic for labels, file [" << label_file_name << "] number [" << magic << "]\n";
126 int nb_pics_labels = read_high_endian_int(label_is);
128 if(nb_pics_labels != _nb_pics) {
129 cerr << "Inconsistency between the number of pictures in [" << picture_file_name << "] (" << _nb_pics << ")"
130 << " and the number of labels in [" << label_file_name << "] (" << nb_pics_labels << ").\n";
134 PixelMaps *pm = new PixelMaps(_nb_pics * _width * _height);
135 _pixel_maps = pm->add_ref();
136 _pixels = new unsigned char *[_nb_pics];
137 _labels = new unsigned char[_nb_pics];
138 _used_picture = new bool[_nb_pics];
140 picture_is.read((char *) _pixel_maps->_core, _nb_pics * _width * _height);
141 label_is.read((char *) _labels, _nb_pics);
143 for(int i = 0; i < _nb_pics * _width * _height; i++) _pixel_maps->_core[i] = 255 - _pixel_maps->_core[i];
146 for(int n = 0; n < _nb_pics; n++) {
147 _pixels[n] = _pixel_maps->_core + n * _width * _height;
148 if(_labels[n] > _nb_obj) _nb_obj = _labels[n];
152 reset_used_pictures();