keras-hub-nightly 0.16.1.dev202410200345__py3-none-any.whl → 0.19.0.dev202412070351__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 +12 -0
- keras_hub/api/models/__init__.py +32 -0
- keras_hub/src/bounding_box/__init__.py +2 -0
- keras_hub/src/bounding_box/converters.py +102 -12
- keras_hub/src/layers/modeling/rms_normalization.py +34 -0
- keras_hub/src/layers/modeling/transformer_encoder.py +27 -7
- keras_hub/src/layers/preprocessing/image_converter.py +5 -0
- keras_hub/src/models/albert/albert_presets.py +0 -8
- keras_hub/src/models/bart/bart_presets.py +0 -6
- keras_hub/src/models/bert/bert_presets.py +0 -20
- keras_hub/src/models/bloom/bloom_presets.py +0 -16
- 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/deberta_v3/deberta_v3_presets.py +0 -10
- keras_hub/src/models/deeplab_v3/deeplab_v3_presets.py +0 -2
- keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py +5 -3
- keras_hub/src/models/densenet/densenet_backbone.py +1 -1
- keras_hub/src/models/densenet/densenet_presets.py +0 -6
- keras_hub/src/models/distil_bert/distil_bert_presets.py +0 -6
- 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 +139 -56
- 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 +192 -0
- keras_hub/src/models/efficientnet/fusedmbconv.py +81 -36
- keras_hub/src/models/efficientnet/mbconv.py +52 -21
- keras_hub/src/models/electra/electra_presets.py +0 -12
- keras_hub/src/models/f_net/f_net_presets.py +0 -4
- keras_hub/src/models/falcon/falcon_presets.py +0 -2
- keras_hub/src/models/flux/__init__.py +5 -0
- keras_hub/src/models/flux/flux_layers.py +494 -0
- keras_hub/src/models/flux/flux_maths.py +218 -0
- keras_hub/src/models/flux/flux_model.py +231 -0
- keras_hub/src/models/flux/flux_presets.py +14 -0
- keras_hub/src/models/flux/flux_text_to_image.py +142 -0
- keras_hub/src/models/flux/flux_text_to_image_preprocessor.py +73 -0
- keras_hub/src/models/gemma/gemma_presets.py +0 -40
- keras_hub/src/models/gpt2/gpt2_presets.py +0 -9
- 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_to_image.py +16 -10
- keras_hub/src/models/inpaint.py +20 -13
- keras_hub/src/models/llama/llama_backbone.py +1 -1
- keras_hub/src/models/llama/llama_presets.py +5 -15
- keras_hub/src/models/llama3/llama3_presets.py +0 -8
- keras_hub/src/models/mistral/mistral_presets.py +0 -6
- keras_hub/src/models/mit/mit_backbone.py +41 -27
- keras_hub/src/models/mit/mit_layers.py +9 -7
- keras_hub/src/models/mit/mit_presets.py +12 -24
- keras_hub/src/models/opt/opt_presets.py +0 -8
- keras_hub/src/models/pali_gemma/pali_gemma_backbone.py +61 -11
- keras_hub/src/models/pali_gemma/pali_gemma_decoder_block.py +21 -23
- keras_hub/src/models/pali_gemma/pali_gemma_presets.py +166 -10
- keras_hub/src/models/pali_gemma/pali_gemma_vit.py +12 -11
- keras_hub/src/models/phi3/phi3_presets.py +0 -4
- keras_hub/src/models/resnet/resnet_presets.py +10 -42
- 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 +99 -36
- 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 +382 -0
- keras_hub/src/models/retinanet/retinanet_object_detector_preprocessor.py +14 -0
- keras_hub/src/models/retinanet/retinanet_presets.py +15 -0
- keras_hub/src/models/roberta/roberta_presets.py +0 -4
- keras_hub/src/models/sam/sam_backbone.py +0 -1
- keras_hub/src/models/sam/sam_image_segmenter.py +9 -10
- keras_hub/src/models/sam/sam_presets.py +0 -6
- keras_hub/src/models/segformer/__init__.py +8 -0
- keras_hub/src/models/segformer/segformer_backbone.py +163 -0
- keras_hub/src/models/segformer/segformer_image_converter.py +8 -0
- keras_hub/src/models/segformer/segformer_image_segmenter.py +171 -0
- keras_hub/src/models/segformer/segformer_image_segmenter_preprocessor.py +31 -0
- keras_hub/src/models/segformer/segformer_presets.py +124 -0
- keras_hub/src/models/stable_diffusion_3/mmdit.py +41 -0
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_backbone.py +38 -21
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_image_to_image.py +3 -3
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_inpaint.py +3 -3
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_presets.py +28 -4
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_text_to_image.py +1 -1
- keras_hub/src/models/t5/t5_backbone.py +5 -4
- keras_hub/src/models/t5/t5_presets.py +41 -13
- keras_hub/src/models/text_to_image.py +13 -5
- keras_hub/src/models/vgg/vgg_backbone.py +1 -1
- keras_hub/src/models/vgg/vgg_presets.py +0 -8
- keras_hub/src/models/whisper/whisper_audio_converter.py +1 -1
- keras_hub/src/models/whisper/whisper_presets.py +0 -20
- keras_hub/src/models/xlm_roberta/xlm_roberta_presets.py +0 -4
- keras_hub/src/tests/test_case.py +25 -0
- keras_hub/src/utils/preset_utils.py +17 -4
- keras_hub/src/utils/timm/convert_efficientnet.py +449 -0
- keras_hub/src/utils/timm/preset_loader.py +3 -0
- keras_hub/src/version_utils.py +1 -1
- {keras_hub_nightly-0.16.1.dev202410200345.dist-info → keras_hub_nightly-0.19.0.dev202412070351.dist-info}/METADATA +15 -26
- {keras_hub_nightly-0.16.1.dev202410200345.dist-info → keras_hub_nightly-0.19.0.dev202412070351.dist-info}/RECORD +109 -76
- {keras_hub_nightly-0.16.1.dev202410200345.dist-info → keras_hub_nightly-0.19.0.dev202412070351.dist-info}/WHEEL +1 -1
- {keras_hub_nightly-0.16.1.dev202410200345.dist-info → keras_hub_nightly-0.19.0.dev202412070351.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,47 @@ 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
|
+
stackwise_depth_coefficients = [
|
140
|
+
kwargs.pop("depth_coefficient")
|
141
|
+
] * num_stacks
|
142
|
+
if "width_coefficient" in kwargs:
|
143
|
+
stackwise_width_coefficients = [
|
144
|
+
kwargs.pop("width_coefficient")
|
145
|
+
] * num_stacks
|
146
|
+
|
115
147
|
image_input = keras.layers.Input(shape=input_shape)
|
116
148
|
|
117
149
|
x = image_input # Intermediate result.
|
118
|
-
if
|
150
|
+
if include_stem_padding:
|
119
151
|
x = keras.layers.ZeroPadding2D(
|
120
|
-
padding=
|
152
|
+
padding=(1, 1),
|
121
153
|
name="stem_conv_pad",
|
122
154
|
)(x)
|
123
155
|
|
124
156
|
# Build stem
|
125
157
|
stem_filters = round_filters(
|
126
158
|
filters=stackwise_input_filters[0],
|
127
|
-
width_coefficient=
|
159
|
+
width_coefficient=stackwise_width_coefficients[0],
|
128
160
|
min_depth=min_depth,
|
129
161
|
depth_divisor=depth_divisor,
|
130
162
|
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
@@ -136,6 +168,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
136
168
|
kernel_size=3,
|
137
169
|
strides=2,
|
138
170
|
padding=stem_conv_padding,
|
171
|
+
data_format=data_format,
|
139
172
|
use_bias=False,
|
140
173
|
kernel_initializer=conv_kernel_initializer(),
|
141
174
|
name="stem_conv",
|
@@ -143,6 +176,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
143
176
|
|
144
177
|
x = keras.layers.BatchNormalization(
|
145
178
|
momentum=batch_norm_momentum,
|
179
|
+
epsilon=batch_norm_epsilon,
|
146
180
|
name="stem_bn",
|
147
181
|
)(x)
|
148
182
|
x = keras.layers.Activation(activation, name="stem_activation")(x)
|
@@ -154,15 +188,19 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
154
188
|
self._pyramid_outputs = {}
|
155
189
|
curr_pyramid_level = 1
|
156
190
|
|
157
|
-
for i in range(
|
191
|
+
for i in range(num_stacks):
|
158
192
|
num_repeats = stackwise_num_repeats[i]
|
159
193
|
input_filters = stackwise_input_filters[i]
|
160
194
|
output_filters = stackwise_output_filters[i]
|
195
|
+
force_input_filters = stackwise_force_input_filters[i]
|
196
|
+
nores = stackwise_nores_option[i]
|
197
|
+
stack_width_coefficient = stackwise_width_coefficients[i]
|
198
|
+
stack_depth_coefficient = stackwise_depth_coefficients[i]
|
161
199
|
|
162
200
|
# Update block input and output filters based on depth multiplier.
|
163
201
|
input_filters = round_filters(
|
164
202
|
filters=input_filters,
|
165
|
-
width_coefficient=
|
203
|
+
width_coefficient=stack_width_coefficient,
|
166
204
|
min_depth=min_depth,
|
167
205
|
depth_divisor=depth_divisor,
|
168
206
|
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
@@ -170,7 +208,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
170
208
|
)
|
171
209
|
output_filters = round_filters(
|
172
210
|
filters=output_filters,
|
173
|
-
width_coefficient=
|
211
|
+
width_coefficient=stack_width_coefficient,
|
174
212
|
min_depth=min_depth,
|
175
213
|
depth_divisor=depth_divisor,
|
176
214
|
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
@@ -179,7 +217,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
179
217
|
|
180
218
|
repeats = round_repeats(
|
181
219
|
repeats=num_repeats,
|
182
|
-
depth_coefficient=
|
220
|
+
depth_coefficient=stack_depth_coefficient,
|
183
221
|
)
|
184
222
|
strides = stackwise_strides[i]
|
185
223
|
squeeze_and_excite_ratio = stackwise_squeeze_and_excite_ratios[i]
|
@@ -195,6 +233,16 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
195
233
|
self._pyramid_outputs[f"P{curr_pyramid_level}"] = x
|
196
234
|
curr_pyramid_level += 1
|
197
235
|
|
236
|
+
if force_input_filters > 0:
|
237
|
+
input_filters = round_filters(
|
238
|
+
filters=force_input_filters,
|
239
|
+
width_coefficient=stack_width_coefficient,
|
240
|
+
min_depth=min_depth,
|
241
|
+
depth_divisor=depth_divisor,
|
242
|
+
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
243
|
+
cap_round_filter_decrease=cap_round_filter_decrease,
|
244
|
+
)
|
245
|
+
|
198
246
|
# 97 is the start of the lowercase alphabet.
|
199
247
|
letter_identifier = chr(j + 97)
|
200
248
|
stackwise_block_type = stackwise_block_types[i]
|
@@ -206,32 +254,50 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
206
254
|
filters_out=output_filters,
|
207
255
|
kernel_size=stackwise_kernel_sizes[i],
|
208
256
|
strides=strides,
|
257
|
+
data_format=data_format,
|
209
258
|
expand_ratio=stackwise_expansion_ratios[i],
|
210
259
|
se_ratio=squeeze_and_excite_ratio,
|
211
260
|
activation=activation,
|
261
|
+
projection_activation=projection_activation,
|
212
262
|
dropout=dropout * block_id / blocks,
|
263
|
+
batch_norm_epsilon=batch_norm_epsilon,
|
213
264
|
name=block_name,
|
214
265
|
)
|
215
266
|
else:
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
kernel_size
|
221
|
-
strides
|
222
|
-
|
223
|
-
activation
|
224
|
-
dropout
|
225
|
-
batch_norm_momentum
|
226
|
-
|
227
|
-
|
267
|
+
constructor = get_conv_constructor(stackwise_block_type)
|
268
|
+
block_kwargs = {
|
269
|
+
"input_filters": input_filters,
|
270
|
+
"output_filters": output_filters,
|
271
|
+
"kernel_size": stackwise_kernel_sizes[i],
|
272
|
+
"strides": strides,
|
273
|
+
"data_format": data_format,
|
274
|
+
"activation": activation,
|
275
|
+
"dropout": dropout * block_id / blocks,
|
276
|
+
"batch_norm_momentum": batch_norm_momentum,
|
277
|
+
"batch_norm_epsilon": batch_norm_epsilon,
|
278
|
+
"nores": nores,
|
279
|
+
"name": block_name,
|
280
|
+
}
|
281
|
+
|
282
|
+
if stackwise_block_type in ("fused", "unfused"):
|
283
|
+
block_kwargs["expand_ratio"] = (
|
284
|
+
stackwise_expansion_ratios[i]
|
285
|
+
)
|
286
|
+
block_kwargs["se_ratio"] = squeeze_and_excite_ratio
|
287
|
+
|
288
|
+
if stackwise_block_type == "fused":
|
289
|
+
block_kwargs["projection_activation"] = (
|
290
|
+
projection_activation
|
291
|
+
)
|
292
|
+
|
293
|
+
block = constructor(**block_kwargs)
|
228
294
|
x = block(x)
|
229
295
|
block_id += 1
|
230
296
|
|
231
297
|
# Build top
|
232
298
|
top_filters = round_filters(
|
233
|
-
filters=
|
234
|
-
width_coefficient=
|
299
|
+
filters=num_features,
|
300
|
+
width_coefficient=stackwise_width_coefficients[-1],
|
235
301
|
min_depth=min_depth,
|
236
302
|
depth_divisor=depth_divisor,
|
237
303
|
use_depth_divisor_as_min_depth=use_depth_divisor_as_min_depth,
|
@@ -241,15 +307,16 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
241
307
|
x = keras.layers.Conv2D(
|
242
308
|
filters=top_filters,
|
243
309
|
kernel_size=1,
|
244
|
-
padding="same",
|
245
310
|
strides=1,
|
311
|
+
padding="same",
|
312
|
+
data_format="channels_last",
|
246
313
|
kernel_initializer=conv_kernel_initializer(),
|
247
314
|
use_bias=False,
|
248
315
|
name="top_conv",
|
249
|
-
data_format="channels_last",
|
250
316
|
)(x)
|
251
317
|
x = keras.layers.BatchNormalization(
|
252
318
|
momentum=batch_norm_momentum,
|
319
|
+
epsilon=batch_norm_epsilon,
|
253
320
|
name="top_bn",
|
254
321
|
)(x)
|
255
322
|
x = keras.layers.Activation(
|
@@ -263,11 +330,12 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
263
330
|
super().__init__(inputs=image_input, outputs=x, **kwargs)
|
264
331
|
|
265
332
|
# === Config ===
|
266
|
-
self.
|
267
|
-
self.
|
333
|
+
self.stackwise_width_coefficients = stackwise_width_coefficients
|
334
|
+
self.stackwise_depth_coefficients = stackwise_depth_coefficients
|
268
335
|
self.dropout = dropout
|
269
336
|
self.depth_divisor = depth_divisor
|
270
337
|
self.min_depth = min_depth
|
338
|
+
self.data_format = data_format
|
271
339
|
self.activation = activation
|
272
340
|
self.stackwise_kernel_sizes = stackwise_kernel_sizes
|
273
341
|
self.stackwise_num_repeats = stackwise_num_repeats
|
@@ -280,18 +348,21 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
280
348
|
self.stackwise_strides = stackwise_strides
|
281
349
|
self.stackwise_block_types = stackwise_block_types
|
282
350
|
|
283
|
-
self.
|
351
|
+
self.stackwise_force_input_filters = stackwise_force_input_filters
|
352
|
+
self.include_stem_padding = include_stem_padding
|
284
353
|
self.use_depth_divisor_as_min_depth = use_depth_divisor_as_min_depth
|
285
354
|
self.cap_round_filter_decrease = cap_round_filter_decrease
|
286
355
|
self.stem_conv_padding = stem_conv_padding
|
287
356
|
self.batch_norm_momentum = batch_norm_momentum
|
357
|
+
self.batch_norm_epsilon = batch_norm_epsilon
|
358
|
+
self.projection_activation = projection_activation
|
288
359
|
|
289
360
|
def get_config(self):
|
290
361
|
config = super().get_config()
|
291
362
|
config.update(
|
292
363
|
{
|
293
|
-
"
|
294
|
-
"
|
364
|
+
"stackwise_width_coefficients": self.stackwise_width_coefficients,
|
365
|
+
"stackwise_depth_coefficients": self.stackwise_depth_coefficients,
|
295
366
|
"dropout": self.dropout,
|
296
367
|
"depth_divisor": self.depth_divisor,
|
297
368
|
"min_depth": self.min_depth,
|
@@ -305,11 +376,14 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
305
376
|
"stackwise_squeeze_and_excite_ratios": self.stackwise_squeeze_and_excite_ratios,
|
306
377
|
"stackwise_strides": self.stackwise_strides,
|
307
378
|
"stackwise_block_types": self.stackwise_block_types,
|
308
|
-
"
|
379
|
+
"stackwise_force_input_filters": self.stackwise_force_input_filters,
|
380
|
+
"include_stem_padding": self.include_stem_padding,
|
309
381
|
"use_depth_divisor_as_min_depth": self.use_depth_divisor_as_min_depth,
|
310
382
|
"cap_round_filter_decrease": self.cap_round_filter_decrease,
|
311
383
|
"stem_conv_padding": self.stem_conv_padding,
|
312
384
|
"batch_norm_momentum": self.batch_norm_momentum,
|
385
|
+
"batch_norm_epsilon": self.batch_norm_epsilon,
|
386
|
+
"projection_activation": self.projection_activation,
|
313
387
|
}
|
314
388
|
)
|
315
389
|
return config
|
@@ -346,10 +420,13 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
346
420
|
kernel_size=3,
|
347
421
|
strides=1,
|
348
422
|
activation="swish",
|
423
|
+
projection_activation=None,
|
349
424
|
expand_ratio=1,
|
350
425
|
se_ratio=0.0,
|
351
426
|
dropout=0.0,
|
427
|
+
batch_norm_epsilon=1e-5,
|
352
428
|
name="",
|
429
|
+
data_format="channels_last",
|
353
430
|
):
|
354
431
|
"""An inverted residual block.
|
355
432
|
|
@@ -375,12 +452,14 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
375
452
|
kernel_size=1,
|
376
453
|
strides=1,
|
377
454
|
padding="same",
|
455
|
+
data_format=data_format,
|
378
456
|
use_bias=False,
|
379
457
|
kernel_initializer=conv_kernel_initializer(),
|
380
458
|
name=name + "expand_conv",
|
381
459
|
)(inputs)
|
382
460
|
x = keras.layers.BatchNormalization(
|
383
461
|
axis=3,
|
462
|
+
epsilon=batch_norm_epsilon,
|
384
463
|
name=name + "expand_bn",
|
385
464
|
)(x)
|
386
465
|
x = keras.layers.Activation(
|
@@ -390,25 +469,23 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
390
469
|
x = inputs
|
391
470
|
|
392
471
|
# Depthwise Convolution
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
conv_pad = "valid"
|
399
|
-
else:
|
400
|
-
conv_pad = "same"
|
401
|
-
|
472
|
+
padding_pixels = kernel_size // 2
|
473
|
+
x = keras.layers.ZeroPadding2D(
|
474
|
+
padding=(padding_pixels, padding_pixels),
|
475
|
+
name=name + "dwconv_pad",
|
476
|
+
)(x)
|
402
477
|
x = keras.layers.DepthwiseConv2D(
|
403
478
|
kernel_size=kernel_size,
|
404
479
|
strides=strides,
|
405
|
-
padding=
|
480
|
+
padding="valid",
|
481
|
+
data_format=data_format,
|
406
482
|
use_bias=False,
|
407
483
|
depthwise_initializer=conv_kernel_initializer(),
|
408
484
|
name=name + "dwconv",
|
409
485
|
)(x)
|
410
486
|
x = keras.layers.BatchNormalization(
|
411
487
|
axis=3,
|
488
|
+
epsilon=batch_norm_epsilon,
|
412
489
|
name=name + "dwconv_bn",
|
413
490
|
)(x)
|
414
491
|
x = keras.layers.Activation(
|
@@ -427,6 +504,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
427
504
|
filters_se,
|
428
505
|
1,
|
429
506
|
padding="same",
|
507
|
+
data_format=data_format,
|
430
508
|
activation=activation,
|
431
509
|
kernel_initializer=conv_kernel_initializer(),
|
432
510
|
name=name + "se_reduce",
|
@@ -435,6 +513,7 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
435
513
|
filters,
|
436
514
|
1,
|
437
515
|
padding="same",
|
516
|
+
data_format=data_format,
|
438
517
|
activation="sigmoid",
|
439
518
|
kernel_initializer=conv_kernel_initializer(),
|
440
519
|
name=name + "se_expand",
|
@@ -453,11 +532,13 @@ class EfficientNetBackbone(FeaturePyramidBackbone):
|
|
453
532
|
)(x)
|
454
533
|
x = keras.layers.BatchNormalization(
|
455
534
|
axis=3,
|
535
|
+
epsilon=batch_norm_epsilon,
|
456
536
|
name=name + "project_bn",
|
457
537
|
)(x)
|
458
|
-
|
459
|
-
|
460
|
-
|
538
|
+
if projection_activation:
|
539
|
+
x = keras.layers.Activation(
|
540
|
+
projection_activation, name=name + "projection_activation"
|
541
|
+
)(x)
|
461
542
|
|
462
543
|
if strides == 1 and filters_in == filters_out:
|
463
544
|
if dropout > 0:
|
@@ -537,9 +618,11 @@ def get_conv_constructor(conv_type):
|
|
537
618
|
return MBConvBlock
|
538
619
|
elif conv_type == "fused":
|
539
620
|
return FusedMBConvBlock
|
621
|
+
elif conv_type == "cba":
|
622
|
+
return CBABlock
|
540
623
|
else:
|
541
624
|
raise ValueError(
|
542
625
|
"Expected `conv_type` to be "
|
543
|
-
"one of 'unfused', 'fused', but got "
|
626
|
+
"one of 'unfused', 'fused', 'cba', but got "
|
544
627
|
f"`conv_type={conv_type}`"
|
545
628
|
)
|
@@ -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 (
|
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
|