Last active
July 24, 2017 10:30
-
-
Save dnkirill/201dacba6c52dae03e481f00daaef590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from skimage import transform | |
from skimage.transform import warp, AffineTransform | |
def rotate_90_deg(X): | |
X_aug = np.zeros_like(X) | |
for i,img in enumerate(X): | |
X_aug[i] = transform.rotate(img, 270.0) | |
return X_aug | |
def rotate_180_deg(X): | |
X_aug = np.zeros_like(X) | |
for i,img in enumerate(X): | |
X_aug[i] = transform.rotate(img, 180.0) | |
return X_aug | |
def rotate_270_deg(X): | |
X_aug = np.zeros_like(X) | |
for i,img in enumerate(X): | |
X_aug[i] = transform.rotate(img, 90.0) | |
return X_aug | |
def rotate_up_to_20_deg(X): | |
X_aug = np.zeros_like(X) | |
delta = 20. | |
for i,img in enumerate(X): | |
X_aug[i] = transform.rotate(img, random.uniform(-delta, delta), mode='edge') | |
return X_aug | |
def flip_vert(X): | |
X_aug = deepcopy(X) | |
return X_aug[:, :, ::-1, :] | |
def flip_horiz(X): | |
X_aug = deepcopy(X) | |
return X_aug[:, ::-1, :, :] | |
def affine_transform(X, shear_angle=0.0, scale_margins=[0.8, 1.5], p=1.0): | |
"""This function allows applying shear, scale and transformations | |
with the specified magnitude and probability p. | |
Args: | |
X: numpy array of images. | |
shear_angle: maximum shear angle in counter-clockwise direction as radians. | |
scale_margins: minimum and maximum margins to be used in scaling. | |
p: a fraction of images to be augmented. | |
""" | |
X_aug = deepcopy(X) | |
shear = shear_angle * np.random.rand() | |
for i in np.random.choice(len(X_aug), int(len(X_aug) * p), replace=False): | |
_scale = random.uniform(scale_margins[0], scale_margins[1]) | |
X_aug[i] = warp(X_aug[i], AffineTransform(scale=(_scale, _scale), shear=shear), mode='edge') | |
return X_aug |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment