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 lambda transform #8

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 @@ -194,7 +194,6 @@ 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`.
Expand All @@ -209,6 +208,14 @@ Given mean: (R, G, B) and std: (R, G, B), will normalize each channel of the tor
- `ToTensor()` - Converts a PIL.Image (RGB) or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]
- `ToPILImage()` - Converts a torch.*Tensor of range [0, 1] and shape C x H x W or numpy ndarray of dtype=uint8, range[0, 255] and shape H x W x C to a PIL.Image of range [0, 255]

## Generic Transofrms
### `Lambda(lambda)`
Given a Python lambda, applies it to the input `img` and returns it.
For example:

```python
transforms.Lambda(lambda x: x.add(10))
```

# Utils

Expand Down
13 changes: 11 additions & 2 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,19 @@ def test_pad(self):
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

def test_lambda(self):
trans = transforms.Lambda(lambda x: x.add(10))
x = torch.randn(10)
y = trans(x)
assert(y.equal(torch.add(x, 10)))

trans = transforms.Lambda(lambda x: x.add_(10))
x = torch.randn(10)
y = trans(x)
assert(y.equal(x))


if __name__ == '__main__':
Expand Down
10 changes: 10 additions & 0 deletions torchvision/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from PIL import Image, ImageOps
import numpy as np
import numbers
import types

class Compose(object):
""" Composes several transforms together.
Expand Down Expand Up @@ -126,6 +127,15 @@ def __init__(self, padding, fill=0):
def __call__(self, img):
return ImageOps.expand(img, border=self.padding, fill=self.fill)

class Lambda(object):
"""Applies a lambda as a transform"""
def __init__(self, lambd):
assert type(lambd) is types.LambdaType
self.lambd = lambd

def __call__(self, img):
return self.lambd(img)


class RandomCrop(object):
"""Crops the given PIL.Image at a random location to have a region of
Expand Down