Skip to content

Add StableDiffusion3 #1820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 62 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
b681ea3
Add VGG16 backbone (#1737)
divyashreepathihalli Aug 8, 2024
33854c6
Add `ResNetBackbone` and `ResNetImageClassifier` (#1765)
james77777778 Aug 12, 2024
dd8df26
Add CSP DarkNet backbone and classifier (#1774)
sachinprasadhs Aug 15, 2024
7381b6d
Add `FeaturePyramidBackbone` and port weights from `timm` for `ResNet…
james77777778 Aug 15, 2024
b76317e
Add DenseNet (#1775)
sachinprasadhs Aug 16, 2024
c822f3a
Add ViTDetBackbone (#1776)
divyashreepathihalli Aug 20, 2024
bc69acd
Add Mix transformer (#1780)
sachinprasadhs Aug 20, 2024
7e180c2
update input_image_shape -> image_shape (#1785)
divyashreepathihalli Aug 21, 2024
12a4ccc
Create __init__.py (#1788)
sachinprasadhs Aug 22, 2024
3e9aaba
Hack package build script to rename to keras-hub (#1793)
mattdangerw Aug 26, 2024
741f98d
Add CLIP and T5XXL for StableDiffusionV3 (#1790)
james77777778 Aug 26, 2024
352e0b7
Add Bounding Box Utils (#1791)
sineeli Aug 28, 2024
1a81234
mobilenet_v3 added in keras-nlp (#1782)
ushareng Aug 28, 2024
894226d
Pkgoogle/efficient net migration (#1778)
pkgoogle Aug 28, 2024
6104520
Add the ResNet_vd backbone (#1766)
gowthamkpr Aug 28, 2024
faffd86
Add `VAEImageDecoder` for StableDiffusionV3 (#1796)
james77777778 Aug 28, 2024
bdf0d7f
Replace `Backbone` with `keras.Model` in `CLIPTextEncoder` and `T5XXL…
james77777778 Aug 28, 2024
85daf81
Add pyramid output for densenet, cspDarknet (#1801)
sachinprasadhs Sep 3, 2024
321b757
Add `MMDiT` for StableDiffusionV3 (#1806)
james77777778 Sep 4, 2024
dfb7123
Add remaining bbox utils (#1804)
sineeli Sep 4, 2024
753047d
Fix timm conversion for rersnet (#1814)
sachinprasadhs Sep 5, 2024
3c239ca
Add `StableDiffusion3`
james77777778 Sep 11, 2024
53e90f1
Fix `_normalize_inputs`
james77777778 Sep 11, 2024
6503f66
Separate CLIP encoders from SD3 backbone.
james77777778 Sep 12, 2024
86f98bb
Simplify `text_to_image` function.
james77777778 Sep 12, 2024
4ad8d47
Address comments
james77777778 Sep 13, 2024
0d6d6a1
Minor update and add docstrings.
james77777778 Sep 13, 2024
edc74fa
Add VGG16 backbone (#1737)
divyashreepathihalli Aug 8, 2024
f305f64
Add `ResNetBackbone` and `ResNetImageClassifier` (#1765)
james77777778 Aug 12, 2024
d090b98
Add CSP DarkNet backbone and classifier (#1774)
sachinprasadhs Aug 15, 2024
4b20620
Add `FeaturePyramidBackbone` and port weights from `timm` for `ResNet…
james77777778 Aug 15, 2024
689a8db
Add DenseNet (#1775)
sachinprasadhs Aug 16, 2024
3f6c936
Add ViTDetBackbone (#1776)
divyashreepathihalli Aug 20, 2024
76d704f
Add Mix transformer (#1780)
sachinprasadhs Aug 20, 2024
d91b61a
update input_image_shape -> image_shape (#1785)
divyashreepathihalli Aug 21, 2024
ab7efdd
Create __init__.py (#1788)
sachinprasadhs Aug 22, 2024
2a11cf1
Hack package build script to rename to keras-hub (#1793)
mattdangerw Aug 26, 2024
40b5153
Add CLIP and T5XXL for StableDiffusionV3 (#1790)
james77777778 Aug 26, 2024
f597523
Add Bounding Box Utils (#1791)
sineeli Aug 28, 2024
1e97bae
mobilenet_v3 added in keras-nlp (#1782)
ushareng Aug 28, 2024
639d983
Pkgoogle/efficient net migration (#1778)
pkgoogle Aug 28, 2024
b10c410
Add the ResNet_vd backbone (#1766)
gowthamkpr Aug 28, 2024
9feb2d8
Add `VAEImageDecoder` for StableDiffusionV3 (#1796)
james77777778 Aug 28, 2024
30cc165
Replace `Backbone` with `keras.Model` in `CLIPTextEncoder` and `T5XXL…
james77777778 Aug 28, 2024
ae3d558
Add pyramid output for densenet, cspDarknet (#1801)
sachinprasadhs Sep 3, 2024
76e8fb6
Add `MMDiT` for StableDiffusionV3 (#1806)
james77777778 Sep 4, 2024
759905e
Add remaining bbox utils (#1804)
sineeli Sep 4, 2024
a5e5d8f
Fix timm conversion for rersnet (#1814)
sachinprasadhs Sep 5, 2024
f76b689
Merge remote-tracking branch 'upstream/keras-hub' into add-sd3
james77777778 Sep 15, 2024
ec7f53d
Fix
james77777778 Sep 15, 2024
ab10eaa
Update
james77777778 Sep 15, 2024
79f3a01
Rename to diffuser and decoder
james77777778 Sep 15, 2024
fb73693
Define functional model
james77777778 Sep 16, 2024
aa42194
Merge remote-tracking branch 'upstream/master' into add-sd3
james77777778 Sep 23, 2024
403e4b8
Merge from upstream/master
james77777778 Sep 23, 2024
1363024
Delete old SD3
james77777778 Sep 23, 2024
51ca7ea
Fix copyright
james77777778 Sep 23, 2024
0f72e58
Rename to keras_hub
james77777778 Sep 23, 2024
ef425d0
Address comments
james77777778 Sep 24, 2024
74a4b07
Update
james77777778 Sep 24, 2024
2fb4953
Fix CI
james77777778 Sep 24, 2024
6e086cf
Fix bugs occurred in keras3.1
james77777778 Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions keras_hub/api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
from keras_hub.src.models.bloom.bloom_tokenizer import BloomTokenizer
from keras_hub.src.models.causal_lm import CausalLM
from keras_hub.src.models.causal_lm_preprocessor import CausalLMPreprocessor
from keras_hub.src.models.clip.clip_preprocessor import CLIPPreprocessor
from keras_hub.src.models.clip.clip_tokenizer import CLIPTokenizer
from keras_hub.src.models.csp_darknet.csp_darknet_backbone import (
CSPDarkNetBackbone,
)
Expand Down Expand Up @@ -257,14 +259,25 @@
from keras_hub.src.models.roberta.roberta_tokenizer import RobertaTokenizer
from keras_hub.src.models.seq_2_seq_lm import Seq2SeqLM
from keras_hub.src.models.seq_2_seq_lm_preprocessor import Seq2SeqLMPreprocessor
from keras_hub.src.models.stable_diffusion_3.stable_diffusion_3_backbone import (
StableDiffusion3Backbone,
)
from keras_hub.src.models.stable_diffusion_3.stable_diffusion_3_text_to_image import (
StableDiffusion3TextToImage,
)
from keras_hub.src.models.stable_diffusion_3.stable_diffusion_3_text_to_image_preprocessor import (
StableDiffusion3TextToImagePreprocessor,
)
from keras_hub.src.models.t5.t5_backbone import T5Backbone
from keras_hub.src.models.t5.t5_preprocessor import T5Preprocessor
from keras_hub.src.models.t5.t5_tokenizer import T5Tokenizer
from keras_hub.src.models.task import Task
from keras_hub.src.models.text_classifier import TextClassifier
from keras_hub.src.models.text_classifier import TextClassifier as Classifier
from keras_hub.src.models.text_classifier_preprocessor import (
TextClassifierPreprocessor,
)
from keras_hub.src.models.text_to_image import TextToImage
from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone
from keras_hub.src.models.vgg.vgg_image_classifier import VGGImageClassifier
from keras_hub.src.models.vit_det.vit_det_backbone import ViTDetBackbone
Expand Down
1 change: 1 addition & 0 deletions keras_hub/api/tokenizers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from keras_hub.src.models.bart.bart_tokenizer import BartTokenizer
from keras_hub.src.models.bert.bert_tokenizer import BertTokenizer
from keras_hub.src.models.bloom.bloom_tokenizer import BloomTokenizer
from keras_hub.src.models.clip.clip_tokenizer import CLIPTokenizer
from keras_hub.src.models.deberta_v3.deberta_v3_tokenizer import (
DebertaV3Tokenizer,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from keras import dtype_policies
from keras import layers
from keras import ops

Expand Down Expand Up @@ -43,7 +44,7 @@ def __init__(
intermediate_activation = quick_gelu

self.layer_norm_1 = layers.LayerNormalization(
epsilon=0.00001, dtype=self.dtype_policy, name="layer_norm_1"
epsilon=1e-5, dtype="float32", name="layer_norm_1"
)
self.attention = layers.MultiHeadAttention(
num_heads,
Expand All @@ -52,7 +53,7 @@ def __init__(
name="attention",
)
self.layer_norm_2 = layers.LayerNormalization(
epsilon=0.00001, dtype=self.dtype_policy, name="layer_norm_2"
epsilon=1e-5, dtype="float32", name="layer_norm_2"
)
self.dense_1 = layers.Dense(
self.intermediate_dim, dtype=self.dtype_policy, name="dense_1"
Expand All @@ -67,6 +68,11 @@ def __init__(
def build(self, input_shape):
self.layer_norm_1.build(input_shape)
self.attention.build(input_shape, input_shape, input_shape)
# Before Keras 3.2, there was no setter for `dtype_policy`. Directly
# assign a `DTypePolicy` instead.
self.attention._softmax.dtype_policy = dtype_policies.DTypePolicy(
"float32"
)
self.layer_norm_2.build(input_shape)
self.dense_1.build(input_shape)
input_shape = self.dense_1.compute_output_shape(input_shape)
Expand Down
147 changes: 147 additions & 0 deletions keras_hub/src/models/clip/clip_preprocessor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Copyright 2024 The KerasHub Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import keras

from keras_hub.src.api_export import keras_hub_export
from keras_hub.src.layers.preprocessing.start_end_packer import StartEndPacker
from keras_hub.src.models.clip.clip_tokenizer import CLIPTokenizer
from keras_hub.src.models.preprocessor import Preprocessor
from keras_hub.src.utils.tensor_utils import preprocessing_function

try:
import tensorflow as tf
except ImportError:
tf = None


@keras_hub_export("keras_hub.models.CLIPPreprocessor")
class CLIPPreprocessor(Preprocessor):
"""CLIP preprocessing layer which tokenizes and packs inputs.

This preprocessing layer will do 2 things:

- Tokenize the inputs using the `tokenizer`.
- Construct a dictionary with keys `"token_ids"`, `"padding_mask"`.

This layer can be used directly with `tf.data.Dataset.map` to preprocess
string data in the `(x, y, sample_weight)` format used by
`keras.Model.fit`.

The call method of this layer accepts three arguments, `x`, `y`, and
`sample_weight`. `x` can be a python string or tensor representing a single
segment, a list of python strings representing a batch of single segments,
or a list of tensors representing multiple segments to be packed together.
`y` and `sample_weight` are both optional, can have any format, and will be
passed through unaltered.

`CLIPPreprocessor` forces the input to have only one segment, as CLIP is
mainly used for generation tasks. For tasks having multi-segment inputs
like "glue/mnli", please use a model designed for classification purposes
such as BERT or RoBERTa.

Args:
tokenizer: A `keras_hub.models.CLIPTokenizer` instance.
sequence_length: The length of the packed inputs.
add_start_token: If `True`, the preprocessor will prepend the tokenizer
start token to each input sequence.
add_end_token: If `True`, the preprocessor will append the tokenizer
end token to each input sequence.
to_lower: bool. Whether to lower the inputs.

Call arguments:
x: A string, `tf.Tensor` or list of python strings.
y: Any label data. Will be passed through unaltered.
sample_weight: Any label weight data. Will be passed through unaltered.
sequence_length: Pass to override the configured `sequence_length` of
the layer.
"""

# TODO: Add example once we have a CLIP model.

tokenizer_cls = CLIPTokenizer

def __init__(
self,
tokenizer,
sequence_length=77,
add_start_token=True,
add_end_token=True,
to_lower=True,
**kwargs,
):
super().__init__(**kwargs)
self.tokenizer = tokenizer
self.packer = None
self.sequence_length = sequence_length
self.add_start_token = add_start_token
self.add_end_token = add_end_token
self.to_lower = to_lower

def build(self, input_shape):
# Defer packer creation to `build()` so that we can be sure tokenizer
# assets have loaded when restoring a saved model.
self.packer = StartEndPacker(
start_value=self.tokenizer.start_token_id,
end_value=self.tokenizer.end_token_id,
pad_value=self.tokenizer.end_token_id,
sequence_length=self.sequence_length,
return_padding_mask=True,
)
self.built = True

@preprocessing_function
def call(
self,
x,
y=None,
sample_weight=None,
sequence_length=None,
):
sequence_length = sequence_length or self.sequence_length
if self.to_lower:
x = tf.strings.lower(x)
token_ids, padding_mask = self.packer(
self.tokenizer(x),
sequence_length=sequence_length,
add_start_value=self.add_start_token,
add_end_value=self.add_end_token,
)
x = {
"token_ids": token_ids,
"padding_mask": padding_mask,
}
return keras.utils.pack_x_y_sample_weight(x, y, sample_weight)

def get_config(self):
config = super().get_config()
config.update(
{
"sequence_length": self.sequence_length,
"add_start_token": self.add_start_token,
"add_end_token": self.add_end_token,
"to_lower": self.to_lower,
}
)
return config

@property
def sequence_length(self):
"""The padded length of model input sequences."""
return self._sequence_length

@sequence_length.setter
def sequence_length(self, value):
self._sequence_length = value
if self.packer is not None:
self.packer.sequence_length = value
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
# limitations under the License.
import pytest

from keras_hub.src.models.stable_diffusion_v3.clip_preprocessor import (
CLIPPreprocessor,
)
from keras_hub.src.models.stable_diffusion_v3.clip_tokenizer import (
CLIPTokenizer,
)
from keras_hub.src.models.clip.clip_preprocessor import CLIPPreprocessor
from keras_hub.src.models.clip.clip_tokenizer import CLIPTokenizer
from keras_hub.src.tests.test_case import TestCase


Expand All @@ -43,7 +39,7 @@ def test_preprocessor_basics(self):
input_data=self.input_data,
expected_output={
"token_ids": [[5, 1, 2, 1, 3, 4, 4, 4]],
"padding_mask": [[1, 1, 1, 1, 1, 0, 0, 0]],
"padding_mask": [[1, 1, 1, 1, 1, 1, 0, 0]],
},
)

Expand All @@ -54,17 +50,16 @@ def test_no_start_end_token(self):
sequence_length=8,
add_start_token=False,
add_end_token=False,
pad_with_end_token=False,
)
x = preprocessor(input_data)
self.assertAllEqual(x["token_ids"], [[1, 2, 1, 3, 0, 0, 0, 0]] * 4)
self.assertAllEqual(x["token_ids"], [[1, 2, 1, 3, 4, 4, 4, 4]] * 4)
self.assertAllEqual(x["padding_mask"], [[1, 1, 1, 1, 0, 0, 0, 0]] * 4)

def test_sequence_length_override(self):
input_data = " airplane airport"
preprocessor = CLIPPreprocessor(**self.init_kwargs)
x = preprocessor(input_data, sequence_length=4)
self.assertAllEqual(x["token_ids"], [5, 1, 2, 1])
x = preprocessor(input_data, sequence_length=5)
self.assertAllEqual(x["token_ids"], [5, 1, 2, 1, 4])

@pytest.mark.kaggle_key_required
@pytest.mark.extra_large
Expand Down
Loading
Loading