Skip to content

Commit

Permalink
Two fixes for image resizing in preprocessing
Browse files Browse the repository at this point in the history
1. Properly display when are not resizing the input image in
   `model.summary()`
2. Allow setting the `image_size` directly on a preprocessing layer.

2. is just to allow a more consistent way to set the input shape
across tasks. We now have:

```python
text_classifier = keras_hub.models.TextClassifer.from_preset(
    "bert_base_en",
)
text_classifier.preprocessor.sequence_length = 256

image_classifier = keras_hub.models.TextClassifer.from_preset(
    "bert_base_en",
)
image_classifier.preprocessor.image_size = (256, 256)

multi_modal_lm = keras_hub.models.CausalLM.from_preset(
    "some_preset",
)
multi_modal_lm.preprocessor.sequence_length = 256
multi_modal_lm.preprocessor.image_size = (256, 256)
```
  • Loading branch information
mattdangerw committed Oct 17, 2024
1 parent 716ef30 commit 5742836
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions keras_hub/src/models/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ def image_converter(self):
def image_converter(self, value):
self._image_converter = value

@property
def image_size(self):
"""Shortcut to get/set the image size of the image converter."""
if self.image_converter is None:
return None
return self.image_converter.image_size

@image_size.setter
def image_size(self, value):
if self.image_converter is None:
raise ValueError(
"Cannot set `image_size` on preprocessor if `image_converter` "
" is `None`."
)
self.image_converter.image_size = value

def get_config(self):
config = super().get_config()
if self.tokenizer:
Expand Down
7 changes: 5 additions & 2 deletions keras_hub/src/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def summary(

def highlight_number(x):
if x is None:
f"[color(45)]{x}[/]"
return f"[color(45)]{x}[/]"
return f"[color(34)]{x:,}[/]" # Format number with commas.

def highlight_symbol(x):
Expand Down Expand Up @@ -339,7 +339,10 @@ def add_layer(layer, info):
add_layer(layer, info)
elif isinstance(layer, ImageConverter):
info = "Image size: "
info += highlight_shape(layer.image_size)
image_size = layer.image_size
if image_size is None:
image_size = (None, None)
info += highlight_shape(image_size)
add_layer(layer, info)
elif isinstance(layer, AudioConverter):
info = "Audio shape: "
Expand Down

0 comments on commit 5742836

Please sign in to comment.