Skip to content

Commit

Permalink
feat: fixed bbox size for online creation and bbox size randomization
Browse files Browse the repository at this point in the history
  • Loading branch information
pnsuau authored and beniz committed Mar 2, 2023
1 parent 89cbe59 commit 5cd6227
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
31 changes: 31 additions & 0 deletions data/online_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
def crop_image(
img_path,
bbox_path,
mask_random_offset,
mask_delta,
crop_delta,
mask_square,
Expand All @@ -24,6 +25,7 @@ def crop_image(
crop_coordinates=None,
select_cat=-1,
crop_center=False,
fixed_mask_size=-1,
):

margin = context_pixels * 2
Expand Down Expand Up @@ -102,6 +104,23 @@ def crop_image(
xmin -= mask_delta_x
xmax += mask_delta_x

if len(mask_random_offset) == 1:
mask_random_offset_x = mask_random_offset[0]
mask_random_offset_y = mask_random_offset[0]
elif len(mask_random_offset) == 2:
mask_random_offset_x = mask_random_offset[0]
mask_random_offset_y = mask_random_offset[1]

# from ratio to pixel gap
mask_random_offset_x = mask_random_offset_x * (xmax - xmin)
mask_random_offset_y = mask_random_offset_y * (ymax - ymin)

if mask_random_offset_x > 0 or mask_random_offset_y > 0:
ymin -= random.randint(0, mask_random_offset_y)
ymax += random.randint(0, mask_random_offset_y)
xmin -= random.randint(0, mask_random_offset_x)
xmax += random.randint(0, mask_random_offset_x)

if mask_square:
sdiff = (xmax - xmin) - (ymax - ymin)
if sdiff > 0:
Expand All @@ -111,6 +130,16 @@ def crop_image(
xmax += -int(sdiff / 2)
xmin -= -int(sdiff / 2)

if fixed_mask_size > 0:
xdiff = fixed_mask_size - (xmax - xmin)
ydiff = fixed_mask_size - (ymax - ymin)

ymax += int(ydiff / 2)
ymin -= int(ydiff / 2)

xmax += int(xdiff / 2)
xmin -= int(xdiff / 2)

xmin = max(0, xmin)
ymin = max(0, ymin)
xmax = min(xmax, img.shape[1])
Expand Down Expand Up @@ -321,6 +350,7 @@ def fill_mask_with_color(img, mask, colors):
def sanitize_paths(
paths_img,
paths_bb,
mask_random_offset,
mask_delta,
crop_delta,
mask_square,
Expand Down Expand Up @@ -357,6 +387,7 @@ def sanitize_paths(
crop_image(
path_img,
path_bb,
mask_random_offset=mask_random_offset,
mask_delta=mask_delta,
crop_delta=0,
mask_square=mask_square,
Expand Down
6 changes: 6 additions & 0 deletions data/temporal_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,29 @@ def get_img(
cur_A_img_path,
cur_A_label_path,
mask_delta=self.opt.data_online_creation_mask_delta_A,
mask_random_offset=self.opt.data_online_creation_mask_random_offset_A,
crop_delta=self.opt.data_online_creation_crop_delta_A,
mask_square=self.opt.data_online_creation_mask_square_A,
crop_dim=self.opt.data_online_creation_crop_size_A,
output_dim=self.opt.data_load_size,
context_pixels=self.opt.data_online_context_pixels,
load_size=self.opt.data_online_creation_load_size_A,
get_crop_coordinates=True,
fixed_mask_size=self.opt.data_online_fixed_mask_size,
)
cur_A_img, cur_A_label = crop_image(
cur_A_img_path,
cur_A_label_path,
mask_delta=self.opt.data_online_creation_mask_delta_A,
mask_random_offset=self.opt.data_online_creation_mask_random_offset_A,
crop_delta=self.opt.data_online_creation_crop_delta_A,
mask_square=self.opt.data_online_creation_mask_square_A,
crop_dim=self.opt.data_online_creation_crop_size_A,
output_dim=self.opt.data_load_size,
context_pixels=self.opt.data_online_context_pixels,
load_size=self.opt.data_online_creation_load_size_A,
crop_coordinates=crop_coordinates,
fixed_mask_size=self.opt.data_online_fixed_mask_size,
)

except Exception as e:
Expand Down Expand Up @@ -209,13 +213,15 @@ def get_img(
cur_B_img_path,
cur_B_label_path,
mask_delta=self.opt.data_online_creation_mask_delta_B,
mask_random_offset=self.opt.data_online_creation_mask_random_offset_B,
crop_delta=self.opt.data_online_creation_crop_delta_B,
mask_square=self.opt.data_online_creation_mask_square_B,
crop_dim=self.opt.data_online_creation_crop_size_B,
output_dim=self.opt.data_load_size,
context_pixels=self.opt.data_online_context_pixels,
load_size=self.opt.data_online_creation_load_size_B,
crop_coordinates=crop_coordinates,
fixed_mask_size=self.opt.data_online_fixed_mask_size,
)

except Exception as e:
Expand Down
9 changes: 9 additions & 0 deletions data/unaligned_labeled_mask_online_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def sanitize(self):
self.A_img_paths,
self.A_label_mask_paths,
mask_delta=self.opt.data_online_creation_mask_delta_A,
mask_random_offset=self.opt.data_online_creation_mask_random_offset_A,
crop_delta=self.opt.data_online_creation_crop_delta_A,
mask_square=self.opt.data_online_creation_mask_square_A,
crop_dim=self.opt.data_online_creation_crop_size_A,
Expand All @@ -183,6 +184,7 @@ def sanitize(self):
self.A_img_paths_val,
self.A_label_mask_paths_val,
mask_delta=self.opt.data_online_creation_mask_delta_A,
mask_random_offset=self.opt.data_online_creation_mask_random_offset_A,
crop_delta=self.opt.data_online_creation_crop_delta_A,
mask_square=self.opt.data_online_creation_mask_square_A,
crop_dim=self.opt.data_online_creation_crop_size_A,
Expand All @@ -204,6 +206,7 @@ def sanitize(self):
self.B_img_paths,
self.B_label_mask_paths,
mask_delta=self.opt.data_online_creation_mask_delta_B,
mask_random_offset=self.opt.data_online_creation_mask_random_offset_B,
crop_delta=self.opt.data_online_creation_crop_delta_B,
mask_square=self.opt.data_online_creation_mask_square_B,
crop_dim=self.opt.data_online_creation_crop_size_B,
Expand All @@ -224,6 +227,7 @@ def sanitize(self):
self.B_img_paths_val,
self.B_label_mask_paths_val,
mask_delta=self.opt.data_online_creation_mask_delta_B,
mask_random_offset=self.opt.data_online_creation_mask_random_offset_B,
crop_delta=self.opt.data_online_creation_crop_delta_B,
mask_square=self.opt.data_online_creation_mask_square_B,
crop_dim=self.opt.data_online_creation_crop_size_B,
Expand Down Expand Up @@ -252,18 +256,21 @@ def get_img(
index=None,
):
# Domain A

try:
A_img, A_label_mask = crop_image(
A_img_path,
A_label_mask_path,
mask_delta=self.opt.data_online_creation_mask_delta_A,
mask_random_offset=self.opt.data_online_creation_mask_random_offset_A,
crop_delta=self.opt.data_online_creation_crop_delta_A,
mask_square=self.opt.data_online_creation_mask_square_A,
crop_dim=self.opt.data_online_creation_crop_size_A,
output_dim=self.opt.data_load_size,
context_pixels=self.opt.data_online_context_pixels,
load_size=self.opt.data_online_creation_load_size_A,
select_cat=self.opt.data_online_select_category,
fixed_mask_size=self.opt.data_online_fixed_mask_size,
)

except Exception as e:
Expand Down Expand Up @@ -291,12 +298,14 @@ def get_img(
B_img_path,
B_label_mask_path,
mask_delta=self.opt.data_online_creation_mask_delta_B,
mask_random_offset=self.opt.data_online_creation_mask_random_offset_B,
crop_delta=self.opt.data_online_creation_crop_delta_B,
mask_square=self.opt.data_online_creation_mask_square_B,
crop_dim=self.opt.data_online_creation_crop_size_B,
output_dim=self.opt.data_load_size,
context_pixels=self.opt.data_online_context_pixels,
load_size=self.opt.data_online_creation_load_size_B,
fixed_mask_size=self.opt.data_online_fixed_mask_size,
)
B, B_label_mask = self.transform(B_img, B_label_mask)

Expand Down
27 changes: 26 additions & 1 deletion options/base_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,17 @@ def initialize(self, parser):
type=int,
default=[0],
nargs="*",
help="mask offset to allow generation of a bigger object in domain B (for semantic loss) for domain A, format : width (x) height (y) or only one size if square",
help="ratio mask offset to allow generation of a bigger object in domain B (for semantic loss) for domain A, format : width (x) height (y) or only one size if square",
)

parser.add_argument(
"--data_online_creation_mask_random_offset_A",
type=float,
default=[0.0],
nargs="*",
help="ratio mask size randomization (only to make bigger one) to robustify the image generation in domain A, format : width (x) height (y) or only one size if square",
)

parser.add_argument(
"--data_online_creation_mask_square_A",
action="store_true",
Expand Down Expand Up @@ -694,6 +703,15 @@ def initialize(self, parser):
nargs="*",
help="mask offset to allow genaration of a bigger object in domain B (for semantic loss) for domain B, format : width (y) height (x) or only one size if square",
)

parser.add_argument(
"--data_online_creation_mask_random_offset_B",
type=float,
default=[0.0],
nargs="*",
help="mask size randomization (only to make bigger one) to robustify the image generation in domain B, format : width (y) height (x) or only one size if square",
)

parser.add_argument(
"--data_online_creation_mask_square_B",
action="store_true",
Expand All @@ -706,6 +724,13 @@ def initialize(self, parser):
help="context pixel band around the crop, unused for generation, only for disc ",
)

parser.add_argument(
"--data_online_fixed_mask_size",
type=int,
default=-1,
help="if >0, it will be used as fixed bbox size (warning: in dataset resolution ie before resizing) ",
)

parser.add_argument(
"--data_sanitize_paths",
action="store_true",
Expand Down

0 comments on commit 5cd6227

Please sign in to comment.