keras-hub-nightly 0.15.0.dev20240823171555__py3-none-any.whl → 0.16.0.dev2024092017__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/__init__.py +0 -6
- keras_hub/api/__init__.py +2 -0
- keras_hub/api/bounding_box/__init__.py +36 -0
- keras_hub/api/layers/__init__.py +14 -0
- keras_hub/api/models/__init__.py +97 -48
- keras_hub/api/tokenizers/__init__.py +30 -0
- keras_hub/api/utils/__init__.py +22 -0
- keras_hub/src/api_export.py +15 -9
- keras_hub/src/bounding_box/__init__.py +13 -0
- keras_hub/src/bounding_box/converters.py +529 -0
- keras_hub/src/bounding_box/formats.py +162 -0
- keras_hub/src/bounding_box/iou.py +263 -0
- keras_hub/src/bounding_box/to_dense.py +95 -0
- keras_hub/src/bounding_box/to_ragged.py +99 -0
- keras_hub/src/bounding_box/utils.py +194 -0
- keras_hub/src/bounding_box/validate_format.py +99 -0
- keras_hub/src/layers/preprocessing/audio_converter.py +121 -0
- keras_hub/src/layers/preprocessing/image_converter.py +130 -0
- keras_hub/src/layers/preprocessing/masked_lm_mask_generator.py +2 -0
- keras_hub/src/layers/preprocessing/multi_segment_packer.py +9 -8
- keras_hub/src/layers/preprocessing/preprocessing_layer.py +2 -29
- keras_hub/src/layers/preprocessing/random_deletion.py +33 -31
- keras_hub/src/layers/preprocessing/random_swap.py +33 -31
- keras_hub/src/layers/preprocessing/resizing_image_converter.py +101 -0
- keras_hub/src/layers/preprocessing/start_end_packer.py +3 -2
- keras_hub/src/models/albert/__init__.py +1 -2
- keras_hub/src/models/albert/albert_masked_lm_preprocessor.py +6 -86
- keras_hub/src/models/albert/{albert_classifier.py → albert_text_classifier.py} +34 -10
- keras_hub/src/models/albert/{albert_preprocessor.py → albert_text_classifier_preprocessor.py} +14 -70
- keras_hub/src/models/albert/albert_tokenizer.py +17 -36
- keras_hub/src/models/backbone.py +12 -34
- keras_hub/src/models/bart/__init__.py +1 -2
- keras_hub/src/models/bart/bart_seq_2_seq_lm_preprocessor.py +21 -148
- keras_hub/src/models/bart/bart_tokenizer.py +12 -39
- keras_hub/src/models/bert/__init__.py +1 -5
- keras_hub/src/models/bert/bert_masked_lm_preprocessor.py +6 -87
- keras_hub/src/models/bert/bert_presets.py +1 -4
- keras_hub/src/models/bert/{bert_classifier.py → bert_text_classifier.py} +19 -12
- keras_hub/src/models/bert/{bert_preprocessor.py → bert_text_classifier_preprocessor.py} +14 -70
- keras_hub/src/models/bert/bert_tokenizer.py +17 -35
- keras_hub/src/models/bloom/__init__.py +1 -2
- keras_hub/src/models/bloom/bloom_causal_lm_preprocessor.py +6 -91
- keras_hub/src/models/bloom/bloom_tokenizer.py +12 -41
- keras_hub/src/models/causal_lm.py +10 -29
- keras_hub/src/models/causal_lm_preprocessor.py +195 -0
- keras_hub/src/models/csp_darknet/csp_darknet_backbone.py +54 -15
- keras_hub/src/models/deberta_v3/__init__.py +1 -4
- keras_hub/src/models/deberta_v3/deberta_v3_masked_lm_preprocessor.py +14 -77
- keras_hub/src/models/deberta_v3/{deberta_v3_classifier.py → deberta_v3_text_classifier.py} +16 -11
- keras_hub/src/models/deberta_v3/{deberta_v3_preprocessor.py → deberta_v3_text_classifier_preprocessor.py} +23 -64
- keras_hub/src/models/deberta_v3/deberta_v3_tokenizer.py +30 -25
- keras_hub/src/models/densenet/densenet_backbone.py +46 -22
- keras_hub/src/models/distil_bert/__init__.py +1 -4
- keras_hub/src/models/distil_bert/distil_bert_masked_lm_preprocessor.py +14 -76
- keras_hub/src/models/distil_bert/{distil_bert_classifier.py → distil_bert_text_classifier.py} +17 -12
- keras_hub/src/models/distil_bert/{distil_bert_preprocessor.py → distil_bert_text_classifier_preprocessor.py} +23 -63
- keras_hub/src/models/distil_bert/distil_bert_tokenizer.py +19 -35
- keras_hub/src/models/efficientnet/__init__.py +13 -0
- keras_hub/src/models/efficientnet/efficientnet_backbone.py +569 -0
- keras_hub/src/models/efficientnet/fusedmbconv.py +229 -0
- keras_hub/src/models/efficientnet/mbconv.py +238 -0
- keras_hub/src/models/electra/__init__.py +1 -2
- keras_hub/src/models/electra/electra_tokenizer.py +17 -32
- keras_hub/src/models/f_net/__init__.py +1 -2
- keras_hub/src/models/f_net/f_net_masked_lm_preprocessor.py +12 -78
- keras_hub/src/models/f_net/{f_net_classifier.py → f_net_text_classifier.py} +17 -10
- keras_hub/src/models/f_net/{f_net_preprocessor.py → f_net_text_classifier_preprocessor.py} +19 -63
- keras_hub/src/models/f_net/f_net_tokenizer.py +17 -35
- keras_hub/src/models/falcon/__init__.py +1 -2
- keras_hub/src/models/falcon/falcon_causal_lm_preprocessor.py +6 -89
- keras_hub/src/models/falcon/falcon_tokenizer.py +12 -35
- keras_hub/src/models/gemma/__init__.py +1 -2
- keras_hub/src/models/gemma/gemma_causal_lm_preprocessor.py +6 -90
- keras_hub/src/models/gemma/gemma_decoder_block.py +1 -1
- keras_hub/src/models/gemma/gemma_tokenizer.py +12 -23
- keras_hub/src/models/gpt2/__init__.py +1 -2
- keras_hub/src/models/gpt2/gpt2_causal_lm_preprocessor.py +6 -89
- keras_hub/src/models/gpt2/gpt2_preprocessor.py +12 -90
- keras_hub/src/models/gpt2/gpt2_tokenizer.py +12 -34
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_causal_lm_preprocessor.py +6 -91
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_tokenizer.py +12 -34
- keras_hub/src/models/image_classifier.py +0 -5
- keras_hub/src/models/image_classifier_preprocessor.py +83 -0
- keras_hub/src/models/llama/__init__.py +1 -2
- keras_hub/src/models/llama/llama_causal_lm_preprocessor.py +6 -85
- keras_hub/src/models/llama/llama_tokenizer.py +12 -25
- keras_hub/src/models/llama3/__init__.py +1 -2
- keras_hub/src/models/llama3/llama3_causal_lm_preprocessor.py +6 -89
- keras_hub/src/models/llama3/llama3_tokenizer.py +12 -33
- keras_hub/src/models/masked_lm.py +0 -2
- keras_hub/src/models/masked_lm_preprocessor.py +156 -0
- keras_hub/src/models/mistral/__init__.py +1 -2
- keras_hub/src/models/mistral/mistral_causal_lm_preprocessor.py +6 -91
- keras_hub/src/models/mistral/mistral_tokenizer.py +12 -23
- keras_hub/src/models/mix_transformer/mix_transformer_backbone.py +2 -2
- keras_hub/src/models/mobilenet/__init__.py +13 -0
- keras_hub/src/models/mobilenet/mobilenet_backbone.py +530 -0
- keras_hub/src/models/mobilenet/mobilenet_image_classifier.py +114 -0
- keras_hub/src/models/opt/__init__.py +1 -2
- keras_hub/src/models/opt/opt_causal_lm_preprocessor.py +6 -93
- keras_hub/src/models/opt/opt_tokenizer.py +12 -41
- keras_hub/src/models/pali_gemma/__init__.py +1 -4
- keras_hub/src/models/pali_gemma/pali_gemma_causal_lm_preprocessor.py +28 -28
- keras_hub/src/models/pali_gemma/pali_gemma_image_converter.py +25 -0
- keras_hub/src/models/pali_gemma/pali_gemma_presets.py +5 -5
- keras_hub/src/models/pali_gemma/pali_gemma_tokenizer.py +11 -3
- keras_hub/src/models/phi3/__init__.py +1 -2
- keras_hub/src/models/phi3/phi3_causal_lm.py +3 -9
- keras_hub/src/models/phi3/phi3_causal_lm_preprocessor.py +6 -89
- keras_hub/src/models/phi3/phi3_tokenizer.py +12 -36
- keras_hub/src/models/preprocessor.py +72 -83
- keras_hub/src/models/resnet/__init__.py +6 -0
- keras_hub/src/models/resnet/resnet_backbone.py +390 -42
- keras_hub/src/models/resnet/resnet_image_classifier.py +33 -6
- keras_hub/src/models/resnet/resnet_image_classifier_preprocessor.py +28 -0
- keras_hub/src/models/{llama3/llama3_preprocessor.py → resnet/resnet_image_converter.py} +7 -5
- keras_hub/src/models/resnet/resnet_presets.py +95 -0
- keras_hub/src/models/retinanet/__init__.py +13 -0
- keras_hub/src/models/retinanet/anchor_generator.py +175 -0
- keras_hub/src/models/retinanet/box_matcher.py +259 -0
- keras_hub/src/models/retinanet/non_max_supression.py +578 -0
- keras_hub/src/models/roberta/__init__.py +1 -2
- keras_hub/src/models/roberta/roberta_masked_lm_preprocessor.py +22 -74
- keras_hub/src/models/roberta/{roberta_classifier.py → roberta_text_classifier.py} +16 -11
- keras_hub/src/models/roberta/{roberta_preprocessor.py → roberta_text_classifier_preprocessor.py} +21 -53
- keras_hub/src/models/roberta/roberta_tokenizer.py +13 -52
- keras_hub/src/models/seq_2_seq_lm_preprocessor.py +269 -0
- keras_hub/src/models/stable_diffusion_v3/__init__.py +13 -0
- keras_hub/src/models/stable_diffusion_v3/clip_encoder_block.py +103 -0
- keras_hub/src/models/stable_diffusion_v3/clip_preprocessor.py +93 -0
- keras_hub/src/models/stable_diffusion_v3/clip_text_encoder.py +149 -0
- keras_hub/src/models/stable_diffusion_v3/clip_tokenizer.py +167 -0
- keras_hub/src/models/stable_diffusion_v3/mmdit.py +427 -0
- keras_hub/src/models/stable_diffusion_v3/mmdit_block.py +317 -0
- keras_hub/src/models/stable_diffusion_v3/t5_xxl_preprocessor.py +74 -0
- keras_hub/src/models/stable_diffusion_v3/t5_xxl_text_encoder.py +155 -0
- keras_hub/src/models/stable_diffusion_v3/vae_attention.py +126 -0
- keras_hub/src/models/stable_diffusion_v3/vae_image_decoder.py +186 -0
- keras_hub/src/models/t5/__init__.py +1 -2
- keras_hub/src/models/t5/t5_tokenizer.py +13 -23
- keras_hub/src/models/task.py +71 -116
- keras_hub/src/models/{classifier.py → text_classifier.py} +19 -13
- keras_hub/src/models/text_classifier_preprocessor.py +138 -0
- keras_hub/src/models/whisper/__init__.py +1 -2
- keras_hub/src/models/whisper/{whisper_audio_feature_extractor.py → whisper_audio_converter.py} +20 -18
- keras_hub/src/models/whisper/whisper_backbone.py +0 -3
- keras_hub/src/models/whisper/whisper_presets.py +10 -10
- keras_hub/src/models/whisper/whisper_tokenizer.py +20 -16
- keras_hub/src/models/xlm_roberta/__init__.py +1 -4
- keras_hub/src/models/xlm_roberta/xlm_roberta_masked_lm_preprocessor.py +26 -72
- keras_hub/src/models/xlm_roberta/{xlm_roberta_classifier.py → xlm_roberta_text_classifier.py} +16 -11
- keras_hub/src/models/xlm_roberta/{xlm_roberta_preprocessor.py → xlm_roberta_text_classifier_preprocessor.py} +26 -53
- keras_hub/src/models/xlm_roberta/xlm_roberta_tokenizer.py +25 -10
- keras_hub/src/tests/test_case.py +46 -0
- keras_hub/src/tokenizers/byte_pair_tokenizer.py +30 -17
- keras_hub/src/tokenizers/byte_tokenizer.py +14 -15
- keras_hub/src/tokenizers/sentence_piece_tokenizer.py +20 -7
- keras_hub/src/tokenizers/tokenizer.py +67 -32
- keras_hub/src/tokenizers/unicode_codepoint_tokenizer.py +14 -15
- keras_hub/src/tokenizers/word_piece_tokenizer.py +34 -47
- keras_hub/src/utils/imagenet/__init__.py +13 -0
- keras_hub/src/utils/imagenet/imagenet_utils.py +1067 -0
- keras_hub/src/utils/keras_utils.py +0 -50
- keras_hub/src/utils/preset_utils.py +230 -68
- keras_hub/src/utils/tensor_utils.py +187 -69
- keras_hub/src/utils/timm/convert_resnet.py +19 -16
- keras_hub/src/utils/timm/preset_loader.py +66 -0
- keras_hub/src/utils/transformers/convert_albert.py +193 -0
- keras_hub/src/utils/transformers/convert_bart.py +373 -0
- keras_hub/src/utils/transformers/convert_bert.py +7 -17
- keras_hub/src/utils/transformers/convert_distilbert.py +10 -20
- keras_hub/src/utils/transformers/convert_gemma.py +5 -19
- keras_hub/src/utils/transformers/convert_gpt2.py +5 -18
- keras_hub/src/utils/transformers/convert_llama3.py +7 -18
- keras_hub/src/utils/transformers/convert_mistral.py +129 -0
- keras_hub/src/utils/transformers/convert_pali_gemma.py +7 -29
- keras_hub/src/utils/transformers/preset_loader.py +77 -0
- keras_hub/src/utils/transformers/safetensor_utils.py +2 -2
- keras_hub/src/version_utils.py +1 -1
- keras_hub_nightly-0.16.0.dev2024092017.dist-info/METADATA +202 -0
- keras_hub_nightly-0.16.0.dev2024092017.dist-info/RECORD +334 -0
- {keras_hub_nightly-0.15.0.dev20240823171555.dist-info → keras_hub_nightly-0.16.0.dev2024092017.dist-info}/WHEEL +1 -1
- keras_hub/src/models/bart/bart_preprocessor.py +0 -276
- keras_hub/src/models/bloom/bloom_preprocessor.py +0 -185
- keras_hub/src/models/electra/electra_preprocessor.py +0 -154
- keras_hub/src/models/falcon/falcon_preprocessor.py +0 -187
- keras_hub/src/models/gemma/gemma_preprocessor.py +0 -191
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_preprocessor.py +0 -145
- keras_hub/src/models/llama/llama_preprocessor.py +0 -189
- keras_hub/src/models/mistral/mistral_preprocessor.py +0 -190
- keras_hub/src/models/opt/opt_preprocessor.py +0 -188
- keras_hub/src/models/phi3/phi3_preprocessor.py +0 -190
- keras_hub/src/models/whisper/whisper_preprocessor.py +0 -326
- keras_hub/src/utils/timm/convert.py +0 -37
- keras_hub/src/utils/transformers/convert.py +0 -101
- keras_hub_nightly-0.15.0.dev20240823171555.dist-info/METADATA +0 -34
- keras_hub_nightly-0.15.0.dev20240823171555.dist-info/RECORD +0 -297
- {keras_hub_nightly-0.15.0.dev20240823171555.dist-info → keras_hub_nightly-0.16.0.dev2024092017.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,99 @@
|
|
1
|
+
# Copyright 2024 The KerasHub Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
from keras_hub.src.api_export import keras_hub_export
|
16
|
+
|
17
|
+
try:
|
18
|
+
import tensorflow as tf
|
19
|
+
except ImportError:
|
20
|
+
tf = None
|
21
|
+
|
22
|
+
|
23
|
+
@keras_hub_export("keras_hub.bounding_box.validate_format")
|
24
|
+
def validate_format(bounding_boxes, variable_name="bounding_boxes"):
|
25
|
+
"""validates that a given set of bounding boxes complies with KerasHub
|
26
|
+
format.
|
27
|
+
|
28
|
+
For a set of bounding boxes to be valid it must satisfy the following
|
29
|
+
conditions:
|
30
|
+
- `bounding_boxes` must be a dictionary
|
31
|
+
- contains keys `"boxes"` and `"classes"`
|
32
|
+
- each entry must have matching first two dimensions; representing the batch
|
33
|
+
axis and the number of boxes per image axis.
|
34
|
+
- either both `"boxes"` and `"classes"` are batched, or both are unbatched.
|
35
|
+
|
36
|
+
Additionally, one of the following must be satisfied:
|
37
|
+
- `"boxes"` and `"classes"` are both Ragged
|
38
|
+
- `"boxes"` and `"classes"` are both Dense
|
39
|
+
- `"boxes"` and `"classes"` are unbatched
|
40
|
+
|
41
|
+
Args:
|
42
|
+
bounding_boxes: dictionary of bounding boxes according to KerasCV
|
43
|
+
format.
|
44
|
+
|
45
|
+
Raises:
|
46
|
+
ValueError if any of the above conditions are not met
|
47
|
+
"""
|
48
|
+
if not isinstance(bounding_boxes, dict):
|
49
|
+
raise ValueError(
|
50
|
+
f"Expected `{variable_name}` to be a dictionary, got "
|
51
|
+
f"`{variable_name}={bounding_boxes}`."
|
52
|
+
)
|
53
|
+
if not all([x in bounding_boxes for x in ["boxes", "classes"]]):
|
54
|
+
raise ValueError(
|
55
|
+
f"Expected `{variable_name}` to be a dictionary containing keys "
|
56
|
+
"`'classes'` and `'boxes'`. Got "
|
57
|
+
f"`{variable_name}.keys()={bounding_boxes.keys()}`."
|
58
|
+
)
|
59
|
+
|
60
|
+
boxes = bounding_boxes.get("boxes")
|
61
|
+
classes = bounding_boxes.get("classes")
|
62
|
+
info = {}
|
63
|
+
|
64
|
+
is_batched = len(boxes.shape) == 3
|
65
|
+
info["is_batched"] = is_batched
|
66
|
+
info["ragged"] = isinstance(boxes, tf.RaggedTensor)
|
67
|
+
|
68
|
+
if not is_batched:
|
69
|
+
if boxes.shape[:1] != classes.shape[:1]:
|
70
|
+
raise ValueError(
|
71
|
+
"Expected `boxes` and `classes` to have matching dimensions "
|
72
|
+
"on the first axis when operating in unbatched mode. Got "
|
73
|
+
f"`boxes.shape={boxes.shape}`, `classes.shape={classes.shape}`."
|
74
|
+
)
|
75
|
+
|
76
|
+
info["classes_one_hot"] = len(classes.shape) == 2
|
77
|
+
# No Ragged checks needed in unbatched mode.
|
78
|
+
return info
|
79
|
+
|
80
|
+
info["classes_one_hot"] = len(classes.shape) == 3
|
81
|
+
|
82
|
+
if isinstance(boxes, tf.RaggedTensor) != isinstance(
|
83
|
+
classes, tf.RaggedTensor
|
84
|
+
):
|
85
|
+
raise ValueError(
|
86
|
+
"Either both `boxes` and `classes` "
|
87
|
+
"should be Ragged, or neither should be ragged."
|
88
|
+
f" Got `type(boxes)={type(boxes)}`, type(classes)={type(classes)}."
|
89
|
+
)
|
90
|
+
|
91
|
+
# Batched mode checks
|
92
|
+
if boxes.shape[:2] != classes.shape[:2]:
|
93
|
+
raise ValueError(
|
94
|
+
"Expected `boxes` and `classes` to have matching dimensions "
|
95
|
+
"on the first two axes when operating in batched mode. "
|
96
|
+
f"Got `boxes.shape={boxes.shape}`, `classes.shape={classes.shape}`."
|
97
|
+
)
|
98
|
+
|
99
|
+
return info
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# Copyright 2024 The KerasHub Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
from keras_hub.src.api_export import keras_hub_export
|
15
|
+
from keras_hub.src.layers.preprocessing.preprocessing_layer import (
|
16
|
+
PreprocessingLayer,
|
17
|
+
)
|
18
|
+
from keras_hub.src.utils.preset_utils import AUDIO_CONVERTER_CONFIG_FILE
|
19
|
+
from keras_hub.src.utils.preset_utils import builtin_presets
|
20
|
+
from keras_hub.src.utils.preset_utils import find_subclass
|
21
|
+
from keras_hub.src.utils.preset_utils import get_preset_loader
|
22
|
+
from keras_hub.src.utils.preset_utils import save_serialized_object
|
23
|
+
from keras_hub.src.utils.python_utils import classproperty
|
24
|
+
|
25
|
+
|
26
|
+
@keras_hub_export("keras_hub.layers.AudioConverter")
|
27
|
+
class AudioConverter(PreprocessingLayer):
|
28
|
+
"""Convert raw audio for models that support audio input.
|
29
|
+
|
30
|
+
This class converts from raw audio tensors of any length, to preprocessed
|
31
|
+
audio for pretrained model inputs. It is meant to be a convenient way to
|
32
|
+
write custom preprocessing code that is not model specific. This layer
|
33
|
+
should be instantiated via the `from_preset()` constructor, which will
|
34
|
+
create the correct subclass of this layer for the model preset.
|
35
|
+
|
36
|
+
The layer will take as input a raw audio tensor with shape `(batch_size,
|
37
|
+
num_samples)`, and output a preprocessed audio input for modeling. The exact
|
38
|
+
structure of the preprocessed input will vary per model. Preprocessing
|
39
|
+
will often include computing a spectogram of the raw audio signal.
|
40
|
+
|
41
|
+
Examples:
|
42
|
+
```python
|
43
|
+
# Load an audio converter from a preset.
|
44
|
+
converter = keras_hub.layers.AudioConverter.from_preset("whisper_base_en")
|
45
|
+
# Convert some raw audio input.
|
46
|
+
converter(np.ones(2, 1_000))
|
47
|
+
```
|
48
|
+
"""
|
49
|
+
|
50
|
+
backbone_cls = None
|
51
|
+
|
52
|
+
def audio_shape(self):
|
53
|
+
"""Returns the preprocessed size of a single audio sample."""
|
54
|
+
return (None,)
|
55
|
+
|
56
|
+
@classproperty
|
57
|
+
def presets(cls):
|
58
|
+
"""List built-in presets for an `AudioConverter` subclass."""
|
59
|
+
return builtin_presets(cls)
|
60
|
+
|
61
|
+
@classmethod
|
62
|
+
def from_preset(
|
63
|
+
cls,
|
64
|
+
preset,
|
65
|
+
**kwargs,
|
66
|
+
):
|
67
|
+
"""Instantiate a `keras_hub.layers.AudioConverter` from a model preset.
|
68
|
+
|
69
|
+
A preset is a directory of configs, weights and other file assets used
|
70
|
+
to save and load a pre-trained model. The `preset` can be passed as
|
71
|
+
one of:
|
72
|
+
|
73
|
+
1. a built-in preset identifier like `'whisper_base_en'`
|
74
|
+
2. a Kaggle Models handle like
|
75
|
+
`'kaggle://user/whisper/keras/whisper_base_en'`
|
76
|
+
3. a Hugging Face handle like `'hf://user/whisper_base_en'`
|
77
|
+
4. a path to a local preset directory like `'./whisper_base_en'`
|
78
|
+
|
79
|
+
You can run `cls.presets.keys()` to list all built-in presets available
|
80
|
+
on the class.
|
81
|
+
|
82
|
+
This constructor can be called in one of two ways. Either from the base
|
83
|
+
class like `keras_hub.models.AudioConverter.from_preset()`, or from a
|
84
|
+
model class like `keras_hub.models.WhisperAudioConverter.from_preset()`.
|
85
|
+
If calling from the base class, the subclass of the returning object
|
86
|
+
will be inferred from the config in the preset directory.
|
87
|
+
|
88
|
+
Args:
|
89
|
+
preset: string. A built-in preset identifier, a Kaggle Models
|
90
|
+
handle, a Hugging Face handle, or a path to a local directory.
|
91
|
+
load_weights: bool. If `True`, the weights will be loaded into the
|
92
|
+
model architecture. If `False`, the weights will be randomly
|
93
|
+
initialized.
|
94
|
+
|
95
|
+
Examples:
|
96
|
+
```python
|
97
|
+
# Load an audio converter from a preset.
|
98
|
+
converter = keras_hub.layers.AudioConverter.from_preset(
|
99
|
+
"whisper_base_en"
|
100
|
+
)
|
101
|
+
# Convert some raw mono channel audio input.
|
102
|
+
converter(np.ones(2, 1_000))
|
103
|
+
```
|
104
|
+
"""
|
105
|
+
loader = get_preset_loader(preset)
|
106
|
+
backbone_cls = loader.check_backbone_class()
|
107
|
+
if cls.backbone_cls != backbone_cls:
|
108
|
+
cls = find_subclass(preset, cls, backbone_cls)
|
109
|
+
return loader.load_audio_converter(cls, **kwargs)
|
110
|
+
|
111
|
+
def save_to_preset(self, preset_dir):
|
112
|
+
"""Save audio converter to a preset directory.
|
113
|
+
|
114
|
+
Args:
|
115
|
+
preset_dir: The path to the local model preset directory.
|
116
|
+
"""
|
117
|
+
save_serialized_object(
|
118
|
+
self,
|
119
|
+
preset_dir,
|
120
|
+
config_file=AUDIO_CONVERTER_CONFIG_FILE,
|
121
|
+
)
|
@@ -0,0 +1,130 @@
|
|
1
|
+
# Copyright 2024 The KerasHub Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
from keras_hub.src.api_export import keras_hub_export
|
15
|
+
from keras_hub.src.layers.preprocessing.preprocessing_layer import (
|
16
|
+
PreprocessingLayer,
|
17
|
+
)
|
18
|
+
from keras_hub.src.utils.preset_utils import IMAGE_CONVERTER_CONFIG_FILE
|
19
|
+
from keras_hub.src.utils.preset_utils import builtin_presets
|
20
|
+
from keras_hub.src.utils.preset_utils import find_subclass
|
21
|
+
from keras_hub.src.utils.preset_utils import get_preset_loader
|
22
|
+
from keras_hub.src.utils.preset_utils import save_serialized_object
|
23
|
+
from keras_hub.src.utils.python_utils import classproperty
|
24
|
+
|
25
|
+
|
26
|
+
@keras_hub_export("keras_hub.layers.ImageConverter")
|
27
|
+
class ImageConverter(PreprocessingLayer):
|
28
|
+
"""Convert raw image for models that support image input.
|
29
|
+
|
30
|
+
This class converts from raw images of any size, to preprocessed
|
31
|
+
images for pretrained model inputs. It is meant to be a convenient way to
|
32
|
+
write custom preprocessing code that is not model specific. This layer
|
33
|
+
should be instantiated via the `from_preset()` constructor, which will
|
34
|
+
create the correct subclass of this layer for the model preset.
|
35
|
+
|
36
|
+
The layer will take as input a raw image tensor in the channels last or
|
37
|
+
channels first format, and output a preprocessed image input for modeling.
|
38
|
+
The exact structure of the output will vary per model, though in most cases
|
39
|
+
this layer will simply resize the image to the size needed by the model
|
40
|
+
input.
|
41
|
+
|
42
|
+
Examples:
|
43
|
+
```python
|
44
|
+
# Resize images for `"pali_gemma_3b_224"`.
|
45
|
+
converter = keras_hub.layers.ImageConverter.from_preset("pali_gemma_3b_224")
|
46
|
+
converter(np.ones(2, 512, 512, 3)) # Output shape: (2, 224, 224, 3)
|
47
|
+
# Resize images for `"pali_gemma_3b_448"`.
|
48
|
+
converter = keras_hub.layers.ImageConverter.from_preset("pali_gemma_3b_448")
|
49
|
+
converter(np.ones(2, 512, 512, 3)) # Output shape: (2, 448, 448, 3)
|
50
|
+
```
|
51
|
+
"""
|
52
|
+
|
53
|
+
backbone_cls = None
|
54
|
+
|
55
|
+
def image_size(self):
|
56
|
+
"""Returns the default size of a single image."""
|
57
|
+
return (None, None)
|
58
|
+
|
59
|
+
@classproperty
|
60
|
+
def presets(cls):
|
61
|
+
"""List built-in presets for an `ImageConverter` subclass."""
|
62
|
+
return builtin_presets(cls)
|
63
|
+
|
64
|
+
@classmethod
|
65
|
+
def from_preset(
|
66
|
+
cls,
|
67
|
+
preset,
|
68
|
+
**kwargs,
|
69
|
+
):
|
70
|
+
"""Instantiate a `keras_hub.layers.ImageConverter` from a model preset.
|
71
|
+
|
72
|
+
A preset is a directory of configs, weights and other file assets used
|
73
|
+
to save and load a pre-trained model. The `preset` can be passed as
|
74
|
+
one of:
|
75
|
+
|
76
|
+
1. a built-in preset identifier like `'pali_gemma_3b_224'`
|
77
|
+
2. a Kaggle Models handle like
|
78
|
+
`'kaggle://user/paligemma/keras/pali_gemma_3b_224'`
|
79
|
+
3. a Hugging Face handle like `'hf://user/pali_gemma_3b_224'`
|
80
|
+
4. a path to a local preset directory like `'./pali_gemma_3b_224'`
|
81
|
+
|
82
|
+
You can run `cls.presets.keys()` to list all built-in presets available
|
83
|
+
on the class.
|
84
|
+
|
85
|
+
This constructor can be called in one of two ways. Either from the base
|
86
|
+
class like `keras_hub.models.ImageConverter.from_preset()`, or from a
|
87
|
+
model class like
|
88
|
+
`keras_hub.models.PaliGemmaImageConverter.from_preset()`. If calling
|
89
|
+
from the base class, the subclass of the returning object will be
|
90
|
+
inferred from the config in the preset directory.
|
91
|
+
|
92
|
+
Args:
|
93
|
+
preset: string. A built-in preset identifier, a Kaggle Models
|
94
|
+
handle, a Hugging Face handle, or a path to a local directory.
|
95
|
+
load_weights: bool. If `True`, the weights will be loaded into the
|
96
|
+
model architecture. If `False`, the weights will be randomly
|
97
|
+
initialized.
|
98
|
+
|
99
|
+
Examples:
|
100
|
+
```python
|
101
|
+
# Resize images for `"pali_gemma_3b_224"`.
|
102
|
+
converter = keras_hub.layers.ImageConverter.from_preset(
|
103
|
+
"pali_gemma_3b_224"
|
104
|
+
)
|
105
|
+
converter(np.ones(2, 512, 512, 3)) # Output shape: (2, 224, 224, 3)
|
106
|
+
# Override arguments on the base class.
|
107
|
+
converter = keras_hub.layers.ImageConverter.from_preset(
|
108
|
+
"pali_gemma_3b_448",
|
109
|
+
crop_to_aspect_ratio=False,
|
110
|
+
)
|
111
|
+
converter(np.ones(2, 512, 512, 3)) # (2, 448, 448, 3)
|
112
|
+
```
|
113
|
+
"""
|
114
|
+
loader = get_preset_loader(preset)
|
115
|
+
backbone_cls = loader.check_backbone_class()
|
116
|
+
if cls.backbone_cls != backbone_cls:
|
117
|
+
cls = find_subclass(preset, cls, backbone_cls)
|
118
|
+
return loader.load_image_converter(cls, **kwargs)
|
119
|
+
|
120
|
+
def save_to_preset(self, preset_dir):
|
121
|
+
"""Save image converter to a preset directory.
|
122
|
+
|
123
|
+
Args:
|
124
|
+
preset_dir: The path to the local model preset directory.
|
125
|
+
"""
|
126
|
+
save_serialized_object(
|
127
|
+
self,
|
128
|
+
preset_dir,
|
129
|
+
config_file=IMAGE_CONVERTER_CONFIG_FILE,
|
130
|
+
)
|
@@ -18,6 +18,7 @@ from keras_hub.src.layers.preprocessing.preprocessing_layer import (
|
|
18
18
|
PreprocessingLayer,
|
19
19
|
)
|
20
20
|
from keras_hub.src.utils.tensor_utils import convert_to_ragged_batch
|
21
|
+
from keras_hub.src.utils.tensor_utils import preprocessing_function
|
21
22
|
|
22
23
|
try:
|
23
24
|
import tensorflow as tf
|
@@ -165,6 +166,7 @@ class MaskedLMMaskGenerator(PreprocessingLayer):
|
|
165
166
|
random_token_rate=self.random_token_rate,
|
166
167
|
)
|
167
168
|
|
169
|
+
@preprocessing_function
|
168
170
|
def call(self, inputs):
|
169
171
|
inputs, unbatched, rectangular = convert_to_ragged_batch(inputs)
|
170
172
|
|
@@ -17,6 +17,7 @@ from keras_hub.src.layers.preprocessing.preprocessing_layer import (
|
|
17
17
|
PreprocessingLayer,
|
18
18
|
)
|
19
19
|
from keras_hub.src.utils.tensor_utils import convert_to_ragged_batch
|
20
|
+
from keras_hub.src.utils.tensor_utils import preprocessing_function
|
20
21
|
|
21
22
|
try:
|
22
23
|
import tensorflow as tf
|
@@ -193,23 +194,22 @@ class MultiSegmentPacker(PreprocessingLayer):
|
|
193
194
|
def _sanitize_inputs(self, inputs):
|
194
195
|
"""Force inputs to a list of rank 2 ragged tensors."""
|
195
196
|
# Sanitize inputs.
|
196
|
-
if not isinstance(inputs,
|
197
|
+
if not isinstance(inputs, tuple):
|
197
198
|
inputs = (inputs,)
|
198
199
|
if not inputs:
|
199
200
|
raise ValueError(
|
200
201
|
"At least one input is required for packing. "
|
201
202
|
f"Received: `inputs={inputs}`"
|
202
203
|
)
|
203
|
-
|
204
|
-
|
205
|
-
)
|
206
|
-
if len(set(
|
207
|
-
ranks = [1 if unbatched else 2 for unbatched in unbatched_list]
|
204
|
+
# convert_to_ragged_batch returns (x, unbatched, regtangular) triplets.
|
205
|
+
triplets = [convert_to_ragged_batch(x) for x in inputs]
|
206
|
+
x, unbatched, rectangular = list(zip(*triplets))
|
207
|
+
if len(set(unbatched)) != 1:
|
208
208
|
raise ValueError(
|
209
209
|
"All inputs for packing must have the same rank. "
|
210
|
-
f"Received: `inputs={inputs}
|
210
|
+
f"Received: `inputs={inputs}`."
|
211
211
|
)
|
212
|
-
return
|
212
|
+
return x, unbatched[0]
|
213
213
|
|
214
214
|
def _trim_inputs(self, inputs):
|
215
215
|
"""Trim inputs to desired length."""
|
@@ -282,6 +282,7 @@ class MultiSegmentPacker(PreprocessingLayer):
|
|
282
282
|
segment_ids = tf.concat(segment_ids_to_combine, 1)
|
283
283
|
return token_ids, segment_ids
|
284
284
|
|
285
|
+
@preprocessing_function
|
285
286
|
def call(
|
286
287
|
self,
|
287
288
|
inputs,
|
@@ -13,17 +13,8 @@
|
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
15
|
import keras
|
16
|
-
from keras import tree
|
17
16
|
|
18
17
|
from keras_hub.src.utils.tensor_utils import assert_tf_libs_installed
|
19
|
-
from keras_hub.src.utils.tensor_utils import (
|
20
|
-
convert_to_backend_tensor_or_python_list,
|
21
|
-
)
|
22
|
-
|
23
|
-
try:
|
24
|
-
import tensorflow as tf
|
25
|
-
except ImportError:
|
26
|
-
tf = None
|
27
18
|
|
28
19
|
|
29
20
|
class PreprocessingLayer(keras.layers.Layer):
|
@@ -31,9 +22,10 @@ class PreprocessingLayer(keras.layers.Layer):
|
|
31
22
|
|
32
23
|
def __init__(self, **kwargs):
|
33
24
|
assert_tf_libs_installed(self.__class__.__name__)
|
34
|
-
|
35
25
|
super().__init__(**kwargs)
|
26
|
+
# Don't convert inputs (we want tf tensors not backend tensors).
|
36
27
|
self._convert_input_args = False
|
28
|
+
# Allow raw inputs like python strings.
|
37
29
|
self._allow_non_tensor_positional_args = True
|
38
30
|
# Most pre-preprocessing has no build.
|
39
31
|
if not hasattr(self, "build"):
|
@@ -41,22 +33,3 @@ class PreprocessingLayer(keras.layers.Layer):
|
|
41
33
|
|
42
34
|
def get_build_config(self):
|
43
35
|
return None
|
44
|
-
|
45
|
-
def __call__(self, *args, **kwargs):
|
46
|
-
# Always place on CPU for preprocessing, to avoid expensive back and
|
47
|
-
# forth copies to GPU before the trainable model.
|
48
|
-
with tf.device("cpu"):
|
49
|
-
outputs = super().__call__(*args, **kwargs)
|
50
|
-
|
51
|
-
# Jax and Torch lack native string and ragged types.
|
52
|
-
# If we are running on those backends and not running with tf.data
|
53
|
-
# (we are outside a tf.function), we covert all ragged and string
|
54
|
-
# tensor to pythonic types.
|
55
|
-
is_tf_backend = keras.config.backend() == "tensorflow"
|
56
|
-
is_in_tf_graph = not tf.executing_eagerly()
|
57
|
-
if not is_tf_backend and not is_in_tf_graph:
|
58
|
-
outputs = tree.map_structure(
|
59
|
-
convert_to_backend_tensor_or_python_list, outputs
|
60
|
-
)
|
61
|
-
|
62
|
-
return outputs
|
@@ -21,6 +21,7 @@ from keras_hub.src.layers.preprocessing.preprocessing_layer import (
|
|
21
21
|
from keras_hub.src.utils.tensor_utils import convert_to_ragged_batch
|
22
22
|
from keras_hub.src.utils.tensor_utils import is_int_dtype
|
23
23
|
from keras_hub.src.utils.tensor_utils import is_string_dtype
|
24
|
+
from keras_hub.src.utils.tensor_utils import preprocessing_function
|
24
25
|
|
25
26
|
try:
|
26
27
|
import tensorflow as tf
|
@@ -65,55 +66,55 @@ class RandomDeletion(PreprocessingLayer):
|
|
65
66
|
|
66
67
|
Word level usage.
|
67
68
|
>>> keras.utils.set_random_seed(1337)
|
68
|
-
>>>
|
69
|
-
>>>
|
70
|
-
>>>
|
71
|
-
>>>
|
72
|
-
|
73
|
-
|
69
|
+
>>> x = ["Hey I like", "Keras and Tensorflow"]
|
70
|
+
>>> x = list(map(lambda x: x.split(), x))
|
71
|
+
>>> augmenter = keras_hub.layers.RandomDeletion(rate=0.4, seed=42)
|
72
|
+
>>> y = augmenter(x)
|
73
|
+
>>> list(map(lambda y: " ".join(y), y))
|
74
|
+
['I like', 'and']
|
74
75
|
|
75
76
|
Character level usage.
|
76
77
|
>>> keras.utils.set_random_seed(1337)
|
77
|
-
>>>
|
78
|
-
>>>
|
79
|
-
>>>
|
80
|
-
>>>
|
81
|
-
|
82
|
-
|
78
|
+
>>> x = ["Hey Dude", "Speed Up"]
|
79
|
+
>>> x = list(map(lambda x: list(x), x))
|
80
|
+
>>> augmenter = keras_hub.layers.RandomDeletion(rate=0.4, seed=42)
|
81
|
+
>>> y = augmenter(x)
|
82
|
+
>>> list(map(lambda y: "".join(y), y))
|
83
|
+
['H Dude', 'pedUp']
|
83
84
|
|
84
85
|
Usage with skip_list.
|
85
86
|
>>> keras.utils.set_random_seed(1337)
|
86
|
-
>>>
|
87
|
-
>>>
|
87
|
+
>>> x = ["Hey I like", "Keras and Tensorflow"]
|
88
|
+
>>> x = list(map(lambda x: x.split(), x))
|
89
|
+
>>> augmenter = keras_hub.layers.RandomDeletion(rate=0.4,
|
88
90
|
... skip_list=["Keras", "Tensorflow"], seed=42)
|
89
|
-
>>>
|
90
|
-
>>>
|
91
|
-
|
92
|
-
numpy=array([b'I like', b'Keras Tensorflow'], dtype=object)>
|
91
|
+
>>> y = augmenter(x)
|
92
|
+
>>> list(map(lambda y: " ".join(y), y))
|
93
|
+
['I like', 'Keras Tensorflow']
|
93
94
|
|
94
95
|
Usage with skip_fn.
|
95
96
|
>>> def skip_fn(word):
|
96
97
|
... return tf.strings.regex_full_match(word, r"\\pP")
|
97
98
|
>>> keras.utils.set_random_seed(1337)
|
98
|
-
>>>
|
99
|
-
>>>
|
99
|
+
>>> x = ["Hey I like", "Keras and Tensorflow"]
|
100
|
+
>>> x = list(map(lambda x: x.split(), x))
|
101
|
+
>>> augmenter = keras_hub.layers.RandomDeletion(rate=0.4,
|
100
102
|
... skip_fn=skip_fn, seed=42)
|
101
|
-
>>>
|
102
|
-
>>>
|
103
|
-
|
104
|
-
dtype=object)>
|
103
|
+
>>> y = augmenter(x)
|
104
|
+
>>> list(map(lambda y: " ".join(y), y))
|
105
|
+
['I like', 'and']
|
105
106
|
|
106
107
|
Usage with skip_py_fn.
|
107
108
|
>>> def skip_py_fn(word):
|
108
109
|
... return len(word) < 4
|
109
110
|
>>> keras.utils.set_random_seed(1337)
|
110
|
-
>>>
|
111
|
-
>>>
|
111
|
+
>>> x = ["Hey I like", "Keras and Tensorflow"]
|
112
|
+
>>> x = list(map(lambda x: x.split(), x))
|
113
|
+
>>> augmenter = RandomDeletion(rate=0.4,
|
112
114
|
... skip_py_fn=skip_py_fn, seed=42)
|
113
|
-
>>>
|
114
|
-
>>>
|
115
|
-
|
116
|
-
numpy=array([b'Hey I', b'and Tensorflow'], dtype=object)>
|
115
|
+
>>> y = augmenter(x)
|
116
|
+
>>> list(map(lambda y: " ".join(y), y))
|
117
|
+
['Hey I', 'and Tensorflow']
|
117
118
|
"""
|
118
119
|
|
119
120
|
def __init__(
|
@@ -170,8 +171,9 @@ class RandomDeletion(PreprocessingLayer):
|
|
170
171
|
default_value=False,
|
171
172
|
)
|
172
173
|
|
174
|
+
@preprocessing_function
|
173
175
|
def call(self, inputs):
|
174
|
-
inputs, unbatched,
|
176
|
+
inputs, unbatched, rectangular = convert_to_ragged_batch(inputs)
|
175
177
|
|
176
178
|
skip_masks = None
|
177
179
|
if self.skip_list:
|