Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.6.1 #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "rust-lang.rust-analyzer"
}
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mnist"
description = "MNIST data set parser."
version = "0.6.0"
version = "0.6.1"
authors = ["David McNeil <[email protected]>"]
repository = "https://github.com/davidMcneil/mnist"
documentation = "https://docs.rs/mnist"
Expand All @@ -28,7 +28,6 @@ pbr = {version = "1.0", optional = true}
flate2 = {version = "1.0.2", optional = true, features = ["rust_backend"], default-features = false}

[dev-dependencies]
ndarray = "0.14"
image = "0.23"
# show-image is used to visualize datasets in a pop-up window
show-image = {version = "0.6", features = ["image"]}
ndarray = "0.16.1"
image = "0.25.5"
show-image = { version = "0.14.0", git = "https://github.com/robohouse-delft/show-image-rs.git", features = ["image", "png"] }
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ An example of downloading this dataset may be found by running:
```sh
$ cargo run --features download --example fashion_mnist
```

## Troubleshooting

### On Mac


`ld: library not found for -lSDL2`

```shell
brew link sdl2
brew unlink sdl2 && brew link sdl2
export LIBRARY_PATH="$LIBRARY_PATH:/opt/homebrew/lib" # needed to link to SDL2 library

cargo clean
cargo run --example mnist -F download
```
27 changes: 13 additions & 14 deletions examples/fashion_mnist.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use image::*;
use mnist::*;
use ndarray::prelude::*;
use show_image::{make_window_full, Event, WindowOptions};
use show_image::{create_window, event, WindowOptions};

#[show_image::main]
fn main() {
let (trn_size, _rows, _cols) = (50_000, 28, 28);

Expand All @@ -27,24 +28,22 @@ fn main() {
.mapv(|x| x as f32 / 256.);

let image = bw_ndarray2_to_rgb_image(train_data.slice(s![item_num, .., ..]).to_owned());
let window_options = WindowOptions {
name: "image".to_string(),
size: [100, 100],
resizable: true,
preserve_aspect_ratio: true,
};
let window = make_window_full(window_options).unwrap();
window.set_image(image, "test_result").unwrap();
let window_options = WindowOptions::new().set_size(Some([100, 100]));
let window = create_window("image", window_options).unwrap();
window.set_image("test_result", image).unwrap();

for event in window.events() {
if let Event::KeyboardEvent(event) = event {
if event.key == show_image::KeyCode::Escape {
// Wait for the window to be closed or Escape to be pressed.
for event in window.event_channel().map_err(|e| e.to_string()).unwrap() {
if let event::WindowEvent::KeyboardInput(event) = event {
if !event.is_synthetic
&& event.input.key_code == Some(event::VirtualKeyCode::Escape)
&& event.input.state.is_pressed()
{
println!("Escape pressed!");
break;
}
}
}

show_image::stop().unwrap();
}

fn return_item_description_from_number(val: u8) {
Expand Down
28 changes: 14 additions & 14 deletions examples/mnist.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use image::*;
use mnist::*;
use ndarray::prelude::*;
use show_image::{make_window_full, Event, WindowOptions};
use show_image::{create_window, event, WindowOptions};

#[show_image::main]
fn main() {
let (trn_size, _rows, _cols) = (50_000, 28, 28);

Expand All @@ -28,24 +29,23 @@ fn main() {
.mapv(|x| x as f32 / 256.);

let image = bw_ndarray2_to_rgb_image(train_data.slice(s![item_num, .., ..]).to_owned());
let window_options = WindowOptions {
name: "image".to_string(),
size: [100, 100],
resizable: true,
preserve_aspect_ratio: true,
};
let window = make_window_full(window_options).unwrap();
window.set_image(image, "test_result").unwrap();
let window_options = WindowOptions::new().set_size(Some([100, 100]));
let window = create_window("image", window_options).unwrap();

window.set_image("test_result", image).unwrap();

for event in window.events() {
if let Event::KeyboardEvent(event) = event {
if event.key == show_image::KeyCode::Escape {
// Wait for the window to be closed or Escape to be pressed.
for event in window.event_channel().map_err(|e| e.to_string()).unwrap() {
if let event::WindowEvent::KeyboardInput(event) = event {
if !event.is_synthetic
&& event.input.key_code == Some(event::VirtualKeyCode::Escape)
&& event.input.state.is_pressed()
{
println!("Escape pressed!");
break;
}
}
}

show_image::stop().unwrap();
}

fn return_item_description_from_number(val: u8) {
Expand Down
16 changes: 10 additions & 6 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ use pbr::ProgressBar;
use std::convert::TryInto;
use std::thread;

#[allow(unused)]
use log::Level;

use std::time::Duration;

#[cfg(target_family = "unix")]
use std::os::unix::fs::MetadataExt;
#[cfg(target_family = "windows")]
use std::os::windows::fs::MetadataExt;

#[cfg(target_family = "unix")]
fn file_size(meta: &MetadataExt) -> usize {
fn file_size(meta: &dyn MetadataExt) -> usize {
meta.size() as usize
}

Expand Down Expand Up @@ -70,6 +73,7 @@ pub(super) fn download_and_extract(
Ok(())
}

#[allow(unused_variables)]
fn download(
base_url: &str,
archive: &str,
Expand All @@ -81,10 +85,10 @@ fn download(
let url = Path::new(base_url).join(archive);
let file_name = download_dir.to_str().unwrap().to_owned() + archive; //.clone();
if Path::new(&file_name).exists() {
log::info!(
" File {:?} already exists, skipping downloading.",
file_name
);
log::info!(
" File {:?} already exists, skipping downloading.",
file_name
);
} else {
log::info!(
"- Downloading from file from {} and saving to file as: {}",
Expand All @@ -105,7 +109,7 @@ fn download(
current_size = file_size(&meta);

pb.set(current_size.try_into().unwrap());
thread::sleep_ms(10);
thread::sleep(Duration::from_millis(10));
}
pb.finish_println(" ");
});
Expand Down
64 changes: 39 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,22 @@ use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

// From https://github.com/cvdfoundation/mnist

// Training
// https://storage.googleapis.com/cvdf-datasets/mnist/train-images-idx3-ubyte.gz
// https://storage.googleapis.com/cvdf-datasets/mnist/train-labels-idx1-ubyte.gz

// Testing
// https://storage.googleapis.com/cvdf-datasets/mnist/t10k-images-idx3-ubyte.gz
// https://storage.googleapis.com/cvdf-datasets/mnist/t10k-labels-idx1-ubyte.gz

static BASE_PATH: &str = "data/";
static BASE_URL: &str = "http://yann.lecun.com/exdb/mnist";
// old, doesn't work, gives 404
// static BASE_URL: &str = "http://yann.lecun.com/exdb/mnist";
static BASE_URL: &str = "https://storage.googleapis.com/cvdf-datasets/mnist/";

#[allow(dead_code)]
static FASHION_BASE_URL: &str = "http://fashion-mnist.s3-website.eu-central-1.amazonaws.com";
static TRN_IMG_FILENAME: &str = "train-images-idx3-ubyte";
static TRN_LBL_FILENAME: &str = "train-labels-idx1-ubyte";
Expand Down Expand Up @@ -401,6 +415,7 @@ impl<'a> MnistBuilder<'a> {
/// If `trn_len + val_len + tst_len > 70,000`.
pub fn finalize(&self) -> Mnist {
if self.download_and_extract {
#[cfg(feature = "download")]
let base_url = if self.use_fashion_data {
FASHION_BASE_URL
} else if self.base_url != BASE_URL {
Expand Down Expand Up @@ -430,10 +445,9 @@ impl<'a> MnistBuilder<'a> {
let available_length = (TRN_LEN + TST_LEN) as usize;
assert!(
total_length <= available_length,
format!(
"Total data set length ({}) greater than maximum possible length ({}).",
total_length, available_length
)
"Total data set length ({}) greater than maximum possible length ({}).",
total_length,
available_length
);
let mut trn_img = images(
&Path::new(self.base_path).join(self.trn_img_filename),
Expand All @@ -457,8 +471,8 @@ impl<'a> MnistBuilder<'a> {
let mut val_lbl = trn_lbl.split_off(trn_len);
let mut tst_img = val_img.split_off(val_len * ROWS * COLS);
let mut tst_lbl = val_lbl.split_off(val_len);
tst_img.split_off(tst_len * ROWS * COLS);
tst_lbl.split_off(tst_len);
let _ = tst_img.split_off(tst_len * ROWS * COLS);
let _ = tst_lbl.split_off(tst_len);
if self.lbl_format == LabelFormat::OneHotVector {
fn digit2one_hot(v: Vec<u8>) -> Vec<u8> {
v.iter()
Expand Down Expand Up @@ -559,20 +573,18 @@ fn labels(path: &Path, expected_length: u32) -> Vec<u8> {
.unwrap_or_else(|_| panic!("Unable to read magic number from {:?}.", path));
assert!(
LBL_MAGIC_NUMBER == magic_number,
format!(
"Expected magic number {} got {}.",
LBL_MAGIC_NUMBER, magic_number
)
"Expected magic number {} got {}.",
LBL_MAGIC_NUMBER,
magic_number
);
let length = file
.read_u32::<BigEndian>()
.unwrap_or_else(|_| panic!("Unable to length from {:?}.", path));
assert!(
expected_length == length,
format!(
"Expected data set length of {} got {}.",
expected_length, length
)
"Expected data set length of {} got {}.",
expected_length,
length
);
file.bytes().map(|b| b.unwrap()).collect()
}
Expand All @@ -596,36 +608,38 @@ fn images(path: &Path, expected_length: u32) -> Vec<u8> {
.unwrap_or_else(|_| panic!("Unable to read magic number from {:?}.", path));
assert!(
IMG_MAGIC_NUMBER == magic_number,
format!(
"Expected magic number {} got {}.",
IMG_MAGIC_NUMBER, magic_number
)
"Expected magic number {} got {}.",
IMG_MAGIC_NUMBER,
magic_number
);
let length = file
.read_u32::<BigEndian>()
.unwrap_or_else(|_| panic!("Unable to length from {:?}.", path));
assert!(
expected_length == length,
format!(
"Expected data set length of {} got {}.",
expected_length, length
)
"Expected data set length of {} got {}.",
expected_length,
length
);
let rows = file
.read_u32::<BigEndian>()
.unwrap_or_else(|_| panic!("Unable to number of rows from {:?}.", path))
as usize;
assert!(
ROWS == rows,
format!("Expected rows length of {} got {}.", ROWS, rows)
"Expected rows length of {} got {}.",
ROWS,
rows
);
let cols = file
.read_u32::<BigEndian>()
.unwrap_or_else(|_| panic!("Unable to number of columns from {:?}.", path))
as usize;
assert!(
COLS == cols,
format!("Expected cols length of {} got {}.", COLS, cols)
"Expected cols length of {} got {}.",
COLS,
cols
);
// Convert `file` from a Vec to a slice.
file.to_vec()
Expand Down