keras-hub-nightly 0.16.1.dev202410020340__py3-none-any.whl → 0.19.0.dev202501260345__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- keras_hub/api/layers/__init__.py +21 -3
- keras_hub/api/models/__init__.py +71 -12
- keras_hub/api/tokenizers/__init__.py +1 -1
- keras_hub/src/bounding_box/__init__.py +2 -0
- keras_hub/src/bounding_box/converters.py +102 -12
- keras_hub/src/layers/modeling/f_net_encoder.py +1 -1
- keras_hub/src/layers/modeling/masked_lm_head.py +2 -1
- keras_hub/src/layers/modeling/reversible_embedding.py +3 -16
- keras_hub/src/layers/modeling/rms_normalization.py +36 -0
- keras_hub/src/layers/modeling/rotary_embedding.py +3 -2
- keras_hub/src/layers/modeling/token_and_position_embedding.py +1 -1
- keras_hub/src/layers/modeling/transformer_decoder.py +8 -6
- keras_hub/src/layers/modeling/transformer_encoder.py +29 -7
- keras_hub/src/layers/preprocessing/audio_converter.py +3 -7
- keras_hub/src/layers/preprocessing/image_converter.py +170 -34
- keras_hub/src/metrics/bleu.py +4 -3
- keras_hub/src/models/albert/albert_presets.py +4 -12
- keras_hub/src/models/albert/albert_text_classifier.py +7 -7
- keras_hub/src/models/backbone.py +3 -14
- keras_hub/src/models/bart/bart_backbone.py +4 -4
- keras_hub/src/models/bart/bart_presets.py +3 -9
- keras_hub/src/models/bart/bart_seq_2_seq_lm.py +9 -8
- keras_hub/src/models/basnet/__init__.py +5 -0
- keras_hub/src/models/basnet/basnet.py +122 -0
- keras_hub/src/models/basnet/basnet_backbone.py +366 -0
- keras_hub/src/models/basnet/basnet_image_converter.py +8 -0
- keras_hub/src/models/basnet/basnet_preprocessor.py +14 -0
- keras_hub/src/models/basnet/basnet_presets.py +17 -0
- keras_hub/src/models/bert/bert_presets.py +14 -32
- keras_hub/src/models/bert/bert_text_classifier.py +3 -3
- keras_hub/src/models/bloom/bloom_presets.py +8 -24
- keras_hub/src/models/causal_lm.py +56 -12
- keras_hub/src/models/clip/__init__.py +5 -0
- keras_hub/src/models/clip/clip_backbone.py +286 -0
- keras_hub/src/models/clip/clip_encoder_block.py +19 -4
- keras_hub/src/models/clip/clip_image_converter.py +8 -0
- keras_hub/src/models/clip/clip_presets.py +93 -0
- keras_hub/src/models/clip/clip_text_encoder.py +4 -1
- keras_hub/src/models/clip/clip_tokenizer.py +18 -3
- keras_hub/src/models/clip/clip_vision_embedding.py +101 -0
- keras_hub/src/models/clip/clip_vision_encoder.py +159 -0
- keras_hub/src/models/csp_darknet/csp_darknet_backbone.py +2 -1
- keras_hub/src/models/csp_darknet/csp_darknet_image_classifier.py +0 -109
- keras_hub/src/models/deberta_v3/deberta_v3_backbone.py +1 -1
- keras_hub/src/models/deberta_v3/deberta_v3_presets.py +5 -15
- keras_hub/src/models/deberta_v3/deberta_v3_text_classifier.py +4 -4
- keras_hub/src/models/deberta_v3/disentangled_attention_encoder.py +4 -4
- keras_hub/src/models/deberta_v3/disentangled_self_attention.py +3 -2
- keras_hub/src/models/deberta_v3/relative_embedding.py +1 -1
- keras_hub/src/models/deeplab_v3/__init__.py +7 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_backbone.py +200 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_image_converter.py +10 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_image_segmeter_preprocessor.py +16 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_layers.py +215 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_presets.py +17 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py +111 -0
- keras_hub/src/models/densenet/densenet_backbone.py +6 -4
- keras_hub/src/models/densenet/densenet_image_classifier.py +1 -129
- keras_hub/src/models/densenet/densenet_image_converter.py +2 -4
- keras_hub/src/models/densenet/densenet_presets.py +9 -15
- keras_hub/src/models/distil_bert/distil_bert_masked_lm.py +1 -1
- keras_hub/src/models/distil_bert/distil_bert_masked_lm_preprocessor.py +2 -2
- keras_hub/src/models/distil_bert/distil_bert_presets.py +5 -10
- keras_hub/src/models/distil_bert/distil_bert_text_classifier.py +5 -5
- keras_hub/src/models/distil_bert/distil_bert_tokenizer.py +3 -3
- keras_hub/src/models/efficientnet/__init__.py +9 -0
- keras_hub/src/models/efficientnet/cba.py +141 -0
- keras_hub/src/models/efficientnet/efficientnet_backbone.py +160 -61
- keras_hub/src/models/efficientnet/efficientnet_image_classifier.py +14 -0
- keras_hub/src/models/efficientnet/efficientnet_image_classifier_preprocessor.py +16 -0
- keras_hub/src/models/efficientnet/efficientnet_image_converter.py +10 -0
- keras_hub/src/models/efficientnet/efficientnet_presets.py +193 -0
- keras_hub/src/models/efficientnet/fusedmbconv.py +84 -41
- keras_hub/src/models/efficientnet/mbconv.py +53 -22
- keras_hub/src/models/electra/electra_backbone.py +2 -2
- keras_hub/src/models/electra/electra_presets.py +6 -18
- keras_hub/src/models/f_net/f_net_presets.py +2 -6
- keras_hub/src/models/f_net/f_net_text_classifier.py +3 -3
- keras_hub/src/models/f_net/f_net_text_classifier_preprocessor.py +3 -3
- keras_hub/src/models/falcon/falcon_backbone.py +5 -3
- keras_hub/src/models/falcon/falcon_causal_lm.py +18 -8
- keras_hub/src/models/falcon/falcon_presets.py +1 -3
- keras_hub/src/models/falcon/falcon_tokenizer.py +7 -2
- keras_hub/src/models/feature_pyramid_backbone.py +1 -1
- keras_hub/src/models/flux/__init__.py +5 -0
- keras_hub/src/models/flux/flux_layers.py +496 -0
- keras_hub/src/models/flux/flux_maths.py +225 -0
- keras_hub/src/models/flux/flux_model.py +236 -0
- keras_hub/src/models/flux/flux_presets.py +3 -0
- keras_hub/src/models/flux/flux_text_to_image.py +146 -0
- keras_hub/src/models/flux/flux_text_to_image_preprocessor.py +73 -0
- keras_hub/src/models/gemma/gemma_backbone.py +35 -20
- keras_hub/src/models/gemma/gemma_causal_lm.py +2 -2
- keras_hub/src/models/gemma/gemma_decoder_block.py +3 -1
- keras_hub/src/models/gemma/gemma_presets.py +29 -63
- keras_hub/src/models/gpt2/gpt2_causal_lm.py +2 -2
- keras_hub/src/models/gpt2/gpt2_presets.py +5 -14
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_attention.py +2 -1
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_causal_lm.py +3 -3
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_decoder.py +2 -1
- keras_hub/src/models/image_classifier.py +147 -2
- keras_hub/src/models/image_classifier_preprocessor.py +6 -3
- keras_hub/src/models/image_object_detector.py +87 -0
- keras_hub/src/models/image_object_detector_preprocessor.py +57 -0
- keras_hub/src/models/image_segmenter.py +0 -5
- keras_hub/src/models/image_segmenter_preprocessor.py +29 -4
- keras_hub/src/models/image_to_image.py +417 -0
- keras_hub/src/models/inpaint.py +520 -0
- keras_hub/src/models/llama/llama_backbone.py +138 -12
- keras_hub/src/models/llama/llama_causal_lm.py +3 -1
- keras_hub/src/models/llama/llama_presets.py +10 -20
- keras_hub/src/models/llama3/llama3_backbone.py +12 -11
- keras_hub/src/models/llama3/llama3_causal_lm.py +1 -1
- keras_hub/src/models/llama3/llama3_presets.py +4 -12
- keras_hub/src/models/llama3/llama3_tokenizer.py +25 -2
- keras_hub/src/models/mistral/mistral_backbone.py +16 -15
- keras_hub/src/models/mistral/mistral_causal_lm.py +6 -4
- keras_hub/src/models/mistral/mistral_presets.py +3 -9
- keras_hub/src/models/mistral/mistral_transformer_decoder.py +2 -1
- keras_hub/src/models/mit/__init__.py +6 -0
- keras_hub/src/models/{mix_transformer/mix_transformer_backbone.py → mit/mit_backbone.py} +47 -36
- keras_hub/src/models/mit/mit_image_classifier.py +12 -0
- keras_hub/src/models/mit/mit_image_classifier_preprocessor.py +12 -0
- keras_hub/src/models/mit/mit_image_converter.py +8 -0
- keras_hub/src/models/{mix_transformer/mix_transformer_layers.py → mit/mit_layers.py} +20 -13
- keras_hub/src/models/mit/mit_presets.py +139 -0
- keras_hub/src/models/mobilenet/mobilenet_backbone.py +8 -8
- keras_hub/src/models/mobilenet/mobilenet_image_classifier.py +0 -92
- keras_hub/src/models/opt/opt_causal_lm.py +2 -2
- keras_hub/src/models/opt/opt_presets.py +4 -12
- keras_hub/src/models/pali_gemma/pali_gemma_backbone.py +63 -17
- keras_hub/src/models/pali_gemma/pali_gemma_causal_lm.py +3 -1
- keras_hub/src/models/pali_gemma/pali_gemma_decoder_block.py +21 -23
- keras_hub/src/models/pali_gemma/pali_gemma_image_converter.py +2 -4
- keras_hub/src/models/pali_gemma/pali_gemma_presets.py +173 -17
- keras_hub/src/models/pali_gemma/pali_gemma_vit.py +14 -26
- keras_hub/src/models/phi3/phi3_causal_lm.py +3 -1
- keras_hub/src/models/phi3/phi3_decoder.py +0 -1
- keras_hub/src/models/phi3/phi3_presets.py +2 -6
- keras_hub/src/models/phi3/phi3_rotary_embedding.py +1 -1
- keras_hub/src/models/preprocessor.py +25 -11
- keras_hub/src/models/resnet/resnet_backbone.py +3 -14
- keras_hub/src/models/resnet/resnet_image_classifier.py +0 -137
- keras_hub/src/models/resnet/resnet_image_converter.py +2 -4
- keras_hub/src/models/resnet/resnet_presets.py +127 -18
- keras_hub/src/models/retinanet/__init__.py +5 -0
- keras_hub/src/models/retinanet/anchor_generator.py +52 -53
- keras_hub/src/models/retinanet/feature_pyramid.py +103 -39
- keras_hub/src/models/retinanet/non_max_supression.py +1 -0
- keras_hub/src/models/retinanet/prediction_head.py +192 -0
- keras_hub/src/models/retinanet/retinanet_backbone.py +146 -0
- keras_hub/src/models/retinanet/retinanet_image_converter.py +53 -0
- keras_hub/src/models/retinanet/retinanet_label_encoder.py +49 -51
- keras_hub/src/models/retinanet/retinanet_object_detector.py +381 -0
- keras_hub/src/models/retinanet/retinanet_object_detector_preprocessor.py +14 -0
- keras_hub/src/models/retinanet/retinanet_presets.py +16 -0
- keras_hub/src/models/roberta/roberta_backbone.py +2 -2
- keras_hub/src/models/roberta/roberta_presets.py +6 -8
- keras_hub/src/models/roberta/roberta_text_classifier.py +3 -3
- keras_hub/src/models/sam/__init__.py +5 -0
- keras_hub/src/models/sam/sam_backbone.py +2 -3
- keras_hub/src/models/sam/sam_image_converter.py +2 -4
- keras_hub/src/models/sam/sam_image_segmenter.py +16 -16
- keras_hub/src/models/sam/sam_image_segmenter_preprocessor.py +11 -1
- keras_hub/src/models/sam/sam_layers.py +5 -3
- keras_hub/src/models/sam/sam_presets.py +3 -9
- keras_hub/src/models/sam/sam_prompt_encoder.py +4 -2
- keras_hub/src/models/sam/sam_transformer.py +5 -4
- keras_hub/src/models/segformer/__init__.py +8 -0
- keras_hub/src/models/segformer/segformer_backbone.py +167 -0
- keras_hub/src/models/segformer/segformer_image_converter.py +8 -0
- keras_hub/src/models/segformer/segformer_image_segmenter.py +184 -0
- keras_hub/src/models/segformer/segformer_image_segmenter_preprocessor.py +31 -0
- keras_hub/src/models/segformer/segformer_presets.py +136 -0
- keras_hub/src/models/seq_2_seq_lm_preprocessor.py +1 -1
- keras_hub/src/models/stable_diffusion_3/flow_match_euler_discrete_scheduler.py +8 -1
- keras_hub/src/models/stable_diffusion_3/mmdit.py +577 -190
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_backbone.py +189 -163
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_image_to_image.py +178 -0
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_inpaint.py +193 -0
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_presets.py +43 -7
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_text_to_image.py +25 -14
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_text_to_image_preprocessor.py +1 -1
- keras_hub/src/models/t5/t5_backbone.py +5 -4
- keras_hub/src/models/t5/t5_presets.py +47 -19
- keras_hub/src/models/task.py +47 -39
- keras_hub/src/models/text_classifier.py +2 -2
- keras_hub/src/models/text_to_image.py +106 -41
- keras_hub/src/models/vae/__init__.py +1 -0
- keras_hub/src/models/vae/vae_backbone.py +184 -0
- keras_hub/src/models/vae/vae_layers.py +739 -0
- keras_hub/src/models/vgg/__init__.py +5 -0
- keras_hub/src/models/vgg/vgg_backbone.py +4 -24
- keras_hub/src/models/vgg/vgg_image_classifier.py +139 -33
- keras_hub/src/models/vgg/vgg_image_classifier_preprocessor.py +12 -0
- keras_hub/src/models/vgg/vgg_image_converter.py +8 -0
- keras_hub/src/models/vgg/vgg_presets.py +48 -0
- keras_hub/src/models/vit/__init__.py +5 -0
- keras_hub/src/models/vit/vit_backbone.py +152 -0
- keras_hub/src/models/vit/vit_image_classifier.py +187 -0
- keras_hub/src/models/vit/vit_image_classifier_preprocessor.py +12 -0
- keras_hub/src/models/vit/vit_image_converter.py +73 -0
- keras_hub/src/models/vit/vit_layers.py +391 -0
- keras_hub/src/models/vit/vit_presets.py +126 -0
- keras_hub/src/models/vit_det/vit_det_backbone.py +6 -4
- keras_hub/src/models/vit_det/vit_layers.py +3 -3
- keras_hub/src/models/whisper/whisper_audio_converter.py +2 -4
- keras_hub/src/models/whisper/whisper_backbone.py +6 -5
- keras_hub/src/models/whisper/whisper_decoder.py +3 -5
- keras_hub/src/models/whisper/whisper_presets.py +10 -30
- keras_hub/src/models/xlm_roberta/xlm_roberta_masked_lm.py +1 -1
- keras_hub/src/models/xlm_roberta/xlm_roberta_masked_lm_preprocessor.py +2 -2
- keras_hub/src/models/xlm_roberta/xlm_roberta_presets.py +2 -6
- keras_hub/src/models/xlm_roberta/xlm_roberta_text_classifier.py +4 -4
- keras_hub/src/models/xlm_roberta/xlm_roberta_tokenizer.py +2 -1
- keras_hub/src/models/xlnet/relative_attention.py +20 -19
- keras_hub/src/models/xlnet/xlnet_backbone.py +2 -2
- keras_hub/src/models/xlnet/xlnet_content_and_query_embedding.py +3 -5
- keras_hub/src/models/xlnet/xlnet_encoder.py +7 -9
- keras_hub/src/samplers/contrastive_sampler.py +2 -3
- keras_hub/src/samplers/sampler.py +2 -1
- keras_hub/src/tests/test_case.py +41 -6
- keras_hub/src/tokenizers/byte_pair_tokenizer.py +7 -3
- keras_hub/src/tokenizers/byte_tokenizer.py +3 -10
- keras_hub/src/tokenizers/sentence_piece_tokenizer.py +2 -9
- keras_hub/src/tokenizers/sentence_piece_tokenizer_trainer.py +9 -11
- keras_hub/src/tokenizers/tokenizer.py +10 -13
- keras_hub/src/tokenizers/unicode_codepoint_tokenizer.py +9 -7
- keras_hub/src/tokenizers/word_piece_tokenizer_trainer.py +10 -3
- keras_hub/src/utils/keras_utils.py +2 -13
- keras_hub/src/utils/pipeline_model.py +3 -3
- keras_hub/src/utils/preset_utils.py +196 -144
- keras_hub/src/utils/tensor_utils.py +4 -4
- keras_hub/src/utils/timm/convert_densenet.py +6 -4
- keras_hub/src/utils/timm/convert_efficientnet.py +447 -0
- keras_hub/src/utils/timm/convert_resnet.py +1 -1
- keras_hub/src/utils/timm/convert_vgg.py +85 -0
- keras_hub/src/utils/timm/preset_loader.py +14 -9
- keras_hub/src/utils/transformers/convert_llama3.py +21 -5
- keras_hub/src/utils/transformers/convert_vit.py +150 -0
- keras_hub/src/utils/transformers/preset_loader.py +23 -0
- keras_hub/src/utils/transformers/safetensor_utils.py +4 -3
- keras_hub/src/version_utils.py +1 -1
- {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.19.0.dev202501260345.dist-info}/METADATA +86 -68
- keras_hub_nightly-0.19.0.dev202501260345.dist-info/RECORD +423 -0
- {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.19.0.dev202501260345.dist-info}/WHEEL +1 -1
- keras_hub/src/layers/preprocessing/resizing_image_converter.py +0 -138
- keras_hub/src/models/mix_transformer/__init__.py +0 -0
- keras_hub/src/models/mix_transformer/mix_transformer_classifier.py +0 -119
- keras_hub/src/models/stable_diffusion_3/vae_image_decoder.py +0 -320
- keras_hub_nightly-0.16.1.dev202410020340.dist-info/RECORD +0 -357
- {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.19.0.dev202501260345.dist-info}/top_level.txt +0 -0
@@ -3,6 +3,7 @@ import math
|
|
3
3
|
import keras
|
4
4
|
|
5
5
|
from keras_hub.src.api_export import keras_hub_export
|
6
|
+
from keras_hub.src.models.efficientnet.cba import CBABlock
|
6
7
|
from keras_hub.src.models.efficientnet.fusedmbconv import FusedMBConvBlock
|
7
8
|
from keras_hub.src.models.efficientnet.mbconv import MBConvBlock
|
8
9
|
from keras_hub.src.models.feature_pyramid_backbone import FeaturePyramidBackbone
|
@@ -26,15 +27,12 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
26
27
|
(https://arxiv.org/abs/2104.00298) (ICML 2021)
|
27
28
|
|
28
29
|
Args:
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
activation: activation function to use between each convolutional layer.
|
36
|
-
input_shape: optional shape tuple, it should have exactly 3 input
|
37
|
-
channels.
|
30
|
+
stackwise_width_coefficients: list[float], scaling coefficient
|
31
|
+
for network width. If single float, it is assumed that this value
|
32
|
+
applies to all stacks.
|
33
|
+
stackwise_depth_coefficients: list[float], scaling coefficient
|
34
|
+
for network depth. If single float, it is assumed that this value
|
35
|
+
applies to all stacks.
|
38
36
|
stackwise_kernel_sizes: list of ints, the kernel sizes used for each
|
39
37
|
conv block.
|
40
38
|
stackwise_num_repeats: list of ints, number of times to repeat each
|
@@ -54,8 +52,24 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
54
52
|
MBConvBlock, but instead of using a depthwise convolution and a 1x1
|
55
53
|
output convolution blocks fused blocks use a single 3x3 convolution
|
56
54
|
block.
|
55
|
+
stackwise_force_input_filters: list of ints, overrides
|
56
|
+
stackwise_input_filters if > 0. Primarily used to parameterize stem
|
57
|
+
filters (usually stackwise_input_filters[0]) differrently than stack
|
58
|
+
input filters.
|
59
|
+
stackwise_nores_option: list of bools, toggles if residiual connection
|
60
|
+
is not used. If False (default), the stack will use residual
|
61
|
+
connections, otherwise not.
|
62
|
+
dropout: float, dropout rate at skip connections. The default
|
63
|
+
value is set to 0.2.
|
64
|
+
depth_divisor: integer, a unit of network width. The default value is
|
65
|
+
set to 8.
|
57
66
|
min_depth: integer, minimum number of filters. Can be None and ignored
|
58
67
|
if use_depth_divisor_as_min_depth is set to True.
|
68
|
+
activation: activation function to use between each convolutional layer.
|
69
|
+
input_shape: optional shape tuple, it should have exactly 3 input
|
70
|
+
channels.
|
71
|
+
|
72
|
+
|
59
73
|
include_initial_padding: bool, whether to include initial zero padding
|
60
74
|
(as per v1).
|
61
75
|
use_depth_divisor_as_min_depth: bool, whether to use depth_divisor as
|
@@ -66,6 +80,8 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
66
80
|
stem_conv_padding: str, can be 'same' or 'valid'. Padding for the stem.
|
67
81
|
batch_norm_momentum: float, momentum for the moving average calcualtion
|
68
82
|
in the batch normalization layers.
|
83
|
+
batch_norm_epsilon: float, epsilon for batch norm calcualtions. Used
|
84
|
+
in denominator for calculations to prevent divide by 0 errors.
|
69
85
|
|
70
86
|
Example:
|
71
87
|
```python
|
@@ -90,8 +106,8 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
90
106
|
def __init__(
|
91
107
|
self,
|
92
108
|
*,
|
93
|
-
|
94
|
-
|
109
|
+
stackwise_width_coefficients=None,
|
110
|
+
stackwise_depth_coefficients=None,
|
95
111
|
stackwise_kernel_sizes,
|
96
112
|
stackwise_num_repeats,
|
97
113
|
stackwise_input_filters,
|
@@ -100,31 +116,51 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
100
116
|
stackwise_squeeze_and_excite_ratios,
|
101
117
|
stackwise_strides,
|
102
118
|
stackwise_block_types,
|
119
|
+
stackwise_force_input_filters=[0] * 7,
|
120
|
+
stackwise_nores_option=[False] * 7,
|
103
121
|
dropout=0.2,
|
104
122
|
depth_divisor=8,
|
105
123
|
min_depth=8,
|
106
124
|
input_shape=(None, None, 3),
|
125
|
+
data_format="channels_last",
|
107
126
|
activation="swish",
|
108
|
-
|
127
|
+
include_stem_padding=True,
|
109
128
|
use_depth_divisor_as_min_depth=False,
|
110
129
|
cap_round_filter_decrease=False,
|
111
|
-
stem_conv_padding="
|
130
|
+
stem_conv_padding="valid",
|
112
131
|
batch_norm_momentum=0.9,
|
132
|
+
batch_norm_epsilon=1e-5,
|
133
|
+
projection_activation=None,
|
134
|
+
num_features=1280,
|
113
135
|
**kwargs,
|
114
136
|
):
|
137
|
+
num_stacks = len(stackwise_kernel_sizes)
|
138
|
+
if "depth_coefficient" in kwargs:
|
139
|
+
depth_coefficient = kwargs.pop("depth_coefficient")
|
140
|
+
if not isinstance(depth_coefficient, (list, tuple)):
|
141
|
+
stackwise_depth_coefficients = [depth_coefficient] * num_stacks
|
142
|
+
else:
|
143
|
+
stackwise_depth_coefficients = depth_coefficient
|
144
|
+
if "width_coefficient" in kwargs:
|
145
|
+
width_coefficient = kwargs.pop("width_coefficient")
|
146
|
+
if not isinstance(width_coefficient, (list, tuple)):
|
147
|
+
stackwise_width_coefficients = [width_coefficient] * num_stacks
|
148
|
+
else:
|
149
|
+
stackwise_width_coefficients = width_coefficient
|
150
|
+
|
115
151
|
image_input = keras.layers.Input(shape=input_shape)
|
116
152
|
|
117
153
|
x = image_input # Intermediate result.
|
118
|
-
if
|
154
|
+
if include_stem_padding:
|
119
155
|
x = keras.layers.ZeroPadding2D(
|
120
|
-
padding=
|
156
|
+
padding=(1, 1),
|
121
157
|
name="stem_conv_pad",
|
122
158
|
)(x)
|
123
159
|
|
124
160
|
# Build stem
|
125
161
|
stem_filters = round_filters(
|
126
162
|
filters=stackwise_input_filters[0],
|
127
|
-
width_coefficient=
|
163
|
+
width_coefficient=stackwise_width_coefficients[0],
|
128
164
|
min_depth=min_depth,
|
129
165
|
depth_divisor=depth_divisor,
|
130
166
|
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
@@ -136,6 +172,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
136
172
|
kernel_size=3,
|
137
173
|
strides=2,
|
138
174
|
padding=stem_conv_padding,
|
175
|
+
data_format=data_format,
|
139
176
|
use_bias=False,
|
140
177
|
kernel_initializer=conv_kernel_initializer(),
|
141
178
|
name="stem_conv",
|
@@ -143,6 +180,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
143
180
|
|
144
181
|
x = keras.layers.BatchNormalization(
|
145
182
|
momentum=batch_norm_momentum,
|
183
|
+
epsilon=batch_norm_epsilon,
|
146
184
|
name="stem_bn",
|
147
185
|
)(x)
|
148
186
|
x = keras.layers.Activation(activation, name="stem_activation")(x)
|
@@ -154,15 +192,19 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
154
192
|
self._pyramid_outputs = {}
|
155
193
|
curr_pyramid_level = 1
|
156
194
|
|
157
|
-
for i in range(
|
195
|
+
for i in range(num_stacks):
|
158
196
|
num_repeats = stackwise_num_repeats[i]
|
159
197
|
input_filters = stackwise_input_filters[i]
|
160
198
|
output_filters = stackwise_output_filters[i]
|
199
|
+
force_input_filters = stackwise_force_input_filters[i]
|
200
|
+
nores = stackwise_nores_option[i]
|
201
|
+
stack_width_coefficient = stackwise_width_coefficients[i]
|
202
|
+
stack_depth_coefficient = stackwise_depth_coefficients[i]
|
161
203
|
|
162
204
|
# Update block input and output filters based on depth multiplier.
|
163
205
|
input_filters = round_filters(
|
164
206
|
filters=input_filters,
|
165
|
-
width_coefficient=
|
207
|
+
width_coefficient=stack_width_coefficient,
|
166
208
|
min_depth=min_depth,
|
167
209
|
depth_divisor=depth_divisor,
|
168
210
|
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
@@ -170,7 +212,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
170
212
|
)
|
171
213
|
output_filters = round_filters(
|
172
214
|
filters=output_filters,
|
173
|
-
width_coefficient=
|
215
|
+
width_coefficient=stack_width_coefficient,
|
174
216
|
min_depth=min_depth,
|
175
217
|
depth_divisor=depth_divisor,
|
176
218
|
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
@@ -179,7 +221,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
179
221
|
|
180
222
|
repeats = round_repeats(
|
181
223
|
repeats=num_repeats,
|
182
|
-
depth_coefficient=
|
224
|
+
depth_coefficient=stack_depth_coefficient,
|
183
225
|
)
|
184
226
|
strides = stackwise_strides[i]
|
185
227
|
squeeze_and_excite_ratio = stackwise_squeeze_and_excite_ratios[i]
|
@@ -195,6 +237,16 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
195
237
|
self._pyramid_outputs[f"P{curr_pyramid_level}"] = x
|
196
238
|
curr_pyramid_level += 1
|
197
239
|
|
240
|
+
if force_input_filters > 0:
|
241
|
+
input_filters = round_filters(
|
242
|
+
filters=force_input_filters,
|
243
|
+
width_coefficient=stack_width_coefficient,
|
244
|
+
min_depth=min_depth,
|
245
|
+
depth_divisor=depth_divisor,
|
246
|
+
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
247
|
+
cap_round_filter_decrease=cap_round_filter_decrease,
|
248
|
+
)
|
249
|
+
|
198
250
|
# 97 is the start of the lowercase alphabet.
|
199
251
|
letter_identifier = chr(j + 97)
|
200
252
|
stackwise_block_type = stackwise_block_types[i]
|
@@ -206,32 +258,50 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
206
258
|
filters_out=output_filters,
|
207
259
|
kernel_size=stackwise_kernel_sizes[i],
|
208
260
|
strides=strides,
|
261
|
+
data_format=data_format,
|
209
262
|
expand_ratio=stackwise_expansion_ratios[i],
|
210
263
|
se_ratio=squeeze_and_excite_ratio,
|
211
264
|
activation=activation,
|
265
|
+
projection_activation=projection_activation,
|
212
266
|
dropout=dropout * block_id / blocks,
|
267
|
+
batch_norm_epsilon=batch_norm_epsilon,
|
213
268
|
name=block_name,
|
214
269
|
)
|
215
270
|
else:
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
kernel_size
|
221
|
-
strides
|
222
|
-
|
223
|
-
activation
|
224
|
-
dropout
|
225
|
-
batch_norm_momentum
|
226
|
-
|
227
|
-
|
271
|
+
constructor = get_conv_constructor(stackwise_block_type)
|
272
|
+
block_kwargs = {
|
273
|
+
"input_filters": input_filters,
|
274
|
+
"output_filters": output_filters,
|
275
|
+
"kernel_size": stackwise_kernel_sizes[i],
|
276
|
+
"strides": strides,
|
277
|
+
"data_format": data_format,
|
278
|
+
"activation": activation,
|
279
|
+
"dropout": dropout * block_id / blocks,
|
280
|
+
"batch_norm_momentum": batch_norm_momentum,
|
281
|
+
"batch_norm_epsilon": batch_norm_epsilon,
|
282
|
+
"nores": nores,
|
283
|
+
"name": block_name,
|
284
|
+
}
|
285
|
+
|
286
|
+
if stackwise_block_type in ("fused", "unfused"):
|
287
|
+
block_kwargs["expand_ratio"] = (
|
288
|
+
stackwise_expansion_ratios[i]
|
289
|
+
)
|
290
|
+
block_kwargs["se_ratio"] = squeeze_and_excite_ratio
|
291
|
+
|
292
|
+
if stackwise_block_type == "fused":
|
293
|
+
block_kwargs["projection_activation"] = (
|
294
|
+
projection_activation
|
295
|
+
)
|
296
|
+
|
297
|
+
block = constructor(**block_kwargs)
|
228
298
|
x = block(x)
|
229
299
|
block_id += 1
|
230
300
|
|
231
301
|
# Build top
|
232
302
|
top_filters = round_filters(
|
233
|
-
filters=
|
234
|
-
width_coefficient=
|
303
|
+
filters=num_features,
|
304
|
+
width_coefficient=stackwise_width_coefficients[-1],
|
235
305
|
min_depth=min_depth,
|
236
306
|
depth_divisor=depth_divisor,
|
237
307
|
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
@@ -241,15 +311,16 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
241
311
|
x = keras.layers.Conv2D(
|
242
312
|
filters=top_filters,
|
243
313
|
kernel_size=1,
|
244
|
-
padding="same",
|
245
314
|
strides=1,
|
315
|
+
padding="same",
|
316
|
+
data_format="channels_last",
|
246
317
|
kernel_initializer=conv_kernel_initializer(),
|
247
318
|
use_bias=False,
|
248
319
|
name="top_conv",
|
249
|
-
data_format="channels_last",
|
250
320
|
)(x)
|
251
321
|
x = keras.layers.BatchNormalization(
|
252
322
|
momentum=batch_norm_momentum,
|
323
|
+
epsilon=batch_norm_epsilon,
|
253
324
|
name="top_bn",
|
254
325
|
)(x)
|
255
326
|
x = keras.layers.Activation(
|
@@ -263,11 +334,12 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
263
334
|
super().__init__(inputs=image_input, outputs=x, **kwargs)
|
264
335
|
|
265
336
|
# === Config ===
|
266
|
-
self.
|
267
|
-
self.
|
337
|
+
self.stackwise_width_coefficients = stackwise_width_coefficients
|
338
|
+
self.stackwise_depth_coefficients = stackwise_depth_coefficients
|
268
339
|
self.dropout = dropout
|
269
340
|
self.depth_divisor = depth_divisor
|
270
341
|
self.min_depth = min_depth
|
342
|
+
self.data_format = data_format
|
271
343
|
self.activation = activation
|
272
344
|
self.stackwise_kernel_sizes = stackwise_kernel_sizes
|
273
345
|
self.stackwise_num_repeats = stackwise_num_repeats
|
@@ -280,18 +352,25 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
280
352
|
self.stackwise_strides = stackwise_strides
|
281
353
|
self.stackwise_block_types = stackwise_block_types
|
282
354
|
|
283
|
-
self.
|
355
|
+
self.stackwise_force_input_filters = stackwise_force_input_filters
|
356
|
+
self.include_stem_padding = include_stem_padding
|
284
357
|
self.use_depth_divisor_as_min_depth = use_depth_divisor_as_min_depth
|
285
358
|
self.cap_round_filter_decrease = cap_round_filter_decrease
|
286
359
|
self.stem_conv_padding = stem_conv_padding
|
287
360
|
self.batch_norm_momentum = batch_norm_momentum
|
361
|
+
self.batch_norm_epsilon = batch_norm_epsilon
|
362
|
+
self.projection_activation = projection_activation
|
288
363
|
|
289
364
|
def get_config(self):
|
290
365
|
config = super().get_config()
|
291
366
|
config.update(
|
292
367
|
{
|
293
|
-
"
|
294
|
-
|
368
|
+
"stackwise_width_coefficients": (
|
369
|
+
self.stackwise_width_coefficients
|
370
|
+
),
|
371
|
+
"stackwise_depth_coefficients": (
|
372
|
+
self.stackwise_depth_coefficients
|
373
|
+
),
|
295
374
|
"dropout": self.dropout,
|
296
375
|
"depth_divisor": self.depth_divisor,
|
297
376
|
"min_depth": self.min_depth,
|
@@ -302,20 +381,29 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
302
381
|
"stackwise_input_filters": self.stackwise_input_filters,
|
303
382
|
"stackwise_output_filters": self.stackwise_output_filters,
|
304
383
|
"stackwise_expansion_ratios": self.stackwise_expansion_ratios,
|
305
|
-
"stackwise_squeeze_and_excite_ratios":
|
384
|
+
"stackwise_squeeze_and_excite_ratios": (
|
385
|
+
self.stackwise_squeeze_and_excite_ratios
|
386
|
+
),
|
306
387
|
"stackwise_strides": self.stackwise_strides,
|
307
388
|
"stackwise_block_types": self.stackwise_block_types,
|
308
|
-
"
|
309
|
-
|
389
|
+
"stackwise_force_input_filters": (
|
390
|
+
self.stackwise_force_input_filters
|
391
|
+
),
|
392
|
+
"include_stem_padding": self.include_stem_padding,
|
393
|
+
"use_depth_divisor_as_min_depth": (
|
394
|
+
self.use_depth_divisor_as_min_depth
|
395
|
+
),
|
310
396
|
"cap_round_filter_decrease": self.cap_round_filter_decrease,
|
311
397
|
"stem_conv_padding": self.stem_conv_padding,
|
312
398
|
"batch_norm_momentum": self.batch_norm_momentum,
|
399
|
+
"batch_norm_epsilon": self.batch_norm_epsilon,
|
400
|
+
"projection_activation": self.projection_activation,
|
313
401
|
}
|
314
402
|
)
|
315
403
|
return config
|
316
404
|
|
317
405
|
def _correct_pad_downsample(self, inputs, kernel_size):
|
318
|
-
"""Returns a tuple for zero-padding
|
406
|
+
"""Returns a tuple for zero-padding a 2D convolution with downsampling.
|
319
407
|
|
320
408
|
Args:
|
321
409
|
inputs: Input tensor.
|
@@ -346,10 +434,13 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
346
434
|
kernel_size=3,
|
347
435
|
strides=1,
|
348
436
|
activation="swish",
|
437
|
+
projection_activation=None,
|
349
438
|
expand_ratio=1,
|
350
439
|
se_ratio=0.0,
|
351
440
|
dropout=0.0,
|
441
|
+
batch_norm_epsilon=1e-5,
|
352
442
|
name="",
|
443
|
+
data_format="channels_last",
|
353
444
|
):
|
354
445
|
"""An inverted residual block.
|
355
446
|
|
@@ -359,9 +450,11 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
359
450
|
filters_out: integer, the number of output filters.
|
360
451
|
kernel_size: integer, the dimension of the convolution window.
|
361
452
|
strides: integer, the stride of the convolution.
|
362
|
-
activation: activation function to use between each convolutional
|
453
|
+
activation: activation function to use between each convolutional
|
454
|
+
layer.
|
363
455
|
expand_ratio: integer, scaling coefficient for the input filters.
|
364
|
-
se_ratio: float between 0 and 1, fraction to squeeze the input
|
456
|
+
se_ratio: float between 0 and 1, fraction to squeeze the input
|
457
|
+
filters.
|
365
458
|
dropout: float between 0 and 1, fraction of the input units to drop.
|
366
459
|
name: string, block label.
|
367
460
|
|
@@ -375,12 +468,14 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
375
468
|
kernel_size=1,
|
376
469
|
strides=1,
|
377
470
|
padding="same",
|
471
|
+
data_format=data_format,
|
378
472
|
use_bias=False,
|
379
473
|
kernel_initializer=conv_kernel_initializer(),
|
380
474
|
name=name + "expand_conv",
|
381
475
|
)(inputs)
|
382
476
|
x = keras.layers.BatchNormalization(
|
383
477
|
axis=3,
|
478
|
+
epsilon=batch_norm_epsilon,
|
384
479
|
name=name + "expand_bn",
|
385
480
|
)(x)
|
386
481
|
x = keras.layers.Activation(
|
@@ -390,25 +485,23 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
390
485
|
x = inputs
|
391
486
|
|
392
487
|
# Depthwise Convolution
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
conv_pad = "valid"
|
399
|
-
else:
|
400
|
-
conv_pad = "same"
|
401
|
-
|
488
|
+
padding_pixels = kernel_size // 2
|
489
|
+
x = keras.layers.ZeroPadding2D(
|
490
|
+
padding=(padding_pixels, padding_pixels),
|
491
|
+
name=name + "dwconv_pad",
|
492
|
+
)(x)
|
402
493
|
x = keras.layers.DepthwiseConv2D(
|
403
494
|
kernel_size=kernel_size,
|
404
495
|
strides=strides,
|
405
|
-
padding=
|
496
|
+
padding="valid",
|
497
|
+
data_format=data_format,
|
406
498
|
use_bias=False,
|
407
499
|
depthwise_initializer=conv_kernel_initializer(),
|
408
500
|
name=name + "dwconv",
|
409
501
|
)(x)
|
410
502
|
x = keras.layers.BatchNormalization(
|
411
503
|
axis=3,
|
504
|
+
epsilon=batch_norm_epsilon,
|
412
505
|
name=name + "dwconv_bn",
|
413
506
|
)(x)
|
414
507
|
x = keras.layers.Activation(
|
@@ -427,6 +520,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
427
520
|
filters_se,
|
428
521
|
1,
|
429
522
|
padding="same",
|
523
|
+
data_format=data_format,
|
430
524
|
activation=activation,
|
431
525
|
kernel_initializer=conv_kernel_initializer(),
|
432
526
|
name=name + "se_reduce",
|
@@ -435,6 +529,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
435
529
|
filters,
|
436
530
|
1,
|
437
531
|
padding="same",
|
532
|
+
data_format=data_format,
|
438
533
|
activation="sigmoid",
|
439
534
|
kernel_initializer=conv_kernel_initializer(),
|
440
535
|
name=name + "se_expand",
|
@@ -453,11 +548,13 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
453
548
|
)(x)
|
454
549
|
x = keras.layers.BatchNormalization(
|
455
550
|
axis=3,
|
551
|
+
epsilon=batch_norm_epsilon,
|
456
552
|
name=name + "project_bn",
|
457
553
|
)(x)
|
458
|
-
|
459
|
-
|
460
|
-
|
554
|
+
if projection_activation:
|
555
|
+
x = keras.layers.Activation(
|
556
|
+
projection_activation, name=name + "projection_activation"
|
557
|
+
)(x)
|
461
558
|
|
462
559
|
if strides == 1 and filters_in == filters_out:
|
463
560
|
if dropout > 0:
|
@@ -537,9 +634,11 @@ def get_conv_constructor(conv_type):
|
|
537
634
|
return MBConvBlock
|
538
635
|
elif conv_type == "fused":
|
539
636
|
return FusedMBConvBlock
|
637
|
+
elif conv_type == "cba":
|
638
|
+
return CBABlock
|
540
639
|
else:
|
541
640
|
raise ValueError(
|
542
641
|
"Expected `conv_type` to be "
|
543
|
-
"one of 'unfused', 'fused', but got "
|
642
|
+
"one of 'unfused', 'fused', 'cba', but got "
|
544
643
|
f"`conv_type={conv_type}`"
|
545
644
|
)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
from keras_hub.src.api_export import keras_hub_export
|
2
|
+
from keras_hub.src.models.efficientnet.efficientnet_backbone import (
|
3
|
+
EfficientNetBackbone,
|
4
|
+
)
|
5
|
+
from keras_hub.src.models.efficientnet.efficientnet_image_classifier_preprocessor import ( # noqa: E501
|
6
|
+
EfficientNetImageClassifierPreprocessor,
|
7
|
+
)
|
8
|
+
from keras_hub.src.models.image_classifier import ImageClassifier
|
9
|
+
|
10
|
+
|
11
|
+
@keras_hub_export("keras_hub.models.EfficientNetImageClassifier")
|
12
|
+
class EfficientNetImageClassifier(ImageClassifier):
|
13
|
+
backbone_cls = EfficientNetBackbone
|
14
|
+
preprocessor_cls = EfficientNetImageClassifierPreprocessor
|
@@ -0,0 +1,16 @@
|
|
1
|
+
from keras_hub.src.api_export import keras_hub_export
|
2
|
+
from keras_hub.src.models.efficientnet.efficientnet_backbone import (
|
3
|
+
EfficientNetBackbone,
|
4
|
+
)
|
5
|
+
from keras_hub.src.models.efficientnet.efficientnet_image_converter import (
|
6
|
+
EfficientNetImageConverter,
|
7
|
+
)
|
8
|
+
from keras_hub.src.models.image_classifier_preprocessor import (
|
9
|
+
ImageClassifierPreprocessor,
|
10
|
+
)
|
11
|
+
|
12
|
+
|
13
|
+
@keras_hub_export("keras_hub.models.EfficientNetImageClassifierPreprocessor")
|
14
|
+
class EfficientNetImageClassifierPreprocessor(ImageClassifierPreprocessor):
|
15
|
+
backbone_cls = EfficientNetBackbone
|
16
|
+
image_converter_cls = EfficientNetImageConverter
|
@@ -0,0 +1,10 @@
|
|
1
|
+
from keras_hub.src.api_export import keras_hub_export
|
2
|
+
from keras_hub.src.layers.preprocessing.image_converter import ImageConverter
|
3
|
+
from keras_hub.src.models.efficientnet.efficientnet_backbone import (
|
4
|
+
EfficientNetBackbone,
|
5
|
+
)
|
6
|
+
|
7
|
+
|
8
|
+
@keras_hub_export("keras_hub.layers.EfficientNetImageConverter")
|
9
|
+
class EfficientNetImageConverter(ImageConverter):
|
10
|
+
backbone_cls = EfficientNetBackbone
|