Skip to content

Commit

Permalink
Merge pull request keras-team#1566 from tpsatish95/master
Browse files Browse the repository at this point in the history
added random_shear() to random_transform
  • Loading branch information
fchollet committed Jan 28, 2016
2 parents ecdce97 + 5cf50f6 commit 59dcd7b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
14 changes: 8 additions & 6 deletions docs/templates/preprocessing/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keras.preprocessing.image.ImageDataGenerator(featurewise_center=True,
rotation_range=0.,
width_shift_range=0.,
height_shift_range=0.,
shear_range=0.,
horizontal_flip=False,
vertical_flip=False)
```
Expand All @@ -25,24 +26,25 @@ Generate batches of tensor image data with real-time data augmentation. The data
- __rotation_range__: Int. Degree range for random rotations.
- __width_shift_range__: Float (fraction of total width). Range for random horizontal shifts.
- __height_shift_range__: Float (fraction of total height). Range for random vertical shifts.
- __shear_range__: Float. Shear Intensity (Shear angle in counter-clockwise direction as radians)
- __horizontal_flip__: Boolean. Randomly flip inputs horizontally.
- __vertical_flip__: Boolean. Randomly flip inputs vertically.

- __Methods__:
- __fit(X)__: Required if featurewise_center or featurewise_std_normalization or zca_whitening. Compute necessary quantities on some sample data.
- __Arguments__:
- __Arguments__:
- __X__: sample data.
- __augment__: Boolean (default: False). Whether to fit on randomly augmented samples.
- __rounds__: int (default: 1). If augment, how many augmentation passes over the data to use.
- __flow(X, y)__:
- __Arguments__:
- __Arguments__:
- __X__: data.
- __y__: labels.
- __batch_size__: int (default: 32).
- __shuffle__: boolean (defaut: False).
- __save_to_dir__: None or str. This allows you to optimally specify a directory to which to save the augmented pictures being generated (useful for visualizing what you are doing).
- __save_to_dir__: None or str. This allows you to optimally specify a directory to which to save the augmented pictures being generated (useful for visualizing what you are doing).
- __save_prefix__: str. Prefix to use for filenames of saved pictures.
- __save_format__: one of "png", jpeg".
- __save_format__: one of "png", jpeg".

- __Example__:
```python
Expand All @@ -58,7 +60,7 @@ datagen = ImageDataGenerator(
height_shift_range=0.2,
horizontal_flip=True)

# compute quantities required for featurewise normalization
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(X_train)

Expand All @@ -77,4 +79,4 @@ for e in range(nb_epoch):
# we need to break the loop by hand because
# the generator loops indefinitely
break
```
```
17 changes: 12 additions & 5 deletions keras/preprocessing/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ def random_barrel_transform(x, intensity):
pass


def random_shear(x, intensity):
# TODO
pass

def random_shear(x, intensity, fill_mode="nearest", cval=0.):
shear = random.uniform(-intensity, intensity)
shear_matrix = np.array([[1.0, -math.sin(shear), 0.0],
[0.0, math.cos(shear), 0.0],
[0.0, 0.0, 1.0]])
x = ndimage.interpolation.affine_transform(x, shear_matrix,
mode=fill_mode, order=3, cval=cval)
return x

def random_channel_shift(x, rg):
# TODO
Expand Down Expand Up @@ -132,8 +136,10 @@ def __init__(self,
rotation_range=0., # degrees (0 to 180)
width_shift_range=0., # fraction of total width
height_shift_range=0., # fraction of total height
shear_range=0., # shear intensity (shear angle in radians)
horizontal_flip=False,
vertical_flip=False):

self.__dict__.update(locals())
self.mean = None
self.std = None
Expand Down Expand Up @@ -206,7 +212,8 @@ def random_transform(self, x):
if self.vertical_flip:
if random.random() < 0.5:
x = vertical_flip(x)

if self.shear_range:
x = random_shear(x,self.shear_range)
# TODO:
# zoom
# barrel/fisheye
Expand Down
1 change: 1 addition & 0 deletions tests/keras/preprocessing/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def test_image_data_generator():
rotation_range=90.,
width_shift_range=10.,
height_shift_range=10.,
shear_range=0.5,
horizontal_flip=True,
vertical_flip=True)
generator.fit(images, augment=True)
Expand Down

0 comments on commit 59dcd7b

Please sign in to comment.