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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Fashion-MNIST dataset of clothing images.
//!
//! 70,000 grayscale images of clothing articles from the Zalando catalog, each
//! 28×28 pixels, split into a 60,000-image training partition and a
//! 10,000-image test partition. The task is to recognize which of 10 garment
//! classes an image shows.
//!
//! Fashion-MNIST matches [`mnist`](crate::dataset::mnist) in image size, class
//! count, partition sizes, and file format. Its classes overlap more, so it is
//! the harder task of the two.
//!
//! **Columns (2):**
//!
//! | Name | Type | Description |
//! |----------|-----------|------------------------------------------------------------------------------------|
//! | `pixels` | `Bytes` | 784 pixel intensities per image, one 28×28 image flattened in row-major order, each value in `0..=255` |
//! | `class` | `Integer` | the garment class, one of `0`-`9` |
//!
//! The source designates `pixels` as the input
//! ([`FashionMnist::FEATURE_NAMES`](crate::FashionMnist::FEATURE_NAMES)) and `class` as the label
//! ([`FashionMnist::TARGET`](crate::FashionMnist::TARGET)). See
//! [`FashionMnist::CLASS_NAMES`](crate::FashionMnist::CLASS_NAMES) for the
//! name of each class code.
//!
//! **Samples:**
//! - Training partition: 60,000 (exactly 6,000 per class)
//! - Test partition: 10,000 (exactly 1,000 per class)
//! - Both: 70,000
//!
//! **Application:** Multi-class image classification / garment recognition
//!
//! **Missing values:** none.
//!
//! **Source:** Xiao, H., Rasul, K., and Vollgraf, R. (2017). Fashion-MNIST.
//! Zalando Research, released under the MIT license.
//! <https://github.com/zalandoresearch/fashion-mnist>
use ;
use crate;
use crateimpl_ml_dataset;
use ;
/// The name of the dataset.
const FASHION_MNIST_DATASET_NAME: &str = "fashion_mnist";
/// Number of samples in the training partition.
const N_TRAIN_SAMPLES: usize = 60_000;
/// Number of samples in the test partition.
const N_TEST_SAMPLES: usize = 10_000;
/// The training partition: 60,000 images.
static TRAIN_PARTITION: Partition = Partition ;
/// The test partition: 10,000 images.
static TEST_PARTITION: Partition = Partition ;
/// Subset selector: the training partition (60,000 images).
const SUBSET_TRAIN: & = &;
/// Subset selector: the test partition (10,000 images).
const SUBSET_TEST: & = &;
/// Subset selector: both partitions (70,000 images, train followed by test).
const SUBSET_ALL: & = &;
/// A struct that represents the Fashion-MNIST dataset with lazy loading.
///
/// The dataset loads only when you call a data accessor method. After the first
/// load, the dataset caches the data for later accesses.
///
/// # About Dataset
///
/// Fashion-MNIST holds 70,000 grayscale images of clothing articles from the
/// Zalando catalog, each 28×28 pixels, with the garment class each one shows.
/// It matches [`Mnist`](crate::Mnist) in image size, class count, partition
/// sizes, and file format, so the two loaders present the same interface.
///
/// Fashion-MNIST is the harder task of the two. The garment classes overlap far
/// more than the handwritten digits do, and the pullover, coat, and shirt
/// classes are the ones that confuse a model.
///
/// # Subsets
///
/// The source ships two partitions, and three constructors select them:
///
/// - [`FashionMnist::new`]: the training partition, 60,000 images
/// - [`FashionMnist::new_test`]: the test partition, 10,000 images
/// - [`FashionMnist::new_all`]: both, 70,000 images, train followed by test
///
/// Keep the two partitions apart to compare a result with published work. The
/// standard protocol trains on the 60,000 and reports on the 10,000.
///
/// Each partition caches its own two files, so an instance downloads only what
/// its subset needs.
///
/// # Columns
///
/// | Name | Type | Description |
/// |----------|-----------|------------------------------------------------------------------------------------|
/// | `pixels` | `Bytes` | 784 pixel intensities per image, one 28×28 image flattened in row-major order, each value in `0..=255` |
/// | `class` | `Integer` | the garment class, one of `0`-`9` |
///
/// The source designates `pixels` as the input
/// ([`FashionMnist::FEATURE_NAMES`]) and `class` as the label
/// ([`FashionMnist::TARGET`]).
///
/// Missing values: none.
///
/// In the `pixels` column, `0` is the background. The column holds one row of
/// 784 bytes per image. A view of that row shaped `(28, 28)` reads the same
/// bytes, at no copy. A garment can reach the edge of its frame, so a border
/// pixel is not always background.
///
/// [`FashionMnist::CLASS_NAMES`] maps a `class` code to its name:
///
/// | Code | Class | Code | Class |
/// |------|-------------|------|------------|
/// | `0` | T-shirt/top | `5` | Sandal |
/// | `1` | Trouser | `6` | Shirt |
/// | `2` | Pullover | `7` | Sneaker |
/// | `3` | Dress | `8` | Bag |
/// | `4` | Coat | `9` | Ankle boot |
///
/// The classes are **exactly** balanced: 6,000 images per class in the training
/// partition and 1,000 per class in the test partition. A plain
/// [`train_test_split`](crate::preprocessing::train_test_split) therefore needs
/// no stratification to keep the classes even.
///
/// # Source format
///
/// The source ships four gzip-compressed IDX files, one image file and one label
/// file per partition. IDX is a binary format: a big-endian header of 4-byte
/// integers, then the raw bytes. The storage directory holds each file
/// **decompressed**, under the source name prefixed with `fashion-`. The
/// upstream files carry the same names as the MNIST files, and the prefix keeps
/// them apart. [`Mnist`](crate::Mnist) and `FashionMnist` can therefore share
/// one storage directory.
///
/// See more information at <https://github.com/zalandoresearch/fashion-mnist>.
///
/// # License
///
/// Zalando Research releases Fashion-MNIST under the MIT license.
///
/// # Citation
///
/// Xiao, H., Rasul, K., and Vollgraf, R. (2017). "Fashion-MNIST: a Novel Image
/// Dataset for Benchmarking Machine Learning Algorithms." arXiv:1708.07747.
/// <https://arxiv.org/abs/1708.07747>
///
/// # Thread Safety
///
/// This struct implements `Send` and `Sync` automatically, because all fields
/// implement them. This makes the struct safe to share across threads. The
/// internal [`Dataset`] makes lazy initialization thread-safe.
///
/// # Example
/// ```no_run
/// use dataset_ml::FashionMnist;
///
/// // the loader creates the directory if it does not exist
/// let download_dir = "./fashion_mnist";
///
/// let mut dataset = FashionMnist::new(download_dir);
/// let table = dataset.data().unwrap();
///
/// assert_eq!(table.n_samples(), 60000);
/// assert_eq!(table.n_columns(), 2);
///
/// // The `pixels` column holds one 784-byte row per image.
/// let pixels = table.column("pixels").unwrap().as_bytes().unwrap();
/// assert_eq!(pixels.shape(), &[60000, 784]);
///
/// // A (n_samples, 28, 28) view reads the same bytes, at no copy.
/// let images = pixels.view().into_shape_with_order((60000, 28, 28)).unwrap();
/// assert_eq!(images.shape(), &[60000, 28, 28]);
///
/// // Ask for the feature matrix when you want it. The pixels become `f64`.
/// let features = table.numeric_matrix(&FashionMnist::FEATURE_NAMES).unwrap();
/// assert_eq!(features.shape(), &[60000, 784]);
///
/// // Scale the pixels to [0, 1] for a model.
/// let scaled = features.mapv(|pixel| pixel / 255.0);
/// assert_eq!(scaled.shape(), &[60000, 784]);
///
/// // Name the class of the first image.
/// let classes = table.column(FashionMnist::TARGET).unwrap().as_integer().unwrap();
/// let name = FashionMnist::CLASS_NAMES[classes[0] as usize];
/// println!("the first image shows a {name}");
///
/// // `get_data_mut()` edits the table in place. This needs no clone and no
/// // reload. The change stays cached.
/// if let Some(table) = dataset.get_data_mut() {
/// if let Some(column) = table.column_mut("pixels") {
/// if let dataset_ml::ColumnData::Bytes(values) = column.data_mut() {
/// values[[0, 0]] = 255;
/// }
/// }
/// }
/// assert!(dataset.get_data().is_some());
///
/// // `take_data()` moves the owned table out with no clone. This leaves the
/// // instance reusable.
/// let owned = dataset.take_data().unwrap();
/// assert_eq!(owned.n_samples(), 60000);
///
/// // `into_data()` also returns the owned table with no clone, but it consumes
/// // the instance.
/// let owned = dataset.into_data().unwrap();
/// assert_eq!(owned.n_samples(), 60000);
/// ```
impl_ml_dataset!;