keras-hub-nightly 0.16.1.dev202409250340__py3-none-any.whl → 0.16.1.dev202409260340__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 (35) hide show
  1. keras_hub/api/layers/__init__.py +3 -0
  2. keras_hub/api/models/__init__.py +16 -0
  3. keras_hub/api/tokenizers/__init__.py +1 -0
  4. keras_hub/src/models/{stable_diffusion_v3 → clip}/clip_encoder_block.py +8 -2
  5. keras_hub/src/models/clip/clip_preprocessor.py +147 -0
  6. keras_hub/src/models/{stable_diffusion_v3 → clip}/clip_text_encoder.py +60 -57
  7. keras_hub/src/models/{stable_diffusion_v3 → clip}/clip_tokenizer.py +69 -30
  8. keras_hub/src/models/densenet/__init__.py +6 -0
  9. keras_hub/src/models/densenet/densenet_backbone.py +11 -8
  10. keras_hub/src/models/densenet/densenet_image_classifier.py +27 -4
  11. keras_hub/src/models/densenet/densenet_image_classifier_preprocessor.py +27 -0
  12. keras_hub/src/models/densenet/densenet_image_converter.py +23 -0
  13. keras_hub/src/models/densenet/densenet_presets.py +56 -0
  14. keras_hub/src/models/stable_diffusion_3/__init__.py +13 -0
  15. keras_hub/src/models/stable_diffusion_3/flow_match_euler_discrete_scheduler.py +93 -0
  16. keras_hub/src/models/{stable_diffusion_v3 → stable_diffusion_3}/mmdit.py +351 -26
  17. keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_backbone.py +630 -0
  18. keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_text_to_image.py +151 -0
  19. keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_text_to_image_preprocessor.py +77 -0
  20. keras_hub/src/models/{stable_diffusion_v3/t5_xxl_text_encoder.py → stable_diffusion_3/t5_encoder.py} +7 -7
  21. keras_hub/src/models/stable_diffusion_3/vae_image_decoder.py +333 -0
  22. keras_hub/src/models/{stable_diffusion_v3/t5_xxl_preprocessor.py → t5/t5_preprocessor.py} +12 -3
  23. keras_hub/src/models/text_to_image.py +295 -0
  24. keras_hub/src/utils/timm/convert_densenet.py +107 -0
  25. keras_hub/src/utils/timm/preset_loader.py +3 -0
  26. keras_hub/src/version_utils.py +1 -1
  27. {keras_hub_nightly-0.16.1.dev202409250340.dist-info → keras_hub_nightly-0.16.1.dev202409260340.dist-info}/METADATA +1 -1
  28. {keras_hub_nightly-0.16.1.dev202409250340.dist-info → keras_hub_nightly-0.16.1.dev202409260340.dist-info}/RECORD +31 -23
  29. keras_hub/src/models/stable_diffusion_v3/clip_preprocessor.py +0 -93
  30. keras_hub/src/models/stable_diffusion_v3/mmdit_block.py +0 -317
  31. keras_hub/src/models/stable_diffusion_v3/vae_attention.py +0 -126
  32. keras_hub/src/models/stable_diffusion_v3/vae_image_decoder.py +0 -186
  33. /keras_hub/src/models/{stable_diffusion_v3 → clip}/__init__.py +0 -0
  34. {keras_hub_nightly-0.16.1.dev202409250340.dist-info → keras_hub_nightly-0.16.1.dev202409260340.dist-info}/WHEEL +0 -0
  35. {keras_hub_nightly-0.16.1.dev202409250340.dist-info → keras_hub_nightly-0.16.1.dev202409260340.dist-info}/top_level.txt +0 -0
@@ -15,6 +15,9 @@ import keras
15
15
 
16
16
  from keras_hub.src.api_export import keras_hub_export
17
17
  from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone
18
+ from keras_hub.src.models.densenet.densenet_image_classifier_preprocessor import (
19
+ DenseNetImageClassifierPreprocessor,
20
+ )
18
21
  from keras_hub.src.models.image_classifier import ImageClassifier
19
22
 
20
23
 
@@ -32,7 +35,13 @@ class DenseNetImageClassifier(ImageClassifier):
32
35
  num_classes: int. The number of classes to predict.
33
36
  activation: `None`, str or callable. The activation function to use on
34
37
  the `Dense` layer. Set `activation=None` to return the output
35
- logits. Defaults to `"softmax"`.
38
+ logits. Defaults to `None`.
39
+ pooling: A pooling layer to use before the final classification layer,
40
+ must be one of "avg" or "max". Use "avg" for
41
+ `GlobalAveragePooling2D` and "max" for "GlobalMaxPooling2D.
42
+ preprocessor: A `keras_hub.models.DenseNetImageClassifierPreprocessor`
43
+ or `None`. If `None`, this model will not apply preprocessing, and
44
+ inputs should be preprocessed before calling the model.
36
45
 
37
46
  Examples:
38
47
 
@@ -86,18 +95,29 @@ class DenseNetImageClassifier(ImageClassifier):
86
95
  """
87
96
 
88
97
  backbone_cls = DenseNetBackbone
98
+ preprocessor_cls = DenseNetImageClassifierPreprocessor
89
99
 
90
100
  def __init__(
91
101
  self,
92
102
  backbone,
93
103
  num_classes,
94
- activation="softmax",
95
- preprocessor=None, # adding this dummy arg for saved model test
96
- # TODO: once preprocessor flow is figured out, this needs to be updated
104
+ activation=None,
105
+ pooling="avg",
106
+ preprocessor=None,
97
107
  **kwargs,
98
108
  ):
99
109
  # === Layers ===
100
110
  self.backbone = backbone
111
+ self.preprocessor = preprocessor
112
+ if pooling == "avg":
113
+ self.pooler = keras.layers.GlobalAveragePooling2D()
114
+ elif pooling == "max":
115
+ self.pooler = keras.layers.GlobalMaxPooling2D()
116
+ else:
117
+ raise ValueError(
118
+ "Unknown `pooling` type. Polling should be either `'avg'` or "
119
+ f"`'max'`. Received: pooling={pooling}."
120
+ )
101
121
  self.output_dense = keras.layers.Dense(
102
122
  num_classes,
103
123
  activation=activation,
@@ -107,6 +127,7 @@ class DenseNetImageClassifier(ImageClassifier):
107
127
  # === Functional Model ===
108
128
  inputs = self.backbone.input
109
129
  x = self.backbone(inputs)
130
+ x = self.pooler(x)
110
131
  outputs = self.output_dense(x)
111
132
  super().__init__(
112
133
  inputs=inputs,
@@ -117,6 +138,7 @@ class DenseNetImageClassifier(ImageClassifier):
117
138
  # === Config ===
118
139
  self.num_classes = num_classes
119
140
  self.activation = activation
141
+ self.pooling = pooling
120
142
 
121
143
  def get_config(self):
122
144
  # Backbone serialized in `super`
@@ -125,6 +147,7 @@ class DenseNetImageClassifier(ImageClassifier):
125
147
  {
126
148
  "num_classes": self.num_classes,
127
149
  "activation": self.activation,
150
+ "pooling": self.pooling,
128
151
  }
129
152
  )
130
153
  return config
@@ -0,0 +1,27 @@
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
+ from keras_hub.src.api_export import keras_hub_export
15
+ from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone
16
+ from keras_hub.src.models.densenet.densenet_image_converter import (
17
+ DenseNetImageConverter,
18
+ )
19
+ from keras_hub.src.models.image_classifier_preprocessor import (
20
+ ImageClassifierPreprocessor,
21
+ )
22
+
23
+
24
+ @keras_hub_export("keras_hub.models.DenseNetImageClassifierPreprocessor")
25
+ class DenseNetImageClassifierPreprocessor(ImageClassifierPreprocessor):
26
+ backbone_cls = DenseNetBackbone
27
+ image_converter_cls = DenseNetImageConverter
@@ -0,0 +1,23 @@
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
+ from keras_hub.src.api_export import keras_hub_export
15
+ from keras_hub.src.layers.preprocessing.resizing_image_converter import (
16
+ ResizingImageConverter,
17
+ )
18
+ from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone
19
+
20
+
21
+ @keras_hub_export("keras_hub.layers.DenseNetImageConverter")
22
+ class DenseNetImageConverter(ResizingImageConverter):
23
+ backbone_cls = DenseNetBackbone
@@ -0,0 +1,56 @@
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
+ """DenseNet preset configurations."""
15
+
16
+ backbone_presets = {
17
+ "densenet_121_imagenet": {
18
+ "metadata": {
19
+ "description": (
20
+ "121-layer DenseNet model pre-trained on the ImageNet 1k dataset "
21
+ "at a 224x224 resolution."
22
+ ),
23
+ "params": 7037504,
24
+ "official_name": "DenseNet",
25
+ "path": "densenet",
26
+ "model_card": "https://arxiv.org/abs/1608.06993",
27
+ },
28
+ "kaggle_handle": "kaggle://kerashub/densenet/keras/densenet_121_imagenet",
29
+ },
30
+ "densenet_169_imagenet": {
31
+ "metadata": {
32
+ "description": (
33
+ "169-layer DenseNet model pre-trained on the ImageNet 1k dataset "
34
+ "at a 224x224 resolution."
35
+ ),
36
+ "params": 12642880,
37
+ "official_name": "DenseNet",
38
+ "path": "densenet",
39
+ "model_card": "https://arxiv.org/abs/1608.06993",
40
+ },
41
+ "kaggle_handle": "kaggle://kerashub/densenet/keras/densenet_169_imagenet",
42
+ },
43
+ "densenet_201_imagenet": {
44
+ "metadata": {
45
+ "description": (
46
+ "201-layer DenseNet model pre-trained on the ImageNet 1k dataset "
47
+ "at a 224x224 resolution."
48
+ ),
49
+ "params": 18321984,
50
+ "official_name": "DenseNet",
51
+ "path": "densenet",
52
+ "model_card": "https://arxiv.org/abs/1608.06993",
53
+ },
54
+ "kaggle_handle": "kaggle://kerashub/densenet/keras/densenet_201_imagenet",
55
+ },
56
+ }
@@ -0,0 +1,13 @@
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.
@@ -0,0 +1,93 @@
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
+ from keras import layers
15
+ from keras import ops
16
+
17
+
18
+ class FlowMatchEulerDiscreteScheduler(layers.Layer):
19
+ """Flow-matching sampling euler scheduler.
20
+
21
+ This layer is used to compute the discrete sigmas for the diffusion chain.
22
+ Typically, the sigma refers to the amount of noise added during the
23
+ diffusion process.
24
+
25
+ Args:
26
+ num_train_timesteps: int. The number of diffusion steps to train the
27
+ model.
28
+ shift: float. The shift value for the timestep schedule.
29
+ **kwargs: other keyword arguments passed to `keras.layers.Layer`,
30
+ including `name`, `dtype` etc.
31
+
32
+ Call arguments:
33
+ inputs: The current step of the diffusion process.
34
+ num_steps: The total number of steps in the diffusion process.
35
+
36
+ References:
37
+ - [Common Diffusion Noise Schedules and Sample Steps are Flawed](
38
+ https://arxiv.org/abs/2305.08891).
39
+ - [Scaling Rectified Flow Transformers for High-Resolution Image Synthesis](
40
+ https://arxiv.org/abs/2403.03206).
41
+ """
42
+
43
+ def __init__(self, num_train_timesteps=1000, shift=1.0, **kwargs):
44
+ super().__init__(**kwargs)
45
+ self.num_train_timesteps = int(num_train_timesteps)
46
+ self.shift = float(shift)
47
+
48
+ timesteps = ops.linspace(
49
+ 1, num_train_timesteps, num_train_timesteps, dtype="float32"
50
+ )
51
+ timesteps = ops.flip(timesteps, axis=0)
52
+ sigmas = self._timestep_to_sigma(timesteps)
53
+
54
+ self.timesteps = ops.multiply(sigmas, num_train_timesteps)
55
+ self.sigma_min = sigmas[-1]
56
+ self.sigma_max = sigmas[0]
57
+
58
+ def _sigma_to_timestep(self, sigma):
59
+ return sigma * self.num_train_timesteps
60
+
61
+ def _timestep_to_sigma(self, timestep):
62
+ sigma = ops.divide(timestep, self.num_train_timesteps)
63
+ if self.shift != 1.0:
64
+ sigma = ops.divide(
65
+ ops.multiply(self.shift, sigma),
66
+ ops.add(1, ops.multiply(self.shift - 1.0, sigma)),
67
+ )
68
+ return sigma
69
+
70
+ def call(self, inputs, num_steps):
71
+ start = self._sigma_to_timestep(self.sigma_max)
72
+ end = self._sigma_to_timestep(self.sigma_min)
73
+ step_size = ops.divide(
74
+ ops.subtract(end, start), ops.subtract(num_steps, 1)
75
+ )
76
+ timestep = ops.add(start, ops.multiply(inputs, step_size))
77
+ sigma = ops.maximum(self._timestep_to_sigma(timestep), 0.0)
78
+ timestep = self._sigma_to_timestep(sigma)
79
+ return sigma, timestep
80
+
81
+ def get_config(self):
82
+ config = super().get_config()
83
+ config.update(
84
+ {
85
+ "num_train_timesteps": self.num_train_timesteps,
86
+ "shift": self.shift,
87
+ }
88
+ )
89
+ return config
90
+
91
+ def compute_output_shape(self):
92
+ # Returns a tuple of (sigma, timestep).
93
+ return (None,), (None,)