mnist_skip/
skip.rs

1// Copyright (C) 2024 Hallvard Høyland Lavik
2
3use neurons::{activation, network, objective, optimizer, plot, tensor};
4
5use std::fs::File;
6use std::io::{BufReader, Read, Result};
7
8fn read(reader: &mut dyn Read) -> Result<u32> {
9    let mut buffer = [0; 4];
10    reader.read_exact(&mut buffer)?;
11    Ok(u32::from_be_bytes(buffer))
12}
13
14fn load_mnist(path: &str) -> Result<Vec<tensor::Tensor>> {
15    let mut reader = BufReader::new(File::open(path)?);
16    let mut images: Vec<tensor::Tensor> = Vec::new();
17
18    let _magic_number = read(&mut reader)?;
19    let num_images = read(&mut reader)?;
20    let num_rows = read(&mut reader)?;
21    let num_cols = read(&mut reader)?;
22
23    for _ in 0..num_images {
24        let mut image: Vec<Vec<f32>> = Vec::new();
25        for _ in 0..num_rows {
26            let mut row: Vec<f32> = Vec::new();
27            for _ in 0..num_cols {
28                let mut pixel = [0];
29                reader.read_exact(&mut pixel)?;
30                row.push(pixel[0] as f32 / 255.0);
31            }
32            image.push(row);
33        }
34        images.push(tensor::Tensor::triple(vec![image]).resize(tensor::Shape::Triple(1, 14, 14)));
35    }
36
37    Ok(images)
38}
39
40fn load_labels(file_path: &str, numbers: usize) -> Result<Vec<tensor::Tensor>> {
41    let mut reader = BufReader::new(File::open(file_path)?);
42    let _magic_number = read(&mut reader)?;
43    let num_labels = read(&mut reader)?;
44
45    let mut _labels = vec![0; num_labels as usize];
46    reader.read_exact(&mut _labels)?;
47
48    Ok(_labels
49        .iter()
50        .map(|&x| tensor::Tensor::one_hot(x as usize, numbers))
51        .collect())
52}
53
54fn main() {
55    let x_train = load_mnist("./examples/datasets/mnist/train-images-idx3-ubyte").unwrap();
56    let y_train = load_labels("./examples/datasets/mnist/train-labels-idx1-ubyte", 10).unwrap();
57    let x_test = load_mnist("./examples/datasets/mnist/t10k-images-idx3-ubyte").unwrap();
58    let y_test = load_labels("./examples/datasets/mnist/t10k-labels-idx1-ubyte", 10).unwrap();
59    println!(
60        "Train: {} images, Test: {} images",
61        x_train.len(),
62        x_test.len()
63    );
64
65    let x_train: Vec<&tensor::Tensor> = x_train.iter().collect();
66    let y_train: Vec<&tensor::Tensor> = y_train.iter().collect();
67    let x_test: Vec<&tensor::Tensor> = x_test.iter().collect();
68    let y_test: Vec<&tensor::Tensor> = y_test.iter().collect();
69
70    let mut network = network::Network::new(tensor::Shape::Triple(1, 14, 14));
71
72    network.convolution(
73        1,
74        (3, 3),
75        (1, 1),
76        (1, 1),
77        (1, 1),
78        activation::Activation::ReLU,
79        None,
80    );
81    network.convolution(
82        1,
83        (3, 3),
84        (1, 1),
85        (1, 1),
86        (1, 1),
87        activation::Activation::ReLU,
88        None,
89    );
90    network.convolution(
91        1,
92        (3, 3),
93        (1, 1),
94        (1, 1),
95        (1, 1),
96        activation::Activation::ReLU,
97        None,
98    );
99    network.maxpool((2, 2), (2, 2));
100    network.dense(10, activation::Activation::Softmax, true, None);
101
102    network.connect(0, 3);
103
104    network.set_optimizer(optimizer::Adam::create(0.001, 0.9, 0.999, 1e-8, None));
105    network.set_objective(
106        objective::Objective::CrossEntropy, // Objective function
107        None,                               // Gradient clipping
108    );
109
110    println!("{}", network);
111
112    // Train the network
113    let (train_loss, val_loss, val_acc) = network.learn(
114        &x_train,
115        &y_train,
116        Some((&x_test, &y_test, 10)),
117        32,
118        25,
119        Some(5),
120    );
121    plot::loss(
122        &train_loss,
123        &val_loss,
124        &val_acc,
125        "SKIP : MNIST",
126        "./output/mnist/skip.png",
127    );
128
129    // Validate the network
130    let (val_loss, val_acc) = network.validate(&x_test, &y_test, 1e-6);
131    println!(
132        "Final validation accuracy: {:.2} % and loss: {:.5}",
133        val_acc * 100.0,
134        val_loss
135    );
136
137    // Use the network
138    let prediction = network.predict(x_test.get(0).unwrap());
139    println!(
140        "Prediction on input: Target: {}. Output: {}.",
141        y_test[0].argmax(),
142        prediction.argmax()
143    );
144
145    // let x = x_test.get(5).unwrap();
146    // let y = y_test.get(5).unwrap();
147    // plot::heatmap(
148    //     &x,
149    //     &format!("Target: {}", y.argmax()),
150    //     "./output/mnist/input.png",
151    // );
152
153    // Plot the pre- and post-activation heatmaps for each (image) layer.
154    // let (pre, post, _) = network.forward(x);
155    // for (i, (i_pre, i_post)) in pre.iter().zip(post.iter()).enumerate() {
156    //     let pre_title = format!("layer_{}_pre", i);
157    //     let post_title = format!("layer_{}_post", i);
158    //     let pre_file = format!("layer_{}_pre.png", i);
159    //     let post_file = format!("layer_{}_post.png", i);
160    //     plot::heatmap(&i_pre, &pre_title, &pre_file);
161    //     plot::heatmap(&i_post, &post_title, &post_file);
162    // }
163}