Unverified Commit e50c2e36 authored by vfdev's avatar vfdev Committed by GitHub
Browse files

Improved docs and tests for (#2371)

- RandomCrop: crop with padding using all commonly supported modes
parent 44806038
...@@ -26,24 +26,29 @@ class Tester(unittest.TestCase): ...@@ -26,24 +26,29 @@ class Tester(unittest.TestCase):
transformed_pil_img = getattr(F, func)(pil_img, **fn_kwargs) transformed_pil_img = getattr(F, func)(pil_img, **fn_kwargs)
self.compareTensorToPIL(transformed_tensor, transformed_pil_img) self.compareTensorToPIL(transformed_tensor, transformed_pil_img)
def _test_geom_op(self, func, method, fn_kwargs=None, meth_kwargs=None): def _test_class_geom_op(self, method, meth_kwargs=None):
if fn_kwargs is None:
fn_kwargs = {}
if meth_kwargs is None: if meth_kwargs is None:
meth_kwargs = {} meth_kwargs = {}
tensor, pil_img = self._create_data(height=10, width=10) tensor, pil_img = self._create_data(height=10, width=10)
transformed_tensor = getattr(F, func)(tensor, **fn_kwargs) # test for class interface
transformed_pil_img = getattr(F, func)(pil_img, **fn_kwargs) f = getattr(T, method)(**meth_kwargs)
scripted_fn = torch.jit.script(f)
# set seed to reproduce the same transformation for tensor and PIL image
torch.manual_seed(12)
transformed_tensor = f(tensor)
torch.manual_seed(12)
transformed_pil_img = f(pil_img)
self.compareTensorToPIL(transformed_tensor, transformed_pil_img) self.compareTensorToPIL(transformed_tensor, transformed_pil_img)
scripted_fn = torch.jit.script(getattr(F, func)) torch.manual_seed(12)
transformed_tensor_script = scripted_fn(tensor, **fn_kwargs) transformed_tensor_script = scripted_fn(tensor)
self.assertTrue(transformed_tensor.equal(transformed_tensor_script)) self.assertTrue(transformed_tensor.equal(transformed_tensor_script))
# test for class interface def _test_geom_op(self, func, method, fn_kwargs=None, meth_kwargs=None):
f = getattr(T, method)(**meth_kwargs) self._test_functional_geom_op(func, fn_kwargs)
scripted_fn = torch.jit.script(f) self._test_class_geom_op(method, meth_kwargs)
scripted_fn(tensor)
def test_random_horizontal_flip(self): def test_random_horizontal_flip(self):
self._test_geom_op('hflip', 'RandomHorizontalFlip') self._test_geom_op('hflip', 'RandomHorizontalFlip')
...@@ -107,21 +112,20 @@ class Tester(unittest.TestCase): ...@@ -107,21 +112,20 @@ class Tester(unittest.TestCase):
'crop', 'RandomCrop', fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs 'crop', 'RandomCrop', fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs
) )
tensor = torch.randint(0, 255, (3, 10, 10), dtype=torch.uint8) sizes = [5, [5, ], [6, 6]]
# Test torchscript of transforms.RandomCrop with size as int padding_configs = [
f = T.RandomCrop(size=5) {"padding_mode": "constant", "fill": 0},
scripted_fn = torch.jit.script(f) {"padding_mode": "constant", "fill": 10},
scripted_fn(tensor) {"padding_mode": "constant", "fill": 20},
{"padding_mode": "edge"},
# Test torchscript of transforms.RandomCrop with size as [int, ] {"padding_mode": "reflect"},
f = T.RandomCrop(size=[5, ], padding=[2, ]) ]
scripted_fn = torch.jit.script(f)
scripted_fn(tensor) for size in sizes:
for padding_config in padding_configs:
# Test torchscript of transforms.RandomCrop with size as list config = dict(padding_config)
f = T.RandomCrop(size=[6, 6]) config["size"] = size
scripted_fn = torch.jit.script(f) self._test_class_geom_op("RandomCrop", config)
scripted_fn(tensor)
def test_center_crop(self): def test_center_crop(self):
fn_kwargs = {"output_size": (4, 5)} fn_kwargs = {"output_size": (4, 5)}
......
...@@ -371,7 +371,7 @@ def pad(img: Tensor, padding: List[int], fill: int = 0, padding_mode: str = "con ...@@ -371,7 +371,7 @@ def pad(img: Tensor, padding: List[int], fill: int = 0, padding_mode: str = "con
length 3, it is used to fill R, G, B channels respectively. length 3, it is used to fill R, G, B channels respectively.
This value is only used when the padding_mode is constant. Only int value is supported for Tensors. This value is only used when the padding_mode is constant. Only int value is supported for Tensors.
padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant. padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
Only "constant" is supported for Tensors as of now. Mode symmetric is not yet supported for Tensor inputs.
- constant: pads with a constant value, this value is specified with fill - constant: pads with a constant value, this value is specified with fill
......
...@@ -368,7 +368,8 @@ def pad(img: Tensor, padding: List[int], fill: int = 0, padding_mode: str = "con ...@@ -368,7 +368,8 @@ def pad(img: Tensor, padding: List[int], fill: int = 0, padding_mode: str = "con
list of length 1: ``[padding, ]``. list of length 1: ``[padding, ]``.
fill (int): Pixel fill value for constant fill. Default is 0. fill (int): Pixel fill value for constant fill. Default is 0.
This value is only used when the padding_mode is constant This value is only used when the padding_mode is constant
padding_mode (str): Type of padding. Only "constant" is supported for Tensors as of now. padding_mode (str): Type of padding. Should be: constant, edge or reflect. Default is constant.
Mode symmetric is not yet supported for Tensor inputs.
- constant: pads with a constant value, this value is specified with fill - constant: pads with a constant value, this value is specified with fill
......
...@@ -305,7 +305,7 @@ class Pad(torch.nn.Module): ...@@ -305,7 +305,7 @@ class Pad(torch.nn.Module):
length 3, it is used to fill R, G, B channels respectively. length 3, it is used to fill R, G, B channels respectively.
This value is only used when the padding_mode is constant This value is only used when the padding_mode is constant
padding_mode (str): Type of padding. Should be: constant, edge, reflect or symmetric. padding_mode (str): Type of padding. Should be: constant, edge, reflect or symmetric.
Default is constant. Only "constant" is supported for Tensors as of now. Default is constant. Mode symmetric is not yet supported for Tensor inputs.
- constant: pads with a constant value, this value is specified with fill - constant: pads with a constant value, this value is specified with fill
...@@ -469,6 +469,7 @@ class RandomCrop(torch.nn.Module): ...@@ -469,6 +469,7 @@ class RandomCrop(torch.nn.Module):
length 3, it is used to fill R, G, B channels respectively. length 3, it is used to fill R, G, B channels respectively.
This value is only used when the padding_mode is constant This value is only used when the padding_mode is constant
padding_mode (str): Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant. padding_mode (str): Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
Mode symmetric is not yet supported for Tensor inputs.
- constant: pads with a constant value, this value is specified with fill - constant: pads with a constant value, this value is specified with fill
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment