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

adding padding to RandomCrop, as well as transforms.Pad #7

Merged
merged 1 commit into from
Nov 27, 2016
Merged
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ Crops the given PIL.Image at the center to have a region of
the given size. size can be a tuple (target_height, target_width)
or an integer, in which case the target will be of a square shape (size, size)

### `RandomCrop(size)`
### `RandomCrop(size, padding=0)`
Crops the given PIL.Image at a random location to have a region of
the given size. size can be a tuple (target_height, target_width)
or an integer, in which case the target will be of a square shape (size, size)
If `padding` is non-zero, then the image is first zero-padded on each side with `padding` pixels.

### `RandomHorizontalFlip()`
Randomly horizontally flips the given PIL.Image with a probability of 0.5
Expand All @@ -193,6 +194,12 @@ This is popularly used to train the Inception networks
- size: size of the smaller edge
- interpolation: Default: PIL.Image.BILINEAR


### `Pad(padding, fill=0)`
Pads the given image on each side with `padding` number of pixels, and the padding pixels are filled with
pixel value `fill`.
If a `5x5` image is padded with `padding=1` then it becomes `7x7`

## Transforms on torch.*Tensor

### `Normalize(mean, std)`
Expand Down
234 changes: 234 additions & 0 deletions test/sanity_checks.ipynb

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ def test_random_crop(self):
assert result.size(1) == oheight
assert result.size(2) == owidth

padding = random.randint(1, 20)
result = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomCrop((oheight, owidth), padding=padding),
transforms.ToTensor(),
])(img)
assert result.size(1) == oheight
assert result.size(2) == owidth

def test_pad(self):
height = random.randint(10, 32) * 2
width = random.randint(10, 32) * 2
img = torch.ones(3, height, width)
padding = random.randint(1, 20)
result = transforms.Compose([
transforms.ToPILImage(),
transforms.Pad(padding),
transforms.ToTensor(),
])(img)
print(height, width, padding)
print(result.size(1), result.size(2))
assert result.size(1) == height + 2*padding
assert result.size(2) == width + 2*padding


if __name__ == '__main__':
Expand Down
16 changes: 14 additions & 2 deletions torchvision/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import torch
import math
import random
from PIL import Image
from PIL import Image, ImageOps
import numpy as np
import numbers

Expand Down Expand Up @@ -115,6 +115,18 @@ def __call__(self, img):
return img.crop((x1, y1, x1 + tw, y1 + th))


class Pad(object):
"""Pads the given PIL.Image on all sides with the given "pad" value"""
def __init__(self, padding, fill=0):
assert isinstance(padding, numbers.Number)
assert isinstance(fill, numbers.Number)
self.padding = padding
self.fill = fill

def __call__(self, img):
return ImageOps.expand(img, border=self.padding, fill=self.fill)


class RandomCrop(object):
"""Crops the given PIL.Image at a random location to have a region of
the given size. size can be a tuple (target_height, target_width)
Expand All @@ -129,7 +141,7 @@ def __init__(self, size, padding=0):

def __call__(self, img):
if self.padding > 0:
raise NotImplementedError()
img = ImageOps.expand(img, border=self.padding, fill=0)

w, h = img.size
th, tw = self.size
Expand Down