-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathImageRecognitionAndClassificationWithR
68 lines (57 loc) · 1.69 KB
/
ImageRecognitionAndClassificationWithR
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
# Load Packages
library(EBImage)
library(keras)
# To install EBimage package, you can run following 2 lines;
# install.packages("BiocManager")
# BiocManager::install("EBImage")
# Read images
setwd('---')
pics <- c('p1.jpg', 'p2.jpg', 'p3.jpg', 'p4.jpg', 'p5.jpg', 'p6.jpg',
'c1.jpg', 'c2.jpg', 'c3.jpg', 'c4.jpg', 'c5.jpg', 'c6.jpg')
mypic <- list()
for (i in 1:12) {mypic[[i]] <- readImage(pics[i])}
# Explore
print(mypic[[1]])
display(mypic[[8]])
summary(mypic[[1]])
hist(mypic[[2]])
str(mypic)
# Resize
for (i in 1:12) {mypic[[i]] <- resize(mypic[[i]], ---, ---)}
# Reshape
for (i in 1:12) {mypic[[i]] <- array_reshape(mypic[[i]], c(---, ---,---))}
# Row Bind
trainx <- NULL
for (i in 7:11) {trainx <- rbind(trainx, mypic[[i]])}
str(trainx)
testx <- ---(mypic[[6]], mypic[[12]])
trainy <- c(0,0,0,0,0,1,1,1,1,1 )
testy <- c(---, ---)
# One Hot Encoding
trainLabels <- ---(trainy)
testLabels <- ---(testy)
# Model
model <- keras_model_sequential()
model %>%
layer_dense(units = 256, activation = ---, input_shape = c(2352)) %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dense(units = 2, activation = ---)
summary(model)
# Compile
model %>%
compile(loss = ---,
optimizer = optimizer_rmsprop(),
metrics = c('accuracy'))
# Fit Model
history <- model %>%
fit(trainx,
---,
epochs = 30,
batch_size = 32,
validation_split = 0.2)
# Evaluation & Prediction - train data
model %>% evaluate(---, ---)
pred <- model %>% predict_classes(trainx)
table(Predicted = pred, Actual = trainy)
prob <- model %>% predict_proba(trainx)
cbind(prob, Prected = pred, Actual= trainy)