-
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import tensorflow as tf | ||
| from keras_hub.src.models.controlnet.controlnet_backbone import ( | ||
| ControlNetBackbone, | ||
| ) | ||
|
|
||
|
|
||
| def test_controlnet_backbone_smoke(): | ||
| """Basic smoke test: model builds and runs.""" | ||
| model = ControlNetBackbone() | ||
|
|
||
| x = tf.random.uniform((1, 512, 512, 1)) | ||
| outputs = model(x) | ||
|
|
||
| assert isinstance(outputs, dict) | ||
|
|
||
|
|
||
| def test_controlnet_backbone_required_keys(): | ||
| """Ensure expected feature scales exist.""" | ||
| 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(): | ||
| """Each output should be a 4D tensor (B, H, W, C).""" | ||
| 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 | ||
|
||
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)