keras-hub-nightly 0.15.0.dev20240911134614__py3-none-any.whl → 0.16.0.dev202409201942__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 (52) hide show
  1. keras_hub/__init__.py +0 -6
  2. keras_hub/api/__init__.py +1 -0
  3. keras_hub/api/models/__init__.py +22 -17
  4. keras_hub/{src/models/llama3/llama3_preprocessor.py → api/utils/__init__.py} +7 -8
  5. keras_hub/src/api_export.py +15 -9
  6. keras_hub/src/models/albert/albert_text_classifier.py +6 -1
  7. keras_hub/src/models/bert/bert_text_classifier.py +6 -1
  8. keras_hub/src/models/deberta_v3/deberta_v3_text_classifier.py +6 -1
  9. keras_hub/src/models/densenet/densenet_backbone.py +1 -1
  10. keras_hub/src/models/distil_bert/distil_bert_text_classifier.py +6 -1
  11. keras_hub/src/models/f_net/f_net_text_classifier.py +6 -1
  12. keras_hub/src/models/gemma/gemma_decoder_block.py +1 -1
  13. keras_hub/src/models/gpt2/gpt2_preprocessor.py +7 -78
  14. keras_hub/src/models/pali_gemma/pali_gemma_tokenizer.py +1 -1
  15. keras_hub/src/models/preprocessor.py +1 -5
  16. keras_hub/src/models/resnet/resnet_backbone.py +3 -16
  17. keras_hub/src/models/resnet/resnet_image_classifier.py +26 -3
  18. keras_hub/src/models/resnet/resnet_presets.py +12 -12
  19. keras_hub/src/models/retinanet/__init__.py +13 -0
  20. keras_hub/src/models/retinanet/anchor_generator.py +175 -0
  21. keras_hub/src/models/retinanet/box_matcher.py +259 -0
  22. keras_hub/src/models/retinanet/non_max_supression.py +578 -0
  23. keras_hub/src/models/roberta/roberta_text_classifier.py +6 -1
  24. keras_hub/src/models/task.py +6 -6
  25. keras_hub/src/models/text_classifier.py +12 -1
  26. keras_hub/src/models/xlm_roberta/xlm_roberta_text_classifier.py +6 -1
  27. keras_hub/src/tests/test_case.py +21 -0
  28. keras_hub/src/tokenizers/byte_pair_tokenizer.py +1 -0
  29. keras_hub/src/tokenizers/sentence_piece_tokenizer.py +1 -0
  30. keras_hub/src/tokenizers/word_piece_tokenizer.py +1 -0
  31. keras_hub/src/utils/imagenet/__init__.py +13 -0
  32. keras_hub/src/utils/imagenet/imagenet_utils.py +1067 -0
  33. keras_hub/src/utils/preset_utils.py +24 -33
  34. keras_hub/src/utils/tensor_utils.py +14 -14
  35. keras_hub/src/utils/timm/convert_resnet.py +0 -1
  36. keras_hub/src/utils/timm/preset_loader.py +6 -7
  37. keras_hub/src/version_utils.py +1 -1
  38. keras_hub_nightly-0.16.0.dev202409201942.dist-info/METADATA +202 -0
  39. {keras_hub_nightly-0.15.0.dev20240911134614.dist-info → keras_hub_nightly-0.16.0.dev202409201942.dist-info}/RECORD +41 -45
  40. {keras_hub_nightly-0.15.0.dev20240911134614.dist-info → keras_hub_nightly-0.16.0.dev202409201942.dist-info}/WHEEL +1 -1
  41. keras_hub/src/models/bart/bart_preprocessor.py +0 -264
  42. keras_hub/src/models/bloom/bloom_preprocessor.py +0 -178
  43. keras_hub/src/models/electra/electra_preprocessor.py +0 -155
  44. keras_hub/src/models/falcon/falcon_preprocessor.py +0 -180
  45. keras_hub/src/models/gemma/gemma_preprocessor.py +0 -184
  46. keras_hub/src/models/gpt_neo_x/gpt_neo_x_preprocessor.py +0 -138
  47. keras_hub/src/models/llama/llama_preprocessor.py +0 -182
  48. keras_hub/src/models/mistral/mistral_preprocessor.py +0 -183
  49. keras_hub/src/models/opt/opt_preprocessor.py +0 -181
  50. keras_hub/src/models/phi3/phi3_preprocessor.py +0 -183
  51. keras_hub_nightly-0.15.0.dev20240911134614.dist-info/METADATA +0 -33
  52. {keras_hub_nightly-0.15.0.dev20240911134614.dist-info → keras_hub_nightly-0.16.0.dev202409201942.dist-info}/top_level.txt +0 -0
keras_hub/__init__.py CHANGED
@@ -11,12 +11,6 @@
11
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
- """DO NOT EDIT.
15
-
16
- This file was autogenerated. Do not edit it by hand,
17
- since your modifications would be overwritten.
18
- """
19
-
20
14
  import os
21
15
 
22
16
  # sentencepiece segfaults on some version of tensorflow if tf is imported first.
keras_hub/api/__init__.py CHANGED
@@ -23,6 +23,7 @@ from keras_hub.api import metrics
23
23
  from keras_hub.api import models
24
24
  from keras_hub.api import samplers
25
25
  from keras_hub.api import tokenizers
26
+ from keras_hub.api import utils
26
27
  from keras_hub.src.utils.preset_utils import upload_preset
27
28
  from keras_hub.src.version_utils import __version__
28
29
  from keras_hub.src.version_utils import version
@@ -25,6 +25,9 @@ from keras_hub.src.models.albert.albert_masked_lm_preprocessor import (
25
25
  from keras_hub.src.models.albert.albert_text_classifier import (
26
26
  AlbertTextClassifier,
27
27
  )
28
+ from keras_hub.src.models.albert.albert_text_classifier import (
29
+ AlbertTextClassifier as AlbertClassifier,
30
+ )
28
31
  from keras_hub.src.models.albert.albert_text_classifier_preprocessor import (
29
32
  AlbertTextClassifierPreprocessor,
30
33
  )
@@ -34,7 +37,6 @@ from keras_hub.src.models.albert.albert_text_classifier_preprocessor import (
34
37
  from keras_hub.src.models.albert.albert_tokenizer import AlbertTokenizer
35
38
  from keras_hub.src.models.backbone import Backbone
36
39
  from keras_hub.src.models.bart.bart_backbone import BartBackbone
37
- from keras_hub.src.models.bart.bart_preprocessor import BartPreprocessor
38
40
  from keras_hub.src.models.bart.bart_seq_2_seq_lm import BartSeq2SeqLM
39
41
  from keras_hub.src.models.bart.bart_seq_2_seq_lm_preprocessor import (
40
42
  BartSeq2SeqLMPreprocessor,
@@ -46,6 +48,9 @@ from keras_hub.src.models.bert.bert_masked_lm_preprocessor import (
46
48
  BertMaskedLMPreprocessor,
47
49
  )
48
50
  from keras_hub.src.models.bert.bert_text_classifier import BertTextClassifier
51
+ from keras_hub.src.models.bert.bert_text_classifier import (
52
+ BertTextClassifier as BertClassifier,
53
+ )
49
54
  from keras_hub.src.models.bert.bert_text_classifier_preprocessor import (
50
55
  BertTextClassifierPreprocessor,
51
56
  )
@@ -58,7 +63,6 @@ from keras_hub.src.models.bloom.bloom_causal_lm import BloomCausalLM
58
63
  from keras_hub.src.models.bloom.bloom_causal_lm_preprocessor import (
59
64
  BloomCausalLMPreprocessor,
60
65
  )
61
- from keras_hub.src.models.bloom.bloom_preprocessor import BloomPreprocessor
62
66
  from keras_hub.src.models.bloom.bloom_tokenizer import BloomTokenizer
63
67
  from keras_hub.src.models.causal_lm import CausalLM
64
68
  from keras_hub.src.models.causal_lm_preprocessor import CausalLMPreprocessor
@@ -80,6 +84,9 @@ from keras_hub.src.models.deberta_v3.deberta_v3_masked_lm_preprocessor import (
80
84
  from keras_hub.src.models.deberta_v3.deberta_v3_text_classifier import (
81
85
  DebertaV3TextClassifier,
82
86
  )
87
+ from keras_hub.src.models.deberta_v3.deberta_v3_text_classifier import (
88
+ DebertaV3TextClassifier as DebertaV3Classifier,
89
+ )
83
90
  from keras_hub.src.models.deberta_v3.deberta_v3_text_classifier_preprocessor import (
84
91
  DebertaV3TextClassifierPreprocessor,
85
92
  )
@@ -105,6 +112,9 @@ from keras_hub.src.models.distil_bert.distil_bert_masked_lm_preprocessor import
105
112
  from keras_hub.src.models.distil_bert.distil_bert_text_classifier import (
106
113
  DistilBertTextClassifier,
107
114
  )
115
+ from keras_hub.src.models.distil_bert.distil_bert_text_classifier import (
116
+ DistilBertTextClassifier as DistilBertClassifier,
117
+ )
108
118
  from keras_hub.src.models.distil_bert.distil_bert_text_classifier_preprocessor import (
109
119
  DistilBertTextClassifierPreprocessor,
110
120
  )
@@ -118,9 +128,6 @@ from keras_hub.src.models.efficientnet.efficientnet_backbone import (
118
128
  EfficientNetBackbone,
119
129
  )
120
130
  from keras_hub.src.models.electra.electra_backbone import ElectraBackbone
121
- from keras_hub.src.models.electra.electra_preprocessor import (
122
- ElectraPreprocessor,
123
- )
124
131
  from keras_hub.src.models.electra.electra_tokenizer import ElectraTokenizer
125
132
  from keras_hub.src.models.f_net.f_net_backbone import FNetBackbone
126
133
  from keras_hub.src.models.f_net.f_net_masked_lm import FNetMaskedLM
@@ -128,6 +135,9 @@ from keras_hub.src.models.f_net.f_net_masked_lm_preprocessor import (
128
135
  FNetMaskedLMPreprocessor,
129
136
  )
130
137
  from keras_hub.src.models.f_net.f_net_text_classifier import FNetTextClassifier
138
+ from keras_hub.src.models.f_net.f_net_text_classifier import (
139
+ FNetTextClassifier as FNetClassifier,
140
+ )
131
141
  from keras_hub.src.models.f_net.f_net_text_classifier_preprocessor import (
132
142
  FNetTextClassifierPreprocessor,
133
143
  )
@@ -140,7 +150,6 @@ from keras_hub.src.models.falcon.falcon_causal_lm import FalconCausalLM
140
150
  from keras_hub.src.models.falcon.falcon_causal_lm_preprocessor import (
141
151
  FalconCausalLMPreprocessor,
142
152
  )
143
- from keras_hub.src.models.falcon.falcon_preprocessor import FalconPreprocessor
144
153
  from keras_hub.src.models.falcon.falcon_tokenizer import FalconTokenizer
145
154
  from keras_hub.src.models.feature_pyramid_backbone import FeaturePyramidBackbone
146
155
  from keras_hub.src.models.gemma.gemma_backbone import GemmaBackbone
@@ -148,7 +157,6 @@ from keras_hub.src.models.gemma.gemma_causal_lm import GemmaCausalLM
148
157
  from keras_hub.src.models.gemma.gemma_causal_lm_preprocessor import (
149
158
  GemmaCausalLMPreprocessor,
150
159
  )
151
- from keras_hub.src.models.gemma.gemma_preprocessor import GemmaPreprocessor
152
160
  from keras_hub.src.models.gemma.gemma_tokenizer import GemmaTokenizer
153
161
  from keras_hub.src.models.gpt2.gpt2_backbone import GPT2Backbone
154
162
  from keras_hub.src.models.gpt2.gpt2_causal_lm import GPT2CausalLM
@@ -162,9 +170,6 @@ from keras_hub.src.models.gpt_neo_x.gpt_neo_x_causal_lm import GPTNeoXCausalLM
162
170
  from keras_hub.src.models.gpt_neo_x.gpt_neo_x_causal_lm_preprocessor import (
163
171
  GPTNeoXCausalLMPreprocessor,
164
172
  )
165
- from keras_hub.src.models.gpt_neo_x.gpt_neo_x_preprocessor import (
166
- GPTNeoXPreprocessor,
167
- )
168
173
  from keras_hub.src.models.gpt_neo_x.gpt_neo_x_tokenizer import GPTNeoXTokenizer
169
174
  from keras_hub.src.models.image_classifier import ImageClassifier
170
175
  from keras_hub.src.models.image_classifier_preprocessor import (
@@ -175,14 +180,12 @@ from keras_hub.src.models.llama3.llama3_causal_lm import Llama3CausalLM
175
180
  from keras_hub.src.models.llama3.llama3_causal_lm_preprocessor import (
176
181
  Llama3CausalLMPreprocessor,
177
182
  )
178
- from keras_hub.src.models.llama3.llama3_preprocessor import Llama3Preprocessor
179
183
  from keras_hub.src.models.llama3.llama3_tokenizer import Llama3Tokenizer
180
184
  from keras_hub.src.models.llama.llama_backbone import LlamaBackbone
181
185
  from keras_hub.src.models.llama.llama_causal_lm import LlamaCausalLM
182
186
  from keras_hub.src.models.llama.llama_causal_lm_preprocessor import (
183
187
  LlamaCausalLMPreprocessor,
184
188
  )
185
- from keras_hub.src.models.llama.llama_preprocessor import LlamaPreprocessor
186
189
  from keras_hub.src.models.llama.llama_tokenizer import LlamaTokenizer
187
190
  from keras_hub.src.models.masked_lm import MaskedLM
188
191
  from keras_hub.src.models.masked_lm_preprocessor import MaskedLMPreprocessor
@@ -191,9 +194,6 @@ from keras_hub.src.models.mistral.mistral_causal_lm import MistralCausalLM
191
194
  from keras_hub.src.models.mistral.mistral_causal_lm_preprocessor import (
192
195
  MistralCausalLMPreprocessor,
193
196
  )
194
- from keras_hub.src.models.mistral.mistral_preprocessor import (
195
- MistralPreprocessor,
196
- )
197
197
  from keras_hub.src.models.mistral.mistral_tokenizer import MistralTokenizer
198
198
  from keras_hub.src.models.mix_transformer.mix_transformer_backbone import (
199
199
  MiTBackbone,
@@ -210,7 +210,6 @@ from keras_hub.src.models.opt.opt_causal_lm import OPTCausalLM
210
210
  from keras_hub.src.models.opt.opt_causal_lm_preprocessor import (
211
211
  OPTCausalLMPreprocessor,
212
212
  )
213
- from keras_hub.src.models.opt.opt_preprocessor import OPTPreprocessor
214
213
  from keras_hub.src.models.opt.opt_tokenizer import OPTTokenizer
215
214
  from keras_hub.src.models.pali_gemma.pali_gemma_backbone import (
216
215
  PaliGemmaBackbone,
@@ -229,7 +228,6 @@ from keras_hub.src.models.phi3.phi3_causal_lm import Phi3CausalLM
229
228
  from keras_hub.src.models.phi3.phi3_causal_lm_preprocessor import (
230
229
  Phi3CausalLMPreprocessor,
231
230
  )
232
- from keras_hub.src.models.phi3.phi3_preprocessor import Phi3Preprocessor
233
231
  from keras_hub.src.models.phi3.phi3_tokenizer import Phi3Tokenizer
234
232
  from keras_hub.src.models.preprocessor import Preprocessor
235
233
  from keras_hub.src.models.resnet.resnet_backbone import ResNetBackbone
@@ -247,6 +245,9 @@ from keras_hub.src.models.roberta.roberta_masked_lm_preprocessor import (
247
245
  from keras_hub.src.models.roberta.roberta_text_classifier import (
248
246
  RobertaTextClassifier,
249
247
  )
248
+ from keras_hub.src.models.roberta.roberta_text_classifier import (
249
+ RobertaTextClassifier as RobertaClassifier,
250
+ )
250
251
  from keras_hub.src.models.roberta.roberta_text_classifier_preprocessor import (
251
252
  RobertaTextClassifierPreprocessor,
252
253
  )
@@ -260,6 +261,7 @@ from keras_hub.src.models.t5.t5_backbone import T5Backbone
260
261
  from keras_hub.src.models.t5.t5_tokenizer import T5Tokenizer
261
262
  from keras_hub.src.models.task import Task
262
263
  from keras_hub.src.models.text_classifier import TextClassifier
264
+ from keras_hub.src.models.text_classifier import TextClassifier as Classifier
263
265
  from keras_hub.src.models.text_classifier_preprocessor import (
264
266
  TextClassifierPreprocessor,
265
267
  )
@@ -280,6 +282,9 @@ from keras_hub.src.models.xlm_roberta.xlm_roberta_masked_lm_preprocessor import
280
282
  from keras_hub.src.models.xlm_roberta.xlm_roberta_text_classifier import (
281
283
  XLMRobertaTextClassifier,
282
284
  )
285
+ from keras_hub.src.models.xlm_roberta.xlm_roberta_text_classifier import (
286
+ XLMRobertaTextClassifier as XLMRobertaClassifier,
287
+ )
283
288
  from keras_hub.src.models.xlm_roberta.xlm_roberta_text_classifier_preprocessor import (
284
289
  XLMRobertaTextClassifierPreprocessor,
285
290
  )
@@ -11,13 +11,12 @@
11
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
- from keras_hub.src.api_export import keras_hub_export
15
- from keras_hub.src.models.llama3.llama3_backbone import Llama3Backbone
16
- from keras_hub.src.models.llama3.llama3_tokenizer import Llama3Tokenizer
17
- from keras_hub.src.models.llama.llama_preprocessor import LlamaPreprocessor
14
+ """DO NOT EDIT.
18
15
 
16
+ This file was autogenerated. Do not edit it by hand,
17
+ since your modifications would be overwritten.
18
+ """
19
19
 
20
- @keras_hub_export("keras_hub.models.Llama3Preprocessor")
21
- class Llama3Preprocessor(LlamaPreprocessor):
22
- backbone_cls = Llama3Backbone
23
- tokenizer_cls = Llama3Tokenizer
20
+ from keras_hub.src.utils.imagenet.imagenet_utils import (
21
+ decode_imagenet_predictions,
22
+ )
@@ -14,7 +14,7 @@
14
14
 
15
15
  import types
16
16
 
17
- import keras
17
+ from keras.saving import register_keras_serializable
18
18
 
19
19
  try:
20
20
  import namex
@@ -22,14 +22,20 @@ except ImportError:
22
22
  namex = None
23
23
 
24
24
 
25
- def maybe_register_serializable(symbol):
25
+ def maybe_register_serializable(path, symbol):
26
+ if isinstance(path, (list, tuple)):
27
+ # If we have multiple export names, actually make sure to register these
28
+ # first. This makes sure we have a backward compat mapping of old
29
+ # serialized names to new class.
30
+ for name in path:
31
+ name = name.split(".")[-1]
32
+ register_keras_serializable(package="keras_nlp", name=name)(symbol)
33
+ register_keras_serializable(package="keras_hub", name=name)(symbol)
26
34
  if isinstance(symbol, types.FunctionType) or hasattr(symbol, "get_config"):
27
- # We register twice, first with the old name, second with the new name,
28
- # so loading still works under the old name.
29
- # TODO replace keras_nlp with keras-hub after rename.
30
- compat_name = "keras_nlp"
31
- keras.saving.register_keras_serializable(package=compat_name)(symbol)
32
- keras.saving.register_keras_serializable(package="keras_hub")(symbol)
35
+ # We register twice, first with keras_nlp, second with keras_hub,
36
+ # so loading still works for classes saved as "keras_nlp".
37
+ register_keras_serializable(package="keras_nlp")(symbol)
38
+ register_keras_serializable(package="keras_hub")(symbol)
33
39
 
34
40
 
35
41
  if namex:
@@ -39,7 +45,7 @@ if namex:
39
45
  super().__init__(package="keras_hub", path=path)
40
46
 
41
47
  def __call__(self, symbol):
42
- maybe_register_serializable(symbol)
48
+ maybe_register_serializable(self.path, symbol)
43
49
  return super().__call__(symbol)
44
50
 
45
51
  else:
@@ -25,7 +25,12 @@ from keras_hub.src.models.albert.albert_text_classifier_preprocessor import (
25
25
  from keras_hub.src.models.text_classifier import TextClassifier
26
26
 
27
27
 
28
- @keras_hub_export("keras_hub.models.AlbertTextClassifier")
28
+ @keras_hub_export(
29
+ [
30
+ "keras_hub.models.AlbertTextClassifier",
31
+ "keras_hub.models.AlbertClassifier",
32
+ ]
33
+ )
29
34
  class AlbertTextClassifier(TextClassifier):
30
35
  """An end-to-end ALBERT model for classification tasks
31
36
 
@@ -23,7 +23,12 @@ from keras_hub.src.models.bert.bert_text_classifier_preprocessor import (
23
23
  from keras_hub.src.models.text_classifier import TextClassifier
24
24
 
25
25
 
26
- @keras_hub_export("keras_hub.models.BertTextClassifier")
26
+ @keras_hub_export(
27
+ [
28
+ "keras_hub.models.BertTextClassifier",
29
+ "keras_hub.models.BertClassifier",
30
+ ]
31
+ )
27
32
  class BertTextClassifier(TextClassifier):
28
33
  """An end-to-end BERT model for classification tasks.
29
34
 
@@ -28,7 +28,12 @@ from keras_hub.src.models.deberta_v3.deberta_v3_text_classifier_preprocessor imp
28
28
  from keras_hub.src.models.text_classifier import TextClassifier
29
29
 
30
30
 
31
- @keras_hub_export("keras_hub.models.DebertaV3TextClassifier")
31
+ @keras_hub_export(
32
+ [
33
+ "keras_hub.models.DebertaV3TextClassifier",
34
+ "keras_hub.models.DebertaV3Classifier",
35
+ ]
36
+ )
32
37
  class DebertaV3TextClassifier(TextClassifier):
33
38
  """An end-to-end DeBERTa model for classification tasks.
34
39
 
@@ -114,7 +114,7 @@ class DenseNetBackbone(FeaturePyramidBackbone):
114
114
  growth_rate,
115
115
  name=f"conv{len(stackwise_num_repeats) + 1}",
116
116
  )
117
- pyramid_outputs[f"P{len(stackwise_num_repeats) +1}"] = x
117
+ pyramid_outputs[f"P{len(stackwise_num_repeats) + 1}"] = x
118
118
  x = keras.layers.BatchNormalization(
119
119
  axis=channel_axis, epsilon=BN_EPSILON, name="bn"
120
120
  )(x)
@@ -28,7 +28,12 @@ from keras_hub.src.models.distil_bert.distil_bert_text_classifier_preprocessor i
28
28
  from keras_hub.src.models.text_classifier import TextClassifier
29
29
 
30
30
 
31
- @keras_hub_export("keras_hub.models.DistilBertTextClassifier")
31
+ @keras_hub_export(
32
+ [
33
+ "keras_hub.models.DistilBertTextClassifier",
34
+ "keras_hub.models.DistilBertClassifier",
35
+ ]
36
+ )
32
37
  class DistilBertTextClassifier(TextClassifier):
33
38
  """An end-to-end DistilBERT model for classification tasks.
34
39
 
@@ -24,7 +24,12 @@ from keras_hub.src.models.f_net.f_net_text_classifier_preprocessor import (
24
24
  from keras_hub.src.models.text_classifier import TextClassifier
25
25
 
26
26
 
27
- @keras_hub_export("keras_hub.models.FNetTextClassifier")
27
+ @keras_hub_export(
28
+ [
29
+ "keras_hub.models.FNetTextClassifier",
30
+ "keras_hub.models.FNetClassifier",
31
+ ]
32
+ )
28
33
  class FNetTextClassifier(TextClassifier):
29
34
  """An end-to-end f_net model for classification tasks.
30
35
 
@@ -68,7 +68,7 @@ class GemmaDecoderBlock(keras.layers.Layer):
68
68
  self.post_attention_norm = RMSNormalization(
69
69
  epsilon=self.layer_norm_epsilon,
70
70
  dtype=self.dtype_policy,
71
- name="pre_attention_norm",
71
+ name="post_attention_norm",
72
72
  )
73
73
 
74
74
  self.attention = CachedGemmaAttention(
@@ -25,85 +25,12 @@ from keras_hub.src.utils.tensor_utils import preprocessing_function
25
25
 
26
26
  @keras_hub_export("keras_hub.models.GPT2Preprocessor")
27
27
  class GPT2Preprocessor(Preprocessor):
28
- """GPT2 preprocessing layer which tokenizes and packs inputs.
28
+ """Legacy preprocessing layer for GPT2.
29
29
 
30
- This preprocessing layer will do 2 things:
31
-
32
- - Tokenize the inputs using the `tokenizer`.
33
- - Construct a dictionary with keys `"token_ids"`, `"padding_mask"`, that can
34
- be passed directly to a `keras_hub.models.GPT2Backbone`.
35
-
36
- This layer can be used directly with `tf.data.Dataset.map` to preprocess
37
- string data in the `(x, y, sample_weight)` format used by
38
- `keras.Model.fit`.
39
-
40
- The call method of this layer accepts three arguments, `x`, `y`, and
41
- `sample_weight`. `x` can be a python string or tensor representing a single
42
- segment, a list of python strings representing a batch of single segments,
43
- or a list of tensors representing multiple segments to be packed together.
44
- `y` and `sample_weight` are both optional, can have any format, and will be
45
- passed through unaltered.
46
-
47
- `GPT2Preprocessor` forces the input to have only one segment, as GPT2 is
48
- mainly used for generation tasks. For tasks having multi-segment inputs
49
- like "glue/mnli", please use a model designed for classification purposes
50
- such as BERT or RoBERTa.
51
-
52
- Args:
53
- tokenizer: A `keras_hub.models.GPT2Tokenizer` instance.
54
- sequence_length: The length of the packed inputs.
55
- add_start_token: If `True`, the preprocessor will prepend the tokenizer
56
- start token to each input sequence.
57
- add_end_token: If `True`, the preprocessor will append the tokenizer
58
- end token to each input sequence.
59
-
60
- Call arguments:
61
- x: A string, `tf.Tensor` or list of python strings.
62
- y: Any label data. Will be passed through unaltered.
63
- sample_weight: Any label weight data. Will be passed through unaltered.
64
- sequence_length: Pass to override the configured `sequence_length` of
65
- the layer.
66
-
67
- Examples:
68
-
69
- Directly calling the layer on data.
70
- ```python
71
- preprocessor = keras_hub.models.GPT2Preprocessor.from_preset("gpt2_base_en")
72
-
73
- # Tokenize and pack a single sentence.
74
- preprocessor("The quick brown fox jumped.")
75
-
76
- # Tokenize a batch of single sentences.
77
- preprocessor(["The quick brown fox jumped.", "Call me Ishmael."])
78
-
79
- # Custom vocabulary.
80
- features = ["a quick fox.", "a fox quick."]
81
- vocab = {"<|endoftext|>": 0, "a": 4, "Ġquick": 5, "Ġfox": 6}
82
- merges = ["Ġ q", "u i", "c k", "ui ck", "Ġq uick"]
83
- merges += ["Ġ f", "o x", "Ġf ox"]
84
- tokenizer = keras_hub.models.GPT2Tokenizer(
85
- vocabulary=vocab,
86
- merges=merges,
87
- )
88
- preprocessor = keras_hub.models.GPT2Preprocessor(tokenizer=tokenizer)
89
- preprocessor("The quick brown fox jumped.")
90
- ```
91
-
92
- Mapping with `tf.data.Dataset`.
93
- ```python
94
- preprocessor = keras_hub.models.GPT2Preprocessor.from_preset("gpt2_base_en")
95
-
96
- text = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
97
- label = tf.constant([1, 1])
98
-
99
- # Map labeled single sentences.
100
- ds = tf.data.Dataset.from_tensor_slices((text, label))
101
- ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
102
-
103
- # Map unlabeled single sentences.
104
- ds = tf.data.Dataset.from_tensor_slices(text)
105
- ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
106
- ```
30
+ This layer should not be used in new code! All preprocessing layers pair
31
+ directly with a task. E.g. `BertClassifier` and
32
+ `BertClassifierPreprocessor`. Either use `GPT2CausalLMPreprocessor` or
33
+ wrap `GPT2Tokenizer` into a custom preprocessing layer or function.
107
34
  """
108
35
 
109
36
  backbone_cls = GPT2Backbone
@@ -117,6 +44,8 @@ class GPT2Preprocessor(Preprocessor):
117
44
  add_end_token=True,
118
45
  **kwargs,
119
46
  ):
47
+ # TODO: this class has some usage, but barely any, and is no longer
48
+ # documented. We should consider dropping it.
120
49
  super().__init__(**kwargs)
121
50
  self.tokenizer = tokenizer
122
51
  self.packer = None
@@ -12,7 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
  from keras_hub.src.api_export import keras_hub_export
15
- from keras_hub.src.models.gemma.gemma_preprocessor import GemmaTokenizer
15
+ from keras_hub.src.models.gemma.gemma_tokenizer import GemmaTokenizer
16
16
  from keras_hub.src.models.pali_gemma.pali_gemma_backbone import (
17
17
  PaliGemmaBackbone,
18
18
  )
@@ -126,7 +126,6 @@ class Preprocessor(PreprocessingLayer):
126
126
  def from_preset(
127
127
  cls,
128
128
  preset,
129
- load_task_extras=False,
130
129
  **kwargs,
131
130
  ):
132
131
  """Instantiate a `keras_hub.models.Preprocessor` from a model preset.
@@ -150,9 +149,6 @@ class Preprocessor(PreprocessingLayer):
150
149
  Args:
151
150
  preset: string. A built-in preset identifier, a Kaggle Models
152
151
  handle, a Hugging Face handle, or a path to a local directory.
153
- load_task_extras: bool. If `True`, load the saved task preprocessing
154
- configuration from a `preprocessing.json`. You might use this to
155
- restore the sequence length a model was fine-tuned with.
156
152
 
157
153
  Examples:
158
154
  ```python
@@ -179,7 +175,7 @@ class Preprocessor(PreprocessingLayer):
179
175
  # Detect the correct subclass if we need to.
180
176
  if cls.backbone_cls != backbone_cls:
181
177
  cls = find_subclass(preset, cls, backbone_cls)
182
- return loader.load_preprocessor(cls, load_task_extras, **kwargs)
178
+ return loader.load_preprocessor(cls, **kwargs)
183
179
 
184
180
  def save_to_preset(self, preset_dir):
185
181
  """Save preprocessor to a preset directory.
@@ -57,7 +57,7 @@ class ResNetBackbone(FeaturePyramidBackbone):
57
57
  stackwise_num_blocks: list of ints. The number of blocks for each stack.
58
58
  stackwise_num_strides: list of ints. The number of strides for each
59
59
  stack.
60
- block_type: str. The block type to stack. One of `"basic_block"` or
60
+ block_type: str. The block type to stack. One of `"basic_block"`,
61
61
  `"bottleneck_block"`, `"basic_block_vd"` or
62
62
  `"bottleneck_block_vd"`. Use `"basic_block"` for ResNet18 and
63
63
  ResNet34. Use `"bottleneck_block"` for ResNet50, ResNet101 and
@@ -126,7 +126,6 @@ class ResNetBackbone(FeaturePyramidBackbone):
126
126
  use_pre_activation=False,
127
127
  include_rescaling=True,
128
128
  image_shape=(None, None, 3),
129
- pooling="avg",
130
129
  data_format=None,
131
130
  dtype=None,
132
131
  **kwargs,
@@ -285,20 +284,9 @@ class ResNetBackbone(FeaturePyramidBackbone):
285
284
  )(x)
286
285
  x = layers.Activation("relu", dtype=dtype, name="post_relu")(x)
287
286
 
288
- if pooling == "avg":
289
- feature_map_output = layers.GlobalAveragePooling2D(
290
- data_format=data_format, dtype=dtype
291
- )(x)
292
- elif pooling == "max":
293
- feature_map_output = layers.GlobalMaxPooling2D(
294
- data_format=data_format, dtype=dtype
295
- )(x)
296
- else:
297
- feature_map_output = x
298
-
299
287
  super().__init__(
300
288
  inputs=image_input,
301
- outputs=feature_map_output,
289
+ outputs=x,
302
290
  dtype=dtype,
303
291
  **kwargs,
304
292
  )
@@ -313,8 +301,8 @@ class ResNetBackbone(FeaturePyramidBackbone):
313
301
  self.use_pre_activation = use_pre_activation
314
302
  self.include_rescaling = include_rescaling
315
303
  self.image_shape = image_shape
316
- self.pooling = pooling
317
304
  self.pyramid_outputs = pyramid_outputs
305
+ self.data_format = data_format
318
306
 
319
307
  def get_config(self):
320
308
  config = super().get_config()
@@ -329,7 +317,6 @@ class ResNetBackbone(FeaturePyramidBackbone):
329
317
  "use_pre_activation": self.use_pre_activation,
330
318
  "include_rescaling": self.include_rescaling,
331
319
  "image_shape": self.image_shape,
332
- "pooling": self.pooling,
333
320
  }
334
321
  )
335
322
  return config
@@ -45,7 +45,9 @@ class ResNetImageClassifier(ImageClassifier):
45
45
  ```python
46
46
  # Load preset and train
47
47
  images = np.ones((2, 224, 224, 3), dtype="float32")
48
- classifier = keras_hub.models.ResNetImageClassifier.from_preset("resnet50")
48
+ classifier = keras_hub.models.ResNetImageClassifier.from_preset(
49
+ "resnet_50_imagenet"
50
+ )
49
51
  classifier.predict(images)
50
52
  ```
51
53
 
@@ -54,13 +56,17 @@ class ResNetImageClassifier(ImageClassifier):
54
56
  # Load preset and train
55
57
  images = np.ones((2, 224, 224, 3), dtype="float32")
56
58
  labels = [0, 3]
57
- classifier = keras_hub.models.ResNetImageClassifier.from_preset("resnet50")
59
+ classifier = keras_hub.models.ResNetImageClassifier.from_preset(
60
+ "resnet_50_imagenet"
61
+ )
58
62
  classifier.fit(x=images, y=labels, batch_size=2)
59
63
  ```
60
64
 
61
65
  Call `fit()` with custom loss, optimizer and backbone.
62
66
  ```python
63
- classifier = keras_hub.models.ResNetImageClassifier.from_preset("resnet50")
67
+ classifier = keras_hub.models.ResNetImageClassifier.from_preset(
68
+ "resnet_50_imagenet"
69
+ )
64
70
  classifier.compile(
65
71
  loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
66
72
  optimizer=keras.optimizers.Adam(5e-5),
@@ -98,6 +104,7 @@ class ResNetImageClassifier(ImageClassifier):
98
104
  backbone,
99
105
  num_classes,
100
106
  preprocessor=None,
107
+ pooling="avg",
101
108
  activation=None,
102
109
  head_dtype=None,
103
110
  **kwargs,
@@ -107,6 +114,19 @@ class ResNetImageClassifier(ImageClassifier):
107
114
  # === Layers ===
108
115
  self.backbone = backbone
109
116
  self.preprocessor = preprocessor
117
+ if pooling == "avg":
118
+ self.pooler = keras.layers.GlobalAveragePooling2D(
119
+ data_format=backbone.data_format, dtype=head_dtype
120
+ )
121
+ elif pooling == "max":
122
+ self.pooler = keras.layers.GlobalAveragePooling2D(
123
+ data_format=backbone.data_format, dtype=head_dtype
124
+ )
125
+ else:
126
+ raise ValueError(
127
+ "Unknown `pooling` type. Polling should be either `'avg'` or "
128
+ f"`'max'`. Received: pooling={pooling}."
129
+ )
110
130
  self.output_dense = keras.layers.Dense(
111
131
  num_classes,
112
132
  activation=activation,
@@ -117,6 +137,7 @@ class ResNetImageClassifier(ImageClassifier):
117
137
  # === Functional Model ===
118
138
  inputs = self.backbone.input
119
139
  x = self.backbone(inputs)
140
+ x = self.pooler(x)
120
141
  outputs = self.output_dense(x)
121
142
  super().__init__(
122
143
  inputs=inputs,
@@ -127,6 +148,7 @@ class ResNetImageClassifier(ImageClassifier):
127
148
  # === Config ===
128
149
  self.num_classes = num_classes
129
150
  self.activation = activation
151
+ self.pooling = pooling
130
152
 
131
153
  def get_config(self):
132
154
  # Backbone serialized in `super`
@@ -134,6 +156,7 @@ class ResNetImageClassifier(ImageClassifier):
134
156
  config.update(
135
157
  {
136
158
  "num_classes": self.num_classes,
159
+ "pooling": self.pooling,
137
160
  "activation": self.activation,
138
161
  }
139
162
  )