From 523b48cb4cf8bdf882779e213ada99c0a8ad8e49 Mon Sep 17 00:00:00 2001 From: Alykhan Tejani Date: Thu, 14 Sep 2017 23:45:29 +0100 Subject: [PATCH 1/3] make size param of CenterCrop and RandomCrop (w,h) not (h,w) --- test/test_transforms.py | 10 +++++----- torchvision/transforms.py | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 952d411d609..ad2f3340172 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -28,7 +28,7 @@ def test_crop(self): imgnarrow.fill_(0) result = transforms.Compose([ transforms.ToPILImage(), - transforms.CenterCrop((oheight, owidth)), + transforms.CenterCrop((owidth, oheight)), transforms.ToTensor(), ])(img) assert result.sum() == 0, "height: " + str(height) + " width: " \ @@ -37,7 +37,7 @@ def test_crop(self): owidth += 1 result = transforms.Compose([ transforms.ToPILImage(), - transforms.CenterCrop((oheight, owidth)), + transforms.CenterCrop((owidth, oheight)), transforms.ToTensor(), ])(img) sum1 = result.sum() @@ -47,7 +47,7 @@ def test_crop(self): owidth += 1 result = transforms.Compose([ transforms.ToPILImage(), - transforms.CenterCrop((oheight, owidth)), + transforms.CenterCrop((owidth, oheight)), transforms.ToTensor(), ])(img) sum2 = result.sum() @@ -108,7 +108,7 @@ def test_random_crop(self): img = torch.ones(3, height, width) result = transforms.Compose([ transforms.ToPILImage(), - transforms.RandomCrop((oheight, owidth)), + transforms.RandomCrop((owidth, oheight)), transforms.ToTensor(), ])(img) assert result.size(1) == oheight @@ -117,7 +117,7 @@ def test_random_crop(self): padding = random.randint(1, 20) result = transforms.Compose([ transforms.ToPILImage(), - transforms.RandomCrop((oheight, owidth), padding=padding), + transforms.RandomCrop((owidth, oheight), padding=padding), transforms.ToTensor(), ])(img) assert result.size(1) == oheight diff --git a/torchvision/transforms.py b/torchvision/transforms.py index 85f0a82c8c8..e3fd9d4a770 100644 --- a/torchvision/transforms.py +++ b/torchvision/transforms.py @@ -207,7 +207,7 @@ class CenterCrop(object): Args: size (sequence or int): Desired output size of the crop. If size is an - int instead of sequence like (h, w), a square crop (size, size) is + int instead of sequence like (w, h), a square crop (size, size) is made. """ @@ -226,7 +226,7 @@ def __call__(self, img): PIL.Image: Cropped image. """ w, h = img.size - th, tw = self.size + tw, th = self.size x1 = int(round((w - tw) / 2.)) y1 = int(round((h - th) / 2.)) return img.crop((x1, y1, x1 + tw, y1 + th)) @@ -286,7 +286,7 @@ class RandomCrop(object): Args: size (sequence or int): Desired output size of the crop. If size is an - int instead of sequence like (h, w), a square crop (size, size) is + int instead of sequence like (w, h), a square crop (size, size) is made. padding (int or sequence, optional): Optional padding on each border of the image. Default is 0, i.e no padding. If a sequence of length @@ -313,7 +313,7 @@ def __call__(self, img): img = ImageOps.expand(img, border=self.padding, fill=0) w, h = img.size - th, tw = self.size + tw, th = self.size if w == tw and h == th: return img From 4b6bb8d71d90e459f26e9759f0eb7c8f4dc2ea18 Mon Sep 17 00:00:00 2001 From: Alykhan Tejani Date: Sat, 16 Sep 2017 00:52:16 +0100 Subject: [PATCH 2/3] revert swapping size dims in crop and random_crop --- test/test_transforms.py | 10 +++++----- torchvision/transforms.py | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index ad2f3340172..952d411d609 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -28,7 +28,7 @@ def test_crop(self): imgnarrow.fill_(0) result = transforms.Compose([ transforms.ToPILImage(), - transforms.CenterCrop((owidth, oheight)), + transforms.CenterCrop((oheight, owidth)), transforms.ToTensor(), ])(img) assert result.sum() == 0, "height: " + str(height) + " width: " \ @@ -37,7 +37,7 @@ def test_crop(self): owidth += 1 result = transforms.Compose([ transforms.ToPILImage(), - transforms.CenterCrop((owidth, oheight)), + transforms.CenterCrop((oheight, owidth)), transforms.ToTensor(), ])(img) sum1 = result.sum() @@ -47,7 +47,7 @@ def test_crop(self): owidth += 1 result = transforms.Compose([ transforms.ToPILImage(), - transforms.CenterCrop((owidth, oheight)), + transforms.CenterCrop((oheight, owidth)), transforms.ToTensor(), ])(img) sum2 = result.sum() @@ -108,7 +108,7 @@ def test_random_crop(self): img = torch.ones(3, height, width) result = transforms.Compose([ transforms.ToPILImage(), - transforms.RandomCrop((owidth, oheight)), + transforms.RandomCrop((oheight, owidth)), transforms.ToTensor(), ])(img) assert result.size(1) == oheight @@ -117,7 +117,7 @@ def test_random_crop(self): padding = random.randint(1, 20) result = transforms.Compose([ transforms.ToPILImage(), - transforms.RandomCrop((owidth, oheight), padding=padding), + transforms.RandomCrop((oheight, owidth), padding=padding), transforms.ToTensor(), ])(img) assert result.size(1) == oheight diff --git a/torchvision/transforms.py b/torchvision/transforms.py index e3fd9d4a770..85f0a82c8c8 100644 --- a/torchvision/transforms.py +++ b/torchvision/transforms.py @@ -207,7 +207,7 @@ class CenterCrop(object): Args: size (sequence or int): Desired output size of the crop. If size is an - int instead of sequence like (w, h), a square crop (size, size) is + int instead of sequence like (h, w), a square crop (size, size) is made. """ @@ -226,7 +226,7 @@ def __call__(self, img): PIL.Image: Cropped image. """ w, h = img.size - tw, th = self.size + th, tw = self.size x1 = int(round((w - tw) / 2.)) y1 = int(round((h - th) / 2.)) return img.crop((x1, y1, x1 + tw, y1 + th)) @@ -286,7 +286,7 @@ class RandomCrop(object): Args: size (sequence or int): Desired output size of the crop. If size is an - int instead of sequence like (w, h), a square crop (size, size) is + int instead of sequence like (h, w), a square crop (size, size) is made. padding (int or sequence, optional): Optional padding on each border of the image. Default is 0, i.e no padding. If a sequence of length @@ -313,7 +313,7 @@ def __call__(self, img): img = ImageOps.expand(img, border=self.padding, fill=0) w, h = img.size - tw, th = self.size + th, tw = self.size if w == tw and h == th: return img From 9e3a2ee01f4e5ecaa64e3862ab46a09923e784d1 Mon Sep 17 00:00:00 2001 From: Alykhan Tejani Date: Sat, 16 Sep 2017 11:06:31 +0100 Subject: [PATCH 3/3] change Scale's size param to be (h,w) not (w,h) --- test/test_transforms.py | 4 ++-- torchvision/transforms.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 952d411d609..5d0275f946f 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -86,7 +86,7 @@ def test_scale(self): owidth = random.randint(5, 12) * 2 result = transforms.Compose([ transforms.ToPILImage(), - transforms.Scale((owidth, oheight)), + transforms.Scale((oheight, owidth)), transforms.ToTensor(), ])(img) assert result.size(1) == oheight @@ -94,7 +94,7 @@ def test_scale(self): result = transforms.Compose([ transforms.ToPILImage(), - transforms.Scale([owidth, oheight]), + transforms.Scale([oheight, owidth]), transforms.ToTensor(), ])(img) assert result.size(1) == oheight diff --git a/torchvision/transforms.py b/torchvision/transforms.py index 85f0a82c8c8..da58aa12b9a 100644 --- a/torchvision/transforms.py +++ b/torchvision/transforms.py @@ -165,7 +165,7 @@ class Scale(object): Args: size (sequence or int): Desired output size. If size is a sequence like - (w, h), output size will be matched to this. If size is an int, + (h, w), output size will be matched to this. If size is an int, smaller edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to (size * height / width, size) @@ -199,7 +199,7 @@ def __call__(self, img): ow = int(self.size * w / h) return img.resize((ow, oh), self.interpolation) else: - return img.resize(self.size, self.interpolation) + return img.resize(self.size[::-1], self.interpolation) class CenterCrop(object):