Skip to content

Commit e1a53a4

Browse files
authored
Create ImageRecognitionAndClassificationWithR
1 parent 88ed22f commit e1a53a4

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Load Packages
2+
library(EBImage)
3+
library(keras)
4+
5+
# Read images
6+
setwd('---')
7+
pics <- c('p1.jpg', 'p2.jpg', 'p3.jpg', 'p4.jpg', 'p5.jpg', 'p6.jpg',
8+
'c1.jpg', 'c2.jpg', 'c3.jpg', 'c4.jpg', 'c5.jpg', 'c6.jpg')
9+
mypic <- list()
10+
for (i in 1:12) {mypic[[i]] <- readImage(pics[i])}
11+
12+
# Explore
13+
print(mypic[[1]])
14+
display(mypic[[8]])
15+
summary(mypic[[1]])
16+
hist(mypic[[2]])
17+
str(mypic)
18+
19+
# Resize
20+
for (i in 1:12) {mypic[[i]] <- resize(mypic[[i]], ---, ---)}
21+
22+
# Reshape
23+
for (i in 1:12) {mypic[[i]] <- array_reshape(mypic[[i]], c(---, ---,---))}
24+
25+
# Row Bind
26+
trainx <- NULL
27+
for (i in 7:11) {trainx <- rbind(trainx, mypic[[i]])}
28+
str(trainx)
29+
testx <- ---(mypic[[6]], mypic[[12]])
30+
trainy <- c(0,0,0,0,0,1,1,1,1,1 )
31+
testy <- c(---, ---)
32+
33+
# One Hot Encoding
34+
trainLabels <- ---(trainy)
35+
testLabels <- ---(testy)
36+
37+
# Model
38+
model <- keras_model_sequential()
39+
model %>%
40+
layer_dense(units = 256, activation = ---, input_shape = c(2352)) %>%
41+
layer_dense(units = 128, activation = 'relu') %>%
42+
layer_dense(units = 2, activation = ---)
43+
summary(model)
44+
45+
# Compile
46+
model %>%
47+
compile(loss = ---,
48+
optimizer = optimizer_rmsprop(),
49+
metrics = c('accuracy'))
50+
51+
# Fit Model
52+
history <- model %>%
53+
fit(trainx,
54+
---,
55+
epochs = 30,
56+
batch_size = 32,
57+
validation_split = 0.2)
58+
59+
# Evaluation & Prediction - train data
60+
model %>% evaluate(---, ---)
61+
pred <- model %>% predict_classes(trainx)
62+
table(Predicted = pred, Actual = trainy)
63+
prob <- model %>% predict_proba(trainx)
64+
cbind(prob, Prected = pred, Actual= trainy)

0 commit comments

Comments
 (0)