-
Notifications
You must be signed in to change notification settings - Fork 330
Add initial ControlNet backbone #2586
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
Open
Amitavoo
wants to merge
2
commits into
keras-team:master
Choose a base branch
from
Amitavoo:controlnet-backbone
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from .controlnet_backbone import ControlNetBackbone | ||
| from .controlnet_preprocessor import ControlNetPreprocessor | ||
| from .controlnet_unet import ControlNetUNet | ||
| from .controlnet import ControlNet | ||
| from .controlnet_presets import controlnet_presets, from_preset | ||
| from .controlnet_layers import ZeroConv2D, ControlInjection | ||
|
|
||
| __all__ = [ | ||
| "ControlNetBackbone", | ||
| "ControlNetPreprocessor", | ||
| "ControlNetUNet", | ||
| "ControlNet", | ||
| "controlnet_presets", | ||
| "from_preset", | ||
| "ZeroConv2D", | ||
| "ControlInjection", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import keras | ||
|
|
||
| from .controlnet_backbone import ControlNetBackbone | ||
| from .controlnet_preprocessor import ControlNetPreprocessor | ||
| from .controlnet_unet import ControlNetUNet | ||
|
|
||
|
|
||
| class ControlNet(keras.Model): | ||
|
|
||
| def __init__(self, image_size=128, base_channels=64, **kwargs): | ||
| super().__init__(**kwargs) | ||
|
|
||
| self.image_size = image_size | ||
| self.base_channels = base_channels | ||
|
|
||
| self.preprocessor = ControlNetPreprocessor( | ||
| target_size=(image_size, image_size) | ||
| ) | ||
| self.backbone = ControlNetBackbone() | ||
| self.unet = ControlNetUNet(base_channels=base_channels) | ||
|
|
||
| def call(self, inputs): | ||
| image = inputs["image"] | ||
| control = inputs["control"] | ||
|
|
||
| image = self.preprocessor(image) | ||
| control = self.preprocessor(control) | ||
| control_features = self.backbone(control) | ||
|
|
||
| output = self.unet(image, control_features) | ||
|
|
||
| return output | ||
|
|
||
| def get_config(self): | ||
| config = super().get_config() | ||
| config.update( | ||
| { | ||
| "image_size": self.image_size, | ||
| "base_channels": self.base_channels, | ||
| } | ||
| ) | ||
| return config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import keras | ||
| import tensorflow as tf | ||
|
|
||
|
|
||
| class ControlNetBackbone(keras.Model): | ||
| """Lightweight conditioning encoder for ControlNet.""" | ||
|
|
||
| def __init__(self, **kwargs): | ||
| super().__init__(**kwargs) | ||
|
|
||
| self.down1 = keras.layers.Conv2D( | ||
| 64, kernel_size=3, padding="same", activation="relu" | ||
| ) | ||
| self.down2 = keras.layers.Conv2D( | ||
| 128, kernel_size=3, padding="same", activation="relu" | ||
| ) | ||
| self.down3 = keras.layers.Conv2D( | ||
| 256, kernel_size=3, padding="same", activation="relu" | ||
| ) | ||
|
|
||
| self.pool = keras.layers.MaxPooling2D(pool_size=2) | ||
|
|
||
| def build(self, input_shape): | ||
| self.down1.build(input_shape) | ||
| b, h, w, c = input_shape | ||
| half_shape = (b, h // 2, w // 2, 64) | ||
| self.down2.build(half_shape) | ||
| quarter_shape = (b, h // 4, w // 4, 128) | ||
| self.down3.build(quarter_shape) | ||
|
|
||
| super().build(input_shape) | ||
|
|
||
| def call(self, x): | ||
| f1 = self.down1(x) | ||
| p1 = self.pool(f1) | ||
|
|
||
| f2 = self.down2(p1) | ||
| p2 = self.pool(f2) | ||
|
|
||
| f3 = self.down3(p2) | ||
|
|
||
| return { | ||
| "scale_1": f1, | ||
| "scale_2": f2, | ||
| "scale_3": f3, | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
keras_hub/src/models/controlnet/controlnet_backbone_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import tensorflow as tf | ||
| from keras_hub.src.models.controlnet.controlnet_backbone import ( | ||
| ControlNetBackbone, | ||
| ) | ||
|
|
||
|
|
||
| def test_controlnet_backbone_smoke(): | ||
| model = ControlNetBackbone() | ||
| x = tf.random.uniform((1, 512, 512, 1)) | ||
| outputs = model(x) | ||
| assert isinstance(outputs, dict) | ||
|
|
||
|
|
||
| def test_controlnet_backbone_required_keys(): | ||
| model = ControlNetBackbone() | ||
| x = tf.random.uniform((1, 512, 512, 1)) | ||
| outputs = model(x) | ||
|
|
||
| assert "scale_1" in outputs | ||
| assert "scale_2" in outputs | ||
| assert "scale_3" in outputs | ||
|
|
||
|
|
||
| def test_controlnet_backbone_rank(): | ||
| model = ControlNetBackbone() | ||
| x = tf.random.uniform((2, 256, 256, 1)) | ||
| outputs = model(x) | ||
|
|
||
| for v in outputs.values(): | ||
| assert len(v.shape) == 4 | ||
| assert v.shape[0] == 2 | ||
|
|
||
|
|
||
| def test_controlnet_backbone_spatial_scaling(): | ||
| model = ControlNetBackbone() | ||
| x = tf.random.uniform((1, 256, 256, 1)) | ||
| outputs = model(x) | ||
|
|
||
| assert outputs["scale_1"].shape[1:3] == (256, 256) | ||
| assert outputs["scale_2"].shape[1:3] == (128, 128) | ||
| assert outputs["scale_3"].shape[1:3] == (64, 64) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import keras | ||
| from keras import layers | ||
|
|
||
|
|
||
| class ZeroConv2D(layers.Layer): | ||
|
|
||
| def __init__(self, filters, **kwargs): | ||
| super().__init__(**kwargs) | ||
| self.filters = filters | ||
| self.conv = layers.Conv2D( | ||
| filters, | ||
| kernel_size=1, | ||
| padding="same", | ||
| kernel_initializer="zeros", | ||
| bias_initializer="zeros", | ||
| ) | ||
|
|
||
| def call(self, inputs): | ||
| return self.conv(inputs) | ||
|
|
||
| def get_config(self): | ||
| config = super().get_config() | ||
| config.update({"filters": self.filters}) | ||
| return config | ||
|
|
||
|
|
||
| class ControlInjection(layers.Layer): | ||
|
|
||
| def __init__(self, out_channels, **kwargs): | ||
| super().__init__(**kwargs) | ||
| self.out_channels = out_channels | ||
| self.projection = ZeroConv2D(out_channels) | ||
|
|
||
| def call(self, x, control): | ||
| if x.shape[1:3] != control.shape[1:3]: | ||
| raise ValueError( | ||
| f"Spatial mismatch: {x.shape[1:3]} vs {control.shape[1:3]}" | ||
| ) | ||
| control = self.projection(control) | ||
| return x + control | ||
|
|
||
| def get_config(self): | ||
| config = super().get_config() | ||
| config.update({"out_channels": self.out_channels}) | ||
| return config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import tensorflow as tf | ||
| from keras_hub.src.models.controlnet.controlnet_layers import ZeroConv2D | ||
|
|
||
|
|
||
| def test_zero_conv_output_shape(): | ||
| layer = ZeroConv2D(64) | ||
| x = tf.random.uniform((1, 128, 128, 3)) | ||
| y = layer(x) | ||
| assert y.shape == (1, 128, 128, 64) | ||
|
|
||
|
|
||
| def test_zero_conv_initial_output_is_zero(): | ||
| layer = ZeroConv2D(64) | ||
| x = tf.random.uniform((1, 64, 64, 3)) | ||
| y = layer(x) | ||
| assert tf.reduce_sum(tf.abs(y)).numpy() == 0.0 |
39 changes: 39 additions & 0 deletions
39
keras_hub/src/models/controlnet/controlnet_preprocessor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import keras | ||
| import tensorflow as tf | ||
|
|
||
|
|
||
| class ControlNetPreprocessor(keras.layers.Layer): | ||
| def __init__(self, target_size=(512, 512), **kwargs): | ||
| super().__init__(**kwargs) | ||
| self.target_size = tuple(target_size) | ||
|
|
||
| def call(self, inputs): | ||
| x = tf.convert_to_tensor(inputs) | ||
|
|
||
| if x.shape.rank != 4: | ||
| raise ValueError("Inputs must be a 4D tensor (batch, height, width, channels).") | ||
|
|
||
| x = tf.image.resize(x, self.target_size) | ||
| x = tf.cast(x, tf.float32) | ||
|
|
||
| max_val = tf.reduce_max(x) | ||
| x = tf.cond( | ||
| max_val > 1.0, | ||
| lambda: x / 255.0, | ||
| lambda: x, | ||
| ) | ||
|
|
||
| return x | ||
|
|
||
| def compute_output_shape(self, input_shape): | ||
| return ( | ||
| input_shape[0], | ||
| self.target_size[0], | ||
| self.target_size[1], | ||
| input_shape[-1], | ||
| ) | ||
|
|
||
| def get_config(self): | ||
| config = super().get_config() | ||
| config.update({"target_size": self.target_size}) | ||
| return config |
30 changes: 30 additions & 0 deletions
30
keras_hub/src/models/controlnet/controlnet_preprocessor_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import tensorflow as tf | ||
| from keras_hub.src.models.controlnet.controlnet_preprocessor import ControlNetPreprocessor | ||
|
|
||
|
|
||
| def test_controlnet_preprocessor_output_shape(): | ||
| layer = ControlNetPreprocessor(target_size=(128, 128)) | ||
|
|
||
| x = tf.random.uniform((1, 256, 256, 3), maxval=255, dtype=tf.float32) | ||
| y = layer(x) | ||
|
|
||
| assert y.shape == (1, 128, 128, 3) | ||
|
|
||
|
|
||
| def test_controlnet_preprocessor_scaling(): | ||
| layer = ControlNetPreprocessor(target_size=(64, 64)) | ||
|
|
||
| x = tf.ones((1, 128, 128, 3)) * 255.0 | ||
| y = layer(x) | ||
|
|
||
| assert tf.reduce_max(y).numpy() <= 1.0 | ||
| assert tf.reduce_min(y).numpy() >= 0.0 | ||
|
|
||
|
|
||
| def test_controlnet_preprocessor_dtype(): | ||
| layer = ControlNetPreprocessor(target_size=(64, 64)) | ||
|
|
||
| x = tf.random.uniform((1, 128, 128, 3), maxval=255, dtype=tf.float32) | ||
| y = layer(x) | ||
|
|
||
| assert y.dtype == tf.float32 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from .controlnet import ControlNet | ||
|
|
||
|
|
||
| controlnet_presets = { | ||
| "controlnet_base": { | ||
| "description": "Minimal ControlNet base configuration.", | ||
| "config": { | ||
| "image_size": 128, | ||
| "base_channels": 64, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def from_preset(preset_name): | ||
| if preset_name not in controlnet_presets: | ||
| raise ValueError(f"Unknown preset: {preset_name}") | ||
|
|
||
| config = controlnet_presets[preset_name]["config"] | ||
| return ControlNet(**config) |
15 changes: 15 additions & 0 deletions
15
keras_hub/src/models/controlnet/controlnet_presets_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import tensorflow as tf | ||
| from keras_hub.src.models.controlnet.controlnet_presets import from_preset | ||
|
|
||
|
|
||
| def test_controlnet_from_preset(): | ||
| model = from_preset("controlnet_base") | ||
|
|
||
| inputs = { | ||
| "image": tf.random.uniform((1, 128, 128, 3)), | ||
| "control": tf.random.uniform((1, 128, 128, 3)), | ||
| } | ||
|
|
||
| outputs = model(inputs) | ||
|
|
||
| assert outputs.shape == (1, 128, 128, 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import tensorflow as tf | ||
| from keras_hub.src.models.controlnet.controlnet import ControlNet | ||
|
|
||
|
|
||
| def test_controlnet_full_model_smoke(): | ||
| model = ControlNet() | ||
|
|
||
| inputs = { | ||
| "image": tf.random.uniform((1, 128, 128, 3)), | ||
| "control": tf.random.uniform((1, 128, 128, 3)), | ||
| } | ||
|
|
||
| outputs = model(inputs) | ||
|
|
||
| assert outputs.shape == (1, 128, 128, 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import keras | ||
| from .controlnet_layers import ControlInjection | ||
|
|
||
|
|
||
| class ControlNetUNet(keras.Model): | ||
|
|
||
| def __init__(self, base_channels=64, **kwargs): | ||
| super().__init__(**kwargs) | ||
|
|
||
| self.base_channels = base_channels | ||
|
|
||
| self.conv1 = keras.layers.Conv2D( | ||
| base_channels, 3, padding="same", activation="relu" | ||
| ) | ||
|
|
||
| self.inject = ControlInjection(base_channels) | ||
|
|
||
| self.conv2 = keras.layers.Conv2D( | ||
| base_channels, 3, padding="same", activation="relu" | ||
| ) | ||
|
|
||
| self.out_conv = keras.layers.Conv2D( | ||
| 3, 1, padding="same" | ||
| ) | ||
|
|
||
| def call(self, image, control_features): | ||
| if "scale_1" not in control_features: | ||
| raise ValueError("Expected 'scale_1' in control_features.") | ||
|
|
||
| x = self.conv1(image) | ||
| x = self.inject(x, control_features["scale_1"]) | ||
| x = self.conv2(x) | ||
| x = self.out_conv(x) | ||
|
|
||
| return x | ||
|
|
||
| def get_config(self): | ||
| config = super().get_config() | ||
| config.update({"base_channels": self.base_channels}) | ||
| return config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import tensorflow as tf | ||
| from keras_hub.src.models.controlnet.controlnet_unet import ControlNetUNet | ||
|
|
||
|
|
||
| def test_controlnet_unet_smoke(): | ||
| model = ControlNetUNet() | ||
|
|
||
| image = tf.random.uniform((1, 128, 128, 3)) | ||
| control_features = { | ||
| "scale_1": tf.random.uniform((1, 128, 128, 64)) | ||
| } | ||
|
|
||
| outputs = model(image, control_features) | ||
|
|
||
| assert outputs.shape == (1, 128, 128, 3) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
ControlNetBackbonedeviates significantly from the KerasHub style guide. To align with the repository's standards, the model should be refactored.Here are the key issues and how the suggestion addresses them:
keras_hub.models.backbone.Backboneinstead ofkeras.Modelto gain standard functionality likefrom_preset(). (Style Guide: line 86)__init__method, not as a subclassed model with acallmethod. This makes the model structure explicit and avoids the need for a manualbuild()method. (Style Guide: line 79)Argsand anExamplesection. (Style Guide: lines 366-371)get_config()method is required for proper serialization. (Style Guide: line 528)import tensorflow as tfshould be removed to maintain backend-agnostic code. (Style Guide: line 7)pixel_valuesas per the convention for image models. (Style Guide: line 67)@keras_hub_exportto make it part of the public API. (Style Guide: line 85)I've provided a code suggestion that refactors the entire class to follow these guidelines.
References
__init__method, rather than as a subclassed model with acallmethod. (link)keras_hub.models.Backboneto ensure they have standard features likefrom_preset. (link)ArgsandExamplesections. (link)get_config()method for serialization. (link)tensorflowand usingkeras.opsinstead. (link)pixel_valuesis the convention. (link)@keras_hub_exportto be included in the library's public API. (link)