Image Augmentation



In [ ]:
from keras.preprocessing import image
import matplotlib.pyplot as plt

import numpy as np
import cv2

In [ ]:
imgs_dir = ""
img_size = (1024,1024)

In [ ]:
def get_batches(path, gen=image.ImageDataGenerator(dim_ordering="th"), shuffle=True, batch_size=8, class_mode='categorical'):
    return gen.flow_from_directory(path, target_size=img_size, class_mode=class_mode, 
                                   shuffle=shuffle, batch_size=batch_size, color_mode='grayscale')

In [ ]:
def load_image(filepath):
    img = cv2.imread(filepath, cv2.IMREAD_COLOR)
    return img

def load_data(images, img_size):
    data = np.array([load_image(img, img_size).T for img in images])
    return data

In [ ]:
img = load_image("")

In [ ]:
img.shape

In [ ]:
img.reshape(1, *img.shape).shape

In [ ]:
np.array([img]).shape

In [ ]:
X_batch, y_batch = next(get_batches(imgs_dir))
print(X_batch[0].shape)

In [ ]:
shift = 0.1
rotation_range = 90
zoom_range = 0.1
shear_range = 0.1
channel_shift_range = 20
horizontal_flip = True
vertical_flip = True

In [ ]:
imgen = image.ImageDataGenerator(rotation_range=rotation_range, 
                                 width_shift_range=shift, height_shift_range=shift,
                                zoom_range=zoom_range, shear_range=shear_range, channel_shift_range=channel_shift_range,
                                horizontal_flip=horizontal_flip, vertical_flip=vertical_flip)

In [ ]:
for i in range(2):
    res = next(imgen.flow_from_directory(imgs_dir, target_size=img_size, classes=['car_ok'], class_mode='categorical', batch_size=10, 
                              save_to_dir=imgs_dir))

In [ ]:
X_batch, y_batch = next(get_batches(imgs_dir, imgen))
print(X_batch[0].shape)
fig, axes = plt.subplots(len(X_batch))
for i, x in enumerate(X_batch):
    axes[i].imshow(x.reshape(img_size), cmap=ptl.get_cmap('gray'))
plt.show()

Scikit-Image Transform


In [ ]:
from skimage import data
from skimage import transform

In [ ]:
image = data.camera()
plt.imshow(image)
plt.show()

In [ ]:
plt.imshow(transform.rotate(image, 30, mode='edge'))
plt.show()

In [ ]: