Skip to content

Commit a14d9b5

Browse files
committed
add application_efficientnet_b{0:7}()
1 parent 74a0f0c commit a14d9b5

File tree

5 files changed

+368
-0
lines changed

5 files changed

+368
-0
lines changed

NAMESPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ export(application_densenet)
9393
export(application_densenet121)
9494
export(application_densenet169)
9595
export(application_densenet201)
96+
export(application_efficientnet_b0)
97+
export(application_efficientnet_b1)
98+
export(application_efficientnet_b2)
99+
export(application_efficientnet_b3)
100+
export(application_efficientnet_b4)
101+
export(application_efficientnet_b5)
102+
export(application_efficientnet_b6)
103+
export(application_efficientnet_b7)
96104
export(application_inception_resnet_v2)
97105
export(application_inception_v3)
98106
export(application_mobilenet)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
- New vignette: "Transfer learning and fine-tuning".
2828

29+
- New `application_efficientnet_b{1,2,3,4,5,6,7}()`.
30+
2931
- New function `%<-active%`, a ergonomic wrapper around `makeActiveBinding()`
3032
for constructing Python `@property` decorated methods in `%py_class%`.
3133

R/applications.R

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,178 @@ application_nasnetmobile <- function(input_shape = NULL, include_top = TRUE, wei
692692

693693
}
694694

695+
#' Instantiates the EfficientNetB0 architecture
696+
#'
697+
#' @details
698+
#' Reference:
699+
#' - [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](
700+
#' https://arxiv.org/abs/1905.11946) (ICML 2019)
701+
#'
702+
#' This function returns a Keras image classification model,
703+
#' optionally loaded with weights pre-trained on ImageNet.
704+
#'
705+
#' For image classification use cases, see
706+
#' [this page for detailed examples](
707+
#' https://keras.io/api/applications/#usage-examples-for-image-classification-models).
708+
#'
709+
#' For transfer learning use cases, make sure to read the
710+
#' [guide to transfer learning & fine-tuning](
711+
#' https://keras.io/guides/transfer_learning/).
712+
#'
713+
#' EfficientNet models expect their inputs to be float tensors of pixels with values in the [0-255] range.
714+
#'
715+
#' @note
716+
#' Each Keras Application typically expects a specific kind of input preprocessing.
717+
#' For EfficientNet, input preprocessing is included as part of the model
718+
#' (as a `Rescaling` layer), and thus a calling a preprocessing function is not necessary.
719+
#'
720+
#'
721+
#' @param include_top Whether to include the fully-connected
722+
#' layer at the top of the network. Defaults to TRUE.
723+
#'
724+
#' @param weights One of `NULL` (random initialization),
725+
#' `'imagenet'` (pre-training on ImageNet),
726+
#' or the path to the weights file to be loaded. Defaults to `'imagenet'`.
727+
#'
728+
#' @param input_tensor Optional Keras tensor
729+
#' (i.e. output of `layer_input()`)
730+
#' to use as image input for the model.
731+
#'
732+
#' @param input_shape Optional shape list, only to be specified
733+
#' if `include_top` is FALSE.
734+
#' It should have exactly 3 inputs channels.
735+
#'
736+
#' @param pooling Optional pooling mode for feature extraction
737+
#' when `include_top` is `FALSE`. Defaults to `NULL`.
738+
#' - `NULL` means that the output of the model will be
739+
#' the 4D tensor output of the
740+
#' last convolutional layer.
741+
#' - `'avg'` means that global average pooling
742+
#' will be applied to the output of the
743+
#' last convolutional layer, and thus
744+
#' the output of the model will be a 2D tensor.
745+
#' - `'max'` means that global max pooling will
746+
#' be applied.
747+
#'
748+
#' @param classes Optional number of classes to classify images into, only to be
749+
#' specified if `include_top` is TRUE, and if no `weights` argument is
750+
#' specified. Defaults to 1000 (number of ImageNet classes).
751+
#'
752+
#' @param classifier_activation A string or callable. The activation function to
753+
#' use on the "top" layer. Ignored unless `include_top = TRUE`. Set
754+
#' `classifier_activation = NULL` to return the logits of the "top" layer.
755+
#' Defaults to `'softmax'`. When loading pretrained weights,
756+
#' `classifier_activation` can only be `NULL` or `"softmax"`.
757+
#'
758+
#' @param ... for forward compatibility.
759+
#'
760+
#' @seealso
761+
#' + <https://www.tensorflow.org/api_docs/python/tf/keras/applications/efficientnet/EfficientNetB0>
762+
#' + <https://keras.io/api/applications/>
763+
#' @export
764+
application_efficientnet_b0 <-
765+
function(include_top = TRUE, weights = "imagenet",
766+
input_tensor = NULL, input_shape = NULL,
767+
pooling = NULL, classes = 1000L,
768+
classifier_activation = "softmax",
769+
...)
770+
{
771+
args <- capture_args(match.call(), list(classes = as.integer, input_shape = normalize_shape))
772+
do.call(keras$applications$EfficientNetB0, args)
773+
}
774+
775+
#' @export
776+
#' @rdname application_efficientnet_b0
777+
application_efficientnet_b1 <-
778+
function(include_top = TRUE, weights = "imagenet",
779+
input_tensor = NULL, input_shape = NULL,
780+
pooling = NULL, classes = 1000L,
781+
classifier_activation = "softmax",
782+
...)
783+
{
784+
args <- capture_args(match.call(), list(classes = as.integer, input_shape = normalize_shape))
785+
do.call(keras$applications$EfficientNetB1, args)
786+
}
787+
788+
#' @export
789+
#' @rdname application_efficientnet_b0
790+
application_efficientnet_b2 <-
791+
function(include_top = TRUE, weights = "imagenet",
792+
input_tensor = NULL, input_shape = NULL,
793+
pooling = NULL, classes = 1000L,
794+
classifier_activation = "softmax",
795+
...)
796+
{
797+
args <- capture_args(match.call(), list(classes = as.integer, input_shape = normalize_shape))
798+
do.call(keras$applications$EfficientNetB2, args)
799+
}
800+
801+
#' @export
802+
#' @rdname application_efficientnet_b0
803+
application_efficientnet_b3 <-
804+
function(include_top = TRUE, weights = "imagenet",
805+
input_tensor = NULL, input_shape = NULL,
806+
pooling = NULL, classes = 1000L,
807+
classifier_activation = "softmax",
808+
...)
809+
{
810+
args <- capture_args(match.call(), list(classes = as.integer, input_shape = normalize_shape))
811+
do.call(keras$applications$EfficientNetB3, args)
812+
}
813+
814+
#' @export
815+
#' @rdname application_efficientnet_b0
816+
application_efficientnet_b4 <-
817+
function(include_top = TRUE, weights = "imagenet",
818+
input_tensor = NULL, input_shape = NULL,
819+
pooling = NULL, classes = 1000L,
820+
classifier_activation = "softmax",
821+
...)
822+
{
823+
args <- capture_args(match.call(), list(classes = as.integer, input_shape = normalize_shape))
824+
do.call(keras$applications$EfficientNetB4, args)
825+
}
826+
827+
#' @export
828+
#' @rdname application_efficientnet_b0
829+
application_efficientnet_b5 <-
830+
function(include_top = TRUE, weights = "imagenet",
831+
input_tensor = NULL, input_shape = NULL,
832+
pooling = NULL, classes = 1000L,
833+
classifier_activation = "softmax",
834+
...)
835+
{
836+
args <- capture_args(match.call(), list(classes = as.integer, input_shape = normalize_shape))
837+
do.call(keras$applications$EfficientNetB5, args)
838+
}
839+
840+
#' @export
841+
#' @rdname application_efficientnet_b0
842+
application_efficientnet_b6 <-
843+
function(include_top = TRUE, weights = "imagenet",
844+
input_tensor = NULL, input_shape = NULL,
845+
pooling = NULL, classes = 1000L,
846+
classifier_activation = "softmax",
847+
...)
848+
{
849+
args <- capture_args(match.call(), list(classes = as.integer, input_shape = normalize_shape))
850+
do.call(keras$applications$EfficientNetB6, args)
851+
}
852+
853+
#' @export
854+
#' @rdname application_efficientnet_b0
855+
application_efficientnet_b7 <-
856+
function(include_top = TRUE, weights = "imagenet",
857+
input_tensor = NULL, input_shape = NULL,
858+
pooling = NULL, classes = 1000L,
859+
classifier_activation = "softmax",
860+
...)
861+
{
862+
args <- capture_args(match.call(), list(classes = as.integer, input_shape = normalize_shape))
863+
do.call(keras$applications$EfficientNetB7, args)
864+
}
865+
866+
695867
#' @rdname application_nasnet
696868
#' @export
697869
nasnet_preprocess_input <- function(x) {

man/application_efficientnet_b0.Rd

Lines changed: 176 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-applications.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ test_succeeds("keras pre-built models can be instantiated", {
3232
application_vgg16()
3333
application_vgg19()
3434
application_inception_v3()
35+
36+
application_efficientnet_b0()
37+
application_efficientnet_b1()
38+
application_efficientnet_b2()
39+
application_efficientnet_b3()
40+
application_efficientnet_b4()
41+
application_efficientnet_b5()
42+
application_efficientnet_b6()
43+
application_efficientnet_b7()
44+
3545
})
3646

3747
test_succeeds("can use any input_shape", {

0 commit comments

Comments
 (0)