Skip to content

Commit

Permalink
spatial_size -> size
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeier committed Jul 5, 2023
1 parent 9b30731 commit 38b589e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 60 deletions.
24 changes: 11 additions & 13 deletions test/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def load(self, device="cpu"):


# new v2 default
DEFAULT_SPATIAL_SIZE = (17, 11)
DEFAULT_SIZE = (17, 11)
# old v2 defaults
DEFAULT_SQUARE_SPATIAL_SIZE = 15
DEFAULT_LANDSCAPE_SPATIAL_SIZE = (7, 33)
Expand Down Expand Up @@ -495,7 +495,7 @@ def get_num_channels(color_space):


def make_image(
spatial_size=DEFAULT_SPATIAL_SIZE,
size=DEFAULT_SIZE,
*,
color_space="RGB",
batch_dims=(),
Expand All @@ -505,7 +505,7 @@ def make_image(
):
max_value = get_max_value(dtype)
data = torch.testing.make_tensor(
(*batch_dims, get_num_channels(color_space), *spatial_size),
(*batch_dims, get_num_channels(color_space), *size),
low=0,
high=max_value,
dtype=dtype or torch.uint8,
Expand Down Expand Up @@ -635,7 +635,7 @@ def sample_position(values, max_value):

if spatial_size is None:
if size is None:
spatial_size = DEFAULT_SPATIAL_SIZE
spatial_size = DEFAULT_SIZE
else:
height, width = size
height_margin, width_margin = torch.randint(10, (2,)).tolist()
Expand Down Expand Up @@ -713,11 +713,11 @@ class MaskLoader(TensorLoader):
pass


def make_detection_mask(spatial_size=DEFAULT_SPATIAL_SIZE, *, num_objects=5, batch_dims=(), dtype=None, device="cpu"):
def make_detection_mask(size=DEFAULT_SIZE, *, num_objects=5, batch_dims=(), dtype=None, device="cpu"):
"""Make a "detection" mask, i.e. (*, N, H, W), where each object is encoded as one of N boolean masks"""
return datapoints.Mask(
torch.testing.make_tensor(
(*batch_dims, num_objects, *spatial_size),
(*batch_dims, num_objects, *size),
low=0,
high=2,
dtype=dtype or torch.bool,
Expand Down Expand Up @@ -752,13 +752,11 @@ def make_detection_mask_loaders(
make_detection_masks = from_loaders(make_detection_mask_loaders)


def make_segmentation_mask(
spatial_size=DEFAULT_SPATIAL_SIZE, *, num_categories=10, batch_dims=(), dtype=None, device="cpu"
):
def make_segmentation_mask(size=DEFAULT_SIZE, *, num_categories=10, batch_dims=(), dtype=None, device="cpu"):
"""Make a "segmentation" mask, i.e. (*, H, W), where the category is encoded as pixel value"""
return datapoints.Mask(
torch.testing.make_tensor(
(*batch_dims, *spatial_size),
(*batch_dims, *size),
low=0,
high=num_categories,
dtype=dtype or torch.uint8,
Expand Down Expand Up @@ -817,8 +815,8 @@ class VideoLoader(ImageLoader):
pass


def make_video(spatial_size=DEFAULT_SPATIAL_SIZE, *, num_frames=3, batch_dims=(), **kwargs):
return datapoints.Video(make_image(spatial_size, batch_dims=(*batch_dims, num_frames), **kwargs))
def make_video(size=DEFAULT_SIZE, *, num_frames=3, batch_dims=(), **kwargs):
return datapoints.Video(make_image(size, batch_dims=(*batch_dims, num_frames), **kwargs))


def make_video_loader(
Expand All @@ -836,8 +834,8 @@ def fn(shape, dtype, device, memory_format):
return make_video(
(height, width),
num_frames=num_frames,
color_space=color_space,
batch_dims=batch_dims,
color_space=color_space,
dtype=dtype,
device=device,
memory_format=memory_format,
Expand Down
16 changes: 8 additions & 8 deletions test/test_prototype_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test__get_params(self, mocker):
transform = transforms.FixedSizeCrop(size=crop_size)

flat_inputs = [
make_image(spatial_size=spatial_size, color_space="RGB"),
make_image(size=spatial_size, color_space="RGB"),
make_bounding_box(format=BoundingBoxFormat.XYXY, spatial_size=spatial_size, batch_dims=batch_shape),
]
params = transform._get_params(flat_inputs)
Expand Down Expand Up @@ -315,7 +315,7 @@ def test__transform_culling(self, mocker):
bounding_boxes = make_bounding_box(
format=BoundingBoxFormat.XYXY, spatial_size=spatial_size, batch_dims=(batch_size,)
)
masks = make_detection_mask(spatial_size=spatial_size, batch_dims=(batch_size,))
masks = make_detection_mask(size=spatial_size, batch_dims=(batch_size,))
labels = make_label(extra_dims=(batch_size,))

transform = transforms.FixedSizeCrop((-1, -1))
Expand Down Expand Up @@ -495,29 +495,29 @@ def make_datapoints():
size = (600, 800)
num_objects = 22

pil_image = to_image_pil(make_image(spatial_size=size, color_space="RGB"))
pil_image = to_image_pil(make_image(size=size, color_space="RGB"))
target = {
"boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float),
"labels": make_label(extra_dims=(num_objects,), categories=80),
"masks": make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long),
"masks": make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long),
}

yield (pil_image, target)

tensor_image = torch.Tensor(make_image(spatial_size=size, color_space="RGB"))
tensor_image = torch.Tensor(make_image(size=size, color_space="RGB"))
target = {
"boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float),
"labels": make_label(extra_dims=(num_objects,), categories=80),
"masks": make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long),
"masks": make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long),
}

yield (tensor_image, target)

datapoint_image = make_image(spatial_size=size, color_space="RGB")
datapoint_image = make_image(size=size, color_space="RGB")
target = {
"boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float),
"labels": make_label(extra_dims=(num_objects,), categories=80),
"masks": make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long),
"masks": make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long),
}

yield (datapoint_image, target)
Expand Down
18 changes: 9 additions & 9 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ class TestSmoke:
@pytest.mark.parametrize(
"image_or_video",
[
make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE),
make_video(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE),
make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE),
make_video(size=DEFAULT_PORTRAIT_SPATIAL_SIZE),
next(make_pil_images(color_spaces=["RGB"])),
next(make_vanilla_tensor_images()),
],
Expand All @@ -179,8 +179,8 @@ def test_common(self, transform, adapter, container_type, image_or_video, device
spatial_size = F.get_spatial_size(image_or_video)
input = dict(
image_or_video=image_or_video,
image_datapoint=make_image(spatial_size=spatial_size),
video_datapoint=make_video(spatial_size=spatial_size),
image_datapoint=make_image(size=spatial_size),
video_datapoint=make_video(size=spatial_size),
image_pil=next(make_pil_images(sizes=[spatial_size], color_spaces=["RGB"])),
bounding_box_xyxy=make_bounding_box(
format=datapoints.BoundingBoxFormat.XYXY, spatial_size=spatial_size, batch_dims=(3,)
Expand Down Expand Up @@ -227,8 +227,8 @@ def test_common(self, transform, adapter, container_type, image_or_video, device
format=datapoints.BoundingBoxFormat.CXCYWH,
spatial_size=spatial_size,
),
detection_mask=make_detection_mask(spatial_size=spatial_size),
segmentation_mask=make_segmentation_mask(spatial_size=spatial_size),
detection_mask=make_detection_mask(size=spatial_size),
segmentation_mask=make_segmentation_mask(size=spatial_size),
int=0,
float=0.0,
bool=True,
Expand Down Expand Up @@ -353,7 +353,7 @@ def test_random_resized_crop(self, transform, input):
next(make_vanilla_tensor_images()),
next(make_vanilla_tensor_images()),
next(make_pil_images()),
make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE),
make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE),
next(make_videos()),
],
3,
Expand Down Expand Up @@ -1347,8 +1347,8 @@ class TestToDtype:
)
def test_call(self, dtype, expected_dtypes):
sample = dict(
video=make_video(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE, dtype=torch.int64),
image=make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE, dtype=torch.uint8),
video=make_video(size=DEFAULT_PORTRAIT_SPATIAL_SIZE, dtype=torch.int64),
image=make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE, dtype=torch.uint8),
bounding_box=make_bounding_box(format=datapoints.BoundingBoxFormat.XYXY, dtype=torch.float32),
str="str",
int=0,
Expand Down
22 changes: 11 additions & 11 deletions test/test_transforms_v2_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,11 +711,11 @@ def test_call_consistency(config, args_kwargs):
for transform_cls, get_params_args_kwargs in [
(
v2_transforms.RandomResizedCrop,
ArgsKwargs(make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE), scale=[0.3, 0.7], ratio=[0.5, 1.5]),
ArgsKwargs(make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE), scale=[0.3, 0.7], ratio=[0.5, 1.5]),
),
(
v2_transforms.RandomErasing,
ArgsKwargs(make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE), scale=(0.3, 0.7), ratio=(0.5, 1.5)),
ArgsKwargs(make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE), scale=(0.3, 0.7), ratio=(0.5, 1.5)),
),
(v2_transforms.ColorJitter, ArgsKwargs(brightness=None, contrast=None, saturation=None, hue=None)),
(v2_transforms.ElasticTransform, ArgsKwargs(alpha=[15.3, 27.2], sigma=[2.5, 3.9], size=[17, 31])),
Expand All @@ -724,7 +724,7 @@ def test_call_consistency(config, args_kwargs):
v2_transforms.RandomAffine,
ArgsKwargs(degrees=[-20.0, 10.0], translate=None, scale_ranges=None, shears=None, img_size=[15, 29]),
),
(v2_transforms.RandomCrop, ArgsKwargs(make_image(spatial_size=(61, 47)), output_size=(19, 25))),
(v2_transforms.RandomCrop, ArgsKwargs(make_image(size=(61, 47)), output_size=(19, 25))),
(v2_transforms.RandomPerspective, ArgsKwargs(23, 17, 0.5)),
(v2_transforms.RandomRotation, ArgsKwargs(degrees=[-20.0, 10.0])),
(v2_transforms.AutoAugment, ArgsKwargs(5)),
Expand Down Expand Up @@ -1095,33 +1095,33 @@ def make_datapoints(self, with_mask=True):
def make_label(extra_dims, categories):
return torch.randint(categories, extra_dims, dtype=torch.int64)

pil_image = to_image_pil(make_image(spatial_size=size, color_space="RGB"))
pil_image = to_image_pil(make_image(size=size, color_space="RGB"))
target = {
"boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float),
"labels": make_label(extra_dims=(num_objects,), categories=80),
}
if with_mask:
target["masks"] = make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long)
target["masks"] = make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long)

yield (pil_image, target)

tensor_image = torch.Tensor(make_image(spatial_size=size, color_space="RGB", dtype=torch.float32))
tensor_image = torch.Tensor(make_image(size=size, color_space="RGB", dtype=torch.float32))
target = {
"boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float),
"labels": make_label(extra_dims=(num_objects,), categories=80),
}
if with_mask:
target["masks"] = make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long)
target["masks"] = make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long)

yield (tensor_image, target)

datapoint_image = make_image(spatial_size=size, color_space="RGB", dtype=torch.float32)
datapoint_image = make_image(size=size, color_space="RGB", dtype=torch.float32)
target = {
"boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float),
"labels": make_label(extra_dims=(num_objects,), categories=80),
}
if with_mask:
target["masks"] = make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long)
target["masks"] = make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long)

yield (datapoint_image, target)

Expand Down Expand Up @@ -1203,8 +1203,8 @@ def make_datapoints(self, supports_pil=True, image_dtype=torch.uint8):
conv_fns.extend([torch.Tensor, lambda x: x])

for conv_fn in conv_fns:
datapoint_image = make_image(spatial_size=size, color_space="RGB", dtype=image_dtype)
datapoint_mask = make_segmentation_mask(spatial_size=size, num_categories=num_categories, dtype=torch.uint8)
datapoint_image = make_image(size=size, color_space="RGB", dtype=image_dtype)
datapoint_mask = make_segmentation_mask(size=size, num_categories=num_categories, dtype=torch.uint8)

dp = (conv_fn(datapoint_image), datapoint_mask)
dp_ref = (
Expand Down
Loading

0 comments on commit 38b589e

Please sign in to comment.