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

Add jitter box #329

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[127.0, 127.0, 127.0], [128.0, 128.0, 128.0]]
val:
name: CocoDataset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[127.0, 127.0, 127.0], [128.0, 128.0, 128.0]]
val:
name: CocoDataset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[127.0, 127.0, 127.0], [128.0, 128.0, 128.0]]
val:
name: CocoDataset
Expand Down
1 change: 1 addition & 0 deletions config/RepVGG/nanodet-RepVGG-A0_416.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[103.53, 116.28, 123.675], [57.375, 57.12, 58.395]]
val:
name: CocoDataset
Expand Down
1 change: 1 addition & 0 deletions config/nanodet-g.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[103.53, 116.28, 123.675], [57.375, 57.12, 58.395]]
val:
name: CocoDataset
Expand Down
1 change: 1 addition & 0 deletions config/nanodet-m-0.5x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[103.53, 116.28, 123.675], [57.375, 57.12, 58.395]]
val:
name: CocoDataset
Expand Down
1 change: 1 addition & 0 deletions config/nanodet-m-1.5x-416.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[103.53, 116.28, 123.675], [57.375, 57.12, 58.395]]
val:
name: CocoDataset
Expand Down
1 change: 1 addition & 0 deletions config/nanodet-m-1.5x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[103.53, 116.28, 123.675], [57.375, 57.12, 58.395]]
val:
name: CocoDataset
Expand Down
1 change: 1 addition & 0 deletions config/nanodet-m-416.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[103.53, 116.28, 123.675], [57.375, 57.12, 58.395]]
val:
name: CocoDataset
Expand Down
1 change: 1 addition & 0 deletions config/nanodet-m.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ data:
brightness: 0.2
contrast: [0.6, 1.4]
saturation: [0.5, 1.2]
jitter_box: 0.0
normalize: [[103.53, 116.28, 123.675], [57.375, 57.12, 58.395]]
val:
name: CocoDataset
Expand Down
1 change: 1 addition & 0 deletions config/nanodet_custom_xml_dataset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ data:
brightness: 0.2
contrast: [0.8, 1.2]
saturation: [0.8, 1.2]
jitter_box: 0.0
normalize: [[103.53, 116.28, 123.675], [57.375, 57.12, 58.395]]
val:
name: XMLDataset
Expand Down
28 changes: 28 additions & 0 deletions nanodet/data/transform/warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ def get_translate_matrix(translate, width, height):
return T


def get_jitter_boxes(boxes, ratio=0.0):
"""
:param boxes:
:param ratio: adjust each box boundary independently
:return:
"""
x_min, y_min, x_max, y_max = (boxes[:, i] for i in range(4))
width = x_max - x_min
height = y_max - y_min
y_center = y_min + height / 2.0
x_center = x_min + width / 2.0

distortion = 1.0 + np.random.uniform(-ratio, ratio, boxes.shape)
y_min_jitter = height * distortion[:, 0]
x_min_jitter = width * distortion[:, 1]
y_max_jitter = height * distortion[:, 2]
x_max_jitter = width * distortion[:, 3]

y_min, y_max = y_center - (y_min_jitter / 2.0), y_center + (y_max_jitter / 2.0)
x_min, x_max = x_center - (x_min_jitter / 2.0), x_center + (x_max_jitter / 2.0)
jitter_boxes = np.vstack((x_min, y_min, x_max, y_max)).T
return jitter_boxes


def get_resize_matrix(raw_shape, dst_shape, keep_ratio):
"""
Get resize matrix for resizing raw img to input size
Expand Down Expand Up @@ -274,6 +298,7 @@ class ShapeTransform:
shear: Random shear degree.
translate: Random translate ratio.
flip: Random flip probability.
jitter_box: Random adjust box width and height.
"""

def __init__(
Expand All @@ -287,6 +312,7 @@ def __init__(
shear: float = 0.0,
translate: float = 0.0,
flip: float = 0.0,
jitter_box: float = 0.0,
**kwargs
):
self.keep_ratio = keep_ratio
Expand All @@ -298,6 +324,7 @@ def __init__(
self.shear_degree = shear
self.flip_prob = flip
self.translate_ratio = translate
self.jitter_box_ratio = jitter_box

def __call__(self, meta_data, dst_shape):
raw_img = meta_data["img"]
Expand Down Expand Up @@ -342,6 +369,7 @@ def __call__(self, meta_data, dst_shape):
meta_data["warp_matrix"] = M
if "gt_bboxes" in meta_data:
boxes = meta_data["gt_bboxes"]
boxes = get_jitter_boxes(boxes, self.jitter_box_ratio)
meta_data["gt_bboxes"] = warp_boxes(boxes, M, dst_shape[0], dst_shape[1])
if "gt_masks" in meta_data:
for i, mask in enumerate(meta_data["gt_masks"]):
Expand Down