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

Hotfix/alg 1470 drop boxes padding #1107

Merged
merged 13 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
7 changes: 2 additions & 5 deletions documentation/source/ObjectDetection.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,11 @@ MY_CLASSES = ['cat', 'dog', 'donut']
@register_dataset("MyNewDetectionDataset")
class MyNewDetectionDataset(DetectionDataset):
def __init__(self, data_dir: str, samples_dir: str, targets_dir: str, input_dim: Tuple[int, int],
transforms: List[DetectionTransform], max_targets: int = 100, max_num_samples: int = None,
transforms: List[DetectionTransform], max_num_samples: int = None,
class_inclusion_list: Optional[List[str]] = None, **kwargs):
self.sample_paths = None
self.samples_sub_directory = samples_dir
self.targets_sub_directory = targets_dir
self.max_targets = max_targets

# setting cache as False to be able to load non-resized images and crop in one of transforms
super().__init__(data_dir=data_dir, input_dim=input_dim,
Expand Down Expand Up @@ -310,9 +309,7 @@ class MyNewDetectionDataset(DetectionDataset):
lines = targets_file.read().splitlines()
target = np.array([x.strip().strip(',').split(',') for x in lines], dtype=np.float32)

res_target = np.zeros((self.max_targets, 5)) # cls, cx, cy, w, h
if len(target) != 0:
res_target[:len(target)] = target
res_target = np.array(target) if len(target) != 0 else np.zeros((0, 5)) # cls, cx, cy, w, h
annotation = {
'img_path': os.path.join(self.data_dir, sample_path),
'target': res_target,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ train_dataset_params:
prob: 0.5 # probability to apply horizontal flip
- DetectionPaddedRescale:
input_dim: ${dataset_params.train_dataset_params.input_dim}
max_targets: 120
- DetectionTargetsFormatTransform:
input_dim: ${dataset_params.train_dataset_params.input_dim}
output_format: LABEL_CXCYWH
Expand Down Expand Up @@ -65,7 +64,6 @@ val_dataset_params:
- DetectionPaddedRescale:
input_dim: ${dataset_params.val_dataset_params.input_dim}
- DetectionTargetsFormatTransform:
max_targets: 50
input_dim: ${dataset_params.val_dataset_params.input_dim}
output_format: LABEL_CXCYWH
tight_box_rotation: False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ train_dataset_params:
mean: [ 123.675, 116.28, 103.53 ]
std: [ 58.395, 57.12, 57.375 ]
- DetectionTargetsFormatTransform:
max_targets: 256
output_format: LABEL_CXCYWH

tight_box_rotation: False
Expand Down Expand Up @@ -78,7 +77,6 @@ val_dataset_params:
mean: [ 123.675, 116.28, 103.53 ]
std: [ 58.395, 57.12, 57.375 ]
- DetectionTargetsFormatTransform:
max_targets: 256
output_format: LABEL_CXCYWH
tight_box_rotation: False
class_inclusion_list:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ train_dataset_params:
prob: 0.5 # probability to apply horizontal flip
- DetectionPaddedRescale:
input_dim: ${dataset_params.train_dataset_params.input_dim}
max_targets: 120
- DetectionTargetsFormatTransform:
input_dim: ${dataset_params.train_dataset_params.input_dim}
max_targets: 50
output_format: LABEL_NORMALIZED_CXCYWH

tight_box_rotation: False
Expand Down Expand Up @@ -63,7 +61,6 @@ val_dataset_params:
input_dim: ${dataset_params.val_dataset_params.input_dim}
- DetectionTargetsFormatTransform:
input_dim: ${dataset_params.val_dataset_params.input_dim}
max_targets: 50
output_format: LABEL_NORMALIZED_CXCYWH
tight_box_rotation: False
class_inclusion_list:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ train_dataset_params:
prob: 0.5 # probability to apply horizontal flip
- DetectionPaddedRescale:
input_dim: ${dataset_params.train_dataset_params.input_dim}
max_targets: 120
- DetectionTargetsFormatTransform:
input_dim: ${dataset_params.train_dataset_params.input_dim}
output_format: LABEL_CXCYWH
Expand Down Expand Up @@ -75,7 +74,6 @@ val_dataset_params:
- DetectionPaddedRescale:
input_dim: ${dataset_params.val_dataset_params.input_dim}
- DetectionTargetsFormatTransform:
max_targets: 50
input_dim: ${dataset_params.val_dataset_params.input_dim}
output_format: LABEL_CXCYWH
class_inclusion_list:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ train_dataset_params:
flip_prob: 0.5 # probability to apply horizontal flip
- DetectionPaddedRescale:
input_dim: ${dataset_params.train_dataset_params.input_dim}
max_targets: 120
pad_value: 114
- DetectionStandardize:
max_value: 255.
- DetectionTargetsFormatTransform:
max_targets: 256
output_format: LABEL_CXCYWH

tight_box_rotation: False
Expand Down Expand Up @@ -71,7 +69,6 @@ val_dataset_params:
max_value: 255.
- DetectionImagePermute
- DetectionTargetsFormatTransform:
max_targets: 50
input_dim: [640, 640]
output_format: LABEL_CXCYWH
tight_box_rotation: False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ train_dataset_params:
- DetectionPaddedRescale:
input_dim: ${dataset_params.train_dataset_params.input_dim}
- DetectionTargetsFormatTransform:
max_targets: 50
input_dim: ${dataset_params.train_dataset_params.input_dim}
output_format: LABEL_CXCYWH
class_inclusion_list:
Expand All @@ -23,7 +22,6 @@ val_dataset_params:
- DetectionPaddedRescale:
input_dim: ${dataset_params.train_dataset_params.input_dim}
- DetectionTargetsFormatTransform:
max_targets: 50
input_dim: ${dataset_params.train_dataset_params.input_dim}
output_format: LABEL_CXCYWH
images_sub_directory: images/test2007/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ train_dataset_params:
prob: 0.5 # probability to apply horizontal flip
- DetectionPaddedRescale:
input_dim: ${dataset_params.train_dataset_params.input_dim}
max_targets: 300
- DetectionStandardize:
max_value: 255.
- DetectionTargetsFormatTransform:
max_targets: 300
input_dim: ${dataset_params.train_dataset_params.input_dim}
output_format: LABEL_CXCYWH
tight_box_rotation: False
Expand Down Expand Up @@ -76,12 +74,10 @@ val_dataset_params:
transforms:
- DetectionPaddedRescale:
input_dim: ${dataset_params.val_dataset_params.input_dim}
max_targets: 300
pad_value: 114
- DetectionStandardize:
max_value: 255.
- DetectionTargetsFormatTransform:
max_targets: 300
input_dim: ${dataset_params.val_dataset_params.input_dim}
output_format: LABEL_CXCYWH
tight_box_rotation: False
Expand Down
45 changes: 23 additions & 22 deletions src/super_gradients/training/transforms/transforms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections
import math
import random
import warnings
from numbers import Number
from typing import Optional, Union, Tuple, List, Sequence, Dict

Expand Down Expand Up @@ -776,18 +777,20 @@ class DetectionPaddedRescale(DetectionTransform):
Preprocessing transform to be applied last of all transforms for validation.

Image- Rescales and pads to self.input_dim.
Targets- pads targets to max_targets, moves the class label to first index, converts boxes format- xyxy -> cxcywh.
Targets- moves the class label to first index, converts boxes format- xyxy -> cxcywh.

:param input_dim: Final input dimension (default=(640,640))
:param swap: Image axis's to be rearranged.
:param max_targets:
:param pad_value: Padding value for image.
"""

def __init__(self, input_dim: Union[int, Tuple[int, int], None], swap: Tuple[int, ...] = (2, 0, 1), max_targets: int = 50, pad_value: int = 114):
def __init__(
self, input_dim: Union[int, Tuple[int, int], None], swap: Tuple[int, ...] = (2, 0, 1), max_targets: Optional[int] = None, pad_value: int = 114
):
super().__init__()
_max_targets_deprication(max_targets)
self.swap = swap
self.input_dim = ensure_is_tuple_of_two(input_dim)
self.max_targets = max_targets
self.pad_value = pad_value

def __call__(self, sample: dict) -> dict:
Expand All @@ -814,20 +817,18 @@ class DetectionHorizontalFlip(DetectionTransform):
Horizontal Flip for Detection

:param prob: Probability of applying horizontal flip
:param max_targets: Max objects in single image, padding target to this size in case of empty image.
"""

def __init__(self, prob: float, max_targets: int = 120):
def __init__(self, prob: float, max_targets: Optional[int] = None):
super(DetectionHorizontalFlip, self).__init__()
_max_targets_deprication(max_targets)
self.prob = prob
self.max_targets = max_targets

def __call__(self, sample):
image, targets = sample["image"], sample["target"]
if len(targets) == 0:
targets = np.zeros((0, 5), dtype=np.float32)
boxes = targets[:, :4]
if len(boxes) == 0:
targets = np.zeros((self.max_targets, 5), dtype=np.float32)
boxes = targets[:, :4]
image, boxes = _mirror(image, boxes, self.prob)
targets[:, :4] = boxes
sample["target"] = targets
Expand Down Expand Up @@ -1012,7 +1013,6 @@ class DetectionTargetsFormatTransform(DetectionTransform):
:param input_format: Format of the input targets. For instance [xmin, ymin, xmax, ymax, cls_id] refers to XYXY_LABEL.
:param output_format: Format of the output targets. For instance [xmin, ymin, xmax, ymax, cls_id] refers to XYXY_LABEL
:param min_bbox_edge_size: bboxes with edge size lower then this values will be removed.
:param max_targets: Max objects in single image, padding target to this size.
"""

@resolve_param("input_format", ConcatenatedTensorFormatFactory())
Expand All @@ -1023,9 +1023,10 @@ def __init__(
input_format: ConcatenatedTensorFormat = XYXY_LABEL,
output_format: ConcatenatedTensorFormat = LABEL_CXCYWH,
min_bbox_edge_size: float = 1,
max_targets: int = 120,
max_targets: Optional[int] = None,
):
super(DetectionTargetsFormatTransform, self).__init__()
_max_targets_deprication(max_targets)
if isinstance(input_format, DetectionTargetsFormat) or isinstance(output_format, DetectionTargetsFormat):
raise TypeError(
"DetectionTargetsFormat is not supported for input_format and output_format starting from super_gradients==3.0.7.\n"
Expand All @@ -1035,7 +1036,6 @@ def __init__(
)
self.input_format = input_format
self.output_format = output_format
self.max_targets = max_targets
self.min_bbox_edge_size = min_bbox_edge_size
self.input_dim = None

Expand Down Expand Up @@ -1066,8 +1066,7 @@ def apply_on_targets(self, targets: np.ndarray) -> np.ndarray:
"""Convert targets in input_format to output_format, filter small bboxes and pad targets"""
targets = self.targets_format_converter(targets)
targets = self.filter_small_bboxes(targets)
targets = self.pad_targets(targets)
return targets
return np.ascontiguousarray(targets, dtype=np.float32)

def filter_small_bboxes(self, targets: np.ndarray) -> np.ndarray:
"""Filter bboxes smaller than specified threshold."""
Expand All @@ -1078,13 +1077,6 @@ def _is_big_enough(bboxes: np.ndarray) -> np.ndarray:
targets = filter_on_bboxes(fn=_is_big_enough, tensor=targets, tensor_format=self.output_format)
return targets

def pad_targets(self, targets: np.ndarray) -> np.ndarray:
"""Pad targets."""
padded_targets = np.zeros((self.max_targets, targets.shape[-1]))
padded_targets[range(len(targets))[: self.max_targets]] = targets[: self.max_targets]
padded_targets = np.ascontiguousarray(padded_targets, dtype=np.float32)
return padded_targets

def get_equivalent_preprocessing(self) -> List:
return []

Expand Down Expand Up @@ -1331,3 +1323,12 @@ def __init__(self, max_val=255.0):

def forward(self, img):
return img / self.max_val


def _max_targets_deprication(max_targets: Optional[int] = None):
if max_targets is not None:
warnings.warn(
"max_targets is deprecated and will be removed in the future, targets are not padded to the max length anymore. "
"If you are using collate_fn provided by SG, it is safe to simply drop this argument.",
DeprecationWarning,
)
Loading