You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorialposts/2021-01-21-data-loader.md
+28-31Lines changed: 28 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,80 +3,77 @@ title = "Using Flux DataLoader"
3
3
published = "21 January 2021"
4
4
author = "Liliana Badillo, Dhairya Gandhi"
5
5
+++
6
-
6
+
7
7
In this tutorial, we show how to load image data in Flux DataLoader and process it in mini-batches. We use the [DataLoader](https://fluxml.ai/Flux.jl/stable/data/dataloader/#Flux.Data.DataLoader) type to handle iteration over mini-batches of data. For this example, we load the [MNIST dataset](https://juliaml.github.io/MLDatasets.jl/stable/datasets/MNIST/) using the [MLDatasets](https://juliaml.github.io/MLDatasets.jl/stable/) package.
8
-
8
+
9
9
Before we start, make sure you have installed the following packages:
To install these packages, run the following in the REPL:
15
-
15
+
16
16
```julia
17
17
Pkg.add("Flux")
18
18
Pkg.add("MLDatasets")
19
19
```
20
-
21
-
20
+
22
21
Load the packages we'll need:
23
-
22
+
24
23
```julia
25
24
using MLDatasets: MNIST
26
25
using Flux.Data: DataLoader
27
26
using Flux: onehotbatch
28
27
```
29
-
28
+
30
29
## Step1: Loading the MNIST data set
31
-
30
+
32
31
We load the MNIST train and test data from MLDatasets:
33
-
32
+
34
33
```julia
35
-
train_x, train_y = MNIST.traindata(Float32)
36
-
test_x, test_y = MNIST.testdata(Float32)
34
+
train_x, train_y =MNIST(:train)[:]
35
+
test_x, test_y =MNIST(:test)[:]
37
36
```
38
-
37
+
39
38
This code loads the MNIST train and test images as Float32 as well as their labels. The data set `train_x` is a 28×28×60000 multi-dimensional array. It contains 60000 elements and each one of it contains a 28x28 array. Each array represents a 28x28 image (in grayscale) of a handwritten digit. Moreover, each element of the 28x28 arrays is a pixel that represents the amount of light that it contains. On the other hand, `test_y` is a 60000 element vector and each element of this vector represents the label or actual value (0 to 9) of a handwritten digit.
40
-
39
+
41
40
## Step 2: Loading the dataset onto DataLoader
42
-
41
+
43
42
Before we load the data onto a DataLoader, we need to reshape it so that it has the correct shape for Flux. For this example, the MNIST train data must be of the same dimension as our model's input and output layers.
44
-
43
+
45
44
For example, if our model's input layer expects a 28x28x1 multi-dimensional array, we need to reshape the train and test data as follows:
46
-
45
+
47
46
```julia
48
47
train_x =reshape(train_x, 28, 28, 1, :)
49
48
test_x =reshape(test_x, 28, 28, 1, :)
50
49
```
51
-
50
+
52
51
Also, the MNIST labels must be encoded as a vector with the same dimension as the number of categories (unique handwritten digits) in the data set. To encode the labels, we use the [Flux's onehotbatch](https://fluxml.ai/Flux.jl/stable/data/onehot/#Batches-1) function:
Notice that we set the DataLoader `batchsize` to 128. This will enable us to iterate over the data in batches of size 128. Also, by setting `shuffle=true` the DataLoader will shuffle the observations each time that iterations are re-started.
67
-
66
+
68
67
## Step 3: Iterating over the data
69
-
70
-
Finally, we can iterate over the 60000 MNIST train data in mini-batches (most of them of size 128) using the Dataloader that we created in the previous step. Each element of the DataLoader is a tuple `(x, y)` in which `x` represents a 28x28x1 array and `y` a vector that encodes the corresponding label of the image.
71
-
68
+
69
+
Finally, we can iterate over the 60000 MNIST train data in mini-batches (most of them of size 128) using the Dataloader that we created in the previous step. Each element of the DataLoader is a tuple `(x, y)` in which `x` represents a 28x28x1 array and `y` a vector that encodes the corresponding label of the image.
Now, we can create a model and train it using the `data_loader` we just created. For more information on building models in Flux, see [Model-Building Basics](https://fluxml.ai/Flux.jl/stable/models/basics/#Model-Building-Basics-1).
0 commit comments