1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// A simple class and associated functions for parsing the MNIST dataset.

use log::info;
use std::convert::TryFrom;
use std::fs;
use std::io;
use std::io::Read;

// Filenames
const TRAIN_DATA_FILENAME: &str = "train-images-idx3-ubyte";
const TEST_DATA_FILENAME: &str = "t10k-images-idx3-ubyte";
const TRAIN_LABEL_FILENAME: &str = "train-labels-idx1-ubyte";
const TEST_LABEL_FILENAME: &str = "t10k-labels-idx1-ubyte";

// Constants relating to the MNIST dataset. All usize for array/vec indexing.
const IMAGES_MAGIC_NUMBER: usize = 2051;
const LABELS_MAGIC_NUMBER: usize = 2049;
const NUM_TRAIN_IMAGES: usize = 60_000;
const NUM_TEST_IMAGES: usize = 10_000;
const IMAGE_ROWS: usize = 28;
const IMAGE_COLUMNS: usize = 28;

pub struct Mnist {
    // Arrays of images.
    pub train_data: Vec<[u8; IMAGE_ROWS * IMAGE_COLUMNS]>,
    pub test_data: Vec<[u8; IMAGE_ROWS * IMAGE_COLUMNS]>,

    // Arrays of labels.
    pub train_labels: Vec<u8>,
    pub test_labels: Vec<u8>,
}

impl Mnist {
    pub fn new(mnist_path: &str) -> Mnist {
        // ------------------------------------ Get Training Data ---------------------------------
        info!("Reading MNIST training data.");
        let (magic_number, num_images, num_rows, num_cols, train_images) =
            parse_images(&[mnist_path, TRAIN_DATA_FILENAME].concat()).expect(
                &format!(
                    "Training data file \"{}{}\" not found; did you \
                     remember to download and extract it?",
                    mnist_path, TRAIN_DATA_FILENAME,
                )[..],
            );

        // Assert that numbers extracted from the file were as expected.
        assert_eq!(
            magic_number, IMAGES_MAGIC_NUMBER,
            "Magic number for training data does not match expected value."
        );
        assert_eq!(
            num_images, NUM_TRAIN_IMAGES,
            "Number of images in training data does not match expected value."
        );
        assert_eq!(
            num_rows, IMAGE_ROWS,
            "Number of rows per image in training data does not match expected value."
        );
        assert_eq!(
            num_cols, IMAGE_COLUMNS,
            "Numver of columns per image in training data does not match expected value."
        );

        // ------------------------------------ Get Testing Data ----------------------------------
        info!("Reading MNIST testing data.");
        let (magic_number, num_images, num_rows, num_cols, test_images) =
            parse_images(&[mnist_path, TEST_DATA_FILENAME].concat()).expect(
                &format!(
                    "Test data file \"{}{}\" not found; did you \
                     remember to download and extract it?",
                    mnist_path, TEST_DATA_FILENAME,
                )[..],
            );

        // Assert that numbers extracted from the file were as expected.
        assert_eq!(
            magic_number, IMAGES_MAGIC_NUMBER,
            "Magic number for testing data does not match expected value."
        );
        assert_eq!(
            num_images, NUM_TEST_IMAGES,
            "Number of images in testing data does not match expected value."
        );
        assert_eq!(
            num_rows, IMAGE_ROWS,
            "Number of rows per image in testing data does not match expected value."
        );
        assert_eq!(
            num_cols, IMAGE_COLUMNS,
            "Numver of columns per image in testing data does not match expected value."
        );

        // ---------------------------------- Get Training Labels ---------------------------------
        info!("Reading MNIST training labels.");
        let (magic_number, num_labels, train_labels) =
            parse_labels(&[mnist_path, TRAIN_LABEL_FILENAME].concat()).expect(
                &format!(
                    "Training label file \"{}{}\" not found; did you \
                     remember to download and extract it?",
                    mnist_path, TRAIN_LABEL_FILENAME,
                )[..],
            );

        // Assert that numbers extracted from the file were as expected.
        assert_eq!(
            magic_number, LABELS_MAGIC_NUMBER,
            "Magic number for training labels does not match expected value."
        );
        assert_eq!(
            num_labels, NUM_TRAIN_IMAGES,
            "Number of labels in training labels does not match expected value."
        );

        // ----------------------------------- Get Testing Labels ---------------------------------
        info!("Reading MNIST testing labels.");
        let (magic_number, num_labels, test_labels) =
            parse_labels(&[mnist_path, TEST_LABEL_FILENAME].concat()).expect(
                &format!(
                    "Test labels file \"{}{}\" not found; did you \
                     remember to download and extract it?",
                    mnist_path, TEST_LABEL_FILENAME,
                )[..],
            );

        // Assert that numbers extracted from the file were as expected.
        assert_eq!(
            magic_number, LABELS_MAGIC_NUMBER,
            "Magic number for testing labels does not match expected value."
        );
        assert_eq!(
            num_labels, NUM_TEST_IMAGES,
            "Number of labels in testing labels does not match expected value."
        );

        Mnist {
            train_data: train_images,
            test_data: test_images,
            train_labels: train_labels,
            test_labels: test_labels,
        }
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the image directly with .train_data[index]"
    )]
    pub fn get_train_image(&self, index: usize) -> &[u8; IMAGE_ROWS * IMAGE_COLUMNS] {
        &self.train_data[index]
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the image directly with .test_data[index]."
    )]
    pub fn get_test_image(&self, index: usize) -> &[u8; IMAGE_ROWS * IMAGE_COLUMNS] {
        &self.test_data[index]
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the label directly with .train_labels[index]."
    )]
    pub fn get_train_label(&self, index: usize) -> u8 {
        self.train_labels[index]
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the label directly with .test_labels[index]."
    )]
    pub fn get_test_label(&self, index: usize) -> u8 {
        self.test_labels[index]
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the images directly through .train_data."
    )]
    pub fn train_images(&self) -> std::slice::Iter<'_, [u8; 784]> {
        self.train_data.iter()
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the images directly through .test_data."
    )]
    pub fn test_images(&self) -> std::slice::Iter<'_, [u8; 784]> {
        self.test_data.iter()
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the labels directly through .train_labels."
    )]
    pub fn train_labels(&self) -> std::slice::Iter<'_, u8> {
        self.train_labels.iter()
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the labels directly through .test_labels."
    )]
    pub fn test_labels(&self) -> std::slice::Iter<'_, u8> {
        self.test_labels.iter()
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the images/labels directly through .train_data and .train_labels."
    )]
    pub fn train_set(
        &self,
    ) -> std::iter::Zip<std::slice::Iter<'_, [u8; 784]>, std::slice::Iter<'_, u8>> {
        self.train_data.iter().zip(self.train_labels.iter())
    }

    #[deprecated(
        since = "0.1.4",
        note = "Please access the images/labels directly directly throug .test_data and .test_labels."
    )]
    pub fn test_set(
        &self,
    ) -> std::iter::Zip<std::slice::Iter<'_, [u8; 784]>, std::slice::Iter<'_, u8>> {
        self.test_data.iter().zip(self.test_labels.iter())
    }
}

pub fn print_sample_image(image: &[u8; IMAGE_ROWS * IMAGE_COLUMNS], label: u8) {
    // Check that the image isn't empty and has a valid number of rows.
    assert!(image.len() != 0, "There are no pixels in this image.");
    assert_eq!(
        image.len() % usize::try_from(IMAGE_ROWS).unwrap(),
        0,
        "Number of pixels does not evenly divide into number of rows."
    );

    println!("Sample image label: {} \nSample image:", label);

    // Print each row.
    for row in 0..IMAGE_ROWS {
        for col in 0..IMAGE_COLUMNS {
            if image[usize::try_from(row * IMAGE_COLUMNS + col).unwrap()] == 0 {
                print!("__");
            } else {
                print!("##");
            }
        }
        print!("\n");
    }
}

fn parse_images(
    filename: &str,
) -> io::Result<(
    usize,
    usize,
    usize,
    usize,
    Vec<[u8; IMAGE_ROWS * IMAGE_COLUMNS]>,
)> {
    // Open the file.
    let images_data_bytes = fs::File::open(filename)?;
    let images_data_bytes = io::BufReader::new(images_data_bytes);
    let mut buffer_32: [u8; 4] = [0; 4];

    // Get the magic number.
    images_data_bytes
        .get_ref()
        .take(4)
        .read(&mut buffer_32)
        .unwrap();
    let magic_number = usize::try_from(u32::from_be_bytes(buffer_32)).unwrap();

    // Get number of images.
    images_data_bytes
        .get_ref()
        .take(4)
        .read(&mut buffer_32)
        .unwrap();
    let num_images = usize::try_from(u32::from_be_bytes(buffer_32)).unwrap();

    // Get number or rows per image.
    images_data_bytes
        .get_ref()
        .take(4)
        .read(&mut buffer_32)
        .unwrap();
    let num_rows = usize::try_from(u32::from_be_bytes(buffer_32)).unwrap();

    // Get number or columns per image.
    images_data_bytes
        .get_ref()
        .take(4)
        .read(&mut buffer_32)
        .unwrap();
    let num_cols = usize::try_from(u32::from_be_bytes(buffer_32)).unwrap();

    // Buffer for holding image pixels.
    let mut image_buffer: [u8; IMAGE_ROWS * IMAGE_COLUMNS] = [0; IMAGE_ROWS * IMAGE_COLUMNS];

    // Vector to hold all images in the file.
    let mut images: Vec<[u8; IMAGE_ROWS * IMAGE_COLUMNS]> =
        Vec::with_capacity(usize::try_from(num_images).unwrap());

    // Get images from file.
    for _image in 0..num_images {
        images_data_bytes
            .get_ref()
            .take(u64::try_from(num_rows * num_cols).unwrap())
            .read(&mut image_buffer)
            .unwrap();
        images.push(image_buffer.clone());
    }

    Ok((magic_number, num_images, num_rows, num_cols, images))
}

fn parse_labels(filename: &str) -> io::Result<(usize, usize, Vec<u8>)> {
    let labels_data_bytes = fs::File::open(filename)?;
    let labels_data_bytes = io::BufReader::new(labels_data_bytes);
    let mut buffer_32: [u8; 4] = [0; 4];

    // Get the magic number.
    labels_data_bytes
        .get_ref()
        .take(4)
        .read(&mut buffer_32)
        .unwrap();
    let magic_number = usize::try_from(u32::from_be_bytes(buffer_32)).unwrap();

    // Get number of labels.
    labels_data_bytes
        .get_ref()
        .take(4)
        .read(&mut buffer_32)
        .unwrap();
    let num_labels = usize::try_from(u32::from_be_bytes(buffer_32)).unwrap();

    // Buffer for holding image label.
    let mut label_buffer: [u8; 1] = [0; 1];

    // Vector to hold all labels in the file.
    let mut labels: Vec<u8> = Vec::with_capacity(usize::try_from(num_labels).unwrap());

    // Get labels from file.
    for _label in 0..num_labels {
        labels_data_bytes
            .get_ref()
            .take(1)
            .read(&mut label_buffer)
            .unwrap();
        labels.push(label_buffer[0]);
    }
    Ok((magic_number, num_labels, labels))
}