Skip to content
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

Fixed bug in _pad_image that did not support pad_value=(R,B,G) input #1599

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 20 additions & 4 deletions src/super_gradients/training/transforms/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Tuple
import numbers
Louis-Dupont marked this conversation as resolved.
Show resolved Hide resolved
import typing
from typing import Tuple, Union
from dataclasses import dataclass
import cv2

Expand Down Expand Up @@ -89,20 +91,34 @@ def _get_bottom_right_padding_coordinates(input_shape: Tuple[int, int], output_s
return PaddingCoordinates(top=0, bottom=pad_height, left=0, right=pad_width)


def _pad_image(image: np.ndarray, padding_coordinates: PaddingCoordinates, pad_value: int) -> np.ndarray:
def _pad_image(image: np.ndarray, padding_coordinates: PaddingCoordinates, pad_value: Union[int, Tuple[int, ...]]) -> np.ndarray:
"""Pad an image.

:param image: Image to shift. (H, W, C) or (H, W).
:param pad_h: Tuple of (padding_top, padding_bottom).
:param pad_w: Tuple of (padding_left, padding_right).
:param pad_value: Padding value
:param pad_value: Padding value. Can be a single scalar (Same value for all channels) or a tuple of values.
In the latter case, the tuple length must be equal to the number of channels.
:return: Image shifted according to padding coordinates.
"""
pad_h = (padding_coordinates.top, padding_coordinates.bottom)
pad_w = (padding_coordinates.left, padding_coordinates.right)

if len(image.shape) == 3:
return np.pad(image, (pad_h, pad_w, (0, 0)), "constant", constant_values=pad_value)
_, _, num_channels = image.shape

if isinstance(pad_value, numbers.Number):
pad_value = tuple([pad_value] * num_channels)
else:
if isinstance(pad_value, typing.Sized) and len(pad_value) != num_channels:
raise ValueError(f"A pad_value tuple ({pad_value} length should be {num_channels} for an image with {num_channels} channels")

pad_value = tuple(pad_value)

padded_channels = []
for channel_index, pad_value_channel in enumerate(pad_value):
ofrimasad marked this conversation as resolved.
Show resolved Hide resolved
padded_channels.append(np.pad(image[..., channel_index], (pad_h, pad_w), "constant", constant_values=pad_value_channel))
return np.stack(padded_channels, axis=-1)
else:
return np.pad(image, (pad_h, pad_w), "constant", constant_values=pad_value)
BloodAxe marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
31 changes: 30 additions & 1 deletion tests/unit_tests/transforms_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cv2
import matplotlib.pyplot as plt
import numpy as np
from omegaconf import ListConfig

from super_gradients.training.transforms import KeypointsMixup, KeypointsCompose
from super_gradients.training.transforms.keypoint_transforms import (
Expand Down Expand Up @@ -244,7 +245,7 @@ def test_rescale_bboxes(self):
rescaled_bboxes = _rescale_bboxes(targets=bboxes, scale_factors=(sy, sx))
np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)

def test_pad_image(self):
def test_pad_image_with_constant(self):
image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
padding_coordinates = PaddingCoordinates(top=80, bottom=80, left=60, right=60)
pad_value = 0
Expand All @@ -258,6 +259,34 @@ def test_pad_image(self):
self.assertTrue((shifted_image[:, : padding_coordinates.left, :] == pad_value).all())
self.assertTrue((shifted_image[:, -padding_coordinates.right :, :] == pad_value).all())

def test_pad_image_with_tuple(self):
image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
padding_coordinates = PaddingCoordinates(top=80, bottom=80, left=60, right=60)
pad_value = (1, 2, 3)
shifted_image = _pad_image(image, padding_coordinates, pad_value)

# Check if the shifted image has the correct shape
self.assertEqual(shifted_image.shape, (800, 600, 3))
# Check if the padding values are correct
self.assertTrue((shifted_image[: padding_coordinates.top, :, :] == pad_value).all())
self.assertTrue((shifted_image[-padding_coordinates.bottom :, :, :] == pad_value).all())
self.assertTrue((shifted_image[:, : padding_coordinates.left, :] == pad_value).all())
self.assertTrue((shifted_image[:, -padding_coordinates.right :, :] == pad_value).all())

def test_pad_image_with_listconfig(self):
image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
padding_coordinates = PaddingCoordinates(top=80, bottom=80, left=60, right=60)
pad_value = ListConfig([1, 2, 3])
shifted_image = _pad_image(image, padding_coordinates, pad_value)

# Check if the shifted image has the correct shape
self.assertEqual(shifted_image.shape, (800, 600, 3))
# Check if the padding values are correct
self.assertTrue((shifted_image[: padding_coordinates.top, :, :] == pad_value).all())
self.assertTrue((shifted_image[-padding_coordinates.bottom :, :, :] == pad_value).all())
self.assertTrue((shifted_image[:, : padding_coordinates.left, :] == pad_value).all())
self.assertTrue((shifted_image[:, -padding_coordinates.right :, :] == pad_value).all())

def test_shift_bboxes(self):
bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
shift_w, shift_h = 60, 80
Expand Down