-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunglassesModelANN.R
52 lines (35 loc) · 1.5 KB
/
sunglassesModelANN.R
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
#ANN model for detecting whether the person is wearing sunglasses or not
use_session_with_seed(1)
early_stop <- callback_early_stopping(monitor = "val_loss",
patience = 20)
#(input nodes + output nodes)/2
sunglasses_hiddenLayerNodes = ((128*120)+2)/2
#rerandomize the training images & labels so as to ensure all the images fed
#to the model aren't in an ordered fashion
rand_test = sample(1:499, 499)
temp_train_images_ANN = train_images_ANN[rand_test,,]
temp_train_sunglasses_onehot = train_sunglasses_onehot[rand_test,]
#build the ANN sunglasses model
#setting up our layers
model <- keras_model_sequential(layers=list(
layer_flatten(input_shape = c(128,120)),
layer_dense(units=sunglasses_hiddenLayerNodes, activation = 'relu'),
layer_dense(units = ncol(train_sunglasses_onehot),activation = 'softmax')))
model
compile(model,
optimizer ='adam',
loss='categorical_crossentropy',
metrics = 'accuracy')
#fitting the model
sunglassesHistory <- fit(model,
train_images_ANN, train_sunglasses_onehot,
validation_split = 0.2, batch_size=32,
early_stop=list(early_stop),
epochs = 200)
#uncomment below to plot the model history
#plot(emotionHistory, smooth = F)
#get the model score/accuracy against our testing subset
score <- evaluate(model,
test_images_ANN, test_sunglasses_onehot)
cat('Test loss:', score$loss, "\n")
cat('Test accuracy:', score$acc, "\n")