keras-hub-nightly 0.16.1.dev202410100339__py3-none-any.whl → 0.16.1.dev202410110340__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 +1 -0
- keras_hub/api/models/__init__.py +3 -0
- keras_hub/src/models/vgg/__init__.py +1 -0
- keras_hub/src/models/vgg/vgg_backbone.py +1 -2
- keras_hub/src/models/vgg/vgg_image_classifier.py +52 -14
- keras_hub/src/utils/timm/convert_vgg.py +85 -0
- keras_hub/src/utils/timm/preset_loader.py +3 -0
- keras_hub/src/version_utils.py +1 -1
- {keras_hub_nightly-0.16.1.dev202410100339.dist-info → keras_hub_nightly-0.16.1.dev202410110340.dist-info}/METADATA +1 -1
- {keras_hub_nightly-0.16.1.dev202410100339.dist-info → keras_hub_nightly-0.16.1.dev202410110340.dist-info}/RECORD +12 -11
- {keras_hub_nightly-0.16.1.dev202410100339.dist-info → keras_hub_nightly-0.16.1.dev202410110340.dist-info}/WHEEL +0 -0
- {keras_hub_nightly-0.16.1.dev202410100339.dist-info → keras_hub_nightly-0.16.1.dev202410110340.dist-info}/top_level.txt +0 -0
keras_hub/api/layers/__init__.py
CHANGED
@@ -52,6 +52,7 @@ from keras_hub.src.models.resnet.resnet_image_converter import (
|
|
52
52
|
from keras_hub.src.models.sam.sam_image_converter import SAMImageConverter
|
53
53
|
from keras_hub.src.models.sam.sam_mask_decoder import SAMMaskDecoder
|
54
54
|
from keras_hub.src.models.sam.sam_prompt_encoder import SAMPromptEncoder
|
55
|
+
from keras_hub.src.models.vgg.vgg_image_classifier import VGGImageConverter
|
55
56
|
from keras_hub.src.models.whisper.whisper_audio_converter import (
|
56
57
|
WhisperAudioConverter,
|
57
58
|
)
|
keras_hub/api/models/__init__.py
CHANGED
@@ -299,6 +299,9 @@ from keras_hub.src.models.text_classifier_preprocessor import (
|
|
299
299
|
from keras_hub.src.models.text_to_image import TextToImage
|
300
300
|
from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone
|
301
301
|
from keras_hub.src.models.vgg.vgg_image_classifier import VGGImageClassifier
|
302
|
+
from keras_hub.src.models.vgg.vgg_image_classifier import (
|
303
|
+
VGGImageClassifierPreprocessor,
|
304
|
+
)
|
302
305
|
from keras_hub.src.models.vit_det.vit_det_backbone import ViTDetBackbone
|
303
306
|
from keras_hub.src.models.whisper.whisper_backbone import WhisperBackbone
|
304
307
|
from keras_hub.src.models.whisper.whisper_tokenizer import WhisperTokenizer
|
@@ -0,0 +1 @@
|
|
1
|
+
from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone
|
@@ -47,12 +47,11 @@ class VGGBackbone(Backbone):
|
|
47
47
|
image_shape=(None, None, 3),
|
48
48
|
**kwargs,
|
49
49
|
):
|
50
|
-
|
51
50
|
# === Functional Model ===
|
52
51
|
img_input = keras.layers.Input(shape=image_shape)
|
53
52
|
x = img_input
|
54
53
|
|
55
|
-
for stack_index in range(len(stackwise_num_repeats)
|
54
|
+
for stack_index in range(len(stackwise_num_repeats)):
|
56
55
|
x = apply_vgg_block(
|
57
56
|
x=x,
|
58
57
|
num_layers=stackwise_num_repeats[stack_index],
|
@@ -1,11 +1,26 @@
|
|
1
1
|
import keras
|
2
2
|
|
3
3
|
from keras_hub.src.api_export import keras_hub_export
|
4
|
+
from keras_hub.src.layers.preprocessing.image_converter import ImageConverter
|
4
5
|
from keras_hub.src.models.image_classifier import ImageClassifier
|
6
|
+
from keras_hub.src.models.image_classifier_preprocessor import (
|
7
|
+
ImageClassifierPreprocessor,
|
8
|
+
)
|
5
9
|
from keras_hub.src.models.task import Task
|
6
10
|
from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone
|
7
11
|
|
8
12
|
|
13
|
+
@keras_hub_export("keras_hub.layers.VGGImageConverter")
|
14
|
+
class VGGImageConverter(ImageConverter):
|
15
|
+
backbone_cls = VGGBackbone
|
16
|
+
|
17
|
+
|
18
|
+
@keras_hub_export("keras_hub.models.VGGImageClassifierPreprocessor")
|
19
|
+
class VGGImageClassifierPreprocessor(ImageClassifierPreprocessor):
|
20
|
+
backbone_cls = VGGBackbone
|
21
|
+
image_converter_cls = VGGImageConverter
|
22
|
+
|
23
|
+
|
9
24
|
@keras_hub_export("keras_hub.models.VGGImageClassifier")
|
10
25
|
class VGGImageClassifier(ImageClassifier):
|
11
26
|
"""VGG image classification task.
|
@@ -96,13 +111,14 @@ class VGGImageClassifier(ImageClassifier):
|
|
96
111
|
"""
|
97
112
|
|
98
113
|
backbone_cls = VGGBackbone
|
114
|
+
preprocessor_cls = VGGImageClassifierPreprocessor
|
99
115
|
|
100
116
|
def __init__(
|
101
117
|
self,
|
102
118
|
backbone,
|
103
119
|
num_classes,
|
104
120
|
preprocessor=None,
|
105
|
-
pooling="
|
121
|
+
pooling="avg",
|
106
122
|
pooling_hidden_dim=4096,
|
107
123
|
activation=None,
|
108
124
|
dropout=0.0,
|
@@ -141,24 +157,46 @@ class VGGImageClassifier(ImageClassifier):
|
|
141
157
|
"Unknown `pooling` type. Polling should be either `'avg'` or "
|
142
158
|
f"`'max'`. Received: pooling={pooling}."
|
143
159
|
)
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
160
|
+
|
161
|
+
self.head = keras.Sequential(
|
162
|
+
[
|
163
|
+
keras.layers.Conv2D(
|
164
|
+
filters=4096,
|
165
|
+
kernel_size=7,
|
166
|
+
name="fc1",
|
167
|
+
activation=activation,
|
168
|
+
use_bias=True,
|
169
|
+
padding="same",
|
170
|
+
),
|
171
|
+
keras.layers.Dropout(
|
172
|
+
rate=dropout,
|
173
|
+
dtype=head_dtype,
|
174
|
+
name="output_dropout",
|
175
|
+
),
|
176
|
+
keras.layers.Conv2D(
|
177
|
+
filters=4096,
|
178
|
+
kernel_size=1,
|
179
|
+
name="fc2",
|
180
|
+
activation=activation,
|
181
|
+
use_bias=True,
|
182
|
+
padding="same",
|
183
|
+
),
|
184
|
+
self.pooler,
|
185
|
+
keras.layers.Dense(
|
186
|
+
num_classes,
|
187
|
+
activation=activation,
|
188
|
+
dtype=head_dtype,
|
189
|
+
name="predictions",
|
190
|
+
),
|
191
|
+
],
|
192
|
+
name="head",
|
154
193
|
)
|
155
194
|
|
156
195
|
# === Functional Model ===
|
157
196
|
inputs = self.backbone.input
|
158
197
|
x = self.backbone(inputs)
|
159
|
-
|
160
|
-
|
161
|
-
outputs = self.output_dense(x)
|
198
|
+
outputs = self.head(x)
|
199
|
+
|
162
200
|
# Skip the parent class functional model.
|
163
201
|
Task.__init__(
|
164
202
|
self,
|
@@ -0,0 +1,85 @@
|
|
1
|
+
from typing import Any
|
2
|
+
|
3
|
+
import numpy as np
|
4
|
+
|
5
|
+
from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone
|
6
|
+
from keras_hub.src.models.vgg.vgg_image_classifier import VGGImageClassifier
|
7
|
+
|
8
|
+
backbone_cls = VGGBackbone
|
9
|
+
|
10
|
+
|
11
|
+
REPEATS_BY_SIZE = {
|
12
|
+
"vgg11": [1, 1, 2, 2, 2],
|
13
|
+
"vgg13": [2, 2, 2, 2, 2],
|
14
|
+
"vgg16": [2, 2, 3, 3, 3],
|
15
|
+
"vgg19": [2, 2, 4, 4, 4],
|
16
|
+
}
|
17
|
+
|
18
|
+
|
19
|
+
def convert_backbone_config(timm_config):
|
20
|
+
architecture = timm_config["architecture"]
|
21
|
+
stackwise_num_repeats = REPEATS_BY_SIZE[architecture]
|
22
|
+
return dict(
|
23
|
+
stackwise_num_repeats=stackwise_num_repeats,
|
24
|
+
stackwise_num_filters=[64, 128, 256, 512, 512],
|
25
|
+
)
|
26
|
+
|
27
|
+
|
28
|
+
def convert_conv2d(
|
29
|
+
model,
|
30
|
+
loader,
|
31
|
+
keras_layer_name: str,
|
32
|
+
hf_layer_name: str,
|
33
|
+
):
|
34
|
+
loader.port_weight(
|
35
|
+
model.get_layer(keras_layer_name).kernel,
|
36
|
+
hf_weight_key=f"{hf_layer_name}.weight",
|
37
|
+
hook_fn=lambda x, _: np.transpose(x, (2, 3, 1, 0)),
|
38
|
+
)
|
39
|
+
loader.port_weight(
|
40
|
+
model.get_layer(keras_layer_name).bias,
|
41
|
+
hf_weight_key=f"{hf_layer_name}.bias",
|
42
|
+
)
|
43
|
+
|
44
|
+
|
45
|
+
def convert_weights(
|
46
|
+
backbone: VGGBackbone,
|
47
|
+
loader,
|
48
|
+
timm_config: dict[Any],
|
49
|
+
):
|
50
|
+
architecture = timm_config["architecture"]
|
51
|
+
stackwise_num_repeats = REPEATS_BY_SIZE[architecture]
|
52
|
+
|
53
|
+
hf_index_to_keras_layer_name = {}
|
54
|
+
layer_index = 0
|
55
|
+
for block_index, repeats_in_block in enumerate(stackwise_num_repeats):
|
56
|
+
for repeat_index in range(repeats_in_block):
|
57
|
+
hf_index = layer_index
|
58
|
+
layer_index += 2 # Conv + activation layers.
|
59
|
+
layer_name = f"block{block_index + 1}_conv{repeat_index + 1}"
|
60
|
+
hf_index_to_keras_layer_name[hf_index] = layer_name
|
61
|
+
layer_index += 1 # Pooling layer after blocks.
|
62
|
+
|
63
|
+
for hf_index, keras_layer_name in hf_index_to_keras_layer_name.items():
|
64
|
+
convert_conv2d(
|
65
|
+
backbone, loader, keras_layer_name, f"features.{hf_index}"
|
66
|
+
)
|
67
|
+
|
68
|
+
|
69
|
+
def convert_head(
|
70
|
+
task: VGGImageClassifier,
|
71
|
+
loader,
|
72
|
+
timm_config: dict[Any],
|
73
|
+
):
|
74
|
+
convert_conv2d(task.head, loader, "fc1", "pre_logits.fc1")
|
75
|
+
convert_conv2d(task.head, loader, "fc2", "pre_logits.fc2")
|
76
|
+
|
77
|
+
loader.port_weight(
|
78
|
+
task.head.get_layer("predictions").kernel,
|
79
|
+
hf_weight_key="head.fc.weight",
|
80
|
+
hook_fn=lambda x, _: np.transpose(np.squeeze(x)),
|
81
|
+
)
|
82
|
+
loader.port_weight(
|
83
|
+
task.head.get_layer("predictions").bias,
|
84
|
+
hf_weight_key="head.fc.bias",
|
85
|
+
)
|
@@ -5,6 +5,7 @@ from keras_hub.src.utils.preset_utils import PresetLoader
|
|
5
5
|
from keras_hub.src.utils.preset_utils import jax_memory_cleanup
|
6
6
|
from keras_hub.src.utils.timm import convert_densenet
|
7
7
|
from keras_hub.src.utils.timm import convert_resnet
|
8
|
+
from keras_hub.src.utils.timm import convert_vgg
|
8
9
|
from keras_hub.src.utils.transformers.safetensor_utils import SafetensorLoader
|
9
10
|
|
10
11
|
|
@@ -16,6 +17,8 @@ class TimmPresetLoader(PresetLoader):
|
|
16
17
|
self.converter = convert_resnet
|
17
18
|
elif "densenet" in architecture:
|
18
19
|
self.converter = convert_densenet
|
20
|
+
elif "vgg" in architecture:
|
21
|
+
self.converter = convert_vgg
|
19
22
|
else:
|
20
23
|
raise ValueError(
|
21
24
|
"KerasHub has no converter for timm models "
|
keras_hub/src/version_utils.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: keras-hub-nightly
|
3
|
-
Version: 0.16.1.
|
3
|
+
Version: 0.16.1.dev202410110340
|
4
4
|
Summary: Industry-strength Natural Language Processing extensions for Keras.
|
5
5
|
Home-page: https://github.com/keras-team/keras-hub
|
6
6
|
Author: Keras team
|
@@ -1,15 +1,15 @@
|
|
1
1
|
keras_hub/__init__.py,sha256=QGdXyHgYt6cMUAP1ebxwc6oR86dE0dkMxNy2eOCQtFo,855
|
2
2
|
keras_hub/api/__init__.py,sha256=spMxsgqzjpeuC8rY4WP-2kAZ2qwwKRSbFwddXgUjqQE,524
|
3
3
|
keras_hub/api/bounding_box/__init__.py,sha256=T8R_X7BPm0et1xaZq8565uJmid7dylsSFSj4V-rGuFQ,1097
|
4
|
-
keras_hub/api/layers/__init__.py,sha256=
|
4
|
+
keras_hub/api/layers/__init__.py,sha256=NpuVqxRCKbnvuW1QjZiSy7vHsI9Ej95ZCnNp4Lm5Hj8,2515
|
5
5
|
keras_hub/api/metrics/__init__.py,sha256=So8Ec-lOcTzn_UUMmAdzDm8RKkPu2dbRUm2px8gpUEI,381
|
6
|
-
keras_hub/api/models/__init__.py,sha256=
|
6
|
+
keras_hub/api/models/__init__.py,sha256=9-1qQyoGODam73Vlo49EI2fzTwzwgdlCshlTMRrFEqg,14884
|
7
7
|
keras_hub/api/samplers/__init__.py,sha256=n-_SEXxr2LNUzK2FqVFN7alsrkx1P_HOVTeLZKeGCdE,730
|
8
8
|
keras_hub/api/tokenizers/__init__.py,sha256=_f-r_cyUM2fjBB7iO84ThOdqqsAxHNIewJ2EBDlM0cA,2524
|
9
9
|
keras_hub/api/utils/__init__.py,sha256=Gp1E6gG-RtKQS3PBEQEOz9PQvXkXaJ0ySGMqZ7myN7A,215
|
10
10
|
keras_hub/src/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
11
11
|
keras_hub/src/api_export.py,sha256=9pQZK27JObxWZ96QPLBp1OBsjWigh1iuV6RglPGMRk0,1499
|
12
|
-
keras_hub/src/version_utils.py,sha256=
|
12
|
+
keras_hub/src/version_utils.py,sha256=9zvnHnaU7MGFEG0NsmxQpWEG3qxlcuPtyLNw-vhmajA,222
|
13
13
|
keras_hub/src/bounding_box/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
14
14
|
keras_hub/src/bounding_box/converters.py,sha256=a5po8DBm87oz2EXfi-0uEZHCMlCJPIb4-MaZIdYx3Dg,17865
|
15
15
|
keras_hub/src/bounding_box/formats.py,sha256=YmskOz2BOSat7NaE__J9VfpSNGPJJR0znSzA4lp8MMI,3868
|
@@ -295,9 +295,9 @@ keras_hub/src/models/t5/t5_transformer_layer.py,sha256=uDeP84F1x7xJxki5iKe12Zn6e
|
|
295
295
|
keras_hub/src/models/vae/__init__.py,sha256=i3UaSW4IJf76O7lSPE1dyxOVjuHx8iAYKivqvUbDHOw,62
|
296
296
|
keras_hub/src/models/vae/vae_backbone.py,sha256=aYf1sGteFJ7FyR3X8Ek6QBjAT5GjRtQTK2jXhYVJeM4,6671
|
297
297
|
keras_hub/src/models/vae/vae_layers.py,sha256=N83CYM1zgbl1EIjAOs3cFCkJEdxvbXkgM9ghKyljFAg,27752
|
298
|
-
keras_hub/src/models/vgg/__init__.py,sha256=
|
299
|
-
keras_hub/src/models/vgg/vgg_backbone.py,sha256=
|
300
|
-
keras_hub/src/models/vgg/vgg_image_classifier.py,sha256=
|
298
|
+
keras_hub/src/models/vgg/__init__.py,sha256=1ydFmkTOix2kOnDHie3srD4XD0dQ_7iR8OYbJzBM_YM,62
|
299
|
+
keras_hub/src/models/vgg/vgg_backbone.py,sha256=qes1AsKwBDI7eQ3aC1uRievMkVNGXM9TNhtKLb9eZiU,3697
|
300
|
+
keras_hub/src/models/vgg/vgg_image_classifier.py,sha256=bl6XM7l9fnOTGFreqOO3Z1jreusjhA4l7G0xjimfUKA,7829
|
301
301
|
keras_hub/src/models/vit_det/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
302
302
|
keras_hub/src/models/vit_det/vit_det_backbone.py,sha256=GzwHXAfttExqDaGU4R2LAvng1gzjuvO3HMqUPwNUy9g,7656
|
303
303
|
keras_hub/src/models/vit_det/vit_layers.py,sha256=oCKeUw5ckyUAGvmFPuxIiIAqgmC3uqh85LfZcgyh964,19852
|
@@ -353,7 +353,8 @@ keras_hub/src/utils/imagenet/imagenet_utils.py,sha256=MvIvv1WJo51ZXBxy4S7t_DsN3Z
|
|
353
353
|
keras_hub/src/utils/timm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
354
354
|
keras_hub/src/utils/timm/convert_densenet.py,sha256=V-GRjWuDnlh3b1EMxqahwZ3GMwSgOa3v0HOfb2ZZ-d0,3342
|
355
355
|
keras_hub/src/utils/timm/convert_resnet.py,sha256=ee8eTml0ffJKE8avzGoLFcpjPF63DsvoIUArAGa8Ngg,5832
|
356
|
-
keras_hub/src/utils/timm/
|
356
|
+
keras_hub/src/utils/timm/convert_vgg.py,sha256=MT5jGnLrzenPpe66Af_Lp1IdR9KGtsSrcmn6_UPqHvQ,2419
|
357
|
+
keras_hub/src/utils/timm/preset_loader.py,sha256=2GJI2YeKGVovtDqc930uGta12yiyuCL9YrsTyGhqt9Y,3094
|
357
358
|
keras_hub/src/utils/transformers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
358
359
|
keras_hub/src/utils/transformers/convert_albert.py,sha256=VdKclZpCxtDWq3UbUUQZf4fR9DJK_JYZ73B4O_G9skg,7695
|
359
360
|
keras_hub/src/utils/transformers/convert_bart.py,sha256=Tk4h9Md9rwN5wjQbGIVrC7qzDpF8kI8qm-FKL8HlUok,14411
|
@@ -366,7 +367,7 @@ keras_hub/src/utils/transformers/convert_mistral.py,sha256=kVhN9h1ZFVhwkNW8p3wnS
|
|
366
367
|
keras_hub/src/utils/transformers/convert_pali_gemma.py,sha256=B1leeDw96Yvu81hYumf66hIid07k5NLqoeWAJgPnaLs,10649
|
367
368
|
keras_hub/src/utils/transformers/preset_loader.py,sha256=GS44hZUuGQCtzsyn8z44ZpHdftd3DFemwV2hx2bQa-U,2738
|
368
369
|
keras_hub/src/utils/transformers/safetensor_utils.py,sha256=rPK-Uw1CG0DX0d_UAD-r2cG9fw8GI8bvAlrcXfQ9g4c,3323
|
369
|
-
keras_hub_nightly-0.16.1.
|
370
|
-
keras_hub_nightly-0.16.1.
|
371
|
-
keras_hub_nightly-0.16.1.
|
372
|
-
keras_hub_nightly-0.16.1.
|
370
|
+
keras_hub_nightly-0.16.1.dev202410110340.dist-info/METADATA,sha256=8UB_SWjRF9_PRraQNKuRN13SSTXmUCEwQPFGo0nU2Jk,7458
|
371
|
+
keras_hub_nightly-0.16.1.dev202410110340.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
|
372
|
+
keras_hub_nightly-0.16.1.dev202410110340.dist-info/top_level.txt,sha256=N4J6piIWBKa38A4uV-CnIopnOEf8mHAbkNXafXm_CuA,10
|
373
|
+
keras_hub_nightly-0.16.1.dev202410110340.dist-info/RECORD,,
|
File without changes
|