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.
Files changed (198) hide show
  1. keras_hub/__init__.py +0 -6
  2. keras_hub/api/__init__.py +2 -0
  3. keras_hub/api/bounding_box/__init__.py +36 -0
  4. keras_hub/api/layers/__init__.py +14 -0
  5. keras_hub/api/models/__init__.py +97 -48
  6. keras_hub/api/tokenizers/__init__.py +30 -0
  7. keras_hub/api/utils/__init__.py +22 -0
  8. keras_hub/src/api_export.py +15 -9
  9. keras_hub/src/bounding_box/__init__.py +13 -0
  10. keras_hub/src/bounding_box/converters.py +529 -0
  11. keras_hub/src/bounding_box/formats.py +162 -0
  12. keras_hub/src/bounding_box/iou.py +263 -0
  13. keras_hub/src/bounding_box/to_dense.py +95 -0
  14. keras_hub/src/bounding_box/to_ragged.py +99 -0
  15. keras_hub/src/bounding_box/utils.py +194 -0
  16. keras_hub/src/bounding_box/validate_format.py +99 -0
  17. keras_hub/src/layers/preprocessing/audio_converter.py +121 -0
  18. keras_hub/src/layers/preprocessing/image_converter.py +130 -0
  19. keras_hub/src/layers/preprocessing/masked_lm_mask_generator.py +2 -0
  20. keras_hub/src/layers/preprocessing/multi_segment_packer.py +9 -8
  21. keras_hub/src/layers/preprocessing/preprocessing_layer.py +2 -29
  22. keras_hub/src/layers/preprocessing/random_deletion.py +33 -31
  23. keras_hub/src/layers/preprocessing/random_swap.py +33 -31
  24. keras_hub/src/layers/preprocessing/resizing_image_converter.py +101 -0
  25. keras_hub/src/layers/preprocessing/start_end_packer.py +3 -2
  26. keras_hub/src/models/albert/__init__.py +1 -2
  27. keras_hub/src/models/albert/albert_masked_lm_preprocessor.py +6 -86
  28. keras_hub/src/models/albert/{albert_classifier.py → albert_text_classifier.py} +34 -10
  29. keras_hub/src/models/albert/{albert_preprocessor.py → albert_text_classifier_preprocessor.py} +14 -70
  30. keras_hub/src/models/albert/albert_tokenizer.py +17 -36
  31. keras_hub/src/models/backbone.py +12 -34
  32. keras_hub/src/models/bart/__init__.py +1 -2
  33. keras_hub/src/models/bart/bart_seq_2_seq_lm_preprocessor.py +21 -148
  34. keras_hub/src/models/bart/bart_tokenizer.py +12 -39
  35. keras_hub/src/models/bert/__init__.py +1 -5
  36. keras_hub/src/models/bert/bert_masked_lm_preprocessor.py +6 -87
  37. keras_hub/src/models/bert/bert_presets.py +1 -4
  38. keras_hub/src/models/bert/{bert_classifier.py → bert_text_classifier.py} +19 -12
  39. keras_hub/src/models/bert/{bert_preprocessor.py → bert_text_classifier_preprocessor.py} +14 -70
  40. keras_hub/src/models/bert/bert_tokenizer.py +17 -35
  41. keras_hub/src/models/bloom/__init__.py +1 -2
  42. keras_hub/src/models/bloom/bloom_causal_lm_preprocessor.py +6 -91
  43. keras_hub/src/models/bloom/bloom_tokenizer.py +12 -41
  44. keras_hub/src/models/causal_lm.py +10 -29
  45. keras_hub/src/models/causal_lm_preprocessor.py +195 -0
  46. keras_hub/src/models/csp_darknet/csp_darknet_backbone.py +54 -15
  47. keras_hub/src/models/deberta_v3/__init__.py +1 -4
  48. keras_hub/src/models/deberta_v3/deberta_v3_masked_lm_preprocessor.py +14 -77
  49. keras_hub/src/models/deberta_v3/{deberta_v3_classifier.py → deberta_v3_text_classifier.py} +16 -11
  50. keras_hub/src/models/deberta_v3/{deberta_v3_preprocessor.py → deberta_v3_text_classifier_preprocessor.py} +23 -64
  51. keras_hub/src/models/deberta_v3/deberta_v3_tokenizer.py +30 -25
  52. keras_hub/src/models/densenet/densenet_backbone.py +46 -22
  53. keras_hub/src/models/distil_bert/__init__.py +1 -4
  54. keras_hub/src/models/distil_bert/distil_bert_masked_lm_preprocessor.py +14 -76
  55. keras_hub/src/models/distil_bert/{distil_bert_classifier.py → distil_bert_text_classifier.py} +17 -12
  56. keras_hub/src/models/distil_bert/{distil_bert_preprocessor.py → distil_bert_text_classifier_preprocessor.py} +23 -63
  57. keras_hub/src/models/distil_bert/distil_bert_tokenizer.py +19 -35
  58. keras_hub/src/models/efficientnet/__init__.py +13 -0
  59. keras_hub/src/models/efficientnet/efficientnet_backbone.py +569 -0
  60. keras_hub/src/models/efficientnet/fusedmbconv.py +229 -0
  61. keras_hub/src/models/efficientnet/mbconv.py +238 -0
  62. keras_hub/src/models/electra/__init__.py +1 -2
  63. keras_hub/src/models/electra/electra_tokenizer.py +17 -32
  64. keras_hub/src/models/f_net/__init__.py +1 -2
  65. keras_hub/src/models/f_net/f_net_masked_lm_preprocessor.py +12 -78
  66. keras_hub/src/models/f_net/{f_net_classifier.py → f_net_text_classifier.py} +17 -10
  67. keras_hub/src/models/f_net/{f_net_preprocessor.py → f_net_text_classifier_preprocessor.py} +19 -63
  68. keras_hub/src/models/f_net/f_net_tokenizer.py +17 -35
  69. keras_hub/src/models/falcon/__init__.py +1 -2
  70. keras_hub/src/models/falcon/falcon_causal_lm_preprocessor.py +6 -89
  71. keras_hub/src/models/falcon/falcon_tokenizer.py +12 -35
  72. keras_hub/src/models/gemma/__init__.py +1 -2
  73. keras_hub/src/models/gemma/gemma_causal_lm_preprocessor.py +6 -90
  74. keras_hub/src/models/gemma/gemma_decoder_block.py +1 -1
  75. keras_hub/src/models/gemma/gemma_tokenizer.py +12 -23
  76. keras_hub/src/models/gpt2/__init__.py +1 -2
  77. keras_hub/src/models/gpt2/gpt2_causal_lm_preprocessor.py +6 -89
  78. keras_hub/src/models/gpt2/gpt2_preprocessor.py +12 -90
  79. keras_hub/src/models/gpt2/gpt2_tokenizer.py +12 -34
  80. keras_hub/src/models/gpt_neo_x/gpt_neo_x_causal_lm_preprocessor.py +6 -91
  81. keras_hub/src/models/gpt_neo_x/gpt_neo_x_tokenizer.py +12 -34
  82. keras_hub/src/models/image_classifier.py +0 -5
  83. keras_hub/src/models/image_classifier_preprocessor.py +83 -0
  84. keras_hub/src/models/llama/__init__.py +1 -2
  85. keras_hub/src/models/llama/llama_causal_lm_preprocessor.py +6 -85
  86. keras_hub/src/models/llama/llama_tokenizer.py +12 -25
  87. keras_hub/src/models/llama3/__init__.py +1 -2
  88. keras_hub/src/models/llama3/llama3_causal_lm_preprocessor.py +6 -89
  89. keras_hub/src/models/llama3/llama3_tokenizer.py +12 -33
  90. keras_hub/src/models/masked_lm.py +0 -2
  91. keras_hub/src/models/masked_lm_preprocessor.py +156 -0
  92. keras_hub/src/models/mistral/__init__.py +1 -2
  93. keras_hub/src/models/mistral/mistral_causal_lm_preprocessor.py +6 -91
  94. keras_hub/src/models/mistral/mistral_tokenizer.py +12 -23
  95. keras_hub/src/models/mix_transformer/mix_transformer_backbone.py +2 -2
  96. keras_hub/src/models/mobilenet/__init__.py +13 -0
  97. keras_hub/src/models/mobilenet/mobilenet_backbone.py +530 -0
  98. keras_hub/src/models/mobilenet/mobilenet_image_classifier.py +114 -0
  99. keras_hub/src/models/opt/__init__.py +1 -2
  100. keras_hub/src/models/opt/opt_causal_lm_preprocessor.py +6 -93
  101. keras_hub/src/models/opt/opt_tokenizer.py +12 -41
  102. keras_hub/src/models/pali_gemma/__init__.py +1 -4
  103. keras_hub/src/models/pali_gemma/pali_gemma_causal_lm_preprocessor.py +28 -28
  104. keras_hub/src/models/pali_gemma/pali_gemma_image_converter.py +25 -0
  105. keras_hub/src/models/pali_gemma/pali_gemma_presets.py +5 -5
  106. keras_hub/src/models/pali_gemma/pali_gemma_tokenizer.py +11 -3
  107. keras_hub/src/models/phi3/__init__.py +1 -2
  108. keras_hub/src/models/phi3/phi3_causal_lm.py +3 -9
  109. keras_hub/src/models/phi3/phi3_causal_lm_preprocessor.py +6 -89
  110. keras_hub/src/models/phi3/phi3_tokenizer.py +12 -36
  111. keras_hub/src/models/preprocessor.py +72 -83
  112. keras_hub/src/models/resnet/__init__.py +6 -0
  113. keras_hub/src/models/resnet/resnet_backbone.py +390 -42
  114. keras_hub/src/models/resnet/resnet_image_classifier.py +33 -6
  115. keras_hub/src/models/resnet/resnet_image_classifier_preprocessor.py +28 -0
  116. keras_hub/src/models/{llama3/llama3_preprocessor.py → resnet/resnet_image_converter.py} +7 -5
  117. keras_hub/src/models/resnet/resnet_presets.py +95 -0
  118. keras_hub/src/models/retinanet/__init__.py +13 -0
  119. keras_hub/src/models/retinanet/anchor_generator.py +175 -0
  120. keras_hub/src/models/retinanet/box_matcher.py +259 -0
  121. keras_hub/src/models/retinanet/non_max_supression.py +578 -0
  122. keras_hub/src/models/roberta/__init__.py +1 -2
  123. keras_hub/src/models/roberta/roberta_masked_lm_preprocessor.py +22 -74
  124. keras_hub/src/models/roberta/{roberta_classifier.py → roberta_text_classifier.py} +16 -11
  125. keras_hub/src/models/roberta/{roberta_preprocessor.py → roberta_text_classifier_preprocessor.py} +21 -53
  126. keras_hub/src/models/roberta/roberta_tokenizer.py +13 -52
  127. keras_hub/src/models/seq_2_seq_lm_preprocessor.py +269 -0
  128. keras_hub/src/models/stable_diffusion_v3/__init__.py +13 -0
  129. keras_hub/src/models/stable_diffusion_v3/clip_encoder_block.py +103 -0
  130. keras_hub/src/models/stable_diffusion_v3/clip_preprocessor.py +93 -0
  131. keras_hub/src/models/stable_diffusion_v3/clip_text_encoder.py +149 -0
  132. keras_hub/src/models/stable_diffusion_v3/clip_tokenizer.py +167 -0
  133. keras_hub/src/models/stable_diffusion_v3/mmdit.py +427 -0
  134. keras_hub/src/models/stable_diffusion_v3/mmdit_block.py +317 -0
  135. keras_hub/src/models/stable_diffusion_v3/t5_xxl_preprocessor.py +74 -0
  136. keras_hub/src/models/stable_diffusion_v3/t5_xxl_text_encoder.py +155 -0
  137. keras_hub/src/models/stable_diffusion_v3/vae_attention.py +126 -0
  138. keras_hub/src/models/stable_diffusion_v3/vae_image_decoder.py +186 -0
  139. keras_hub/src/models/t5/__init__.py +1 -2
  140. keras_hub/src/models/t5/t5_tokenizer.py +13 -23
  141. keras_hub/src/models/task.py +71 -116
  142. keras_hub/src/models/{classifier.py → text_classifier.py} +19 -13
  143. keras_hub/src/models/text_classifier_preprocessor.py +138 -0
  144. keras_hub/src/models/whisper/__init__.py +1 -2
  145. keras_hub/src/models/whisper/{whisper_audio_feature_extractor.py → whisper_audio_converter.py} +20 -18
  146. keras_hub/src/models/whisper/whisper_backbone.py +0 -3
  147. keras_hub/src/models/whisper/whisper_presets.py +10 -10
  148. keras_hub/src/models/whisper/whisper_tokenizer.py +20 -16
  149. keras_hub/src/models/xlm_roberta/__init__.py +1 -4
  150. keras_hub/src/models/xlm_roberta/xlm_roberta_masked_lm_preprocessor.py +26 -72
  151. keras_hub/src/models/xlm_roberta/{xlm_roberta_classifier.py → xlm_roberta_text_classifier.py} +16 -11
  152. keras_hub/src/models/xlm_roberta/{xlm_roberta_preprocessor.py → xlm_roberta_text_classifier_preprocessor.py} +26 -53
  153. keras_hub/src/models/xlm_roberta/xlm_roberta_tokenizer.py +25 -10
  154. keras_hub/src/tests/test_case.py +46 -0
  155. keras_hub/src/tokenizers/byte_pair_tokenizer.py +30 -17
  156. keras_hub/src/tokenizers/byte_tokenizer.py +14 -15
  157. keras_hub/src/tokenizers/sentence_piece_tokenizer.py +20 -7
  158. keras_hub/src/tokenizers/tokenizer.py +67 -32
  159. keras_hub/src/tokenizers/unicode_codepoint_tokenizer.py +14 -15
  160. keras_hub/src/tokenizers/word_piece_tokenizer.py +34 -47
  161. keras_hub/src/utils/imagenet/__init__.py +13 -0
  162. keras_hub/src/utils/imagenet/imagenet_utils.py +1067 -0
  163. keras_hub/src/utils/keras_utils.py +0 -50
  164. keras_hub/src/utils/preset_utils.py +230 -68
  165. keras_hub/src/utils/tensor_utils.py +187 -69
  166. keras_hub/src/utils/timm/convert_resnet.py +19 -16
  167. keras_hub/src/utils/timm/preset_loader.py +66 -0
  168. keras_hub/src/utils/transformers/convert_albert.py +193 -0
  169. keras_hub/src/utils/transformers/convert_bart.py +373 -0
  170. keras_hub/src/utils/transformers/convert_bert.py +7 -17
  171. keras_hub/src/utils/transformers/convert_distilbert.py +10 -20
  172. keras_hub/src/utils/transformers/convert_gemma.py +5 -19
  173. keras_hub/src/utils/transformers/convert_gpt2.py +5 -18
  174. keras_hub/src/utils/transformers/convert_llama3.py +7 -18
  175. keras_hub/src/utils/transformers/convert_mistral.py +129 -0
  176. keras_hub/src/utils/transformers/convert_pali_gemma.py +7 -29
  177. keras_hub/src/utils/transformers/preset_loader.py +77 -0
  178. keras_hub/src/utils/transformers/safetensor_utils.py +2 -2
  179. keras_hub/src/version_utils.py +1 -1
  180. keras_hub_nightly-0.16.0.dev2024092017.dist-info/METADATA +202 -0
  181. keras_hub_nightly-0.16.0.dev2024092017.dist-info/RECORD +334 -0
  182. {keras_hub_nightly-0.15.0.dev20240823171555.dist-info → keras_hub_nightly-0.16.0.dev2024092017.dist-info}/WHEEL +1 -1
  183. keras_hub/src/models/bart/bart_preprocessor.py +0 -276
  184. keras_hub/src/models/bloom/bloom_preprocessor.py +0 -185
  185. keras_hub/src/models/electra/electra_preprocessor.py +0 -154
  186. keras_hub/src/models/falcon/falcon_preprocessor.py +0 -187
  187. keras_hub/src/models/gemma/gemma_preprocessor.py +0 -191
  188. keras_hub/src/models/gpt_neo_x/gpt_neo_x_preprocessor.py +0 -145
  189. keras_hub/src/models/llama/llama_preprocessor.py +0 -189
  190. keras_hub/src/models/mistral/mistral_preprocessor.py +0 -190
  191. keras_hub/src/models/opt/opt_preprocessor.py +0 -188
  192. keras_hub/src/models/phi3/phi3_preprocessor.py +0 -190
  193. keras_hub/src/models/whisper/whisper_preprocessor.py +0 -326
  194. keras_hub/src/utils/timm/convert.py +0 -37
  195. keras_hub/src/utils/transformers/convert.py +0 -101
  196. keras_hub_nightly-0.15.0.dev20240823171555.dist-info/METADATA +0 -34
  197. keras_hub_nightly-0.15.0.dev20240823171555.dist-info/RECORD +0 -297
  198. {keras_hub_nightly-0.15.0.dev20240823171555.dist-info → keras_hub_nightly-0.16.0.dev2024092017.dist-info}/top_level.txt +0 -0
@@ -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
@@ -67,55 +68,55 @@ class RandomSwap(PreprocessingLayer):
67
68
 
68
69
  Word level usage.
69
70
  >>> keras.utils.set_random_seed(1337)
70
- >>> inputs=tf.strings.split(["Hey I like", "Keras and Tensorflow"])
71
- >>> augmenter=keras_hub.layers.RandomSwap(rate=0.4, seed=42)
72
- >>> augmented=augmenter(inputs)
73
- >>> tf.strings.reduce_join(augmented, separator=" ", axis=-1)
74
- <tf.Tensor: shape=(2,), dtype=string,
75
- numpy=array([b'like I Hey', b'and Keras Tensorflow'], dtype=object)>
71
+ >>> x = ["Hey I like", "Keras and Tensorflow"]
72
+ >>> x = list(map(lambda x: x.split(), x))
73
+ >>> augmenter = keras_hub.layers.RandomSwap(rate=0.4, seed=42)
74
+ >>> y = augmenter(x)
75
+ >>> list(map(lambda y: " ".join(y), y))
76
+ ['like I Hey', 'and Keras Tensorflow']
76
77
 
77
78
  Character level usage.
78
79
  >>> keras.utils.set_random_seed(1337)
79
- >>> inputs=tf.strings.unicode_split(["Hey Dude", "Speed Up"], "UTF-8")
80
- >>> augmenter=keras_hub.layers.RandomSwap(rate=0.4, seed=42)
81
- >>> augmented=augmenter(inputs)
82
- >>> tf.strings.reduce_join(augmented, axis=-1)
83
- <tf.Tensor: shape=(2,), dtype=string,
84
- numpy=array([b'deD yuHe', b'SUede pp'], dtype=object)>
80
+ >>> x = ["Hey Dude", "Speed Up"]
81
+ >>> x = list(map(lambda x: list(x), x))
82
+ >>> augmenter = keras_hub.layers.RandomSwap(rate=0.4, seed=42)
83
+ >>> y = augmenter(x)
84
+ >>> list(map(lambda y: "".join(y), y))
85
+ ['deD yuHe', 'SUede pp']
85
86
 
86
87
  Usage with skip_list.
87
88
  >>> keras.utils.set_random_seed(1337)
88
- >>> inputs=tf.strings.split(["Hey I like", "Keras and Tensorflow"])
89
- >>> augmenter=keras_hub.layers.RandomSwap(rate=0.4,
89
+ >>> x = ["Hey I like", "Keras and Tensorflow"]
90
+ >>> x = list(map(lambda x: x.split(), x))
91
+ >>> augmenter = keras_hub.layers.RandomSwap(rate=0.4,
90
92
  ... skip_list=["Keras"], seed=42)
91
- >>> augmented=augmenter(inputs)
92
- >>> tf.strings.reduce_join(augmented, separator=" ", axis=-1)
93
- <tf.Tensor: shape=(2,), dtype=string,
94
- numpy=array([b'like I Hey', b'Keras and Tensorflow'], dtype=object)>
93
+ >>> y = augmenter(x)
94
+ >>> list(map(lambda y: " ".join(y), y))
95
+ ['like I Hey', 'Keras and Tensorflow']
95
96
 
96
97
  Usage with skip_fn.
97
98
  >>> def skip_fn(word):
98
99
  ... return tf.strings.regex_full_match(word, r"[I, a].*")
99
100
  >>> keras.utils.set_random_seed(1337)
100
- >>> inputs=tf.strings.split(["Hey I like", "Keras and Tensorflow"])
101
- >>> augmenter=keras_hub.layers.RandomSwap(rate=0.9, max_swaps=3,
101
+ >>> x = ["Hey I like", "Keras and Tensorflow"]
102
+ >>> x = list(map(lambda x: x.split(), x))
103
+ >>> augmenter = keras_hub.layers.RandomSwap(rate=0.9, max_swaps=3,
102
104
  ... skip_fn=skip_fn, seed=11)
103
- >>> augmented=augmenter(inputs)
104
- >>> tf.strings.reduce_join(augmented, separator=" ", axis=-1)
105
- <tf.Tensor: shape=(2,), dtype=string,
106
- numpy=array([b'like I Hey', b'Keras and Tensorflow'], dtype=object)>
105
+ >>> y = augmenter(x)
106
+ >>> list(map(lambda y: " ".join(y), y))
107
+ ['like I Hey', 'Keras and Tensorflow']
107
108
 
108
109
  Usage with skip_py_fn.
109
110
  >>> def skip_py_fn(word):
110
111
  ... return len(word) < 4
111
112
  >>> keras.utils.set_random_seed(1337)
112
- >>> inputs=tf.strings.split(["He was drifting along", "With the wind"])
113
- >>> augmenter=keras_hub.layers.RandomSwap(rate=0.8, max_swaps=2,
113
+ >>> x = ["He was drifting along", "With the wind"]
114
+ >>> x = list(map(lambda x: x.split(), x))
115
+ >>> augmenter = keras_hub.layers.RandomSwap(rate=0.8, max_swaps=2,
114
116
  ... skip_py_fn=skip_py_fn, seed=15)
115
- >>> augmented=augmenter(inputs)
116
- >>> tf.strings.reduce_join(augmented, separator=" ", axis=-1)
117
- <tf.Tensor: shape=(2,), dtype=string, numpy=array([b'He was along drifting',
118
- b'wind the With'], dtype=object)>
117
+ >>> y = augmenter(x)
118
+ >>> list(map(lambda y: " ".join(y), y))
119
+ ['He was along drifting', 'wind the With']
119
120
  """
120
121
 
121
122
  def __init__(
@@ -166,8 +167,9 @@ class RandomSwap(PreprocessingLayer):
166
167
  default_value=False,
167
168
  )
168
169
 
170
+ @preprocessing_function
169
171
  def call(self, inputs):
170
- inputs, unbatched, _ = convert_to_ragged_batch(inputs)
172
+ inputs, unbatched, rectangular = convert_to_ragged_batch(inputs)
171
173
 
172
174
  skip_masks = None
173
175
  if self.skip_list:
@@ -0,0 +1,101 @@
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
+ import keras
15
+
16
+ from keras_hub.src.api_export import keras_hub_export
17
+ from keras_hub.src.layers.preprocessing.image_converter import ImageConverter
18
+ from keras_hub.src.utils.tensor_utils import preprocessing_function
19
+
20
+
21
+ @keras_hub_export("keras_hub.layers.ResizingImageConverter")
22
+ class ResizingImageConverter(ImageConverter):
23
+ """An `ImageConverter` that simply resizes the input image.
24
+
25
+ The `ResizingImageConverter` is a subclass of `ImageConverter` for models
26
+ that simply need to resize image tensors before using them for modeling.
27
+ The layer will take as input a raw image tensor (batched or unbatched) in the
28
+ channels last or channels first format, and output a resize tensor.
29
+
30
+ Args:
31
+ height: Integer, the height of the output shape.
32
+ width: Integer, the width of the output shape.
33
+ crop_to_aspect_ratio: If `True`, resize the images without aspect
34
+ ratio distortion. When the original aspect ratio differs
35
+ from the target aspect ratio, the output image will be
36
+ cropped so as to return the
37
+ largest possible window in the image (of size `(height, width)`)
38
+ that matches the target aspect ratio. By default
39
+ (`crop_to_aspect_ratio=False`), aspect ratio may not be preserved.
40
+ interpolation: String, the interpolation method.
41
+ Supports `"bilinear"`, `"nearest"`, `"bicubic"`,
42
+ `"lanczos3"`, `"lanczos5"`. Defaults to `"bilinear"`.
43
+ data_format: String, either `"channels_last"` or `"channels_first"`.
44
+ The ordering of the dimensions in the inputs. `"channels_last"`
45
+ corresponds to inputs with shape `(batch, height, width, channels)`
46
+ while `"channels_first"` corresponds to inputs with shape
47
+ `(batch, channels, height, width)`. It defaults to the
48
+ `image_data_format` value found in your Keras config file at
49
+ `~/.keras/keras.json`. If you never set it, then it will be
50
+ `"channels_last"`.
51
+
52
+ Examples:
53
+ ```python
54
+ # Resize images for `"pali_gemma_3b_224"`.
55
+ converter = keras_hub.layers.ImageConverter.from_preset("pali_gemma_3b_224")
56
+ converter(np.ones(2, 512, 512, 3)) # Output shape: (2, 224, 224, 3)
57
+ # Resize images for `"pali_gemma_3b_224"`.
58
+ converter = keras_hub.layers.ImageConverter.from_preset("pali_gemma_3b_448")
59
+ converter(np.ones(2, 512, 512, 3)) # Output shape: (2, 448, 448, 3)
60
+ ```
61
+ """
62
+
63
+ def __init__(
64
+ self,
65
+ height,
66
+ width,
67
+ crop_to_aspect_ratio=True,
68
+ interpolation="bilinear",
69
+ data_format=None,
70
+ **kwargs,
71
+ ):
72
+ super().__init__(**kwargs)
73
+ # By default, we just do a simple resize. Any model can subclass this
74
+ # layer for preprocessing of a raw image to a model image input.
75
+ self.resizing = keras.layers.Resizing(
76
+ height=height,
77
+ width=width,
78
+ crop_to_aspect_ratio=crop_to_aspect_ratio,
79
+ interpolation=interpolation,
80
+ data_format=data_format,
81
+ )
82
+
83
+ def image_size(self):
84
+ """Returns the preprocessed size of a single image."""
85
+ return (self.resizing.height, self.resizing.width)
86
+
87
+ @preprocessing_function
88
+ def call(self, inputs):
89
+ return self.resizing(inputs)
90
+
91
+ def get_config(self):
92
+ config = super().get_config()
93
+ config.update(
94
+ {
95
+ "height": self.resizing.height,
96
+ "width": self.resizing.width,
97
+ "interpolation": self.resizing.interpolation,
98
+ "crop_to_aspect_ratio": self.resizing.crop_to_aspect_ratio,
99
+ }
100
+ )
101
+ return config
@@ -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
@@ -154,6 +155,7 @@ class StartEndPacker(PreprocessingLayer):
154
155
  self.pad_value = pad_value
155
156
  self.return_padding_mask = return_padding_mask
156
157
 
158
+ @preprocessing_function
157
159
  def call(
158
160
  self,
159
161
  inputs,
@@ -161,8 +163,7 @@ class StartEndPacker(PreprocessingLayer):
161
163
  add_start_value=True,
162
164
  add_end_value=True,
163
165
  ):
164
- inputs, unbatched, _ = convert_to_ragged_batch(inputs)
165
-
166
+ inputs, unbatched, rectangular = convert_to_ragged_batch(inputs)
166
167
  x = inputs # Intermediate result.
167
168
 
168
169
  batch_size = tf.shape(x)[0]
@@ -14,7 +14,6 @@
14
14
 
15
15
  from keras_hub.src.models.albert.albert_backbone import AlbertBackbone
16
16
  from keras_hub.src.models.albert.albert_presets import backbone_presets
17
- from keras_hub.src.models.albert.albert_tokenizer import AlbertTokenizer
18
17
  from keras_hub.src.utils.preset_utils import register_presets
19
18
 
20
- register_presets(backbone_presets, (AlbertBackbone, AlbertTokenizer))
19
+ register_presets(backbone_presets, AlbertBackbone)
@@ -12,18 +12,14 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- import keras
16
- from absl import logging
17
-
18
15
  from keras_hub.src.api_export import keras_hub_export
19
- from keras_hub.src.layers.preprocessing.masked_lm_mask_generator import (
20
- MaskedLMMaskGenerator,
21
- )
22
- from keras_hub.src.models.albert.albert_preprocessor import AlbertPreprocessor
16
+ from keras_hub.src.models.albert.albert_backbone import AlbertBackbone
17
+ from keras_hub.src.models.albert.albert_tokenizer import AlbertTokenizer
18
+ from keras_hub.src.models.masked_lm_preprocessor import MaskedLMPreprocessor
23
19
 
24
20
 
25
21
  @keras_hub_export("keras_hub.models.AlbertMaskedLMPreprocessor")
26
- class AlbertMaskedLMPreprocessor(AlbertPreprocessor):
22
+ class AlbertMaskedLMPreprocessor(MaskedLMPreprocessor):
27
23
  """ALBERT preprocessing for the masked language modeling task.
28
24
 
29
25
  This preprocessing layer will prepare inputs for a masked language modeling
@@ -114,81 +110,5 @@ class AlbertMaskedLMPreprocessor(AlbertPreprocessor):
114
110
  ```
115
111
  """
116
112
 
117
- def __init__(
118
- self,
119
- tokenizer,
120
- sequence_length=512,
121
- truncate="round_robin",
122
- mask_selection_rate=0.15,
123
- mask_selection_length=96,
124
- mask_token_rate=0.8,
125
- random_token_rate=0.1,
126
- **kwargs,
127
- ):
128
- super().__init__(
129
- tokenizer,
130
- sequence_length=sequence_length,
131
- truncate=truncate,
132
- **kwargs,
133
- )
134
- self.mask_selection_rate = mask_selection_rate
135
- self.mask_selection_length = mask_selection_length
136
- self.mask_token_rate = mask_token_rate
137
- self.random_token_rate = random_token_rate
138
- self.masker = None
139
-
140
- def build(self, input_shape):
141
- super().build(input_shape)
142
- # Defer masker creation to `build()` so that we can be sure tokenizer
143
- # assets have loaded when restoring a saved model.
144
- self.masker = MaskedLMMaskGenerator(
145
- mask_selection_rate=self.mask_selection_rate,
146
- mask_selection_length=self.mask_selection_length,
147
- mask_token_rate=self.mask_token_rate,
148
- random_token_rate=self.random_token_rate,
149
- vocabulary_size=self.tokenizer.vocabulary_size(),
150
- mask_token_id=self.tokenizer.mask_token_id,
151
- unselectable_token_ids=[
152
- self.tokenizer.cls_token_id,
153
- self.tokenizer.sep_token_id,
154
- self.tokenizer.pad_token_id,
155
- ],
156
- )
157
-
158
- def get_config(self):
159
- config = super().get_config()
160
- config.update(
161
- {
162
- "mask_selection_rate": self.mask_selection_rate,
163
- "mask_selection_length": self.mask_selection_length,
164
- "mask_token_rate": self.mask_token_rate,
165
- "random_token_rate": self.random_token_rate,
166
- }
167
- )
168
- return config
169
-
170
- def call(self, x, y=None, sample_weight=None):
171
- if y is not None or sample_weight is not None:
172
- logging.warning(
173
- f"{self.__class__.__name__} generates `y` and `sample_weight` "
174
- "based on your input data, but your data already contains `y` "
175
- "or `sample_weight`. Your `y` and `sample_weight` will be "
176
- "ignored."
177
- )
178
-
179
- x = super().call(x)
180
- token_ids, segment_ids, padding_mask = (
181
- x["token_ids"],
182
- x["segment_ids"],
183
- x["padding_mask"],
184
- )
185
- masker_outputs = self.masker(token_ids)
186
- x = {
187
- "token_ids": masker_outputs["token_ids"],
188
- "segment_ids": segment_ids,
189
- "padding_mask": padding_mask,
190
- "mask_positions": masker_outputs["mask_positions"],
191
- }
192
- y = masker_outputs["mask_ids"]
193
- sample_weight = masker_outputs["mask_weights"]
194
- return keras.utils.pack_x_y_sample_weight(x, y, sample_weight)
113
+ backbone_cls = AlbertBackbone
114
+ tokenizer_cls = AlbertTokenizer
@@ -19,12 +19,19 @@ from keras_hub.src.models.albert.albert_backbone import AlbertBackbone
19
19
  from keras_hub.src.models.albert.albert_backbone import (
20
20
  albert_kernel_initializer,
21
21
  )
22
- from keras_hub.src.models.albert.albert_preprocessor import AlbertPreprocessor
23
- from keras_hub.src.models.classifier import Classifier
22
+ from keras_hub.src.models.albert.albert_text_classifier_preprocessor import (
23
+ AlbertTextClassifierPreprocessor,
24
+ )
25
+ from keras_hub.src.models.text_classifier import TextClassifier
24
26
 
25
27
 
26
- @keras_hub_export("keras_hub.models.AlbertClassifier")
27
- class AlbertClassifier(Classifier):
28
+ @keras_hub_export(
29
+ [
30
+ "keras_hub.models.AlbertTextClassifier",
31
+ "keras_hub.models.AlbertClassifier",
32
+ ]
33
+ )
34
+ class AlbertTextClassifier(TextClassifier):
28
35
  """An end-to-end ALBERT model for classification tasks
29
36
 
30
37
  This model attaches a classification head to a `keras_hub.model.AlbertBackbone`
@@ -43,7 +50,7 @@ class AlbertClassifier(Classifier):
43
50
  Args:
44
51
  backbone: A `keras_hub.models.AlertBackbone` instance.
45
52
  num_classes: int. Number of classes to predict.
46
- preprocessor: A `keras_hub.models.AlbertPreprocessor` or `None`. If
53
+ preprocessor: A `keras_hub.models.AlbertTextClassifierPreprocessor` or `None`. If
47
54
  `None`, this model will not apply preprocessing, and inputs should
48
55
  be preprocessed before calling the model.
49
56
  activation: Optional `str` or callable. The
@@ -61,7 +68,7 @@ class AlbertClassifier(Classifier):
61
68
  labels = [0, 3]
62
69
 
63
70
  # Pretrained classifier.
64
- classifier = keras_hub.models.AlbertClassifier.from_preset(
71
+ classifier = keras_hub.models.AlbertTextClassifier.from_preset(
65
72
  "albert_base_en_uncased",
66
73
  num_classes=4,
67
74
  )
@@ -90,7 +97,7 @@ class AlbertClassifier(Classifier):
90
97
  labels = [0, 3]
91
98
 
92
99
  # Pretrained classifier without preprocessing.
93
- classifier = keras_hub.models.AlbertClassifier.from_preset(
100
+ classifier = keras_hub.models.AlbertTextClassifier.from_preset(
94
101
  "albert_base_en_uncased",
95
102
  num_classes=4,
96
103
  preprocessor=None,
@@ -123,7 +130,7 @@ class AlbertClassifier(Classifier):
123
130
  tokenizer = keras_hub.models.AlbertTokenizer(
124
131
  proto=bytes_io.getvalue(),
125
132
  )
126
- preprocessor = keras_hub.models.AlbertPreprocessor(
133
+ preprocessor = keras_hub.models.AlbertTextClassifierPreprocessor(
127
134
  tokenizer=tokenizer,
128
135
  sequence_length=128,
129
136
  )
@@ -136,7 +143,7 @@ class AlbertClassifier(Classifier):
136
143
  intermediate_dim=512,
137
144
  max_sequence_length=128,
138
145
  )
139
- classifier = keras_hub.models.AlbertClassifier(
146
+ classifier = keras_hub.models.AlbertTextClassifier(
140
147
  backbone=backbone,
141
148
  preprocessor=preprocessor,
142
149
  num_classes=4,
@@ -146,7 +153,7 @@ class AlbertClassifier(Classifier):
146
153
  """
147
154
 
148
155
  backbone_cls = AlbertBackbone
149
- preprocessor_cls = AlbertPreprocessor
156
+ preprocessor_cls = AlbertTextClassifierPreprocessor
150
157
 
151
158
  def __init__(
152
159
  self,
@@ -189,6 +196,23 @@ class AlbertClassifier(Classifier):
189
196
  self.activation = keras.activations.get(activation)
190
197
  self.dropout = dropout
191
198
 
199
+ def compile(
200
+ self,
201
+ optimizer="auto",
202
+ loss="auto",
203
+ *,
204
+ metrics="auto",
205
+ **kwargs,
206
+ ):
207
+ if optimizer == "auto":
208
+ optimizer = keras.optimizers.Adam(1e-5)
209
+ super().compile(
210
+ optimizer=optimizer,
211
+ loss=loss,
212
+ metrics=metrics,
213
+ **kwargs,
214
+ )
215
+
192
216
  def get_config(self):
193
217
  config = super().get_config()
194
218
  config.update(
@@ -12,21 +12,21 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- import keras
16
-
17
15
  from keras_hub.src.api_export import keras_hub_export
18
- from keras_hub.src.layers.preprocessing.multi_segment_packer import (
19
- MultiSegmentPacker,
20
- )
16
+ from keras_hub.src.models.albert.albert_backbone import AlbertBackbone
21
17
  from keras_hub.src.models.albert.albert_tokenizer import AlbertTokenizer
22
- from keras_hub.src.models.preprocessor import Preprocessor
23
- from keras_hub.src.utils.keras_utils import (
24
- convert_inputs_to_list_of_tensor_segments,
18
+ from keras_hub.src.models.text_classifier_preprocessor import (
19
+ TextClassifierPreprocessor,
25
20
  )
26
21
 
27
22
 
28
- @keras_hub_export("keras_hub.models.AlbertPreprocessor")
29
- class AlbertPreprocessor(Preprocessor):
23
+ @keras_hub_export(
24
+ [
25
+ "keras_hub.models.AlbertTextClassifierPreprocessor",
26
+ "keras_hub.models.AlbertPreprocessor",
27
+ ]
28
+ )
29
+ class AlbertTextClassifierPreprocessor(TextClassifierPreprocessor):
30
30
  """An ALBERT preprocessing layer which tokenizes and packs inputs.
31
31
 
32
32
  This preprocessing layer will do three things:
@@ -72,7 +72,7 @@ class AlbertPreprocessor(Preprocessor):
72
72
  Examples:
73
73
  Directly calling the layer on data.
74
74
  ```python
75
- preprocessor = keras_hub.models.AlbertPreprocessor.from_preset(
75
+ preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
76
76
  "albert_base_en_uncased"
77
77
  )
78
78
 
@@ -109,13 +109,13 @@ class AlbertPreprocessor(Preprocessor):
109
109
  tokenizer = keras_hub.models.AlbertTokenizer(
110
110
  proto=bytes_io.getvalue(),
111
111
  )
112
- preprocessor = keras_hub.models.AlbertPreprocessor(tokenizer)
112
+ preprocessor = keras_hub.models.AlbertTextClassifierPreprocessor(tokenizer)
113
113
  preprocessor("The quick brown fox jumped.")
114
114
  ```
115
115
 
116
116
  Mapping with `tf.data.Dataset`.
117
117
  ```python
118
- preprocessor = keras_hub.models.AlbertPreprocessor.from_preset(
118
+ preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
119
119
  "albert_base_en_uncased"
120
120
  )
121
121
 
@@ -146,61 +146,5 @@ class AlbertPreprocessor(Preprocessor):
146
146
  ```
147
147
  """
148
148
 
149
+ backbone_cls = AlbertBackbone
149
150
  tokenizer_cls = AlbertTokenizer
150
-
151
- def __init__(
152
- self,
153
- tokenizer,
154
- sequence_length=512,
155
- truncate="round_robin",
156
- **kwargs,
157
- ):
158
- super().__init__(**kwargs)
159
- self.tokenizer = tokenizer
160
- self.packer = None
161
- self.truncate = truncate
162
- self.sequence_length = sequence_length
163
-
164
- def build(self, input_shape):
165
- # Defer packer creation to `build()` so that we can be sure tokenizer
166
- # assets have loaded when restoring a saved model.
167
- self.packer = MultiSegmentPacker(
168
- start_value=self.tokenizer.cls_token_id,
169
- end_value=self.tokenizer.sep_token_id,
170
- pad_value=self.tokenizer.pad_token_id,
171
- truncate=self.truncate,
172
- sequence_length=self.sequence_length,
173
- )
174
- self.built = True
175
-
176
- def get_config(self):
177
- config = super().get_config()
178
- config.update(
179
- {
180
- "sequence_length": self.sequence_length,
181
- "truncate": self.truncate,
182
- }
183
- )
184
- return config
185
-
186
- def call(self, x, y=None, sample_weight=None):
187
- x = convert_inputs_to_list_of_tensor_segments(x)
188
- x = [self.tokenizer(segment) for segment in x]
189
- token_ids, segment_ids = self.packer(x)
190
- x = {
191
- "token_ids": token_ids,
192
- "segment_ids": segment_ids,
193
- "padding_mask": token_ids != self.tokenizer.pad_token_id,
194
- }
195
- return keras.utils.pack_x_y_sample_weight(x, y, sample_weight)
196
-
197
- @property
198
- def sequence_length(self):
199
- """The padded length of model input sequences."""
200
- return self._sequence_length
201
-
202
- @sequence_length.setter
203
- def sequence_length(self, value):
204
- self._sequence_length = value
205
- if self.packer is not None:
206
- self.packer.sequence_length = value
@@ -13,12 +13,18 @@
13
13
  # limitations under the License.
14
14
 
15
15
  from keras_hub.src.api_export import keras_hub_export
16
+ from keras_hub.src.models.albert.albert_backbone import AlbertBackbone
16
17
  from keras_hub.src.tokenizers.sentence_piece_tokenizer import (
17
18
  SentencePieceTokenizer,
18
19
  )
19
20
 
20
21
 
21
- @keras_hub_export("keras_hub.models.AlbertTokenizer")
22
+ @keras_hub_export(
23
+ [
24
+ "keras_hub.tokenizers.AlbertTokenizer",
25
+ "keras_hub.models.AlbertTokenizer",
26
+ ]
27
+ )
22
28
  class AlbertTokenizer(SentencePieceTokenizer):
23
29
  """ALBERT tokenizer layer based on SentencePiece.
24
30
 
@@ -28,10 +34,6 @@ class AlbertTokenizer(SentencePieceTokenizer):
28
34
  ALBERT models and provides a `from_preset()` method to automatically
29
35
  download a matching vocabulary for a ALBERT preset.
30
36
 
31
- This tokenizer does not provide truncation or padding of inputs. It can be
32
- combined with a `keras_hub.models.AlbertPreprocessor` layer for input
33
- packing.
34
-
35
37
  If input is a batch of strings (rank > 0), the layer will output a
36
38
  `tf.RaggedTensor` where the last dimension of the output is ragged.
37
39
 
@@ -84,36 +86,15 @@ class AlbertTokenizer(SentencePieceTokenizer):
84
86
  ```
85
87
  """
86
88
 
87
- def __init__(self, proto, **kwargs):
88
- self.cls_token = "[CLS]"
89
- self.sep_token = "[SEP]"
90
- self.pad_token = "<pad>"
91
- self.mask_token = "[MASK]"
89
+ backbone_cls = AlbertBackbone
92
90
 
91
+ def __init__(self, proto, **kwargs):
92
+ self._add_special_token("[CLS]", "cls_token")
93
+ self._add_special_token("[SEP]", "sep_token")
94
+ self._add_special_token("<pad>", "pad_token")
95
+ self._add_special_token("[MASK]", "mask_token")
96
+ # Also add `tokenizer.start_token` and `tokenizer.end_token` for
97
+ # compatibility with other tokenizers.
98
+ self._add_special_token("[CLS]", "start_token")
99
+ self._add_special_token("[SEP]", "end_token")
93
100
  super().__init__(proto=proto, **kwargs)
94
-
95
- def set_proto(self, proto):
96
- super().set_proto(proto)
97
- if proto is not None:
98
- for token in [
99
- self.cls_token,
100
- self.sep_token,
101
- self.pad_token,
102
- self.mask_token,
103
- ]:
104
- if token not in self.get_vocabulary():
105
- raise ValueError(
106
- f"Cannot find token `'{token}'` in the provided "
107
- f"`vocabulary`. Please provide `'{token}'` in your "
108
- "`vocabulary` or use a pretrained `vocabulary` name."
109
- )
110
-
111
- self.cls_token_id = self.token_to_id(self.cls_token)
112
- self.sep_token_id = self.token_to_id(self.sep_token)
113
- self.pad_token_id = self.token_to_id(self.pad_token)
114
- self.mask_token_id = self.token_to_id(self.mask_token)
115
- else:
116
- self.cls_token_id = None
117
- self.sep_token_id = None
118
- self.pad_token_id = None
119
- self.mask_token_id = None