Skip to content

[Bounding Boxes] Add Support for Bounding Box Transformations during Image Resizing #20368

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 19 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions keras/api/_tf_keras/keras/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
from keras.src.layers.preprocessing.image_preprocessing.center_crop import (
CenterCrop,
)
from keras.src.layers.preprocessing.image_preprocessing.max_num_bounding_box import (
MaxNumBoundingBoxes,
)
from keras.src.layers.preprocessing.image_preprocessing.random_brightness import (
RandomBrightness,
)
Expand Down
1 change: 1 addition & 0 deletions keras/api/_tf_keras/keras/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""

from keras.api.utils import bounding_boxes
from keras.api.utils import legacy
from keras.src.backend.common.global_state import clear_session
from keras.src.backend.common.keras_tensor import is_keras_tensor
Expand Down
21 changes: 21 additions & 0 deletions keras/api/_tf_keras/keras/utils/bounding_boxes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""DO NOT EDIT.

This file was autogenerated. Do not edit it by hand,
since your modifications would be overwritten.
"""

from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
affine_transform,
)
from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
clip_to_image_size,
)
from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
convert_format,
)
from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
crop,
)
from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
pad,
)
3 changes: 3 additions & 0 deletions keras/api/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
from keras.src.layers.preprocessing.image_preprocessing.center_crop import (
CenterCrop,
)
from keras.src.layers.preprocessing.image_preprocessing.max_num_bounding_box import (
MaxNumBoundingBoxes,
)
from keras.src.layers.preprocessing.image_preprocessing.random_brightness import (
RandomBrightness,
)
Expand Down
1 change: 1 addition & 0 deletions keras/api/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""

from keras.api.utils import bounding_boxes
from keras.api.utils import legacy
from keras.src.backend.common.global_state import clear_session
from keras.src.backend.common.keras_tensor import is_keras_tensor
Expand Down
21 changes: 21 additions & 0 deletions keras/api/utils/bounding_boxes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""DO NOT EDIT.

This file was autogenerated. Do not edit it by hand,
since your modifications would be overwritten.
"""

from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
affine_transform,
)
from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
clip_to_image_size,
)
from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
convert_format,
)
from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
crop,
)
from keras.src.layers.preprocessing.image_preprocessing.bounding_boxes.converters import (
pad,
)
3 changes: 3 additions & 0 deletions keras/src/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
from keras.src.layers.preprocessing.image_preprocessing.center_crop import (
CenterCrop,
)
from keras.src.layers.preprocessing.image_preprocessing.max_num_bounding_box import (
MaxNumBoundingBoxes,
)
from keras.src.layers.preprocessing.image_preprocessing.random_brightness import (
RandomBrightness,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ def transform_labels(self, labels, transformation, training=True):
return labels

def transform_bounding_boxes(
self, bounding_boxes, transformation, training=True
self,
bounding_boxes,
transformation,
training=True,
):
return bounding_boxes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def transform_labels(self, labels, transformation, training=True):
raise NotImplementedError()

def transform_bounding_boxes(
self, bounding_boxes, transformation, training=True
self,
bounding_boxes,
transformation,
training=True,
):
raise NotImplementedError()

Expand All @@ -88,13 +91,19 @@ def transform_single_label(self, label, transformation, training=True):
return self.backend.numpy.squeeze(outputs, axis=0)

def transform_single_bounding_box(
self, bounding_box, transformation, training=True
self,
bounding_box,
transformation,
training=True,
):
bounding_boxes = self.backend.numpy.expand_dims(bounding_box, axis=0)
bounding_boxes = self._format_single_input_bounding_box(bounding_box)
outputs = self.transform_bounding_boxes(
bounding_boxes, transformation=transformation, training=training
bounding_boxes,
transformation=transformation,
training=training,
)
return self.backend.numpy.squeeze(outputs, axis=0)
bounding_box = self._format_single_output_bounding_box(outputs)
return bounding_box

def transform_single_segmentation_mask(
self, segmentation_mask, transformation, training=True
Expand Down Expand Up @@ -144,8 +153,11 @@ def call(self, data, training=True):
"`bounding_box_format='xyxy'`."
)
bounding_boxes = densify_bounding_boxes(
data["bounding_boxes"], backend=self.backend
data["bounding_boxes"],
is_batched=is_batched,
backend=self.backend,
)

if is_batched:
data["bounding_boxes"] = self.transform_bounding_boxes(
bounding_boxes,
Expand Down Expand Up @@ -203,6 +215,32 @@ def call(self, data, training=True):
training=training,
)

def _format_single_input_bounding_box(self, bounding_box):
for key in bounding_box:
if key == "labels":
bounding_box[key] = self.backend.numpy.expand_dims(
bounding_box[key], axis=0
)
if key == "boxes":
bounding_box[key] = self.backend.numpy.expand_dims(
bounding_box[key], axis=0
)

return bounding_box

def _format_single_output_bounding_box(self, bounding_boxes):
for key in bounding_boxes:
if key == "labels":
bounding_boxes[key] = self.backend.numpy.squeeze(
bounding_boxes[key], axis=0
)
if key == "boxes":
bounding_boxes[key] = self.backend.numpy.squeeze(
bounding_boxes[key], axis=0
)

return bounding_boxes

def get_config(self):
config = super().get_config()
if self.bounding_box_format is not None:
Expand Down
Loading