automatic commit
[mlp.git] / images.cc
1 /*
2  *  mlp-mnist is an implementation of a multi-layer neural network.
3  *
4  *  Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
5  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
6  *
7  *  This file is part of mlp-mnist.
8  *
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.
12  *
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.
17  *
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/>.
20  *
21  */
22
23 #include "images.h"
24 #include <stdlib.h>
25
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; }
30
31 const unsigned int mnist_pictures_magic = 0x00000803;
32 const unsigned int mnist_labels_magic = 0x00000801;
33
34 inline unsigned int read_high_endian_int(istream &is) {
35   unsigned int result;
36   char *s = (char *) &result;
37   char c;
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;
41   return result;
42 }
43
44 ImageSet::ImageSet() : _nb_pics(-1), _nb_obj(0), _width(-1), _height(-1),
45                        _pixel_maps(0), _pixels(0), _labels(0), _used_picture(0) { }
46
47 ImageSet::~ImageSet() {
48   if(_pixel_maps) _pixel_maps->del_ref();
49   delete[] _pixels;
50   delete[] _labels;
51   delete[] _used_picture;
52 }
53
54 void ImageSet::reset_used_pictures() {
55   for(int p = 0; p < _nb_pics; p++) _used_picture[p] = false;
56 }
57
58 int ImageSet::nb_unused_pictures() {
59   int n = 0;
60   for(int p = 0; p < _nb_pics; p++) if(!_used_picture[p]) n++;
61   return n;
62 }
63
64 int ImageSet::pick_unused_picture() {
65   int m;
66   do { m = int(drand48() * _nb_pics); } while(_used_picture[m]);
67   _used_picture[m] = true;
68   return m;
69 }
70
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";
74     exit(1);
75   }
76
77   _nb_pics = nb;
78   _width = is._width;
79   _height = is._height;
80   _nb_obj = is._nb_obj;
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];
89   }
90
91   reset_used_pictures();
92 }
93
94 void ImageSet::load_mnist_format(char *picture_file_name, char *label_file_name) {
95   unsigned int magic;
96
97   ifstream picture_is(picture_file_name);
98
99   if(picture_is.fail()) {
100     cerr << "Can not open file [" << picture_file_name << "].\n";
101     exit(1);
102   }
103
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";
107     exit(1);
108   }
109
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);
113
114   ifstream label_is(label_file_name);
115   if(label_is.fail()) {
116     cerr << "Can not open file [" << label_file_name << "].\n";
117     exit(1);
118   }
119
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";
123     exit(1);
124   }
125
126   int nb_pics_labels = read_high_endian_int(label_is);
127
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";
131     exit(1);
132   }
133
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];
139
140   picture_is.read((char *) _pixel_maps->_core, _nb_pics * _width * _height);
141   label_is.read((char *) _labels, _nb_pics);
142
143   for(int i = 0; i < _nb_pics * _width * _height; i++) _pixel_maps->_core[i] = 255 - _pixel_maps->_core[i];
144
145   _nb_obj = 0;
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];
149   }
150   _nb_obj++;
151
152   reset_used_pictures();
153 }