7 releases (breaking)

0.6.0 Oct 30, 2023
0.5.0 Nov 8, 2021
0.4.1 Aug 20, 2020
0.4.0 Apr 17, 2017
0.1.0 Jan 16, 2017

#309 in Machine learning

Download history 549/week @ 2024-11-30 647/week @ 2024-12-07 547/week @ 2024-12-14 228/week @ 2024-12-21 157/week @ 2024-12-28 819/week @ 2025-01-04 1586/week @ 2025-01-11 880/week @ 2025-01-18 818/week @ 2025-01-25 1501/week @ 2025-02-01 841/week @ 2025-02-08 405/week @ 2025-02-15 612/week @ 2025-02-22 811/week @ 2025-03-01 702/week @ 2025-03-08 611/week @ 2025-03-15

2,765 downloads per month
Used in 11 crates

MIT license

37KB
640 lines

MNIST

A crate for parsing the MNIST and Fashion MNIST data set into vectors to be used by Rust programs.

Example

use mnist::*;
use ndarray::prelude::*;

fn main() {

    // Deconstruct the returned Mnist struct.
    let Mnist {
        trn_img,
        trn_lbl,
        tst_img,
     tst_lbl,
        ..
    } = MnistBuilder::new()
        .label_format_digit()
        .training_set_length(50_000)
        .validation_set_length(10_000)
        .test_set_length(10_000)
        .finalize();

    let image_num = 0;
    // Can use an Array2 or Array3 here (Array3 for visualization)
    let train_data = Array3::from_shape_vec((50_000, 28, 28), trn_img)
        .expect("Error converting images to Array3 struct")
        .map(|x| *x as f32 / 256.0);
    println!("{:#.1?}\n",train_data.slice(s![image_num, .., ..]));

    // Convert the returned Mnist struct to Array2 format
    let train_labels: Array2<f32> = Array2::from_shape_vec((50_000, 1), trn_lbl)
        .expect("Error converting training labels to Array2 struct")
        .map(|x| *x as f32);
    println!("The first digit is a {:?}",train_labels.slice(s![image_num, ..]) );

    let _test_data = Array3::from_shape_vec((10_000, 28, 28), tst_img)
        .expect("Error converting images to Array3 struct")
        .map(|x| *x as f32 / 256.);

    let _test_labels: Array2<f32> = Array2::from_shape_vec((10_000, 1), tst_lbl)
        .expect("Error converting testing labels to Array2 struct")
        .map(|x| *x as f32);
}

Fashion MNIST

The Fasion MNIST dataset offers a similarly-formatted drop-in replacement dataset for the original MNIST set, but typically poses a more difficult classification challenge that handwritten numbers.

An example of downloading this dataset may be found by running:

$ cargo run --features download --example fashion_mnist

Dependencies

~0.1–8.5MB
~72K SLoC