keras-hub-nightly 0.16.1.dev202410020340__py3-none-any.whl → 0.19.0.dev202501260345__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- keras_hub/api/layers/__init__.py +21 -3
- keras_hub/api/models/__init__.py +71 -12
- keras_hub/api/tokenizers/__init__.py +1 -1
- keras_hub/src/bounding_box/__init__.py +2 -0
- keras_hub/src/bounding_box/converters.py +102 -12
- keras_hub/src/layers/modeling/f_net_encoder.py +1 -1
- keras_hub/src/layers/modeling/masked_lm_head.py +2 -1
- keras_hub/src/layers/modeling/reversible_embedding.py +3 -16
- keras_hub/src/layers/modeling/rms_normalization.py +36 -0
- keras_hub/src/layers/modeling/rotary_embedding.py +3 -2
- keras_hub/src/layers/modeling/token_and_position_embedding.py +1 -1
- keras_hub/src/layers/modeling/transformer_decoder.py +8 -6
- keras_hub/src/layers/modeling/transformer_encoder.py +29 -7
- keras_hub/src/layers/preprocessing/audio_converter.py +3 -7
- keras_hub/src/layers/preprocessing/image_converter.py +170 -34
- keras_hub/src/metrics/bleu.py +4 -3
- keras_hub/src/models/albert/albert_presets.py +4 -12
- keras_hub/src/models/albert/albert_text_classifier.py +7 -7
- keras_hub/src/models/backbone.py +3 -14
- keras_hub/src/models/bart/bart_backbone.py +4 -4
- keras_hub/src/models/bart/bart_presets.py +3 -9
- keras_hub/src/models/bart/bart_seq_2_seq_lm.py +9 -8
- keras_hub/src/models/basnet/__init__.py +5 -0
- keras_hub/src/models/basnet/basnet.py +122 -0
- keras_hub/src/models/basnet/basnet_backbone.py +366 -0
- keras_hub/src/models/basnet/basnet_image_converter.py +8 -0
- keras_hub/src/models/basnet/basnet_preprocessor.py +14 -0
- keras_hub/src/models/basnet/basnet_presets.py +17 -0
- keras_hub/src/models/bert/bert_presets.py +14 -32
- keras_hub/src/models/bert/bert_text_classifier.py +3 -3
- keras_hub/src/models/bloom/bloom_presets.py +8 -24
- keras_hub/src/models/causal_lm.py +56 -12
- keras_hub/src/models/clip/__init__.py +5 -0
- keras_hub/src/models/clip/clip_backbone.py +286 -0
- keras_hub/src/models/clip/clip_encoder_block.py +19 -4
- keras_hub/src/models/clip/clip_image_converter.py +8 -0
- keras_hub/src/models/clip/clip_presets.py +93 -0
- keras_hub/src/models/clip/clip_text_encoder.py +4 -1
- keras_hub/src/models/clip/clip_tokenizer.py +18 -3
- keras_hub/src/models/clip/clip_vision_embedding.py +101 -0
- keras_hub/src/models/clip/clip_vision_encoder.py +159 -0
- keras_hub/src/models/csp_darknet/csp_darknet_backbone.py +2 -1
- keras_hub/src/models/csp_darknet/csp_darknet_image_classifier.py +0 -109
- keras_hub/src/models/deberta_v3/deberta_v3_backbone.py +1 -1
- keras_hub/src/models/deberta_v3/deberta_v3_presets.py +5 -15
- keras_hub/src/models/deberta_v3/deberta_v3_text_classifier.py +4 -4
- keras_hub/src/models/deberta_v3/disentangled_attention_encoder.py +4 -4
- keras_hub/src/models/deberta_v3/disentangled_self_attention.py +3 -2
- keras_hub/src/models/deberta_v3/relative_embedding.py +1 -1
- keras_hub/src/models/deeplab_v3/__init__.py +7 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_backbone.py +200 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_image_converter.py +10 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_image_segmeter_preprocessor.py +16 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_layers.py +215 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_presets.py +17 -0
- keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py +111 -0
- keras_hub/src/models/densenet/densenet_backbone.py +6 -4
- keras_hub/src/models/densenet/densenet_image_classifier.py +1 -129
- keras_hub/src/models/densenet/densenet_image_converter.py +2 -4
- keras_hub/src/models/densenet/densenet_presets.py +9 -15
- keras_hub/src/models/distil_bert/distil_bert_masked_lm.py +1 -1
- keras_hub/src/models/distil_bert/distil_bert_masked_lm_preprocessor.py +2 -2
- keras_hub/src/models/distil_bert/distil_bert_presets.py +5 -10
- keras_hub/src/models/distil_bert/distil_bert_text_classifier.py +5 -5
- keras_hub/src/models/distil_bert/distil_bert_tokenizer.py +3 -3
- keras_hub/src/models/efficientnet/__init__.py +9 -0
- keras_hub/src/models/efficientnet/cba.py +141 -0
- keras_hub/src/models/efficientnet/efficientnet_backbone.py +160 -61
- keras_hub/src/models/efficientnet/efficientnet_image_classifier.py +14 -0
- keras_hub/src/models/efficientnet/efficientnet_image_classifier_preprocessor.py +16 -0
- keras_hub/src/models/efficientnet/efficientnet_image_converter.py +10 -0
- keras_hub/src/models/efficientnet/efficientnet_presets.py +193 -0
- keras_hub/src/models/efficientnet/fusedmbconv.py +84 -41
- keras_hub/src/models/efficientnet/mbconv.py +53 -22
- keras_hub/src/models/electra/electra_backbone.py +2 -2
- keras_hub/src/models/electra/electra_presets.py +6 -18
- keras_hub/src/models/f_net/f_net_presets.py +2 -6
- keras_hub/src/models/f_net/f_net_text_classifier.py +3 -3
- keras_hub/src/models/f_net/f_net_text_classifier_preprocessor.py +3 -3
- keras_hub/src/models/falcon/falcon_backbone.py +5 -3
- keras_hub/src/models/falcon/falcon_causal_lm.py +18 -8
- keras_hub/src/models/falcon/falcon_presets.py +1 -3
- keras_hub/src/models/falcon/falcon_tokenizer.py +7 -2
- keras_hub/src/models/feature_pyramid_backbone.py +1 -1
- keras_hub/src/models/flux/__init__.py +5 -0
- keras_hub/src/models/flux/flux_layers.py +496 -0
- keras_hub/src/models/flux/flux_maths.py +225 -0
- keras_hub/src/models/flux/flux_model.py +236 -0
- keras_hub/src/models/flux/flux_presets.py +3 -0
- keras_hub/src/models/flux/flux_text_to_image.py +146 -0
- keras_hub/src/models/flux/flux_text_to_image_preprocessor.py +73 -0
- keras_hub/src/models/gemma/gemma_backbone.py +35 -20
- keras_hub/src/models/gemma/gemma_causal_lm.py +2 -2
- keras_hub/src/models/gemma/gemma_decoder_block.py +3 -1
- keras_hub/src/models/gemma/gemma_presets.py +29 -63
- keras_hub/src/models/gpt2/gpt2_causal_lm.py +2 -2
- keras_hub/src/models/gpt2/gpt2_presets.py +5 -14
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_attention.py +2 -1
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_causal_lm.py +3 -3
- keras_hub/src/models/gpt_neo_x/gpt_neo_x_decoder.py +2 -1
- keras_hub/src/models/image_classifier.py +147 -2
- keras_hub/src/models/image_classifier_preprocessor.py +6 -3
- keras_hub/src/models/image_object_detector.py +87 -0
- keras_hub/src/models/image_object_detector_preprocessor.py +57 -0
- keras_hub/src/models/image_segmenter.py +0 -5
- keras_hub/src/models/image_segmenter_preprocessor.py +29 -4
- keras_hub/src/models/image_to_image.py +417 -0
- keras_hub/src/models/inpaint.py +520 -0
- keras_hub/src/models/llama/llama_backbone.py +138 -12
- keras_hub/src/models/llama/llama_causal_lm.py +3 -1
- keras_hub/src/models/llama/llama_presets.py +10 -20
- keras_hub/src/models/llama3/llama3_backbone.py +12 -11
- keras_hub/src/models/llama3/llama3_causal_lm.py +1 -1
- keras_hub/src/models/llama3/llama3_presets.py +4 -12
- keras_hub/src/models/llama3/llama3_tokenizer.py +25 -2
- keras_hub/src/models/mistral/mistral_backbone.py +16 -15
- keras_hub/src/models/mistral/mistral_causal_lm.py +6 -4
- keras_hub/src/models/mistral/mistral_presets.py +3 -9
- keras_hub/src/models/mistral/mistral_transformer_decoder.py +2 -1
- keras_hub/src/models/mit/__init__.py +6 -0
- keras_hub/src/models/{mix_transformer/mix_transformer_backbone.py → mit/mit_backbone.py} +47 -36
- keras_hub/src/models/mit/mit_image_classifier.py +12 -0
- keras_hub/src/models/mit/mit_image_classifier_preprocessor.py +12 -0
- keras_hub/src/models/mit/mit_image_converter.py +8 -0
- keras_hub/src/models/{mix_transformer/mix_transformer_layers.py → mit/mit_layers.py} +20 -13
- keras_hub/src/models/mit/mit_presets.py +139 -0
- keras_hub/src/models/mobilenet/mobilenet_backbone.py +8 -8
- keras_hub/src/models/mobilenet/mobilenet_image_classifier.py +0 -92
- keras_hub/src/models/opt/opt_causal_lm.py +2 -2
- keras_hub/src/models/opt/opt_presets.py +4 -12
- keras_hub/src/models/pali_gemma/pali_gemma_backbone.py +63 -17
- keras_hub/src/models/pali_gemma/pali_gemma_causal_lm.py +3 -1
- keras_hub/src/models/pali_gemma/pali_gemma_decoder_block.py +21 -23
- keras_hub/src/models/pali_gemma/pali_gemma_image_converter.py +2 -4
- keras_hub/src/models/pali_gemma/pali_gemma_presets.py +173 -17
- keras_hub/src/models/pali_gemma/pali_gemma_vit.py +14 -26
- keras_hub/src/models/phi3/phi3_causal_lm.py +3 -1
- keras_hub/src/models/phi3/phi3_decoder.py +0 -1
- keras_hub/src/models/phi3/phi3_presets.py +2 -6
- keras_hub/src/models/phi3/phi3_rotary_embedding.py +1 -1
- keras_hub/src/models/preprocessor.py +25 -11
- keras_hub/src/models/resnet/resnet_backbone.py +3 -14
- keras_hub/src/models/resnet/resnet_image_classifier.py +0 -137
- keras_hub/src/models/resnet/resnet_image_converter.py +2 -4
- keras_hub/src/models/resnet/resnet_presets.py +127 -18
- keras_hub/src/models/retinanet/__init__.py +5 -0
- keras_hub/src/models/retinanet/anchor_generator.py +52 -53
- keras_hub/src/models/retinanet/feature_pyramid.py +103 -39
- keras_hub/src/models/retinanet/non_max_supression.py +1 -0
- keras_hub/src/models/retinanet/prediction_head.py +192 -0
- keras_hub/src/models/retinanet/retinanet_backbone.py +146 -0
- keras_hub/src/models/retinanet/retinanet_image_converter.py +53 -0
- keras_hub/src/models/retinanet/retinanet_label_encoder.py +49 -51
- keras_hub/src/models/retinanet/retinanet_object_detector.py +381 -0
- keras_hub/src/models/retinanet/retinanet_object_detector_preprocessor.py +14 -0
- keras_hub/src/models/retinanet/retinanet_presets.py +16 -0
- keras_hub/src/models/roberta/roberta_backbone.py +2 -2
- keras_hub/src/models/roberta/roberta_presets.py +6 -8
- keras_hub/src/models/roberta/roberta_text_classifier.py +3 -3
- keras_hub/src/models/sam/__init__.py +5 -0
- keras_hub/src/models/sam/sam_backbone.py +2 -3
- keras_hub/src/models/sam/sam_image_converter.py +2 -4
- keras_hub/src/models/sam/sam_image_segmenter.py +16 -16
- keras_hub/src/models/sam/sam_image_segmenter_preprocessor.py +11 -1
- keras_hub/src/models/sam/sam_layers.py +5 -3
- keras_hub/src/models/sam/sam_presets.py +3 -9
- keras_hub/src/models/sam/sam_prompt_encoder.py +4 -2
- keras_hub/src/models/sam/sam_transformer.py +5 -4
- keras_hub/src/models/segformer/__init__.py +8 -0
- keras_hub/src/models/segformer/segformer_backbone.py +167 -0
- keras_hub/src/models/segformer/segformer_image_converter.py +8 -0
- keras_hub/src/models/segformer/segformer_image_segmenter.py +184 -0
- keras_hub/src/models/segformer/segformer_image_segmenter_preprocessor.py +31 -0
- keras_hub/src/models/segformer/segformer_presets.py +136 -0
- keras_hub/src/models/seq_2_seq_lm_preprocessor.py +1 -1
- keras_hub/src/models/stable_diffusion_3/flow_match_euler_discrete_scheduler.py +8 -1
- keras_hub/src/models/stable_diffusion_3/mmdit.py +577 -190
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_backbone.py +189 -163
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_image_to_image.py +178 -0
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_inpaint.py +193 -0
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_presets.py +43 -7
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_text_to_image.py +25 -14
- keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_text_to_image_preprocessor.py +1 -1
- keras_hub/src/models/t5/t5_backbone.py +5 -4
- keras_hub/src/models/t5/t5_presets.py +47 -19
- keras_hub/src/models/task.py +47 -39
- keras_hub/src/models/text_classifier.py +2 -2
- keras_hub/src/models/text_to_image.py +106 -41
- keras_hub/src/models/vae/__init__.py +1 -0
- keras_hub/src/models/vae/vae_backbone.py +184 -0
- keras_hub/src/models/vae/vae_layers.py +739 -0
- keras_hub/src/models/vgg/__init__.py +5 -0
- keras_hub/src/models/vgg/vgg_backbone.py +4 -24
- keras_hub/src/models/vgg/vgg_image_classifier.py +139 -33
- keras_hub/src/models/vgg/vgg_image_classifier_preprocessor.py +12 -0
- keras_hub/src/models/vgg/vgg_image_converter.py +8 -0
- keras_hub/src/models/vgg/vgg_presets.py +48 -0
- keras_hub/src/models/vit/__init__.py +5 -0
- keras_hub/src/models/vit/vit_backbone.py +152 -0
- keras_hub/src/models/vit/vit_image_classifier.py +187 -0
- keras_hub/src/models/vit/vit_image_classifier_preprocessor.py +12 -0
- keras_hub/src/models/vit/vit_image_converter.py +73 -0
- keras_hub/src/models/vit/vit_layers.py +391 -0
- keras_hub/src/models/vit/vit_presets.py +126 -0
- keras_hub/src/models/vit_det/vit_det_backbone.py +6 -4
- keras_hub/src/models/vit_det/vit_layers.py +3 -3
- keras_hub/src/models/whisper/whisper_audio_converter.py +2 -4
- keras_hub/src/models/whisper/whisper_backbone.py +6 -5
- keras_hub/src/models/whisper/whisper_decoder.py +3 -5
- keras_hub/src/models/whisper/whisper_presets.py +10 -30
- keras_hub/src/models/xlm_roberta/xlm_roberta_masked_lm.py +1 -1
- keras_hub/src/models/xlm_roberta/xlm_roberta_masked_lm_preprocessor.py +2 -2
- keras_hub/src/models/xlm_roberta/xlm_roberta_presets.py +2 -6
- keras_hub/src/models/xlm_roberta/xlm_roberta_text_classifier.py +4 -4
- keras_hub/src/models/xlm_roberta/xlm_roberta_tokenizer.py +2 -1
- keras_hub/src/models/xlnet/relative_attention.py +20 -19
- keras_hub/src/models/xlnet/xlnet_backbone.py +2 -2
- keras_hub/src/models/xlnet/xlnet_content_and_query_embedding.py +3 -5
- keras_hub/src/models/xlnet/xlnet_encoder.py +7 -9
- keras_hub/src/samplers/contrastive_sampler.py +2 -3
- keras_hub/src/samplers/sampler.py +2 -1
- keras_hub/src/tests/test_case.py +41 -6
- keras_hub/src/tokenizers/byte_pair_tokenizer.py +7 -3
- keras_hub/src/tokenizers/byte_tokenizer.py +3 -10
- keras_hub/src/tokenizers/sentence_piece_tokenizer.py +2 -9
- keras_hub/src/tokenizers/sentence_piece_tokenizer_trainer.py +9 -11
- keras_hub/src/tokenizers/tokenizer.py +10 -13
- keras_hub/src/tokenizers/unicode_codepoint_tokenizer.py +9 -7
- keras_hub/src/tokenizers/word_piece_tokenizer_trainer.py +10 -3
- keras_hub/src/utils/keras_utils.py +2 -13
- keras_hub/src/utils/pipeline_model.py +3 -3
- keras_hub/src/utils/preset_utils.py +196 -144
- keras_hub/src/utils/tensor_utils.py +4 -4
- keras_hub/src/utils/timm/convert_densenet.py +6 -4
- keras_hub/src/utils/timm/convert_efficientnet.py +447 -0
- keras_hub/src/utils/timm/convert_resnet.py +1 -1
- keras_hub/src/utils/timm/convert_vgg.py +85 -0
- keras_hub/src/utils/timm/preset_loader.py +14 -9
- keras_hub/src/utils/transformers/convert_llama3.py +21 -5
- keras_hub/src/utils/transformers/convert_vit.py +150 -0
- keras_hub/src/utils/transformers/preset_loader.py +23 -0
- keras_hub/src/utils/transformers/safetensor_utils.py +4 -3
- keras_hub/src/version_utils.py +1 -1
- {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.19.0.dev202501260345.dist-info}/METADATA +86 -68
- keras_hub_nightly-0.19.0.dev202501260345.dist-info/RECORD +423 -0
- {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.19.0.dev202501260345.dist-info}/WHEEL +1 -1
- keras_hub/src/layers/preprocessing/resizing_image_converter.py +0 -138
- keras_hub/src/models/mix_transformer/__init__.py +0 -0
- keras_hub/src/models/mix_transformer/mix_transformer_classifier.py +0 -119
- keras_hub/src/models/stable_diffusion_3/vae_image_decoder.py +0 -320
- keras_hub_nightly-0.16.1.dev202410020340.dist-info/RECORD +0 -357
- {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.19.0.dev202501260345.dist-info}/top_level.txt +0 -0
@@ -1,8 +1,6 @@
|
|
1
|
-
import keras
|
2
|
-
|
3
1
|
from keras_hub.src.api_export import keras_hub_export
|
4
2
|
from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone
|
5
|
-
from keras_hub.src.models.densenet.densenet_image_classifier_preprocessor import (
|
3
|
+
from keras_hub.src.models.densenet.densenet_image_classifier_preprocessor import ( # noqa: E501
|
6
4
|
DenseNetImageClassifierPreprocessor,
|
7
5
|
)
|
8
6
|
from keras_hub.src.models.image_classifier import ImageClassifier
|
@@ -10,131 +8,5 @@ from keras_hub.src.models.image_classifier import ImageClassifier
|
|
10
8
|
|
11
9
|
@keras_hub_export("keras_hub.models.DenseNetImageClassifier")
|
12
10
|
class DenseNetImageClassifier(ImageClassifier):
|
13
|
-
"""DenseNet image classifier task model.
|
14
|
-
|
15
|
-
To fine-tune with `fit()`, pass a dataset containing tuples of `(x, y)`
|
16
|
-
where `x` is a tensor and `y` is a integer from `[0, num_classes)`.
|
17
|
-
All `ImageClassifier` tasks include a `from_preset()` constructor which can
|
18
|
-
be used to load a pre-trained config and weights.
|
19
|
-
|
20
|
-
Args:
|
21
|
-
backbone: A `keras_hub.models.DenseNetBackbone` instance.
|
22
|
-
num_classes: int. The number of classes to predict.
|
23
|
-
activation: `None`, str or callable. The activation function to use on
|
24
|
-
the `Dense` layer. Set `activation=None` to return the output
|
25
|
-
logits. Defaults to `None`.
|
26
|
-
pooling: A pooling layer to use before the final classification layer,
|
27
|
-
must be one of "avg" or "max". Use "avg" for
|
28
|
-
`GlobalAveragePooling2D` and "max" for "GlobalMaxPooling2D.
|
29
|
-
preprocessor: A `keras_hub.models.DenseNetImageClassifierPreprocessor`
|
30
|
-
or `None`. If `None`, this model will not apply preprocessing, and
|
31
|
-
inputs should be preprocessed before calling the model.
|
32
|
-
|
33
|
-
Examples:
|
34
|
-
|
35
|
-
Call `predict()` to run inference.
|
36
|
-
```python
|
37
|
-
# Load preset and train
|
38
|
-
images = np.ones((2, 224, 224, 3), dtype="float32")
|
39
|
-
classifier = keras_hub.models.DenseNetImageClassifier.from_preset(
|
40
|
-
"densenet121_imagenet")
|
41
|
-
classifier.predict(images)
|
42
|
-
```
|
43
|
-
|
44
|
-
Call `fit()` on a single batch.
|
45
|
-
```python
|
46
|
-
# Load preset and train
|
47
|
-
images = np.ones((2, 224, 224, 3), dtype="float32")
|
48
|
-
labels = [0, 3]
|
49
|
-
classifier = keras_hub.models.DenseNetImageClassifier.from_preset(
|
50
|
-
"densenet121_imagenet")
|
51
|
-
classifier.fit(x=images, y=labels, batch_size=2)
|
52
|
-
```
|
53
|
-
|
54
|
-
Call `fit()` with custom loss, optimizer and backbone.
|
55
|
-
```python
|
56
|
-
classifier = keras_hub.models.DenseNetImageClassifier.from_preset(
|
57
|
-
"densenet121_imagenet")
|
58
|
-
classifier.compile(
|
59
|
-
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
60
|
-
optimizer=keras.optimizers.Adam(5e-5),
|
61
|
-
)
|
62
|
-
classifier.backbone.trainable = False
|
63
|
-
classifier.fit(x=images, y=labels, batch_size=2)
|
64
|
-
```
|
65
|
-
|
66
|
-
Custom backbone.
|
67
|
-
```python
|
68
|
-
images = np.ones((2, 224, 224, 3), dtype="float32")
|
69
|
-
labels = [0, 3]
|
70
|
-
backbone = keras_hub.models.DenseNetBackbone(
|
71
|
-
stackwise_num_filters=[128, 256, 512, 1024],
|
72
|
-
stackwise_depth=[3, 9, 9, 3],
|
73
|
-
block_type="basic_block",
|
74
|
-
image_shape = (224, 224, 3),
|
75
|
-
)
|
76
|
-
classifier = keras_hub.models.DenseNetImageClassifier(
|
77
|
-
backbone=backbone,
|
78
|
-
num_classes=4,
|
79
|
-
)
|
80
|
-
classifier.fit(x=images, y=labels, batch_size=2)
|
81
|
-
```
|
82
|
-
"""
|
83
|
-
|
84
11
|
backbone_cls = DenseNetBackbone
|
85
12
|
preprocessor_cls = DenseNetImageClassifierPreprocessor
|
86
|
-
|
87
|
-
def __init__(
|
88
|
-
self,
|
89
|
-
backbone,
|
90
|
-
num_classes,
|
91
|
-
activation=None,
|
92
|
-
pooling="avg",
|
93
|
-
preprocessor=None,
|
94
|
-
**kwargs,
|
95
|
-
):
|
96
|
-
# === Layers ===
|
97
|
-
self.backbone = backbone
|
98
|
-
self.preprocessor = preprocessor
|
99
|
-
if pooling == "avg":
|
100
|
-
self.pooler = keras.layers.GlobalAveragePooling2D()
|
101
|
-
elif pooling == "max":
|
102
|
-
self.pooler = keras.layers.GlobalMaxPooling2D()
|
103
|
-
else:
|
104
|
-
raise ValueError(
|
105
|
-
"Unknown `pooling` type. Polling should be either `'avg'` or "
|
106
|
-
f"`'max'`. Received: pooling={pooling}."
|
107
|
-
)
|
108
|
-
self.output_dense = keras.layers.Dense(
|
109
|
-
num_classes,
|
110
|
-
activation=activation,
|
111
|
-
name="predictions",
|
112
|
-
)
|
113
|
-
|
114
|
-
# === Functional Model ===
|
115
|
-
inputs = self.backbone.input
|
116
|
-
x = self.backbone(inputs)
|
117
|
-
x = self.pooler(x)
|
118
|
-
outputs = self.output_dense(x)
|
119
|
-
super().__init__(
|
120
|
-
inputs=inputs,
|
121
|
-
outputs=outputs,
|
122
|
-
**kwargs,
|
123
|
-
)
|
124
|
-
|
125
|
-
# === Config ===
|
126
|
-
self.num_classes = num_classes
|
127
|
-
self.activation = activation
|
128
|
-
self.pooling = pooling
|
129
|
-
|
130
|
-
def get_config(self):
|
131
|
-
# Backbone serialized in `super`
|
132
|
-
config = super().get_config()
|
133
|
-
config.update(
|
134
|
-
{
|
135
|
-
"num_classes": self.num_classes,
|
136
|
-
"activation": self.activation,
|
137
|
-
"pooling": self.pooling,
|
138
|
-
}
|
139
|
-
)
|
140
|
-
return config
|
@@ -1,10 +1,8 @@
|
|
1
1
|
from keras_hub.src.api_export import keras_hub_export
|
2
|
-
from keras_hub.src.layers.preprocessing.
|
3
|
-
ResizingImageConverter,
|
4
|
-
)
|
2
|
+
from keras_hub.src.layers.preprocessing.image_converter import ImageConverter
|
5
3
|
from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone
|
6
4
|
|
7
5
|
|
8
6
|
@keras_hub_export("keras_hub.layers.DenseNetImageConverter")
|
9
|
-
class DenseNetImageConverter(
|
7
|
+
class DenseNetImageConverter(ImageConverter):
|
10
8
|
backbone_cls = DenseNetBackbone
|
@@ -4,40 +4,34 @@ backbone_presets = {
|
|
4
4
|
"densenet_121_imagenet": {
|
5
5
|
"metadata": {
|
6
6
|
"description": (
|
7
|
-
"121-layer DenseNet model pre-trained on the ImageNet 1k
|
8
|
-
"at a 224x224 resolution."
|
7
|
+
"121-layer DenseNet model pre-trained on the ImageNet 1k "
|
8
|
+
"dataset at a 224x224 resolution."
|
9
9
|
),
|
10
10
|
"params": 7037504,
|
11
|
-
"official_name": "DenseNet",
|
12
11
|
"path": "densenet",
|
13
|
-
"model_card": "https://arxiv.org/abs/1608.06993",
|
14
12
|
},
|
15
|
-
"kaggle_handle": "kaggle://
|
13
|
+
"kaggle_handle": "kaggle://keras/densenet/keras/densenet_121_imagenet/3",
|
16
14
|
},
|
17
15
|
"densenet_169_imagenet": {
|
18
16
|
"metadata": {
|
19
17
|
"description": (
|
20
|
-
"169-layer DenseNet model pre-trained on the ImageNet 1k
|
21
|
-
"at a 224x224 resolution."
|
18
|
+
"169-layer DenseNet model pre-trained on the ImageNet 1k "
|
19
|
+
"dataset at a 224x224 resolution."
|
22
20
|
),
|
23
21
|
"params": 12642880,
|
24
|
-
"official_name": "DenseNet",
|
25
22
|
"path": "densenet",
|
26
|
-
"model_card": "https://arxiv.org/abs/1608.06993",
|
27
23
|
},
|
28
|
-
"kaggle_handle": "kaggle://
|
24
|
+
"kaggle_handle": "kaggle://keras/densenet/keras/densenet_169_imagenet/3",
|
29
25
|
},
|
30
26
|
"densenet_201_imagenet": {
|
31
27
|
"metadata": {
|
32
28
|
"description": (
|
33
|
-
"201-layer DenseNet model pre-trained on the ImageNet 1k
|
34
|
-
"at a 224x224 resolution."
|
29
|
+
"201-layer DenseNet model pre-trained on the ImageNet 1k "
|
30
|
+
"dataset at a 224x224 resolution."
|
35
31
|
),
|
36
32
|
"params": 18321984,
|
37
|
-
"official_name": "DenseNet",
|
38
33
|
"path": "densenet",
|
39
|
-
"model_card": "https://arxiv.org/abs/1608.06993",
|
40
34
|
},
|
41
|
-
"kaggle_handle": "kaggle://
|
35
|
+
"kaggle_handle": "kaggle://keras/densenet/keras/densenet_201_imagenet/3",
|
42
36
|
},
|
43
37
|
}
|
@@ -8,7 +8,7 @@ from keras_hub.src.models.distil_bert.distil_bert_backbone import (
|
|
8
8
|
from keras_hub.src.models.distil_bert.distil_bert_backbone import (
|
9
9
|
distilbert_kernel_initializer,
|
10
10
|
)
|
11
|
-
from keras_hub.src.models.distil_bert.distil_bert_masked_lm_preprocessor import (
|
11
|
+
from keras_hub.src.models.distil_bert.distil_bert_masked_lm_preprocessor import ( # noqa: E501
|
12
12
|
DistilBertMaskedLMPreprocessor,
|
13
13
|
)
|
14
14
|
from keras_hub.src.models.masked_lm import MaskedLM
|
@@ -17,8 +17,8 @@ class DistilBertMaskedLMPreprocessor(MaskedLMPreprocessor):
|
|
17
17
|
|
18
18
|
This preprocessing layer will prepare inputs for a masked language modeling
|
19
19
|
task. It is primarily intended for use with the
|
20
|
-
`keras_hub.models.DistilBertMaskedLM` task model. Preprocessing will occur
|
21
|
-
multiple steps.
|
20
|
+
`keras_hub.models.DistilBertMaskedLM` task model. Preprocessing will occur
|
21
|
+
in multiple steps.
|
22
22
|
|
23
23
|
1. Tokenize any number of input segments using the `tokenizer`.
|
24
24
|
2. Pack the inputs together using a `keras_hub.layers.MultiSegmentPacker`.
|
@@ -9,11 +9,9 @@ backbone_presets = {
|
|
9
9
|
"teacher model."
|
10
10
|
),
|
11
11
|
"params": 66362880,
|
12
|
-
"official_name": "DistilBERT",
|
13
12
|
"path": "distil_bert",
|
14
|
-
"model_card": "https://huggingface.co/distilbert-base-uncased",
|
15
13
|
},
|
16
|
-
"kaggle_handle": "kaggle://keras/distil_bert/keras/distil_bert_base_en_uncased/
|
14
|
+
"kaggle_handle": "kaggle://keras/distil_bert/keras/distil_bert_base_en_uncased/3",
|
17
15
|
},
|
18
16
|
"distil_bert_base_en": {
|
19
17
|
"metadata": {
|
@@ -23,22 +21,19 @@ backbone_presets = {
|
|
23
21
|
"teacher model."
|
24
22
|
),
|
25
23
|
"params": 65190912,
|
26
|
-
"official_name": "DistilBERT",
|
27
24
|
"path": "distil_bert",
|
28
|
-
"model_card": "https://huggingface.co/distilbert-base-cased",
|
29
25
|
},
|
30
|
-
"kaggle_handle": "kaggle://keras/distil_bert/keras/distil_bert_base_en/
|
26
|
+
"kaggle_handle": "kaggle://keras/distil_bert/keras/distil_bert_base_en/3",
|
31
27
|
},
|
32
28
|
"distil_bert_base_multi": {
|
33
29
|
"metadata": {
|
34
30
|
"description": (
|
35
|
-
"6-layer DistilBERT model where case is maintained. Trained on
|
31
|
+
"6-layer DistilBERT model where case is maintained. Trained on "
|
32
|
+
"Wikipedias of 104 languages"
|
36
33
|
),
|
37
34
|
"params": 134734080,
|
38
|
-
"official_name": "DistilBERT",
|
39
35
|
"path": "distil_bert",
|
40
|
-
"model_card": "https://huggingface.co/distilbert-base-multilingual-cased",
|
41
36
|
},
|
42
|
-
"kaggle_handle": "kaggle://keras/distil_bert/keras/distil_bert_base_multi/
|
37
|
+
"kaggle_handle": "kaggle://keras/distil_bert/keras/distil_bert_base_multi/3",
|
43
38
|
},
|
44
39
|
}
|
@@ -7,7 +7,7 @@ from keras_hub.src.models.distil_bert.distil_bert_backbone import (
|
|
7
7
|
from keras_hub.src.models.distil_bert.distil_bert_backbone import (
|
8
8
|
distilbert_kernel_initializer,
|
9
9
|
)
|
10
|
-
from keras_hub.src.models.distil_bert.distil_bert_text_classifier_preprocessor import (
|
10
|
+
from keras_hub.src.models.distil_bert.distil_bert_text_classifier_preprocessor import ( # noqa: E501
|
11
11
|
DistilBertTextClassifierPreprocessor,
|
12
12
|
)
|
13
13
|
from keras_hub.src.models.text_classifier import TextClassifier
|
@@ -40,9 +40,9 @@ class DistilBertTextClassifier(TextClassifier):
|
|
40
40
|
Args:
|
41
41
|
backbone: A `keras_hub.models.DistilBert` instance.
|
42
42
|
num_classes: int. Number of classes to predict.
|
43
|
-
preprocessor: A `keras_hub.models.DistilBertTextClassifierPreprocessor`
|
44
|
-
`None`, this model will not apply preprocessing, and
|
45
|
-
be preprocessed before calling the model.
|
43
|
+
preprocessor: A `keras_hub.models.DistilBertTextClassifierPreprocessor`
|
44
|
+
or `None`. If `None`, this model will not apply preprocessing, and
|
45
|
+
inputs should be preprocessed before calling the model.
|
46
46
|
activation: Optional `str` or callable. The
|
47
47
|
activation function to use on the model outputs. Set
|
48
48
|
`activation="softmax"` to return output probabilities.
|
@@ -128,7 +128,7 @@ class DistilBertTextClassifier(TextClassifier):
|
|
128
128
|
)
|
129
129
|
classifier.fit(x=features, y=labels, batch_size=2)
|
130
130
|
```
|
131
|
-
"""
|
131
|
+
""" # noqa: E501
|
132
132
|
|
133
133
|
backbone_cls = DistilBertBackbone
|
134
134
|
preprocessor_cls = DistilBertTextClassifierPreprocessor
|
@@ -16,9 +16,9 @@ class DistilBertTokenizer(WordPieceTokenizer):
|
|
16
16
|
|
17
17
|
This tokenizer class will tokenize raw strings into integer sequences and
|
18
18
|
is based on `keras_hub.tokenizers.WordPieceTokenizer`. Unlike the
|
19
|
-
underlying tokenizer, it will check for all special tokens needed by
|
20
|
-
models and provides a `from_preset()` method to automatically
|
21
|
-
a matching vocabulary for a DistilBERT preset.
|
19
|
+
underlying tokenizer, it will check for all special tokens needed by
|
20
|
+
DistilBERT models and provides a `from_preset()` method to automatically
|
21
|
+
download a matching vocabulary for a DistilBERT preset.
|
22
22
|
|
23
23
|
If input is a batch of strings (rank > 0), the layer will output a
|
24
24
|
`tf.RaggedTensor` where the last dimension of the output is ragged.
|
@@ -0,0 +1,9 @@
|
|
1
|
+
from keras_hub.src.models.efficientnet.efficientnet_backbone import (
|
2
|
+
EfficientNetBackbone,
|
3
|
+
)
|
4
|
+
from keras_hub.src.models.efficientnet.efficientnet_presets import (
|
5
|
+
backbone_presets,
|
6
|
+
)
|
7
|
+
from keras_hub.src.utils.preset_utils import register_presets
|
8
|
+
|
9
|
+
register_presets(backbone_presets, EfficientNetBackbone)
|
@@ -0,0 +1,141 @@
|
|
1
|
+
import keras
|
2
|
+
|
3
|
+
BN_AXIS = 3
|
4
|
+
|
5
|
+
|
6
|
+
class CBABlock(keras.layers.Layer):
|
7
|
+
"""
|
8
|
+
Args:
|
9
|
+
input_filters: int, the number of input filters
|
10
|
+
output_filters: int, the number of output filters
|
11
|
+
kernel_size: default 3, the kernel_size to apply to the expansion phase
|
12
|
+
convolutions
|
13
|
+
strides: default 1, the strides to apply to the expansion phase
|
14
|
+
convolutions
|
15
|
+
data_format: str, channels_last (default) or channels_first, expects
|
16
|
+
tensors to be of shape (N, H, W, C) or (N, C, H, W) respectively
|
17
|
+
batch_norm_momentum: default 0.9, the BatchNormalization momentum
|
18
|
+
batch_norm_epsilon: default 1e-3, the BatchNormalization epsilon
|
19
|
+
activation: default "swish", the activation function used between
|
20
|
+
convolution operations
|
21
|
+
dropout: float, the optional dropout rate to apply before the output
|
22
|
+
convolution, defaults to 0.2
|
23
|
+
nores: bool, default False, forces no residual connection if True,
|
24
|
+
otherwise allows it if False.
|
25
|
+
|
26
|
+
Returns:
|
27
|
+
A tensor representing a feature map, passed through the ConvBNAct
|
28
|
+
block
|
29
|
+
|
30
|
+
Note:
|
31
|
+
Not intended to be used outside of the EfficientNet architecture.
|
32
|
+
"""
|
33
|
+
|
34
|
+
def __init__(
|
35
|
+
self,
|
36
|
+
input_filters,
|
37
|
+
output_filters,
|
38
|
+
kernel_size=3,
|
39
|
+
strides=1,
|
40
|
+
data_format="channels_last",
|
41
|
+
batch_norm_momentum=0.9,
|
42
|
+
batch_norm_epsilon=1e-3,
|
43
|
+
activation="swish",
|
44
|
+
dropout=0.2,
|
45
|
+
nores=False,
|
46
|
+
**kwargs,
|
47
|
+
):
|
48
|
+
super().__init__(**kwargs)
|
49
|
+
self.input_filters = input_filters
|
50
|
+
self.output_filters = output_filters
|
51
|
+
self.kernel_size = kernel_size
|
52
|
+
self.strides = strides
|
53
|
+
self.data_format = data_format
|
54
|
+
self.batch_norm_momentum = batch_norm_momentum
|
55
|
+
self.batch_norm_epsilon = batch_norm_epsilon
|
56
|
+
self.activation = activation
|
57
|
+
self.dropout = dropout
|
58
|
+
self.nores = nores
|
59
|
+
|
60
|
+
padding_pixels = kernel_size // 2
|
61
|
+
self.conv1_pad = keras.layers.ZeroPadding2D(
|
62
|
+
padding=(padding_pixels, padding_pixels),
|
63
|
+
name=self.name + "conv_pad",
|
64
|
+
)
|
65
|
+
self.conv1 = keras.layers.Conv2D(
|
66
|
+
filters=self.output_filters,
|
67
|
+
kernel_size=kernel_size,
|
68
|
+
strides=strides,
|
69
|
+
kernel_initializer=self._conv_kernel_initializer(),
|
70
|
+
padding="valid",
|
71
|
+
data_format=data_format,
|
72
|
+
use_bias=False,
|
73
|
+
name=self.name + "conv",
|
74
|
+
)
|
75
|
+
self.bn1 = keras.layers.BatchNormalization(
|
76
|
+
axis=BN_AXIS,
|
77
|
+
momentum=self.batch_norm_momentum,
|
78
|
+
epsilon=self.batch_norm_epsilon,
|
79
|
+
name=self.name + "bn",
|
80
|
+
)
|
81
|
+
self.act = keras.layers.Activation(
|
82
|
+
self.activation, name=self.name + "activation"
|
83
|
+
)
|
84
|
+
|
85
|
+
if self.dropout:
|
86
|
+
self.dropout_layer = keras.layers.Dropout(
|
87
|
+
self.dropout,
|
88
|
+
noise_shape=(None, 1, 1, 1),
|
89
|
+
name=self.name + "drop",
|
90
|
+
)
|
91
|
+
|
92
|
+
def _conv_kernel_initializer(
|
93
|
+
self,
|
94
|
+
scale=2.0,
|
95
|
+
mode="fan_out",
|
96
|
+
distribution="truncated_normal",
|
97
|
+
seed=None,
|
98
|
+
):
|
99
|
+
return keras.initializers.VarianceScaling(
|
100
|
+
scale=scale, mode=mode, distribution=distribution, seed=seed
|
101
|
+
)
|
102
|
+
|
103
|
+
def build(self, input_shape):
|
104
|
+
if self.name is None:
|
105
|
+
self.name = keras.backend.get_uid("block0")
|
106
|
+
|
107
|
+
def call(self, inputs):
|
108
|
+
x = self.conv1_pad(inputs)
|
109
|
+
x = self.conv1(x)
|
110
|
+
x = self.bn1(x)
|
111
|
+
x = self.act(x)
|
112
|
+
|
113
|
+
# Residual:
|
114
|
+
if (
|
115
|
+
self.strides == 1
|
116
|
+
and self.input_filters == self.output_filters
|
117
|
+
and not self.nores
|
118
|
+
):
|
119
|
+
if self.dropout:
|
120
|
+
x = self.dropout_layer(x)
|
121
|
+
x = keras.layers.Add(name=self.name + "add")([x, inputs])
|
122
|
+
return x
|
123
|
+
|
124
|
+
def get_config(self):
|
125
|
+
config = super().get_config()
|
126
|
+
config.update(
|
127
|
+
{
|
128
|
+
"input_filters": self.input_filters,
|
129
|
+
"output_filters": self.output_filters,
|
130
|
+
"kernel_size": self.kernel_size,
|
131
|
+
"strides": self.strides,
|
132
|
+
"data_format": self.data_format,
|
133
|
+
"batch_norm_momentum": self.batch_norm_momentum,
|
134
|
+
"batch_norm_epsilon": self.batch_norm_epsilon,
|
135
|
+
"activation": self.activation,
|
136
|
+
"dropout": self.dropout,
|
137
|
+
"nores": self.nores,
|
138
|
+
}
|
139
|
+
)
|
140
|
+
|
141
|
+
return config
|