-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN.R
140 lines (109 loc) · 3.58 KB
/
CNN.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
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
library(devtools)
library(tensorflow)
library(keras)
#Used to save the various train and test data samples into their
#base_dir <- "C:\\Users\\JoshPC\\Desktop\\food-101"
#train_dir <- file.path(base_dir, "train")
#test_dir <- file.path(base_dir, "test")
#val_dir <- file.path(base_dir, "val")
#dir.create(train_dir)
#dir.create(test_dir)
#dir.create(val_dir)
#directoryToImages <- "images//"
#food101Data <- list.files(path = directoryToImages,
#full.names = FALSE, recursive = TRUE)
#class_labels <- scan("meta//classes.txt", what="", sep="\n")
#data <- data.frame
#data <- food101Data
#require(caTools)
#set.seed(101)
#sample = sample.split(data, SplitRatio = .75)
#train = subset(data, sample == TRUE)
#test = subset(data, sample == FALSE)
#moving train set files into train dir
#for (i in train){
# new_dir <- paste0(train_dir, "/",i)
#old_dir <- paste0("images//",i)
# file.copy(file.path(old_dir),
# file.path(new_dir))
#}
#moving test set files into test dir
#for (i in test){
# new_dir <- paste0(test_dir, "/",i)
# old_dir <- paste0("images//",i)
# file.copy(file.path(old_dir),
# file.path(new_dir))
#}
#END
#Start of CNN creation and training
train_dir <- "FoodCNN/train/"
test_dir <- "FoodCNN/test/"
test_datagen <- image_data_generator(rescale = 1/255)
train_datagen = image_data_generator(
rescale = 1/255,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = TRUE,
fill_mode = "nearest"
)
validation_generator <- flow_images_from_directory(
test_dir,
test_datagen,
target_size = c(224, 224),
batch_size = 64,
class_mode = "categorical"
)
train_generator <- flow_images_from_directory(
train_dir, # Target directory
train_datagen, # Data generator
target_size = c(224, 224), # Resizes all images to 224 × 224
batch_size = 64,
class_mode = "categorical"
)
test_generator <- flow_images_from_directory(
test_dir,
test_datagen,
target_size = c(224, 224),
batch_size = 64,
class_mode = "categorical"
)
conv_base <- application_mobilenet_v2(
weights = "imagenet",
include_top = FALSE,
input_shape = c(224, 224, 3)
)
summary(conv_base)
#Add our top layers for our new data
model <- keras_model_sequential() %>%
conv_base %>%
layer_global_average_pooling_2d() %>%
layer_activation_relu() %>%
layer_dropout(0.7) %>%
layer_dense(101,kernel_regularizer = regularizer_l2(0.01)) %>%
layer_activation_softmax()
model %>% compile(
loss="categorical_crossentropy",
optimizer=optimizer_adam(lr=1e-4),
metrics = c("accuracy")
)
summary(model)
checkpoint_dir <- "FoodCNN/incpCheckpoints/"
checkpoint_name <- paste(checkpoint_dir, "food101_V2_-{val_loss:.4f}-{val_acc:.4f}.hdf5")
#for early stopping, model saving
my_callbacks <- list(callback_early_stopping(monitor = "val_acc", patience = 18, verbose = 1),
callback_model_checkpoint(checkpoint_name,monitor = "val_acc",verbose = 1, save_best_only = TRUE),
callback_reduce_lr_on_plateau(monitor = "val_loss",factor = 0.1,
patience = 10,min_lr = 0))
history <- model %>% fit_generator(
train_generator,
steps_per_epoch = length(train_generator),
epochs = 60,
validation_data = validation_generator,
validation_steps = length(validation_generator),
workers = 6,
callbacks = my_callbacks
)
history