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
@@ -12,19 +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.models.
|
20
|
-
from keras_hub.src.
|
21
|
-
|
22
|
-
)
|
23
|
-
from keras_hub.src.utils.tensor_utils import strip_to_ragged
|
16
|
+
from keras_hub.src.models.causal_lm_preprocessor import CausalLMPreprocessor
|
17
|
+
from keras_hub.src.models.gpt2.gpt2_backbone import GPT2Backbone
|
18
|
+
from keras_hub.src.models.gpt2.gpt2_tokenizer import GPT2Tokenizer
|
24
19
|
|
25
20
|
|
26
21
|
@keras_hub_export("keras_hub.models.GPT2CausalLMPreprocessor")
|
27
|
-
class GPT2CausalLMPreprocessor(
|
22
|
+
class GPT2CausalLMPreprocessor(CausalLMPreprocessor):
|
28
23
|
"""GPT2 Causal LM preprocessor.
|
29
24
|
|
30
25
|
This preprocessing layer is meant for use with
|
@@ -91,83 +86,5 @@ class GPT2CausalLMPreprocessor(GPT2Preprocessor):
|
|
91
86
|
```
|
92
87
|
"""
|
93
88
|
|
94
|
-
|
95
|
-
|
96
|
-
x,
|
97
|
-
y=None,
|
98
|
-
sample_weight=None,
|
99
|
-
sequence_length=None,
|
100
|
-
):
|
101
|
-
if y is not None or sample_weight is not None:
|
102
|
-
logging.warning(
|
103
|
-
"`GPT2CausalLMPreprocessor` generates `y` and `sample_weight` "
|
104
|
-
"based on your input data, but your data already contains `y` "
|
105
|
-
"or `sample_weight`. Your `y` and `sample_weight` will be "
|
106
|
-
"ignored."
|
107
|
-
)
|
108
|
-
sequence_length = sequence_length or self.sequence_length
|
109
|
-
|
110
|
-
x = convert_inputs_to_list_of_tensor_segments(x)[0]
|
111
|
-
x = self.tokenizer(x)
|
112
|
-
# Pad with one extra token to account for the truncation below.
|
113
|
-
token_ids, padding_mask = self.packer(
|
114
|
-
x,
|
115
|
-
sequence_length=sequence_length + 1,
|
116
|
-
add_start_value=self.add_start_token,
|
117
|
-
add_end_value=self.add_end_token,
|
118
|
-
)
|
119
|
-
# The last token does not have a next token, so we truncate it out.
|
120
|
-
x = {
|
121
|
-
"token_ids": token_ids[..., :-1],
|
122
|
-
"padding_mask": padding_mask[..., :-1],
|
123
|
-
}
|
124
|
-
# Target `y` will be the next token.
|
125
|
-
y, sample_weight = token_ids[..., 1:], padding_mask[..., 1:]
|
126
|
-
return keras.utils.pack_x_y_sample_weight(x, y, sample_weight)
|
127
|
-
|
128
|
-
def generate_preprocess(
|
129
|
-
self,
|
130
|
-
x,
|
131
|
-
sequence_length=None,
|
132
|
-
):
|
133
|
-
"""Convert strings to integer token input for generation.
|
134
|
-
|
135
|
-
Similar to calling the layer for training, this method takes in strings
|
136
|
-
or tensor strings, tokenizes and packs the input, and computes a padding
|
137
|
-
mask masking all inputs not filled in with a padded value.
|
138
|
-
|
139
|
-
Unlike calling the layer for training, this method does not compute
|
140
|
-
labels and will never append a `tokenizer.end_token_id` to the end of
|
141
|
-
the sequence (as generation is expected to continue at the end of the
|
142
|
-
inputted prompt).
|
143
|
-
"""
|
144
|
-
if not self.built:
|
145
|
-
self.build(None)
|
146
|
-
|
147
|
-
x = convert_inputs_to_list_of_tensor_segments(x)[0]
|
148
|
-
x = self.tokenizer(x)
|
149
|
-
token_ids, padding_mask = self.packer(
|
150
|
-
x, sequence_length=sequence_length, add_end_value=False
|
151
|
-
)
|
152
|
-
return {
|
153
|
-
"token_ids": token_ids,
|
154
|
-
"padding_mask": padding_mask,
|
155
|
-
}
|
156
|
-
|
157
|
-
def generate_postprocess(
|
158
|
-
self,
|
159
|
-
x,
|
160
|
-
):
|
161
|
-
"""Convert integer token output to strings for generation.
|
162
|
-
|
163
|
-
This method reverses `generate_preprocess()`, by first removing all
|
164
|
-
padding and start/end tokens, and then converting the integer sequence
|
165
|
-
back to a string.
|
166
|
-
"""
|
167
|
-
if not self.built:
|
168
|
-
self.build(None)
|
169
|
-
|
170
|
-
token_ids, padding_mask = x["token_ids"], x["padding_mask"]
|
171
|
-
ids_to_strip = (self.tokenizer.end_token_id,)
|
172
|
-
token_ids = strip_to_ragged(token_ids, padding_mask, ids_to_strip)
|
173
|
-
return self.tokenizer.detokenize(token_ids)
|
89
|
+
backbone_cls = GPT2Backbone
|
90
|
+
tokenizer_cls = GPT2Tokenizer
|
@@ -17,96 +17,23 @@ import keras
|
|
17
17
|
|
18
18
|
from keras_hub.src.api_export import keras_hub_export
|
19
19
|
from keras_hub.src.layers.preprocessing.start_end_packer import StartEndPacker
|
20
|
+
from keras_hub.src.models.gpt2.gpt2_backbone import GPT2Backbone
|
20
21
|
from keras_hub.src.models.gpt2.gpt2_tokenizer import GPT2Tokenizer
|
21
22
|
from keras_hub.src.models.preprocessor import Preprocessor
|
22
|
-
from keras_hub.src.utils.
|
23
|
-
convert_inputs_to_list_of_tensor_segments,
|
24
|
-
)
|
23
|
+
from keras_hub.src.utils.tensor_utils import preprocessing_function
|
25
24
|
|
26
25
|
|
27
26
|
@keras_hub_export("keras_hub.models.GPT2Preprocessor")
|
28
27
|
class GPT2Preprocessor(Preprocessor):
|
29
|
-
"""
|
28
|
+
"""Legacy preprocessing layer for GPT2.
|
30
29
|
|
31
|
-
This
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
be passed directly to a `keras_hub.models.GPT2Backbone`.
|
36
|
-
|
37
|
-
This layer can be used directly with `tf.data.Dataset.map` to preprocess
|
38
|
-
string data in the `(x, y, sample_weight)` format used by
|
39
|
-
`keras.Model.fit`.
|
40
|
-
|
41
|
-
The call method of this layer accepts three arguments, `x`, `y`, and
|
42
|
-
`sample_weight`. `x` can be a python string or tensor representing a single
|
43
|
-
segment, a list of python strings representing a batch of single segments,
|
44
|
-
or a list of tensors representing multiple segments to be packed together.
|
45
|
-
`y` and `sample_weight` are both optional, can have any format, and will be
|
46
|
-
passed through unaltered.
|
47
|
-
|
48
|
-
`GPT2Preprocessor` forces the input to have only one segment, as GPT2 is
|
49
|
-
mainly used for generation tasks. For tasks having multi-segment inputs
|
50
|
-
like "glue/mnli", please use a model designed for classification purposes
|
51
|
-
such as BERT or RoBERTa.
|
52
|
-
|
53
|
-
Args:
|
54
|
-
tokenizer: A `keras_hub.models.GPT2Tokenizer` instance.
|
55
|
-
sequence_length: The length of the packed inputs.
|
56
|
-
add_start_token: If `True`, the preprocessor will prepend the tokenizer
|
57
|
-
start token to each input sequence.
|
58
|
-
add_end_token: If `True`, the preprocessor will append the tokenizer
|
59
|
-
end token to each input sequence.
|
60
|
-
|
61
|
-
Call arguments:
|
62
|
-
x: A string, `tf.Tensor` or list of python strings.
|
63
|
-
y: Any label data. Will be passed through unaltered.
|
64
|
-
sample_weight: Any label weight data. Will be passed through unaltered.
|
65
|
-
sequence_length: Pass to override the configured `sequence_length` of
|
66
|
-
the layer.
|
67
|
-
|
68
|
-
Examples:
|
69
|
-
|
70
|
-
Directly calling the layer on data.
|
71
|
-
```python
|
72
|
-
preprocessor = keras_hub.models.GPT2Preprocessor.from_preset("gpt2_base_en")
|
73
|
-
|
74
|
-
# Tokenize and pack a single sentence.
|
75
|
-
preprocessor("The quick brown fox jumped.")
|
76
|
-
|
77
|
-
# Tokenize a batch of single sentences.
|
78
|
-
preprocessor(["The quick brown fox jumped.", "Call me Ishmael."])
|
79
|
-
|
80
|
-
# Custom vocabulary.
|
81
|
-
features = ["a quick fox.", "a fox quick."]
|
82
|
-
vocab = {"<|endoftext|>": 0, "a": 4, "Ġquick": 5, "Ġfox": 6}
|
83
|
-
merges = ["Ġ q", "u i", "c k", "ui ck", "Ġq uick"]
|
84
|
-
merges += ["Ġ f", "o x", "Ġf ox"]
|
85
|
-
tokenizer = keras_hub.models.GPT2Tokenizer(
|
86
|
-
vocabulary=vocab,
|
87
|
-
merges=merges,
|
88
|
-
)
|
89
|
-
preprocessor = keras_hub.models.GPT2Preprocessor(tokenizer=tokenizer)
|
90
|
-
preprocessor("The quick brown fox jumped.")
|
91
|
-
```
|
92
|
-
|
93
|
-
Mapping with `tf.data.Dataset`.
|
94
|
-
```python
|
95
|
-
preprocessor = keras_hub.models.GPT2Preprocessor.from_preset("gpt2_base_en")
|
96
|
-
|
97
|
-
text = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
|
98
|
-
label = tf.constant([1, 1])
|
99
|
-
|
100
|
-
# Map labeled single sentences.
|
101
|
-
ds = tf.data.Dataset.from_tensor_slices((text, label))
|
102
|
-
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
|
103
|
-
|
104
|
-
# Map unlabeled single sentences.
|
105
|
-
ds = tf.data.Dataset.from_tensor_slices(text)
|
106
|
-
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
|
107
|
-
```
|
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.
|
108
34
|
"""
|
109
35
|
|
36
|
+
backbone_cls = GPT2Backbone
|
110
37
|
tokenizer_cls = GPT2Tokenizer
|
111
38
|
|
112
39
|
def __init__(
|
@@ -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
|
@@ -136,6 +65,7 @@ class GPT2Preprocessor(Preprocessor):
|
|
136
65
|
)
|
137
66
|
self.built = True
|
138
67
|
|
68
|
+
@preprocessing_function
|
139
69
|
def call(
|
140
70
|
self,
|
141
71
|
x,
|
@@ -143,17 +73,9 @@ class GPT2Preprocessor(Preprocessor):
|
|
143
73
|
sample_weight=None,
|
144
74
|
sequence_length=None,
|
145
75
|
):
|
146
|
-
x = convert_inputs_to_list_of_tensor_segments(x)
|
147
|
-
if len(x) != 1:
|
148
|
-
raise ValueError(
|
149
|
-
"GPT2 requires each input feature to contain only "
|
150
|
-
f"one segment, but received {len(x)}. If you are using GPT2 "
|
151
|
-
"for a multi-segment classification task, please refer to "
|
152
|
-
"classification models like BERT or RoBERTa."
|
153
|
-
)
|
154
76
|
sequence_length = sequence_length or self.sequence_length
|
155
77
|
token_ids, padding_mask = self.packer(
|
156
|
-
self.tokenizer(x
|
78
|
+
self.tokenizer(x),
|
157
79
|
sequence_length=sequence_length,
|
158
80
|
add_start_value=self.add_start_token,
|
159
81
|
add_end_value=self.add_end_token,
|
@@ -14,10 +14,16 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
from keras_hub.src.api_export import keras_hub_export
|
17
|
+
from keras_hub.src.models.gpt2.gpt2_backbone import GPT2Backbone
|
17
18
|
from keras_hub.src.tokenizers.byte_pair_tokenizer import BytePairTokenizer
|
18
19
|
|
19
20
|
|
20
|
-
@keras_hub_export(
|
21
|
+
@keras_hub_export(
|
22
|
+
[
|
23
|
+
"keras_hub.tokenizers.GPT2Tokenizer",
|
24
|
+
"keras_hub.models.GPT2Tokenizer",
|
25
|
+
]
|
26
|
+
)
|
21
27
|
class GPT2Tokenizer(BytePairTokenizer):
|
22
28
|
"""A GPT-2 tokenizer using Byte-Pair Encoding subword segmentation.
|
23
29
|
|
@@ -27,8 +33,6 @@ class GPT2Tokenizer(BytePairTokenizer):
|
|
27
33
|
models and provides a `from_preset()` method to automatically download
|
28
34
|
a matching vocabulary for a GPT-2 preset.
|
29
35
|
|
30
|
-
This tokenizer does not provide truncation or padding of inputs.
|
31
|
-
|
32
36
|
If input is a batch of strings (rank > 0), the layer will output a
|
33
37
|
`tf.RaggedTensor` where the last dimension of the output is ragged.
|
34
38
|
|
@@ -65,6 +69,8 @@ class GPT2Tokenizer(BytePairTokenizer):
|
|
65
69
|
```
|
66
70
|
"""
|
67
71
|
|
72
|
+
backbone_cls = GPT2Backbone
|
73
|
+
|
68
74
|
def __init__(
|
69
75
|
self,
|
70
76
|
vocabulary=None,
|
@@ -72,39 +78,11 @@ class GPT2Tokenizer(BytePairTokenizer):
|
|
72
78
|
**kwargs,
|
73
79
|
):
|
74
80
|
# GPT2 uses the same start as end token, i.e., "<|endoftext|>".
|
75
|
-
self.
|
76
|
-
|
81
|
+
self._add_special_token("<|endoftext|>", "end_token")
|
82
|
+
self._add_special_token("<|endoftext|>", "start_token")
|
83
|
+
self.pad_token_id = 0
|
77
84
|
super().__init__(
|
78
85
|
vocabulary=vocabulary,
|
79
86
|
merges=merges,
|
80
|
-
unsplittable_tokens=[self.end_token],
|
81
87
|
**kwargs,
|
82
88
|
)
|
83
|
-
|
84
|
-
def set_vocabulary_and_merges(self, vocabulary, merges):
|
85
|
-
super().set_vocabulary_and_merges(vocabulary, merges)
|
86
|
-
|
87
|
-
if vocabulary is not None:
|
88
|
-
# Check for necessary special tokens.
|
89
|
-
if self.end_token not in self.get_vocabulary():
|
90
|
-
raise ValueError(
|
91
|
-
f"Cannot find token `'{self.end_token}'` in the provided "
|
92
|
-
f"`vocabulary`. Please provide `'{self.end_token}'` in "
|
93
|
-
"your `vocabulary` or use a pretrained `vocabulary` name."
|
94
|
-
)
|
95
|
-
|
96
|
-
self.end_token_id = self.token_to_id(self.end_token)
|
97
|
-
self.start_token_id = self.end_token_id
|
98
|
-
self.pad_token_id = 0
|
99
|
-
else:
|
100
|
-
self.end_token_id = None
|
101
|
-
self.start_token_id = None
|
102
|
-
self.pad_token_id = None
|
103
|
-
|
104
|
-
def get_config(self):
|
105
|
-
config = super().get_config()
|
106
|
-
# In the constructor, we pass the list of special tokens to the
|
107
|
-
# `unsplittable_tokens` arg of the superclass' constructor. Hence, we
|
108
|
-
# delete it from the config here.
|
109
|
-
del config["unsplittable_tokens"]
|
110
|
-
return config
|
@@ -12,21 +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.models.
|
20
|
-
|
21
|
-
|
22
|
-
from keras_hub.src.utils.keras_utils import (
|
23
|
-
convert_inputs_to_list_of_tensor_segments,
|
24
|
-
)
|
25
|
-
from keras_hub.src.utils.tensor_utils import strip_to_ragged
|
16
|
+
from keras_hub.src.models.causal_lm_preprocessor import CausalLMPreprocessor
|
17
|
+
from keras_hub.src.models.gpt_neo_x.gpt_neo_x_backbone import GPTNeoXBackbone
|
18
|
+
from keras_hub.src.models.gpt_neo_x.gpt_neo_x_tokenizer import GPTNeoXTokenizer
|
26
19
|
|
27
20
|
|
28
21
|
@keras_hub_export("keras_hub.models.GPTNeoXCausalLMPreprocessor")
|
29
|
-
class GPTNeoXCausalLMPreprocessor(
|
22
|
+
class GPTNeoXCausalLMPreprocessor(CausalLMPreprocessor):
|
30
23
|
"""GPT-NeoX Causal LM preprocessor.
|
31
24
|
|
32
25
|
This preprocessing layer is meant for use with
|
@@ -59,83 +52,5 @@ class GPTNeoXCausalLMPreprocessor(GPTNeoXPreprocessor):
|
|
59
52
|
|
60
53
|
"""
|
61
54
|
|
62
|
-
|
63
|
-
|
64
|
-
x,
|
65
|
-
y=None,
|
66
|
-
sample_weight=None,
|
67
|
-
sequence_length=None,
|
68
|
-
):
|
69
|
-
if y is not None or sample_weight is not None:
|
70
|
-
logging.warning(
|
71
|
-
"`GPTNeoXCausalLMPreprocessor` generates `y` and `sample_weight` "
|
72
|
-
"based on your input data, but your data already contains `y` "
|
73
|
-
"or `sample_weight`. Your `y` and `sample_weight` will be "
|
74
|
-
"ignored."
|
75
|
-
)
|
76
|
-
sequence_length = sequence_length or self.sequence_length
|
77
|
-
|
78
|
-
x = convert_inputs_to_list_of_tensor_segments(x)[0]
|
79
|
-
x = self.tokenizer(x)
|
80
|
-
# Pad with one extra token to account for the truncation below.
|
81
|
-
token_ids, padding_mask = self.packer(
|
82
|
-
x,
|
83
|
-
sequence_length=sequence_length + 1,
|
84
|
-
add_start_value=self.add_start_token,
|
85
|
-
add_end_value=self.add_end_token,
|
86
|
-
)
|
87
|
-
# The last token does not have a next token, so we truncate it out.
|
88
|
-
x = {
|
89
|
-
"token_ids": token_ids[..., :-1],
|
90
|
-
"padding_mask": padding_mask[..., :-1],
|
91
|
-
}
|
92
|
-
# Target `y` will be the next token.
|
93
|
-
y, sample_weight = token_ids[..., 1:], padding_mask[..., 1:]
|
94
|
-
return keras.utils.pack_x_y_sample_weight(x, y, sample_weight)
|
95
|
-
|
96
|
-
def generate_preprocess(
|
97
|
-
self,
|
98
|
-
x,
|
99
|
-
sequence_length=None,
|
100
|
-
):
|
101
|
-
"""Convert strings to integer token input for generation.
|
102
|
-
|
103
|
-
Similar to calling the layer for training, this method takes in strings
|
104
|
-
or tensor strings, tokenizes and packs the input, and computes a padding
|
105
|
-
mask masking all inputs not filled in with a padded value.
|
106
|
-
|
107
|
-
Unlike calling the layer for training, this method does not compute
|
108
|
-
labels and will never append a `tokenizer.end_token_id` to the end of
|
109
|
-
the sequence (as generation is expected to continue at the end of the
|
110
|
-
inputted prompt).
|
111
|
-
"""
|
112
|
-
if not self.built:
|
113
|
-
self.build(None)
|
114
|
-
|
115
|
-
x = convert_inputs_to_list_of_tensor_segments(x)[0]
|
116
|
-
x = self.tokenizer(x)
|
117
|
-
token_ids, padding_mask = self.packer(
|
118
|
-
x, sequence_length=sequence_length, add_end_value=False
|
119
|
-
)
|
120
|
-
return {
|
121
|
-
"token_ids": token_ids,
|
122
|
-
"padding_mask": padding_mask,
|
123
|
-
}
|
124
|
-
|
125
|
-
def generate_postprocess(
|
126
|
-
self,
|
127
|
-
x,
|
128
|
-
):
|
129
|
-
"""Convert integer token output to strings for generation.
|
130
|
-
|
131
|
-
This method reverses `generate_preprocess()`, by first removing all
|
132
|
-
padding and start/end tokens, and then converting the integer sequence
|
133
|
-
back to a string.
|
134
|
-
"""
|
135
|
-
if not self.built:
|
136
|
-
self.build(None)
|
137
|
-
|
138
|
-
token_ids, padding_mask = x["token_ids"], x["padding_mask"]
|
139
|
-
ids_to_strip = (self.tokenizer.end_token_id,)
|
140
|
-
token_ids = strip_to_ragged(token_ids, padding_mask, ids_to_strip)
|
141
|
-
return self.tokenizer.detokenize(token_ids)
|
55
|
+
backbone_cls = GPTNeoXBackbone
|
56
|
+
tokenizer_cls = GPTNeoXTokenizer
|
@@ -13,10 +13,16 @@
|
|
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.gpt_neo_x.gpt_neo_x_backbone import GPTNeoXBackbone
|
16
17
|
from keras_hub.src.tokenizers.byte_pair_tokenizer import BytePairTokenizer
|
17
18
|
|
18
19
|
|
19
|
-
@keras_hub_export(
|
20
|
+
@keras_hub_export(
|
21
|
+
[
|
22
|
+
"keras_hub.tokenizers.GPTNeoXTokenizer",
|
23
|
+
"keras_hub.models.GPTNeoXTokenizer",
|
24
|
+
]
|
25
|
+
)
|
20
26
|
class GPTNeoXTokenizer(BytePairTokenizer):
|
21
27
|
"""A GPTNeoX tokenizer using Byte-Pair Encoding subword segmentation.
|
22
28
|
|
@@ -26,8 +32,6 @@ class GPTNeoXTokenizer(BytePairTokenizer):
|
|
26
32
|
models and provides a `from_preset()` method to automatically download
|
27
33
|
a matching vocabulary for a GPTNeoX preset.
|
28
34
|
|
29
|
-
This tokenizer does not provide truncation or padding of inputs.
|
30
|
-
|
31
35
|
If input is a batch of strings (rank > 0), the layer will output a
|
32
36
|
`tf.RaggedTensor` where the last dimension of the output is ragged.
|
33
37
|
|
@@ -43,6 +47,8 @@ class GPTNeoXTokenizer(BytePairTokenizer):
|
|
43
47
|
merge entities separated by a space.
|
44
48
|
"""
|
45
49
|
|
50
|
+
backbone_cls = GPTNeoXBackbone
|
51
|
+
|
46
52
|
def __init__(
|
47
53
|
self,
|
48
54
|
vocabulary=None,
|
@@ -50,39 +56,11 @@ class GPTNeoXTokenizer(BytePairTokenizer):
|
|
50
56
|
**kwargs,
|
51
57
|
):
|
52
58
|
# GPTNeoX uses the same start as end token, i.e., "<|endoftext|>".
|
53
|
-
self.
|
54
|
-
|
59
|
+
self._add_special_token("<|endoftext|>", "end_token")
|
60
|
+
self._add_special_token("<|endoftext|>", "start_token")
|
61
|
+
self.pad_token_id = 0
|
55
62
|
super().__init__(
|
56
63
|
vocabulary=vocabulary,
|
57
64
|
merges=merges,
|
58
|
-
unsplittable_tokens=[self.end_token],
|
59
65
|
**kwargs,
|
60
66
|
)
|
61
|
-
|
62
|
-
def set_vocabulary_and_merges(self, vocabulary, merges):
|
63
|
-
super().set_vocabulary_and_merges(vocabulary, merges)
|
64
|
-
|
65
|
-
if vocabulary is not None:
|
66
|
-
# Check for necessary special tokens.
|
67
|
-
if self.end_token not in self.get_vocabulary():
|
68
|
-
raise ValueError(
|
69
|
-
f"Cannot find token `'{self.end_token}'` in the provided "
|
70
|
-
f"`vocabulary`. Please provide `'{self.end_token}'` in "
|
71
|
-
"your `vocabulary` or use a pretrained `vocabulary` name."
|
72
|
-
)
|
73
|
-
|
74
|
-
self.end_token_id = self.token_to_id(self.end_token)
|
75
|
-
self.start_token_id = self.end_token_id
|
76
|
-
self.pad_token_id = 0
|
77
|
-
else:
|
78
|
-
self.end_token_id = None
|
79
|
-
self.start_token_id = None
|
80
|
-
self.pad_token_id = None
|
81
|
-
|
82
|
-
def get_config(self):
|
83
|
-
config = super().get_config()
|
84
|
-
# In the constructor, we pass the list of special tokens to the
|
85
|
-
# `unsplittable_tokens` arg of the superclass' constructor. Hence, we
|
86
|
-
# delete it from the config here.
|
87
|
-
del config["unsplittable_tokens"]
|
88
|
-
return config
|
@@ -33,11 +33,6 @@ class ImageClassifier(Task):
|
|
33
33
|
used to load a pre-trained config and weights.
|
34
34
|
"""
|
35
35
|
|
36
|
-
def __init__(self, *args, **kwargs):
|
37
|
-
super().__init__(*args, **kwargs)
|
38
|
-
# Default compilation.
|
39
|
-
self.compile()
|
40
|
-
|
41
36
|
def compile(
|
42
37
|
self,
|
43
38
|
optimizer="auto",
|
@@ -0,0 +1,83 @@
|
|
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.models.preprocessor import Preprocessor
|
18
|
+
from keras_hub.src.utils.tensor_utils import preprocessing_function
|
19
|
+
|
20
|
+
|
21
|
+
@keras_hub_export("keras_hub.models.ImageClassifierPreprocessor")
|
22
|
+
class ImageClassifierPreprocessor(Preprocessor):
|
23
|
+
"""Base class for image classification preprocessing layers.
|
24
|
+
|
25
|
+
`ImageClassifierPreprocessor` tasks wraps a
|
26
|
+
`keras_hub.layers.ImageConverter` to create a preprocessing layer for
|
27
|
+
image classification tasks. It is intended to be paired with a
|
28
|
+
`keras_hub.models.ImageClassifier` task.
|
29
|
+
|
30
|
+
All `ImageClassifierPreprocessor` take inputs three inputs, `x`, `y`, and
|
31
|
+
`sample_weight`. `x`, the first input, should always be included. It can
|
32
|
+
be a image or batch of images. See examples below. `y` and `sample_weight`
|
33
|
+
are optional inputs that will be passed through unaltered. Usually, `y` will
|
34
|
+
be the classification label, and `sample_weight` will not be provided.
|
35
|
+
|
36
|
+
The layer will output either `x`, an `(x, y)` tuple if labels were provided,
|
37
|
+
or an `(x, y, sample_weight)` tuple if labels and sample weight were
|
38
|
+
provided. `x` will be the input images after all model preprocessing has
|
39
|
+
been applied.
|
40
|
+
|
41
|
+
All `ImageClassifierPreprocessor` tasks include a `from_preset()`
|
42
|
+
constructor which can be used to load a pre-trained config and vocabularies.
|
43
|
+
You can call the `from_preset()` constructor directly on this base class, in
|
44
|
+
which case the correct class for your model will be automatically
|
45
|
+
instantiated.
|
46
|
+
|
47
|
+
Examples.
|
48
|
+
```python
|
49
|
+
preprocessor = keras_hub.models.ImageClassifierPreprocessor.from_preset(
|
50
|
+
"resnet_50",
|
51
|
+
)
|
52
|
+
|
53
|
+
# Resize a single image for resnet 50.
|
54
|
+
x = np.ones((512, 512, 3))
|
55
|
+
x = preprocessor(x)
|
56
|
+
|
57
|
+
# Resize a labeled image.
|
58
|
+
x, y = np.ones((512, 512, 3)), 1
|
59
|
+
x, y = preprocessor(x, y)
|
60
|
+
|
61
|
+
# Resize a batch of labeled images.
|
62
|
+
x, y = [np.ones((512, 512, 3)), np.zeros((512, 512, 3))], [1, 0]
|
63
|
+
x, y = preprocessor(x, y)
|
64
|
+
|
65
|
+
# Use a `tf.data.Dataset`.
|
66
|
+
ds = tf.data.Dataset.from_tensor_slices((x, y)).batch(2)
|
67
|
+
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
|
68
|
+
```
|
69
|
+
"""
|
70
|
+
|
71
|
+
def __init__(
|
72
|
+
self,
|
73
|
+
image_converter=None,
|
74
|
+
**kwargs,
|
75
|
+
):
|
76
|
+
super().__init__(**kwargs)
|
77
|
+
self.image_converter = image_converter
|
78
|
+
|
79
|
+
@preprocessing_function
|
80
|
+
def call(self, x, y=None, sample_weight=None):
|
81
|
+
if self.image_converter:
|
82
|
+
x = self.image_converter(x)
|
83
|
+
return keras.utils.pack_x_y_sample_weight(x, y, sample_weight)
|
@@ -14,7 +14,6 @@
|
|
14
14
|
|
15
15
|
from keras_hub.src.models.llama.llama_backbone import LlamaBackbone
|
16
16
|
from keras_hub.src.models.llama.llama_presets import backbone_presets
|
17
|
-
from keras_hub.src.models.llama.llama_tokenizer import LlamaTokenizer
|
18
17
|
from keras_hub.src.utils.preset_utils import register_presets
|
19
18
|
|
20
|
-
register_presets(backbone_presets,
|
19
|
+
register_presets(backbone_presets, LlamaBackbone)
|