-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRESNET.R
More file actions
70 lines (53 loc) · 2.14 KB
/
RESNET.R
File metadata and controls
70 lines (53 loc) · 2.14 KB
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
#-------------------------------------------------------------------------
## Modelling ##
# Function to create a basic residual block
create_residual_block <- function(input_layer, filters, kernel_size, strides = c(1, 1)) {
x <- input_layer
# First convolutional layer
x <- layer_conv_2d(filters = filters, kernel_size = kernel_size, strides = strides, padding = 'same')(x)
x <- layer_batch_normalization()(x)
x <- layer_activation_relu()(x)
# Second convolutional layer
x <- layer_conv_2d(filters = filters, kernel_size = kernel_size, padding = 'same')(x)
x <- layer_batch_normalization()(x)
# Skip connection
if (strides[1] > 1) {
input_layer <- layer_conv_2d(filters = filters, kernel_size = c(1, 1), strides = strides, padding = 'same')(input_layer)
}
x <- layer_add()(list(x, input_layer))
x <- layer_activation_relu()(x)
return(x)
}
# Function to create ResNet model
create_resnet_model <- function(input_shape = c(224, 224, 3), num_classes = 1000) {
input_layer <- layer_input(shape = input_shape)
x <- layer_conv_2d(filters = 64, kernel_size = c(7, 7), strides = c(2, 2), padding = 'same')(input_layer)
x <- layer_batch_normalization()(x)
x <- layer_activation_relu()(x)
x <- layer_max_pooling_2d(pool_size = c(3, 3), strides = c(2, 2), padding = 'same')(x)
# Adding residual blocks (adjust the number as needed)
num_blocks <- 3
for (i in 1:num_blocks) {
x <- create_residual_block(x, filters = 64, kernel_size = c(3, 3))
}
x <- layer_global_average_pooling_2d()(x)
output <- layer_dense(units = 1, activation = 'sigmoid')(x)
modell <- keras_model(inputs = input_layer, outputs = output)
return(modell)
}
# Create ResNet model
model <- create_resnet_model(input_shape = c(224, 224, 3), num_classes = 1000)
# Compile the model
model %>% compile(
optimizer = 'adam',
loss = 'binary_crossentropy',
metrics = c('accuracy')
)
# Train the model
history <- model %>% fit(
train_data,
steps_per_epoch = 163, # Adjust based on the number of training samples
epochs = 5,
validation_data = test_data,
validation_steps = 19 # Adjust based on the number of test samples
)