-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathDeepNetworkWithMNIST
87 lines (73 loc) · 2.42 KB
/
DeepNetworkWithMNIST
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
#Youtube link https://www.youtube.com/watch?v=5bso_5X7Zu4&t=785s
# MNIST data
library(keras)
mnist <- dataset_mnist()
trainx <- mnist$train$x
trainy <- mnist$train$y
testx <- mnist$test$x
testy <- mnist$test$y
# Plot images
par(mfrow = c(3,3))
for (i in 1:9) plot(as.raster(trainx[i,,], max = 255))
par(mfrow= c(1,1))
# Five
a <- c(1, 12, 36, 48, 66, 101, 133, 139, 146)
par(mfrow = c(3,3))
for (i in a) plot(as.raster(trainx[i,,], max = 255))
par(mfrow= c(1,1))
# Reshape & rescale
trainx <- array_reshape(trainx, c(nrow(trainx), 784))
testx <- array_reshape(testx, c(nrow(testx), 784))
trainx <- trainx / 255
testx <- testx /255
# One hot encoding
trainy <- to_categorical(trainy, 10)
testy <- to_categorical(testy, 10)
# Model
model <- keras_model_sequential()
model %>%
layer_dense(units = 512, activation = 'relu', input_shape = c(784)) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units= 256, activation = 'relu') %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 10, activation = 'softmax')
# Compile
model %>%
compile(loss = 'categorical_crossentropy',
optimizer = optimizer_rmsprop(),
metrics = 'accuracy')
# Fit model
history <- model %>%
fit(trainx,
trainy,
epochs = 30,
batch_size = 32,
validation_split = 0.2)
# Evaluation and Prediction - Test data
model %>% evaluate(testx, testy)
pred <- model %>% predict_classes(testx)
table(Predicted = pred, Actual = mnist$test$y)
prob <- model %>% predict_proba(testx)
cbind(prob, Predicted_class = pred, Actual = mnist$test$y)[1:5,]
# New data
library(EBImage)
setwd("~/Desktop/numbers")
temp = list.files(pattern = "*.jpg")
mypic <- list()
for (i in 1:length(temp)) {mypic[[i]] <- readImage(temp[[i]])}
par(mfrow = c(3,2))
for (i in 1:length(temp)) plot(mypic[[i]])
par(mfrow = c(1,1))
for (i in 1:length(temp)) {colorMode(mypic[[i]]) <- Grayscale}
for (i in 1:length(temp)) {mypic[[i]] <- 1-mypic[[i]]}
for (i in 1:length(temp)) {mypic[[i]] <- resize(mypic[[i]], 28, 28)}
for (i in 1:length(temp)) {mypic[[i]] <- array_reshape(mypic[[i]], c(28,28,3))}
new <- NULL
for (i in 1:length(temp)) {new <- rbind(new, mypic[[i]])}
newx <- new[,1:784]
newy <- c(7, 5,2,0, 5, 3)
# Prediction
pred <- model %>% predict_classes(newx)
table(Predicted = pred, Actual = newy)
prob <- model %>% predict_proba(newx)
cbind(prob, Predicted = pred, Actual = newy)