keras-hub 0.19.3.dev0__py3-none-any.whl → 0.20.0.dev0__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 +6 -0
- keras_hub/api/models/__init__.py +36 -4
- keras_hub/api/tokenizers/__init__.py +4 -0
- keras_hub/src/layers/preprocessing/image_converter.py +123 -14
- keras_hub/src/models/backbone.py +3 -3
- keras_hub/src/models/cspnet/__init__.py +5 -0
- keras_hub/src/models/cspnet/cspnet_backbone.py +1279 -0
- keras_hub/src/models/cspnet/cspnet_image_classifier.py +12 -0
- keras_hub/src/models/cspnet/cspnet_image_classifier_preprocessor.py +14 -0
- keras_hub/src/models/cspnet/cspnet_image_converter.py +8 -0
- keras_hub/src/models/cspnet/cspnet_presets.py +16 -0
- keras_hub/src/models/gemma/gemma_attention.py +26 -17
- keras_hub/src/models/gemma3/gemma3_attention.py +76 -23
- keras_hub/src/models/gemma3/gemma3_backbone.py +117 -46
- keras_hub/src/models/gemma3/gemma3_causal_lm.py +72 -15
- keras_hub/src/models/gemma3/gemma3_causal_lm_preprocessor.py +512 -355
- keras_hub/src/models/gemma3/gemma3_decoder_block.py +23 -19
- keras_hub/src/models/gemma3/gemma3_image_converter.py +6 -0
- keras_hub/src/models/gemma3/gemma3_interleave_embeddings.py +56 -16
- keras_hub/src/models/gemma3/gemma3_presets.py +74 -8
- keras_hub/src/models/gemma3/gemma3_tokenizer.py +9 -0
- keras_hub/src/models/gemma3/{gemma3_vit.py → gemma3_vision_encoder.py} +150 -139
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_attention.py +2 -2
- keras_hub/src/models/llama/llama_attention.py +2 -2
- keras_hub/src/models/mistral/mistral_attention.py +2 -2
- keras_hub/src/models/mobilenet/mobilenet_backbone.py +18 -1
- keras_hub/src/models/mobilenet/mobilenet_image_classifier.py +4 -1
- keras_hub/src/models/mobilenet/mobilenet_presets.py +38 -2
- keras_hub/src/models/pali_gemma/pali_gemma_vit.py +5 -1
- keras_hub/src/models/phi3/phi3_attention.py +2 -2
- keras_hub/src/models/qwen/__init__.py +1 -0
- keras_hub/src/models/qwen/qwen_attention.py +358 -0
- keras_hub/src/models/qwen/qwen_backbone.py +320 -0
- keras_hub/src/models/qwen/qwen_causal_lm.py +293 -0
- keras_hub/src/models/qwen/qwen_causal_lm_preprocessor.py +11 -0
- keras_hub/src/models/qwen/qwen_decoder.py +311 -0
- keras_hub/src/models/qwen/qwen_layernorm.py +32 -0
- keras_hub/src/models/qwen/qwen_tokenizer.py +42 -0
- keras_hub/src/models/retinanet/retinanet_presets.py +13 -2
- keras_hub/src/models/roformer_v2/roformer_v2_attention.py +212 -0
- keras_hub/src/models/roformer_v2/roformer_v2_backbone.py +198 -0
- keras_hub/src/models/roformer_v2/roformer_v2_encoder.py +128 -0
- keras_hub/src/models/roformer_v2/roformer_v2_masked_lm.py +173 -0
- keras_hub/src/models/roformer_v2/roformer_v2_masked_lm_preprocessor.py +125 -0
- keras_hub/src/models/roformer_v2/roformer_v2_presets.py +0 -0
- keras_hub/src/models/roformer_v2/roformer_v2_text_classifier.py +121 -0
- keras_hub/src/models/roformer_v2/roformer_v2_text_classifier_preprocessor.py +128 -0
- keras_hub/src/models/roformer_v2/roformer_v2_tokenizer.py +62 -0
- keras_hub/src/models/siglip/__init__.py +5 -0
- keras_hub/src/models/siglip/siglip_backbone.py +230 -0
- keras_hub/src/models/siglip/siglip_image_converter.py +8 -0
- keras_hub/src/models/siglip/siglip_layers.py +555 -0
- keras_hub/src/models/siglip/siglip_loss.py +35 -0
- keras_hub/src/models/siglip/siglip_preprocessor.py +162 -0
- keras_hub/src/models/siglip/siglip_presets.py +324 -0
- keras_hub/src/models/siglip/siglip_text_encoder.py +140 -0
- keras_hub/src/models/siglip/siglip_tokenizer.py +77 -0
- keras_hub/src/models/siglip/siglip_vision_encoder.py +151 -0
- keras_hub/src/models/stable_diffusion_3/mmdit.py +2 -2
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_text_to_image_preprocessor.py +4 -2
- keras_hub/src/models/text_to_image_preprocessor.py +35 -0
- keras_hub/src/models/vit/vit_image_converter.py +8 -3
- keras_hub/src/tests/test_case.py +4 -0
- keras_hub/src/utils/keras_utils.py +44 -1
- keras_hub/src/utils/preset_utils.py +3 -1
- keras_hub/src/utils/tensor_utils.py +6 -0
- keras_hub/src/utils/timm/convert_cspnet.py +165 -0
- keras_hub/src/utils/timm/convert_mobilenet.py +120 -44
- keras_hub/src/utils/timm/preset_loader.py +9 -0
- keras_hub/src/utils/transformers/convert_qwen.py +148 -0
- keras_hub/src/utils/transformers/preset_loader.py +3 -0
- keras_hub/src/version_utils.py +1 -1
- {keras_hub-0.19.3.dev0.dist-info → keras_hub-0.20.0.dev0.dist-info}/METADATA +1 -1
- {keras_hub-0.19.3.dev0.dist-info → keras_hub-0.20.0.dev0.dist-info}/RECORD +77 -43
- keras_hub/src/models/csp_darknet/csp_darknet_backbone.py +0 -427
- keras_hub/src/models/csp_darknet/csp_darknet_image_classifier.py +0 -10
- /keras_hub/src/models/{csp_darknet → roformer_v2}/__init__.py +0 -0
- {keras_hub-0.19.3.dev0.dist-info → keras_hub-0.20.0.dev0.dist-info}/WHEEL +0 -0
- {keras_hub-0.19.3.dev0.dist-info → keras_hub-0.20.0.dev0.dist-info}/top_level.txt +0 -0
keras_hub/api/layers/__init__.py
CHANGED
|
@@ -42,6 +42,9 @@ from keras_hub.src.models.basnet.basnet_image_converter import (
|
|
|
42
42
|
BASNetImageConverter,
|
|
43
43
|
)
|
|
44
44
|
from keras_hub.src.models.clip.clip_image_converter import CLIPImageConverter
|
|
45
|
+
from keras_hub.src.models.cspnet.cspnet_image_converter import (
|
|
46
|
+
CSPNetImageConverter,
|
|
47
|
+
)
|
|
45
48
|
from keras_hub.src.models.deeplab_v3.deeplab_v3_image_converter import (
|
|
46
49
|
DeepLabV3ImageConverter,
|
|
47
50
|
)
|
|
@@ -73,6 +76,9 @@ from keras_hub.src.models.sam.sam_prompt_encoder import SAMPromptEncoder
|
|
|
73
76
|
from keras_hub.src.models.segformer.segformer_image_converter import (
|
|
74
77
|
SegFormerImageConverter,
|
|
75
78
|
)
|
|
79
|
+
from keras_hub.src.models.siglip.siglip_image_converter import (
|
|
80
|
+
SigLIPImageConverter,
|
|
81
|
+
)
|
|
76
82
|
from keras_hub.src.models.vgg.vgg_image_converter import VGGImageConverter
|
|
77
83
|
from keras_hub.src.models.vit.vit_image_converter import ViTImageConverter
|
|
78
84
|
from keras_hub.src.models.whisper.whisper_audio_converter import (
|
keras_hub/api/models/__init__.py
CHANGED
|
@@ -61,11 +61,12 @@ from keras_hub.src.models.clip.clip_preprocessor import CLIPPreprocessor
|
|
|
61
61
|
from keras_hub.src.models.clip.clip_text_encoder import CLIPTextEncoder
|
|
62
62
|
from keras_hub.src.models.clip.clip_tokenizer import CLIPTokenizer
|
|
63
63
|
from keras_hub.src.models.clip.clip_vision_encoder import CLIPVisionEncoder
|
|
64
|
-
from keras_hub.src.models.
|
|
65
|
-
|
|
64
|
+
from keras_hub.src.models.cspnet.cspnet_backbone import CSPNetBackbone
|
|
65
|
+
from keras_hub.src.models.cspnet.cspnet_image_classifier import (
|
|
66
|
+
CSPNetImageClassifier,
|
|
66
67
|
)
|
|
67
|
-
from keras_hub.src.models.
|
|
68
|
-
|
|
68
|
+
from keras_hub.src.models.cspnet.cspnet_image_classifier_preprocessor import (
|
|
69
|
+
CSPNetImageClassifierPreprocessor,
|
|
69
70
|
)
|
|
70
71
|
from keras_hub.src.models.deberta_v3.deberta_v3_backbone import (
|
|
71
72
|
DebertaV3Backbone,
|
|
@@ -182,6 +183,9 @@ from keras_hub.src.models.gemma3.gemma3_causal_lm_preprocessor import (
|
|
|
182
183
|
Gemma3CausalLMPreprocessor,
|
|
183
184
|
)
|
|
184
185
|
from keras_hub.src.models.gemma3.gemma3_tokenizer import Gemma3Tokenizer
|
|
186
|
+
from keras_hub.src.models.gemma3.gemma3_vision_encoder import (
|
|
187
|
+
Gemma3VisionEncoder,
|
|
188
|
+
)
|
|
185
189
|
from keras_hub.src.models.gpt2.gpt2_backbone import GPT2Backbone
|
|
186
190
|
from keras_hub.src.models.gpt2.gpt2_causal_lm import GPT2CausalLM
|
|
187
191
|
from keras_hub.src.models.gpt2.gpt2_causal_lm_preprocessor import (
|
|
@@ -304,6 +308,24 @@ from keras_hub.src.models.roberta.roberta_text_classifier_preprocessor import (
|
|
|
304
308
|
RobertaTextClassifierPreprocessor as RobertaPreprocessor,
|
|
305
309
|
)
|
|
306
310
|
from keras_hub.src.models.roberta.roberta_tokenizer import RobertaTokenizer
|
|
311
|
+
from keras_hub.src.models.roformer_v2.roformer_v2_backbone import (
|
|
312
|
+
RoformerV2Backbone as RorformerV2Backbone,
|
|
313
|
+
)
|
|
314
|
+
from keras_hub.src.models.roformer_v2.roformer_v2_masked_lm import (
|
|
315
|
+
RoformerV2MaskedLM,
|
|
316
|
+
)
|
|
317
|
+
from keras_hub.src.models.roformer_v2.roformer_v2_masked_lm_preprocessor import (
|
|
318
|
+
RoformerV2MaskedLMPreprocessor,
|
|
319
|
+
)
|
|
320
|
+
from keras_hub.src.models.roformer_v2.roformer_v2_text_classifier import (
|
|
321
|
+
RorformerV2TextClassifier,
|
|
322
|
+
)
|
|
323
|
+
from keras_hub.src.models.roformer_v2.roformer_v2_text_classifier_preprocessor import (
|
|
324
|
+
RoformerV2TextClassifierPreprocessor,
|
|
325
|
+
)
|
|
326
|
+
from keras_hub.src.models.roformer_v2.roformer_v2_tokenizer import (
|
|
327
|
+
RoformerV2Tokenizer,
|
|
328
|
+
)
|
|
307
329
|
from keras_hub.src.models.sam.sam_backbone import SAMBackbone
|
|
308
330
|
from keras_hub.src.models.sam.sam_image_segmenter import SAMImageSegmenter
|
|
309
331
|
from keras_hub.src.models.sam.sam_image_segmenter_preprocessor import (
|
|
@@ -318,6 +340,13 @@ from keras_hub.src.models.segformer.segformer_image_segmenter_preprocessor impor
|
|
|
318
340
|
)
|
|
319
341
|
from keras_hub.src.models.seq_2_seq_lm import Seq2SeqLM
|
|
320
342
|
from keras_hub.src.models.seq_2_seq_lm_preprocessor import Seq2SeqLMPreprocessor
|
|
343
|
+
from keras_hub.src.models.siglip.siglip_backbone import SigLIPBackbone
|
|
344
|
+
from keras_hub.src.models.siglip.siglip_preprocessor import SigLIPPreprocessor
|
|
345
|
+
from keras_hub.src.models.siglip.siglip_text_encoder import SigLIPTextEncoder
|
|
346
|
+
from keras_hub.src.models.siglip.siglip_tokenizer import SigLIPTokenizer
|
|
347
|
+
from keras_hub.src.models.siglip.siglip_vision_encoder import (
|
|
348
|
+
SigLIPVisionEncoder,
|
|
349
|
+
)
|
|
321
350
|
from keras_hub.src.models.stable_diffusion_3.stable_diffusion_3_backbone import (
|
|
322
351
|
StableDiffusion3Backbone,
|
|
323
352
|
)
|
|
@@ -343,6 +372,9 @@ from keras_hub.src.models.text_classifier_preprocessor import (
|
|
|
343
372
|
TextClassifierPreprocessor,
|
|
344
373
|
)
|
|
345
374
|
from keras_hub.src.models.text_to_image import TextToImage
|
|
375
|
+
from keras_hub.src.models.text_to_image_preprocessor import (
|
|
376
|
+
TextToImagePreprocessor,
|
|
377
|
+
)
|
|
346
378
|
from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone
|
|
347
379
|
from keras_hub.src.models.vgg.vgg_image_classifier import VGGImageClassifier
|
|
348
380
|
from keras_hub.src.models.vgg.vgg_image_classifier_preprocessor import (
|
|
@@ -31,6 +31,10 @@ from keras_hub.src.models.pali_gemma.pali_gemma_tokenizer import (
|
|
|
31
31
|
)
|
|
32
32
|
from keras_hub.src.models.phi3.phi3_tokenizer import Phi3Tokenizer
|
|
33
33
|
from keras_hub.src.models.roberta.roberta_tokenizer import RobertaTokenizer
|
|
34
|
+
from keras_hub.src.models.roformer_v2.roformer_v2_tokenizer import (
|
|
35
|
+
RoformerV2Tokenizer,
|
|
36
|
+
)
|
|
37
|
+
from keras_hub.src.models.siglip.siglip_tokenizer import SigLIPTokenizer
|
|
34
38
|
from keras_hub.src.models.t5.t5_tokenizer import T5Tokenizer
|
|
35
39
|
from keras_hub.src.models.whisper.whisper_tokenizer import WhisperTokenizer
|
|
36
40
|
from keras_hub.src.models.xlm_roberta.xlm_roberta_tokenizer import (
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import math
|
|
2
2
|
|
|
3
3
|
import keras
|
|
4
|
+
import ml_dtypes
|
|
4
5
|
import numpy as np
|
|
5
6
|
from keras import ops
|
|
6
7
|
|
|
@@ -15,9 +16,99 @@ from keras_hub.src.utils.preset_utils import get_preset_loader
|
|
|
15
16
|
from keras_hub.src.utils.preset_utils import get_preset_saver
|
|
16
17
|
from keras_hub.src.utils.python_utils import classproperty
|
|
17
18
|
from keras_hub.src.utils.tensor_utils import check_bounding_box_support
|
|
19
|
+
from keras_hub.src.utils.tensor_utils import in_tf_function
|
|
18
20
|
from keras_hub.src.utils.tensor_utils import preprocessing_function
|
|
19
21
|
|
|
20
22
|
|
|
23
|
+
# TODO: Use `keras.layers.Resizing` once `antialias` is configurable.
|
|
24
|
+
# https://github.com/keras-team/keras/pull/20972
|
|
25
|
+
def _saturate_cast(x, dtype, backend_module):
|
|
26
|
+
def get_dtype_min_max(dtype):
|
|
27
|
+
if "bool" == dtype:
|
|
28
|
+
dtype_min = 0
|
|
29
|
+
dtype_max = 1
|
|
30
|
+
elif "int" in dtype:
|
|
31
|
+
dtype_min = ml_dtypes.iinfo(dtype).min
|
|
32
|
+
dtype_max = ml_dtypes.iinfo(dtype).max
|
|
33
|
+
else:
|
|
34
|
+
dtype_min = ml_dtypes.finfo(dtype).min
|
|
35
|
+
dtype_max = ml_dtypes.finfo(dtype).max
|
|
36
|
+
return dtype_min, dtype_max
|
|
37
|
+
|
|
38
|
+
dtype = keras.backend.standardize_dtype(dtype)
|
|
39
|
+
in_dtype = keras.backend.standardize_dtype(x.dtype)
|
|
40
|
+
in_min, in_max = get_dtype_min_max(in_dtype)
|
|
41
|
+
out_min, out_max = get_dtype_min_max(dtype)
|
|
42
|
+
|
|
43
|
+
min_limit = np.maximum(in_min, out_min).astype(in_dtype)
|
|
44
|
+
if min_limit < out_min:
|
|
45
|
+
min_limit = np.nextafter(min_limit, 0, dtype=in_dtype)
|
|
46
|
+
max_limit = np.minimum(in_max, out_max).astype(in_dtype)
|
|
47
|
+
if max_limit > out_max:
|
|
48
|
+
max_limit = np.nextafter(max_limit, 0, dtype=in_dtype)
|
|
49
|
+
|
|
50
|
+
x = backend_module.numpy.clip(x, min_limit, max_limit)
|
|
51
|
+
return backend_module.cast(x, dtype)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class ResizingAntialiasConfigurable(keras.layers.Resizing):
|
|
55
|
+
"""A preprocessing layer which resizes images.
|
|
56
|
+
|
|
57
|
+
This class is the same as `keras.layers.Resizing` but exposes `antialias` as
|
|
58
|
+
a configurable parameter.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
def __init__(
|
|
62
|
+
self,
|
|
63
|
+
height,
|
|
64
|
+
width,
|
|
65
|
+
interpolation="bilinear",
|
|
66
|
+
antialias=False,
|
|
67
|
+
crop_to_aspect_ratio=False,
|
|
68
|
+
pad_to_aspect_ratio=False,
|
|
69
|
+
fill_mode="constant",
|
|
70
|
+
fill_value=0.0,
|
|
71
|
+
data_format=None,
|
|
72
|
+
**kwargs,
|
|
73
|
+
):
|
|
74
|
+
super().__init__(
|
|
75
|
+
height=height,
|
|
76
|
+
width=width,
|
|
77
|
+
interpolation=interpolation,
|
|
78
|
+
crop_to_aspect_ratio=crop_to_aspect_ratio,
|
|
79
|
+
pad_to_aspect_ratio=pad_to_aspect_ratio,
|
|
80
|
+
fill_mode=fill_mode,
|
|
81
|
+
fill_value=fill_value,
|
|
82
|
+
data_format=data_format,
|
|
83
|
+
**kwargs,
|
|
84
|
+
)
|
|
85
|
+
self.antialias = bool(antialias)
|
|
86
|
+
|
|
87
|
+
def transform_images(self, images, transformation=None, training=True):
|
|
88
|
+
size = (self.height, self.width)
|
|
89
|
+
resized = self.backend.image.resize(
|
|
90
|
+
images,
|
|
91
|
+
size=size,
|
|
92
|
+
interpolation=self.interpolation,
|
|
93
|
+
antialias=self.antialias, # Added.
|
|
94
|
+
data_format=self.data_format,
|
|
95
|
+
crop_to_aspect_ratio=self.crop_to_aspect_ratio,
|
|
96
|
+
pad_to_aspect_ratio=self.pad_to_aspect_ratio,
|
|
97
|
+
fill_mode=self.fill_mode,
|
|
98
|
+
fill_value=self.fill_value,
|
|
99
|
+
)
|
|
100
|
+
if resized.dtype == images.dtype:
|
|
101
|
+
return resized
|
|
102
|
+
if keras.backend.is_int_dtype(images.dtype):
|
|
103
|
+
resized = self.backend.numpy.round(resized)
|
|
104
|
+
return _saturate_cast(resized, images.dtype, self.backend)
|
|
105
|
+
|
|
106
|
+
def get_config(self):
|
|
107
|
+
config = super().get_config()
|
|
108
|
+
config.update({"antialias": self.antialias})
|
|
109
|
+
return config
|
|
110
|
+
|
|
111
|
+
|
|
21
112
|
@keras_hub_export("keras_hub.layers.ImageConverter")
|
|
22
113
|
class ImageConverter(PreprocessingLayer):
|
|
23
114
|
"""Preprocess raw images into model ready inputs.
|
|
@@ -65,6 +156,8 @@ class ImageConverter(PreprocessingLayer):
|
|
|
65
156
|
interpolation: String, the interpolation method.
|
|
66
157
|
Supports `"bilinear"`, `"nearest"`, `"bicubic"`,
|
|
67
158
|
`"lanczos3"`, `"lanczos5"`. Defaults to `"bilinear"`.
|
|
159
|
+
antialias: Whether to use an antialiasing filter when downsampling an
|
|
160
|
+
image. Defaults to `False`.
|
|
68
161
|
bounding_box_format: A string specifying the format of the bounding
|
|
69
162
|
boxes, one of `"xyxy"`, `"rel_xyxy"`, `"xywh"`, `"center_xywh"`,
|
|
70
163
|
`"yxyx"`, `"rel_yxyx"`. Specifies the format of the bounding boxes
|
|
@@ -107,6 +200,7 @@ class ImageConverter(PreprocessingLayer):
|
|
|
107
200
|
crop_to_aspect_ratio=True,
|
|
108
201
|
pad_to_aspect_ratio=False,
|
|
109
202
|
interpolation="bilinear",
|
|
203
|
+
antialias=False,
|
|
110
204
|
bounding_box_format="yxyx",
|
|
111
205
|
data_format=None,
|
|
112
206
|
**kwargs,
|
|
@@ -132,12 +226,13 @@ class ImageConverter(PreprocessingLayer):
|
|
|
132
226
|
resizing_kwargs = {}
|
|
133
227
|
if check_bounding_box_support():
|
|
134
228
|
resizing_kwargs["bounding_box_format"] = bounding_box_format
|
|
135
|
-
self.resizing =
|
|
229
|
+
self.resizing = ResizingAntialiasConfigurable(
|
|
136
230
|
height=image_size[0] if image_size else None,
|
|
137
231
|
width=image_size[1] if image_size else None,
|
|
138
232
|
crop_to_aspect_ratio=crop_to_aspect_ratio,
|
|
139
233
|
pad_to_aspect_ratio=pad_to_aspect_ratio,
|
|
140
234
|
interpolation=interpolation,
|
|
235
|
+
antialias=antialias,
|
|
141
236
|
data_format=data_format,
|
|
142
237
|
dtype=self.dtype_policy,
|
|
143
238
|
name="resizing",
|
|
@@ -148,6 +243,7 @@ class ImageConverter(PreprocessingLayer):
|
|
|
148
243
|
self.crop_to_aspect_ratio = crop_to_aspect_ratio
|
|
149
244
|
self.pad_to_aspect_ratio = pad_to_aspect_ratio
|
|
150
245
|
self.interpolation = interpolation
|
|
246
|
+
self.antialias = antialias
|
|
151
247
|
self.bounding_box_format = bounding_box_format
|
|
152
248
|
self.data_format = standardize_data_format(data_format)
|
|
153
249
|
|
|
@@ -175,9 +271,15 @@ class ImageConverter(PreprocessingLayer):
|
|
|
175
271
|
else:
|
|
176
272
|
x = inputs
|
|
177
273
|
if self.scale is not None:
|
|
178
|
-
|
|
274
|
+
# If we are scaling always cast to the compute dtype. We can't
|
|
275
|
+
# leave things as an int type if we are scaling to [0, 1].
|
|
276
|
+
scale = self._expand_non_channel_dims(self.scale, x)
|
|
277
|
+
x, scale = self._convert_types(x, scale, self.compute_dtype)
|
|
278
|
+
x = x * scale
|
|
179
279
|
if self.offset is not None:
|
|
180
|
-
|
|
280
|
+
offset = self._expand_non_channel_dims(self.offset, x)
|
|
281
|
+
x, offset = self._convert_types(x, offset, x.dtype)
|
|
282
|
+
x = x + offset
|
|
181
283
|
if isinstance(inputs, dict):
|
|
182
284
|
inputs["images"] = x
|
|
183
285
|
else:
|
|
@@ -185,23 +287,29 @@ class ImageConverter(PreprocessingLayer):
|
|
|
185
287
|
return inputs
|
|
186
288
|
|
|
187
289
|
def _expand_non_channel_dims(self, value, inputs):
|
|
290
|
+
"""Expand non channel dims so value is broadcastable with inputs."""
|
|
188
291
|
unbatched = len(ops.shape(inputs)) == 3
|
|
189
292
|
channels_first = self.data_format == "channels_first"
|
|
190
293
|
if unbatched:
|
|
191
294
|
broadcast_dims = (1, 2) if channels_first else (0, 1)
|
|
192
295
|
else:
|
|
193
296
|
broadcast_dims = (0, 2, 3) if channels_first else (0, 1, 2)
|
|
194
|
-
#
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
297
|
+
# An numpy value will work backend native ops or with tf.data.
|
|
298
|
+
return np.expand_dims(value, broadcast_dims)
|
|
299
|
+
|
|
300
|
+
def _convert_types(self, x, y, dtype):
|
|
301
|
+
"""Make sure x and y have the same dtype and are on ths same device."""
|
|
302
|
+
if in_tf_function():
|
|
303
|
+
# This could happen on any backend if we are running in tf.data.
|
|
304
|
+
import tensorflow as tf
|
|
305
|
+
|
|
306
|
+
return tf.cast(x, dtype), tf.cast(y, dtype)
|
|
307
|
+
x = ops.cast(x, dtype)
|
|
308
|
+
y = ops.cast(y, dtype)
|
|
309
|
+
if keras.backend.backend() == "torch":
|
|
310
|
+
# Place on the same device as x (the image).
|
|
311
|
+
y = y.to(x.device)
|
|
312
|
+
return x, y
|
|
205
313
|
|
|
206
314
|
def get_config(self):
|
|
207
315
|
config = super().get_config()
|
|
@@ -211,6 +319,7 @@ class ImageConverter(PreprocessingLayer):
|
|
|
211
319
|
"scale": self.scale,
|
|
212
320
|
"offset": self.offset,
|
|
213
321
|
"interpolation": self.interpolation,
|
|
322
|
+
"antialias": self.antialias,
|
|
214
323
|
"crop_to_aspect_ratio": self.crop_to_aspect_ratio,
|
|
215
324
|
"pad_to_aspect_ratio": self.pad_to_aspect_ratio,
|
|
216
325
|
"bounding_box_format": self.bounding_box_format,
|
keras_hub/src/models/backbone.py
CHANGED
|
@@ -194,15 +194,15 @@ class Backbone(keras.Model):
|
|
|
194
194
|
"""
|
|
195
195
|
return ["query_dense", "value_dense", "query", "value"]
|
|
196
196
|
|
|
197
|
-
def enable_lora(self, rank):
|
|
197
|
+
def enable_lora(self, rank, target_names=None):
|
|
198
198
|
"""Enable Lora on the backbone.
|
|
199
199
|
|
|
200
200
|
Calling this method will freeze all weights on the backbone,
|
|
201
201
|
while enabling Lora on the query & value `EinsumDense` layers
|
|
202
202
|
of the attention layers.
|
|
203
203
|
"""
|
|
204
|
-
target_names
|
|
205
|
-
|
|
204
|
+
if target_names is None:
|
|
205
|
+
target_names = self.get_lora_target_names()
|
|
206
206
|
self.trainable = True
|
|
207
207
|
self._lora_enabled_layers = []
|
|
208
208
|
self._lora_rank = rank
|