keras-hub-nightly 0.22.0.dev202508170419__py3-none-any.whl → 0.24.0.dev202511090424__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.
Potentially problematic release.
This version of keras-hub-nightly might be problematic. Click here for more details.
- keras_hub/layers/__init__.py +15 -0
- keras_hub/models/__init__.py +93 -0
- keras_hub/src/layers/modeling/position_embedding.py +21 -6
- keras_hub/src/layers/modeling/reversible_embedding.py +8 -1
- keras_hub/src/layers/modeling/rotary_embedding.py +16 -6
- keras_hub/src/layers/modeling/sine_position_encoding.py +21 -8
- keras_hub/src/layers/modeling/token_and_position_embedding.py +2 -1
- keras_hub/src/models/backbone.py +28 -16
- keras_hub/src/models/causal_lm.py +37 -0
- keras_hub/src/models/causal_lm_preprocessor.py +14 -0
- keras_hub/src/models/clip/clip_presets.py +8 -8
- keras_hub/src/models/d_fine/__init__.py +5 -0
- keras_hub/src/models/d_fine/d_fine_attention.py +461 -0
- keras_hub/src/models/d_fine/d_fine_backbone.py +891 -0
- keras_hub/src/models/d_fine/d_fine_decoder.py +944 -0
- keras_hub/src/models/d_fine/d_fine_encoder.py +365 -0
- keras_hub/src/models/d_fine/d_fine_hybrid_encoder.py +642 -0
- keras_hub/src/models/d_fine/d_fine_image_converter.py +8 -0
- keras_hub/src/models/d_fine/d_fine_layers.py +1828 -0
- keras_hub/src/models/d_fine/d_fine_loss.py +938 -0
- keras_hub/src/models/d_fine/d_fine_object_detector.py +875 -0
- keras_hub/src/models/d_fine/d_fine_object_detector_preprocessor.py +14 -0
- keras_hub/src/models/d_fine/d_fine_presets.py +155 -0
- keras_hub/src/models/d_fine/d_fine_utils.py +827 -0
- keras_hub/src/models/deberta_v3/disentangled_self_attention.py +7 -2
- keras_hub/src/models/depth_anything/__init__.py +9 -0
- keras_hub/src/models/depth_anything/depth_anything_backbone.py +232 -0
- keras_hub/src/models/depth_anything/depth_anything_depth_estimator.py +70 -0
- keras_hub/src/models/depth_anything/depth_anything_depth_estimator_preprocessor.py +16 -0
- keras_hub/src/models/depth_anything/depth_anything_image_converter.py +10 -0
- keras_hub/src/models/depth_anything/depth_anything_layers.py +725 -0
- keras_hub/src/models/depth_anything/depth_anything_loss.py +89 -0
- keras_hub/src/models/depth_anything/depth_anything_presets.py +41 -0
- keras_hub/src/models/depth_anything/interpolate.py +62 -0
- keras_hub/src/models/depth_estimator.py +239 -0
- keras_hub/src/models/depth_estimator_preprocessor.py +78 -0
- keras_hub/src/models/dinov2/dinov2_backbone.py +29 -3
- keras_hub/src/models/dinov2/dinov2_layers.py +16 -4
- keras_hub/src/models/dinov3/__init__.py +5 -0
- keras_hub/src/models/dinov3/dinov3_backbone.py +263 -0
- keras_hub/src/models/dinov3/dinov3_image_converter.py +8 -0
- keras_hub/src/models/dinov3/dinov3_layers.py +1013 -0
- keras_hub/src/models/dinov3/dinov3_presets.py +4 -0
- keras_hub/src/models/gemma/gemma_backbone.py +0 -1
- keras_hub/src/models/gemma/gemma_presets.py +30 -0
- keras_hub/src/models/gemma3/gemma3_attention.py +48 -0
- keras_hub/src/models/gemma3/gemma3_backbone.py +4 -1
- keras_hub/src/models/gemma3/gemma3_decoder_block.py +12 -0
- keras_hub/src/models/gemma3/gemma3_presets.py +39 -0
- keras_hub/src/models/hgnetv2/hgnetv2_backbone.py +4 -1
- keras_hub/src/models/hgnetv2/hgnetv2_encoder.py +3 -2
- keras_hub/src/models/hgnetv2/hgnetv2_layers.py +27 -11
- keras_hub/src/models/image_to_image.py +5 -0
- keras_hub/src/models/inpaint.py +5 -0
- keras_hub/src/models/mobilenetv5/__init__.py +9 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_attention.py +699 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_backbone.py +396 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_blocks.py +890 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_builder.py +436 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_image_classifier.py +157 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_image_classifier_preprocessor.py +16 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_image_converter.py +10 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_layers.py +462 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_presets.py +15 -0
- keras_hub/src/models/mobilenetv5/mobilenetv5_utils.py +146 -0
- keras_hub/src/models/parseq/__init__.py +5 -0
- keras_hub/src/models/parseq/parseq_backbone.py +134 -0
- keras_hub/src/models/parseq/parseq_causal_lm.py +466 -0
- keras_hub/src/models/parseq/parseq_causal_lm_preprocessor.py +168 -0
- keras_hub/src/models/parseq/parseq_decoder.py +418 -0
- keras_hub/src/models/parseq/parseq_image_converter.py +8 -0
- keras_hub/src/models/parseq/parseq_presets.py +15 -0
- keras_hub/src/models/parseq/parseq_tokenizer.py +221 -0
- keras_hub/src/models/qwen3_moe/__init__.py +5 -0
- keras_hub/src/models/qwen3_moe/qwen3_moe_attention.py +371 -0
- keras_hub/src/models/qwen3_moe/qwen3_moe_backbone.py +365 -0
- keras_hub/src/models/qwen3_moe/qwen3_moe_causal_lm.py +357 -0
- keras_hub/src/models/qwen3_moe/qwen3_moe_causal_lm_preprocessor.py +12 -0
- keras_hub/src/models/qwen3_moe/qwen3_moe_decoder.py +672 -0
- keras_hub/src/models/qwen3_moe/qwen3_moe_layernorm.py +45 -0
- keras_hub/src/models/qwen3_moe/qwen3_moe_presets.py +30 -0
- keras_hub/src/models/qwen3_moe/qwen3_moe_tokenizer.py +48 -0
- keras_hub/src/models/sam/sam_prompt_encoder.py +3 -1
- keras_hub/src/models/siglip/siglip_presets.py +15 -0
- keras_hub/src/models/smollm3/smollm3_backbone.py +211 -0
- keras_hub/src/models/smollm3/smollm3_causal_lm.py +310 -0
- keras_hub/src/models/smollm3/smollm3_causal_lm_preprocessor.py +84 -0
- keras_hub/src/models/smollm3/smollm3_layers.py +757 -0
- keras_hub/src/models/smollm3/smollm3_tokenizer.py +60 -0
- keras_hub/src/models/smollm3/smollm3_utils.py +56 -0
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_presets.py +3 -3
- keras_hub/src/models/t5gemma/__init__.py +5 -0
- keras_hub/src/models/t5gemma/t5gemma_attention.py +370 -0
- keras_hub/src/models/t5gemma/t5gemma_backbone.py +366 -0
- keras_hub/src/models/t5gemma/t5gemma_decoder.py +355 -0
- keras_hub/src/models/t5gemma/t5gemma_encoder.py +214 -0
- keras_hub/src/models/t5gemma/t5gemma_layers.py +118 -0
- keras_hub/src/models/t5gemma/t5gemma_presets.py +374 -0
- keras_hub/src/models/t5gemma/t5gemma_seq_2_seq_lm.py +442 -0
- keras_hub/src/models/t5gemma/t5gemma_seq_2_seq_lm_preprocessor.py +216 -0
- keras_hub/src/models/t5gemma/t5gemma_tokenizer.py +84 -0
- keras_hub/src/models/text_to_image.py +5 -0
- keras_hub/src/samplers/beam_sampler.py +6 -6
- keras_hub/src/samplers/sampler.py +8 -6
- keras_hub/src/tests/test_case.py +40 -3
- keras_hub/src/tokenizers/tokenizer.py +15 -0
- keras_hub/src/utils/openvino_utils.py +141 -0
- keras_hub/src/utils/preset_utils.py +58 -2
- keras_hub/src/utils/tensor_utils.py +26 -2
- keras_hub/src/utils/timm/convert_mobilenetv5.py +321 -0
- keras_hub/src/utils/timm/preset_loader.py +8 -4
- keras_hub/src/utils/transformers/convert_dinov2.py +1 -0
- keras_hub/src/utils/transformers/convert_dinov3.py +106 -0
- keras_hub/src/utils/transformers/convert_qwen3_moe.py +216 -0
- keras_hub/src/utils/transformers/convert_smollm3.py +139 -0
- keras_hub/src/utils/transformers/convert_t5gemma.py +229 -0
- keras_hub/src/utils/transformers/convert_vit.py +4 -1
- keras_hub/src/utils/transformers/export/gemma.py +49 -4
- keras_hub/src/utils/transformers/export/hf_exporter.py +71 -25
- keras_hub/src/utils/transformers/preset_loader.py +12 -0
- keras_hub/src/version.py +1 -1
- keras_hub/tokenizers/__init__.py +15 -0
- {keras_hub_nightly-0.22.0.dev202508170419.dist-info → keras_hub_nightly-0.24.0.dev202511090424.dist-info}/METADATA +1 -1
- {keras_hub_nightly-0.22.0.dev202508170419.dist-info → keras_hub_nightly-0.24.0.dev202511090424.dist-info}/RECORD +126 -47
- {keras_hub_nightly-0.22.0.dev202508170419.dist-info → keras_hub_nightly-0.24.0.dev202511090424.dist-info}/WHEEL +0 -0
- {keras_hub_nightly-0.22.0.dev202508170419.dist-info → keras_hub_nightly-0.24.0.dev202511090424.dist-info}/top_level.txt +0 -0
|
@@ -217,9 +217,14 @@ class DisentangledSelfAttention(keras.layers.Layer):
|
|
|
217
217
|
)
|
|
218
218
|
|
|
219
219
|
def _get_log_pos(abs_pos, mid):
|
|
220
|
-
numerator = ops.log(
|
|
220
|
+
numerator = ops.log(
|
|
221
|
+
ops.cast(abs_pos, "float32") / ops.cast(mid, "float32")
|
|
222
|
+
)
|
|
221
223
|
numerator = numerator * ops.cast(mid - 1, dtype=numerator.dtype)
|
|
222
|
-
denominator = ops.log(
|
|
224
|
+
denominator = ops.log(
|
|
225
|
+
ops.cast(self.max_position_embeddings - 1, "float32")
|
|
226
|
+
/ ops.cast(mid, "float32")
|
|
227
|
+
)
|
|
223
228
|
val = ops.ceil(numerator / denominator)
|
|
224
229
|
val = ops.cast(val, dtype=mid.dtype)
|
|
225
230
|
val = val + mid
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from keras_hub.src.models.depth_anything.depth_anything_backbone import (
|
|
2
|
+
DepthAnythingBackbone,
|
|
3
|
+
)
|
|
4
|
+
from keras_hub.src.models.depth_anything.depth_anything_presets import (
|
|
5
|
+
backbone_presets,
|
|
6
|
+
)
|
|
7
|
+
from keras_hub.src.utils.preset_utils import register_presets
|
|
8
|
+
|
|
9
|
+
register_presets(backbone_presets, DepthAnythingBackbone)
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import keras
|
|
2
|
+
from keras import layers
|
|
3
|
+
|
|
4
|
+
from keras_hub.src.api_export import keras_hub_export
|
|
5
|
+
from keras_hub.src.models.backbone import Backbone
|
|
6
|
+
from keras_hub.src.models.depth_anything.depth_anything_layers import (
|
|
7
|
+
DepthAnythingDepthEstimationHead,
|
|
8
|
+
)
|
|
9
|
+
from keras_hub.src.models.depth_anything.depth_anything_layers import (
|
|
10
|
+
DepthAnythingNeck,
|
|
11
|
+
)
|
|
12
|
+
from keras_hub.src.models.dinov2 import DINOV2Backbone
|
|
13
|
+
from keras_hub.src.utils.keras_utils import standardize_data_format
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@keras_hub_export("keras_hub.models.DepthAnythingBackbone")
|
|
17
|
+
class DepthAnythingBackbone(Backbone):
|
|
18
|
+
"""DepthAnything core network with hyperparameters.
|
|
19
|
+
|
|
20
|
+
DepthAnything offers a powerful monocular depth estimation as described in
|
|
21
|
+
[Depth Anything V2](https://arxiv.org/abs/2406.09414).
|
|
22
|
+
|
|
23
|
+
The default constructor gives a fully customizable, randomly initialized
|
|
24
|
+
DepthAnything model with any number of layers, heads, and embedding
|
|
25
|
+
dimensions by providing the DINOV2 as the `image_encoder`. To load preset
|
|
26
|
+
architectures and weights, use the `from_preset` constructor.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
image_encoder: The DINOV2 image encoder for encoding the input images.
|
|
30
|
+
reassemble_factors: List of float. The reassemble factor for each
|
|
31
|
+
feature map from the image encoder. The length of the list must be
|
|
32
|
+
equal to the number of feature maps from the image encoder.
|
|
33
|
+
neck_hidden_dims: int. The size of the neck hidden state.
|
|
34
|
+
fusion_hidden_dim: int. The size of the fusion hidden state.
|
|
35
|
+
head_hidden_dim: int. The size of the neck hidden state.
|
|
36
|
+
head_in_index: int. The index to select the feature from the neck
|
|
37
|
+
features as the input to the head.
|
|
38
|
+
feature_keys: List of string. The keys to select the feature maps from
|
|
39
|
+
the image encoder. If `None`, all feature maps from the image
|
|
40
|
+
encoder will be used. Defaults to `None`.
|
|
41
|
+
data_format: `None` or str. If specified, either `"channels_last"` or
|
|
42
|
+
`"channels_first"`. The ordering of the dimensions in the
|
|
43
|
+
inputs. `"channels_last"` corresponds to inputs with shape
|
|
44
|
+
`(batch_size, height, width, channels)`
|
|
45
|
+
while `"channels_first"` corresponds to inputs with shape
|
|
46
|
+
`(batch_size, channels, height, width)`. It defaults to the
|
|
47
|
+
`image_data_format` value found in your Keras config file at
|
|
48
|
+
`~/.keras/keras.json`. If you never set it, then it will be
|
|
49
|
+
`"channels_last"`.
|
|
50
|
+
dtype: string or `keras.mixed_precision.DTypePolicy`. The dtype to use
|
|
51
|
+
for the models computations and weights. Note that some
|
|
52
|
+
computations, such as softmax and layer normalization will always
|
|
53
|
+
be done a float32 precision regardless of dtype.
|
|
54
|
+
|
|
55
|
+
Example:
|
|
56
|
+
```python
|
|
57
|
+
# Pretrained DepthAnything model.
|
|
58
|
+
input_data = {
|
|
59
|
+
"images": np.ones(shape=(1, 518, 518, 3), dtype="float32"),
|
|
60
|
+
}
|
|
61
|
+
model = keras_hub.models.DepthAnythingBackbone.from_preset(
|
|
62
|
+
"depth_anything_v2_small"
|
|
63
|
+
)
|
|
64
|
+
model(input_data)
|
|
65
|
+
|
|
66
|
+
# Pretrained DepthAnything model with custom image shape.
|
|
67
|
+
input_data = {
|
|
68
|
+
"images": np.ones(shape=(1, 224, 224, 3), dtype="float32"),
|
|
69
|
+
}
|
|
70
|
+
model = keras_hub.models.DepthAnythingBackbone.from_preset(
|
|
71
|
+
"depth_anything_v2_small", image_shape=(224, 224, 3)
|
|
72
|
+
)
|
|
73
|
+
model(input_data)
|
|
74
|
+
|
|
75
|
+
# Randomly initialized DepthAnything model with custom config.
|
|
76
|
+
image_encoder = keras_hub.models.DINOV2Backbone(
|
|
77
|
+
patch_size=14,
|
|
78
|
+
num_layers=4,
|
|
79
|
+
hidden_dim=32,
|
|
80
|
+
num_heads=2,
|
|
81
|
+
intermediate_dim=128,
|
|
82
|
+
image_shape=(224, 224, 3),
|
|
83
|
+
position_embedding_shape=(518, 518),
|
|
84
|
+
)
|
|
85
|
+
model = keras_hub.models.DepthAnythingBackbone(
|
|
86
|
+
image_encoder=image_encoder,
|
|
87
|
+
reassemble_factors=[4, 2, 1, 0.5],
|
|
88
|
+
neck_hidden_dims=[16, 32, 64, 128],
|
|
89
|
+
fusion_hidden_dim=128,
|
|
90
|
+
head_hidden_dim=16,
|
|
91
|
+
head_in_index=-1,
|
|
92
|
+
feature_keys=["Stage1", "Stage2", "Stage3", "Stage4"],
|
|
93
|
+
)
|
|
94
|
+
model(input_data)
|
|
95
|
+
```
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
def __init__(
|
|
99
|
+
self,
|
|
100
|
+
image_encoder,
|
|
101
|
+
reassemble_factors,
|
|
102
|
+
neck_hidden_dims,
|
|
103
|
+
fusion_hidden_dim,
|
|
104
|
+
head_hidden_dim,
|
|
105
|
+
head_in_index,
|
|
106
|
+
feature_keys=None,
|
|
107
|
+
data_format=None,
|
|
108
|
+
dtype=None,
|
|
109
|
+
**kwargs,
|
|
110
|
+
):
|
|
111
|
+
if not isinstance(image_encoder, DINOV2Backbone):
|
|
112
|
+
raise ValueError(
|
|
113
|
+
"`image_encoder` must be a `DINOV2Backbone`. "
|
|
114
|
+
f"Received image_encoder={image_encoder} "
|
|
115
|
+
f"(of type {type(image_encoder)})."
|
|
116
|
+
)
|
|
117
|
+
if feature_keys is not None:
|
|
118
|
+
feature_keys = [str(key) for key in feature_keys]
|
|
119
|
+
for key in feature_keys:
|
|
120
|
+
if key not in image_encoder.pyramid_outputs:
|
|
121
|
+
raise ValueError(
|
|
122
|
+
"All `feature_keys` must be in "
|
|
123
|
+
"`image_encoder.pyramid_outputs`. "
|
|
124
|
+
f"Received feature_keys={feature_keys}, but "
|
|
125
|
+
"`image_encoder.pyramid_outputs` contains "
|
|
126
|
+
f"{list(image_encoder.pyramid_outputs.keys())}."
|
|
127
|
+
)
|
|
128
|
+
else:
|
|
129
|
+
feature_keys = list(image_encoder.pyramid_outputs.keys())
|
|
130
|
+
if len(reassemble_factors) != len(feature_keys):
|
|
131
|
+
raise ValueError(
|
|
132
|
+
"The length of `reassemble_factors` must be equal to the "
|
|
133
|
+
"length of `feature_keys`. "
|
|
134
|
+
f"Received len(reassemble_factors)={len(reassemble_factors)}, "
|
|
135
|
+
f"len(feature_keys)={len(feature_keys)}."
|
|
136
|
+
)
|
|
137
|
+
data_format = standardize_data_format(data_format)
|
|
138
|
+
patch_size = image_encoder.patch_size
|
|
139
|
+
backbone_hidden_dim = image_encoder.hidden_dim
|
|
140
|
+
image_shape = image_encoder.image_shape
|
|
141
|
+
if data_format == "channels_last":
|
|
142
|
+
image_size = (image_shape[0], image_shape[1])
|
|
143
|
+
else:
|
|
144
|
+
image_size = (image_shape[1], image_shape[2])
|
|
145
|
+
|
|
146
|
+
# === Layers ===
|
|
147
|
+
pyramid_outputs = {
|
|
148
|
+
key: value
|
|
149
|
+
for key, value in image_encoder.pyramid_outputs.items()
|
|
150
|
+
if key in feature_keys
|
|
151
|
+
}
|
|
152
|
+
self.feature_extractor = keras.Model(
|
|
153
|
+
inputs=image_encoder.inputs,
|
|
154
|
+
outputs=pyramid_outputs,
|
|
155
|
+
)
|
|
156
|
+
self.feature_extractor.dtype_policy = image_encoder.dtype_policy
|
|
157
|
+
self.neck = DepthAnythingNeck(
|
|
158
|
+
patch_size=patch_size,
|
|
159
|
+
image_size=image_size,
|
|
160
|
+
backbone_hidden_dim=backbone_hidden_dim,
|
|
161
|
+
neck_hidden_dims=neck_hidden_dims,
|
|
162
|
+
reassemble_factors=reassemble_factors,
|
|
163
|
+
fusion_hidden_dim=fusion_hidden_dim,
|
|
164
|
+
num_cls_tokens=1,
|
|
165
|
+
num_register_tokens=image_encoder.num_register_tokens,
|
|
166
|
+
data_format=data_format,
|
|
167
|
+
dtype=dtype,
|
|
168
|
+
name="neck",
|
|
169
|
+
)
|
|
170
|
+
self.head = DepthAnythingDepthEstimationHead(
|
|
171
|
+
patch_size=patch_size,
|
|
172
|
+
patch_height=image_size[0] // patch_size,
|
|
173
|
+
patch_width=image_size[1] // patch_size,
|
|
174
|
+
fusion_hidden_dim=fusion_hidden_dim,
|
|
175
|
+
head_hidden_dim=head_hidden_dim,
|
|
176
|
+
head_in_index=head_in_index,
|
|
177
|
+
data_format=data_format,
|
|
178
|
+
dtype=dtype,
|
|
179
|
+
name="head",
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# === Functional Model ===
|
|
183
|
+
image_input = layers.Input(shape=image_shape, name="images")
|
|
184
|
+
features = self.feature_extractor(image_input)
|
|
185
|
+
features = self.neck(list(features.values()))
|
|
186
|
+
depth_output = self.head(features)
|
|
187
|
+
super().__init__(
|
|
188
|
+
inputs=image_input,
|
|
189
|
+
outputs=depth_output,
|
|
190
|
+
dtype=dtype,
|
|
191
|
+
**kwargs,
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
# === Config ===
|
|
195
|
+
self.image_encoder = image_encoder
|
|
196
|
+
self.reassemble_factors = reassemble_factors
|
|
197
|
+
self.neck_hidden_dims = neck_hidden_dims
|
|
198
|
+
self.fusion_hidden_dim = fusion_hidden_dim
|
|
199
|
+
self.head_hidden_dim = head_hidden_dim
|
|
200
|
+
self.head_in_index = head_in_index
|
|
201
|
+
self.feature_keys = feature_keys
|
|
202
|
+
|
|
203
|
+
def get_config(self):
|
|
204
|
+
config = super().get_config()
|
|
205
|
+
config.update(
|
|
206
|
+
{
|
|
207
|
+
"image_encoder": layers.serialize(self.image_encoder),
|
|
208
|
+
"reassemble_factors": self.reassemble_factors,
|
|
209
|
+
"neck_hidden_dims": self.neck_hidden_dims,
|
|
210
|
+
"fusion_hidden_dim": self.fusion_hidden_dim,
|
|
211
|
+
"head_hidden_dim": self.head_hidden_dim,
|
|
212
|
+
"head_in_index": self.head_in_index,
|
|
213
|
+
"feature_keys": self.feature_keys,
|
|
214
|
+
}
|
|
215
|
+
)
|
|
216
|
+
return config
|
|
217
|
+
|
|
218
|
+
@classmethod
|
|
219
|
+
def from_config(cls, config, custom_objects=None):
|
|
220
|
+
config = config.copy()
|
|
221
|
+
|
|
222
|
+
# Propagate `dtype` to `image_encoder` if needed.
|
|
223
|
+
if "dtype" in config and config["dtype"] is not None:
|
|
224
|
+
dtype_config = config["dtype"]
|
|
225
|
+
if "dtype" not in config["image_encoder"]["config"]:
|
|
226
|
+
config["image_encoder"]["config"]["dtype"] = dtype_config
|
|
227
|
+
|
|
228
|
+
# We expect submodels to be instantiated.
|
|
229
|
+
config["image_encoder"] = layers.deserialize(
|
|
230
|
+
config["image_encoder"], custom_objects=custom_objects
|
|
231
|
+
)
|
|
232
|
+
return cls(**config)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import keras
|
|
2
|
+
|
|
3
|
+
from keras_hub.src.api_export import keras_hub_export
|
|
4
|
+
from keras_hub.src.models.depth_anything.depth_anything_backbone import (
|
|
5
|
+
DepthAnythingBackbone,
|
|
6
|
+
)
|
|
7
|
+
from keras_hub.src.models.depth_anything.depth_anything_depth_estimator_preprocessor import ( # noqa: E501
|
|
8
|
+
DepthAnythingDepthEstimatorPreprocessor,
|
|
9
|
+
)
|
|
10
|
+
from keras_hub.src.models.depth_anything.depth_anything_loss import (
|
|
11
|
+
DepthAnythingLoss,
|
|
12
|
+
)
|
|
13
|
+
from keras_hub.src.models.depth_estimator import DepthEstimator
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@keras_hub_export("keras_hub.models.DepthAnythingDepthEstimator")
|
|
17
|
+
class DepthAnythingDepthEstimator(DepthEstimator):
|
|
18
|
+
backbone_cls = DepthAnythingBackbone
|
|
19
|
+
preprocessor_cls = DepthAnythingDepthEstimatorPreprocessor
|
|
20
|
+
|
|
21
|
+
def compile(
|
|
22
|
+
self,
|
|
23
|
+
optimizer="auto",
|
|
24
|
+
loss="auto",
|
|
25
|
+
*,
|
|
26
|
+
metrics="auto",
|
|
27
|
+
**kwargs,
|
|
28
|
+
):
|
|
29
|
+
"""Configures the `DepthEstimator` task for training.
|
|
30
|
+
|
|
31
|
+
The `DepthEstimator` task extends the default compilation signature of
|
|
32
|
+
`keras.Model.compile` with defaults for `optimizer`, `loss`, and
|
|
33
|
+
`metrics`. To override these defaults, pass any value
|
|
34
|
+
to these arguments during compilation.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
optimizer: `"auto"`, an optimizer name, or a `keras.Optimizer`
|
|
38
|
+
instance. Defaults to `"auto"`, which uses the default optimizer
|
|
39
|
+
for the given model and task. See `keras.Model.compile` and
|
|
40
|
+
`keras.optimizers` for more info on possible `optimizer` values.
|
|
41
|
+
loss: `"auto"`, a loss name, or a `keras.losses.Loss` instance.
|
|
42
|
+
Defaults to `"auto"`, where a `DepthAnythingLoss` loss will be
|
|
43
|
+
applied for the depth estimation task. See
|
|
44
|
+
`keras.Model.compile` and `keras.losses` for more info on
|
|
45
|
+
possible `loss` values.
|
|
46
|
+
metrics: `"auto"`, or a dict of metrics to be evaluated by
|
|
47
|
+
the model during training and testing. Defaults to `"auto"`,
|
|
48
|
+
where a `keras.metrics.RootMeanSquaredError` will be applied to
|
|
49
|
+
track the accuracy of the model during training. See
|
|
50
|
+
`keras.Model.compile` and `keras.metrics` for more info on
|
|
51
|
+
possible `metrics` values.
|
|
52
|
+
**kwargs: See `keras.Model.compile` for a full list of arguments
|
|
53
|
+
supported by the compile method.
|
|
54
|
+
"""
|
|
55
|
+
if optimizer == "auto":
|
|
56
|
+
optimizer = keras.optimizers.AdamW(5e-5)
|
|
57
|
+
if loss == "auto":
|
|
58
|
+
loss = {
|
|
59
|
+
"depths": DepthAnythingLoss(
|
|
60
|
+
min_depth=self.min_depth, max_depth=self.max_depth
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
if metrics == "auto":
|
|
64
|
+
metrics = {"depths": keras.metrics.RootMeanSquaredError()}
|
|
65
|
+
super().compile(
|
|
66
|
+
optimizer=optimizer,
|
|
67
|
+
loss=loss,
|
|
68
|
+
metrics=metrics,
|
|
69
|
+
**kwargs,
|
|
70
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from keras_hub.src.api_export import keras_hub_export
|
|
2
|
+
from keras_hub.src.models.depth_anything.depth_anything_backbone import (
|
|
3
|
+
DepthAnythingBackbone,
|
|
4
|
+
)
|
|
5
|
+
from keras_hub.src.models.depth_anything.depth_anything_image_converter import (
|
|
6
|
+
DepthAnythingImageConverter,
|
|
7
|
+
)
|
|
8
|
+
from keras_hub.src.models.depth_estimator_preprocessor import (
|
|
9
|
+
DepthEstimatorPreprocessor,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@keras_hub_export("keras_hub.models.DepthAnythingDepthEstimatorPreprocessor")
|
|
14
|
+
class DepthAnythingDepthEstimatorPreprocessor(DepthEstimatorPreprocessor):
|
|
15
|
+
backbone_cls = DepthAnythingBackbone
|
|
16
|
+
image_converter_cls = DepthAnythingImageConverter
|
|
@@ -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.depth_anything.depth_anything_backbone import (
|
|
4
|
+
DepthAnythingBackbone,
|
|
5
|
+
)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@keras_hub_export("keras_hub.layers.DepthAnythingImageConverter")
|
|
9
|
+
class DepthAnythingImageConverter(ImageConverter):
|
|
10
|
+
backbone_cls = DepthAnythingBackbone
|