-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRscript.toyResNet.R
109 lines (93 loc) · 4.04 KB
/
Rscript.toyResNet.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
##Sys.setenv(LD_LIBRARY_PATH=paste("/usr/local/cuda-11.0/targets/x86_64-linux/lib/", Sys.getenv("LD_LIBRARY_PATH"),sep=":",collapse=":"))
library(keras);
library(tfdatasets);
library(tensorflow);
## dyn.load('/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudart.so.11.0');
## dyn.load('/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcublas.so.11');
## dyn.load('/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcublasLt.so.11');
## dyn.load('/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcusparse.so.11');
## dyn.load('/usr/local/cuda/lib64/libcudnn.so.8');
data.dir <- ".";
images <- list.files(data.dir, pattern = ".jpg", recursive = TRUE)
##length(images)
classes <- list.dirs(data.dir, full.names = FALSE, recursive = FALSE)
##set up the iterator to loop through the images in the data;
list_ds <- file_list_dataset(file_pattern = paste0(data.dir, "/*/*"))
list_ds %>% reticulate::as_iterator() %>% reticulate::iter_next()
##based upon the files and folders, need to pair the dataset and the labels;
get_label <- function(file_path) {
parts <- tf$strings$split(file_path, "/")
parts[-2] %>%
tf$equal(classes) %>%
tf$cast(dtype = tf$float64)
}
decode_img <- function(file_path, height = 227, width = 227) {
size <- as.integer(c(height, width))
file_path %>%
tf$io$read_file() %>%
tf$image$decode_jpeg(channels = 3) %>%
tf$image$convert_image_dtype(dtype = tf$float64) %>%
tf$image$resize(size = size)
}
preprocess_path <- function(file_path) {
list(
decode_img(file_path),
get_label(file_path)
)
}
labeled_ds <- list_ds %>%
dataset_map(.,preprocess_path)
labeled_ds %>%
reticulate::as_iterator() %>%
reticulate::iter_next()
prepare <- function(ds, batch_size, shuffle_buffer_size) {
if (shuffle_buffer_size > 0)
ds <- ds %>% dataset_shuffle(shuffle_buffer_size)
ds %>%
dataset_batch(batch_size) %>%
# `prefetch` lets the dataset fetch batches in the background while the model
# is training.
dataset_prefetch(buffer_size = tf$data$experimental$AUTOTUNE)
}
##alternative model definition:
get.ResNet.toy <- function(x) {
x1 <- layer_conv_2d(x,filters = 3, kernel_size = c(3,3), activation = "relu",
input_shape = c(227,227,3),stride=1,padding='same');
x1 <- layer_conv_2d(x1,filters = 3, kernel_size = c(3,3), activation = "relu",
input_shape = c(227,227,3),stride=1,padding='same');
##add layers;
x <- layer_add(list(x,x1));
x <- layer_conv_2d(x,filters = 96, kernel_size = c(11,11), activation = "relu",
input_shape = c(227,227,3),stride=4);
x <- layer_max_pooling_2d(x,pool_size = c(3,3),stride=2);
x <- layer_conv_2d(x,filters = 96, kernel_size = c(5,5), activation = "relu",
input_shape = c(27,27,96),stride=1);
x <- layer_max_pooling_2d(x,pool_size = c(3,3),stride=2);
x <- layer_conv_2d(x,filters = 384, kernel_size = c(3,3), activation = "relu",
input_shape = c(13,13,384),stride=1);
x <- layer_conv_2d(x,filters = 384, kernel_size = c(3,3), activation = "relu",
input_shape = c(13,13,384),stride=1);
x <- layer_conv_2d(x,filters = 256, kernel_size = c(3,3), activation = "relu",
input_shape = c(13,13,384),stride=1);
x <- layer_max_pooling_2d(x,pool_size = c(3,3),stride=2);
x <- layer_flatten(x);
x <- layer_dense(x,units = 9216, activation = "relu");
x <- layer_dense(x,units = 4096, activation = "relu");
x <- layer_dense(x,units = 4096, activation = "relu");
x <- layer_dense(x,units = 8, activation = "softmax");
return(x);
}
inputs <- layer_input(shape = c(227,227,3));
model.resnet <- keras_model(inputs=inputs, outputs=get.ResNet.toy(inputs));
summary(model.resnet)
model.resnet %>% compile(
optimizer = "adam",
loss = "categorical_crossentropy",
metrics = "accuracy"
)
model.resnet %>%
fit(
prepare(labeled_ds, batch_size = 32, shuffle_buffer_size = 500),
epochs = 5,
verbose = 2
)