vignettes/famous_architectures.Rmd
famous_architectures.Rmd
Examples of some famous architectures.
# Creates a network that illustrates depthwise separable convolution
depthwise_separable <- local({
input <-
layer_input(
shape = c(3, 64, 64),
dtype = 'float32',
name = 'input'
)
conv_1x1 <- input %>%
layer_conv_2d(8, kernel_size = c(1, 1), name = "1x1_convolution")
conv_1 <- conv_1x1 %>%
layer_conv_2d(8, kernel_size = c(3, 3), name = "3x3_convolution_1")
conv_2 <- conv_1x1 %>%
layer_conv_2d(8, kernel_size = c(3, 3), name = "3x3_convolution_2")
conv_3 <- conv_1x1 %>%
layer_conv_2d(8, kernel_size = c(3, 3), name = "3x3_convolution_3")
output <- layer_concatenate(
c(conv_1, conv_2, conv_3),
name = "concat"
)
keras_model(
inputs = c(input),
outputs = c(output)
)
})
depthwise_separable
#> Model
#> ___________________________________________________________________________
#> Layer (type) Output Shape Param # Connected to
#> ===========================================================================
#> input (InputLayer) (None, 3, 64, 64 0
#> ___________________________________________________________________________
#> 1x1_convolution (Conv2D (None, 3, 64, 8) 520 input[0][0]
#> ___________________________________________________________________________
#> 3x3_convolution_1 (Conv (None, 1, 62, 8) 584 1x1_convolution[0][0]
#> ___________________________________________________________________________
#> 3x3_convolution_2 (Conv (None, 1, 62, 8) 584 1x1_convolution[0][0]
#> ___________________________________________________________________________
#> 3x3_convolution_3 (Conv (None, 1, 62, 8) 584 1x1_convolution[0][0]
#> ___________________________________________________________________________
#> concat (Concatenate) (None, 1, 62, 24 0 3x3_convolution_1[0][0]
#> 3x3_convolution_2[0][0]
#> 3x3_convolution_3[0][0]
#> ===========================================================================
#> Total params: 2,272
#> Trainable params: 2,272
#> Non-trainable params: 0
#> ___________________________________________________________________________
depthwise_separable %>% plot_model()
# Creates a network that illustrates a module of the resnet network
# references: https://arxiv.org/pdf/1610.02357.pdf
resnet <- local({
input <- layer_input(shape = c(3, 64, 64), dtype = 'float32')
stream_1 <- input %>%
layer_conv_2d(1, kernel_size = c(3, 3), padding = "same", activation = "relu") %>%
layer_conv_2d(1, kernel_size = c(3, 3), padding = "same", activation = "relu")
output <- layer_add(c(input, stream_1))
keras_model(inputs = c(input),
outputs = c(output))
})
resnet
#> Model
#> ___________________________________________________________________________
#> Layer (type) Output Shape Param # Connected to
#> ===========================================================================
#> input_1 (InputLayer) (None, 3, 64, 64 0
#> ___________________________________________________________________________
#> conv2d_1 (Conv2D) (None, 3, 64, 1) 577 input_1[0][0]
#> ___________________________________________________________________________
#> conv2d_2 (Conv2D) (None, 3, 64, 1) 10 conv2d_1[0][0]
#> ___________________________________________________________________________
#> add_1 (Add) (None, 3, 64, 64 0 input_1[0][0]
#> conv2d_2[0][0]
#> ===========================================================================
#> Total params: 587
#> Trainable params: 587
#> Non-trainable params: 0
#> ___________________________________________________________________________
resnet %>% plot_model()
# Creates a network that illustrates the inception v3 network
# references: https://arxiv.org/pdf/1610.02357.pdf
inception_v3 <- local({
input <- layer_input(shape = c(3, 64, 64), dtype = 'float32')
stream_1 <- input %>%
layer_conv_2d(1, kernel_size = c(1, 1), filters = 3)
stream_2 <- input %>%
layer_conv_2d(1, kernel_size = c(1, 1)) %>%
layer_conv_2d(1, kernel_size = c(3, 3), padding = "same")
stream_3 <- input %>%
layer_average_pooling_2d(pool_size = c(1, 1)) %>%
layer_conv_2d(8, kernel_size = c(3, 3), padding = "same")
stream_4 <- input %>%
layer_conv_2d(8, kernel_size = c(1, 1)) %>%
layer_conv_2d(8, kernel_size = c(3, 3), padding = "same") %>%
layer_conv_2d(8, kernel_size = c(3, 3), padding = "same")
output <- layer_concatenate(
c(stream_1, stream_2, stream_3, stream_4),
name = "concat"
)
keras_model(inputs = c(input),
outputs = c(output))
})
inception_v3
#> Model
#> ___________________________________________________________________________
#> Layer (type) Output Shape Param # Connected to
#> ===========================================================================
#> input_2 (InputLayer) (None, 3, 64, 64 0
#> ___________________________________________________________________________
#> conv2d_7 (Conv2D) (None, 3, 64, 8) 520 input_2[0][0]
#> ___________________________________________________________________________
#> conv2d_4 (Conv2D) (None, 3, 64, 1) 65 input_2[0][0]
#> ___________________________________________________________________________
#> average_pooling2d_1 (Av (None, 3, 64, 64 0 input_2[0][0]
#> ___________________________________________________________________________
#> conv2d_8 (Conv2D) (None, 3, 64, 8) 584 conv2d_7[0][0]
#> ___________________________________________________________________________
#> conv2d_3 (Conv2D) (None, 3, 64, 3) 195 input_2[0][0]
#> ___________________________________________________________________________
#> conv2d_5 (Conv2D) (None, 3, 64, 1) 10 conv2d_4[0][0]
#> ___________________________________________________________________________
#> conv2d_6 (Conv2D) (None, 3, 64, 8) 4616 average_pooling2d_1[0][0]
#> ___________________________________________________________________________
#> conv2d_9 (Conv2D) (None, 3, 64, 8) 584 conv2d_8[0][0]
#> ___________________________________________________________________________
#> concat (Concatenate) (None, 3, 64, 20 0 conv2d_3[0][0]
#> conv2d_5[0][0]
#> conv2d_6[0][0]
#> conv2d_9[0][0]
#> ===========================================================================
#> Total params: 6,574
#> Trainable params: 6,574
#> Non-trainable params: 0
#> ___________________________________________________________________________
inception_v3 %>% plot_model()