-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path90-keras-basic-classification.Rmd
More file actions
216 lines (183 loc) · 5.02 KB
/
90-keras-basic-classification.Rmd
File metadata and controls
216 lines (183 loc) · 5.02 KB
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
---
title: "Keras Example"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{bash}
pip3 install virtualenv
```
```{r}
library(keras)
install_keras()
```
```{r}
fashion_mnist <- dataset_fashion_mnist()
c(train_images, train_labels) %<-% fashion_mnist$train
c(test_images, test_labels) %<-% fashion_mnist$test
```
```{r}
class_names = c('T-shirt/top',
'Trouser',
'Pullover',
'Dress',
'Coat',
'Sandal',
'Shirt',
'Sneaker',
'Bag',
'Ankle boot')
```
```{r}
dim(train_images)
```
```{r}
dim(train_labels)
```
```{r}
train_labels[1:20]
```
```{r}
dim(test_images)
```
```{r}
dim(test_labels)
```
## Preprocess the data
```{r}
library(tidyr)
library(ggplot2)
image_1 <- as.data.frame(train_images[1, , ])
colnames(image_1) <- seq_len(ncol(image_1))
image_1$y <- seq_len(nrow(image_1))
image_1 <- gather(image_1, "x", "value", -y)
image_1$x <- as.integer(image_1$x)
ggplot(image_1, aes(x = x, y = y, fill = value)) +
geom_tile() +
scale_fill_gradient(low = "white", high = "black", na.value = NA) +
scale_y_reverse() +
theme_minimal() +
theme(panel.grid = element_blank()) +
theme(aspect.ratio = 1) +
xlab("") +
ylab("")
```
```{r}
train_images <- train_images / 255
test_images <- test_images / 255
```
```{r}
par(mfcol=c(5,5))
par(mar=c(0, 0, 1.5, 0), xaxs='i', yaxs='i')
for (i in 1:25) {
img <- train_images[i, , ]
img <- t(apply(img, 2, rev))
image(1:28, 1:28, img, col = gray((0:255)/255), xaxt = 'n', yaxt = 'n',
main = paste(class_names[train_labels[i] + 1]))
}
```
## Train the model
### Setup the layers
```{r}
model <- keras_model_sequential()
model %>%
layer_flatten(input_shape = c(28, 28)) %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dense(units = 10, activation = 'softmax')
```
### Compile the model
```{r}
model %>% compile(
optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = c('accuracy')
)
```
### Train the model
```{r}
model %>% fit(train_images, train_labels, epochs = 5)
```
```{r}
score <- model %>% evaluate(test_images, test_labels)
cat('Test loss:', score$loss, "\n")
cat('Test accuracy:', score$acc, "\n")
```
### Make predictions
```{r}
predictions <- model %>% predict(test_images)
```
Here, the model has predicted the label for each image in the testing set. Let’s take a look at the first prediction:
```{r}
predictions[1, ]
```
[1] 1.247006e-06 8.371295e-08 1.987903e-07 1.078801e-06 2.053094e-07 1.419330e-02 9.533844e-06
[8] 5.745503e-02 2.694531e-05 9.283124e-01
A prediction is an array of 10 numbers. These describe the “confidence” of the model that the image corresponds to each of the 10 different articles of clothing. We can see which label has the highest confidence value:
```{r}
which.max(predictions[1, ])
```
[1] 10
Alternatively, we can also directly get the class prediction:
```{r}
class_pred <- model %>% predict_classes(test_images)
class_pred[1:20]
```
[1] 9 2 1 1 6 1 4 6 5 7 4 5 5 3 4 1 2 2 8 0
As the labels are 0-based, this actually means a predicted label of 9 (to be found in class_names[9]). So the model is most confident that this image is an ankle boot. And we can check the test label to see this is correct:
```{r}
test_labels[1]
```
[1] 9
Let’s plot several images with their predictions. Correct prediction labels are green and incorrect prediction labels are red.
```{r}
par(mfcol=c(5,5))
par(mar=c(0, 0, 1.5, 0), xaxs='i', yaxs='i')
for (i in 1:25) {
img <- test_images[i, , ]
img <- t(apply(img, 2, rev))
# subtract 1 as labels go from 0 to 9
predicted_label <- which.max(predictions[i, ]) - 1
true_label <- test_labels[i]
if (predicted_label == true_label) {
color <- '#008800'
} else {
color <- '#bb0000'
}
image(1:28, 1:28, img, col = gray((0:255)/255), xaxt = 'n', yaxt = 'n',
main = paste0(class_names[predicted_label + 1], " (",
class_names[true_label + 1], ")"),
col.main = color)
}
```
Finally, use the trained model to make a prediction about a single image.
```{r}
# Grab an image from the test dataset
# take care to keep the batch dimension, as this is expected by the model
img <- test_images[1, , , drop = FALSE]
dim(img)
```
[1] 1 28 28
Now predict the image:
```{r}
predictions <- model %>% predict(img)
predictions
```
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1.247006e-06 8.371312e-08 1.987903e-07 1.0788e-06 2.053096e-07 0.01419331 9.533853e-06
[,8] [,9] [,10]
[1,] 0.05745504 2.694531e-05 0.9283124
predict returns a list of lists, one for each image in the batch of data. Grab the predictions for our (only) image in the batch:
```{r}
# subtract 1 as labels are 0-based
prediction <- predictions[1, ] - 1
which.max(prediction)
```
[1] 10
Or, directly getting the class prediction again:
```{r}
class_pred <- model %>% predict_classes(img)
class_pred
```
[1] 9
And, as before, the model predicts a label of 9.