Last active
December 28, 2019 20:34
-
-
Save miladfa7/46865507a7729f50ea410eb6fa164226 to your computer and use it in GitHub Desktop.
Image Augmentation for Increasing sample of dataset
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 tensorflow | |
from PIL import Image | |
import glob | |
from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
from tensorflow.keras.preprocessing.image import img_to_array | |
from tensorflow.keras.preprocessing.image import load_img | |
import numpy as np | |
aug = ImageDataGenerator( | |
rotation_range=30, | |
zoom_range=0.15, | |
width_shift_range=0.2, | |
height_shift_range=0.2, | |
shear_range=0.15, | |
horizontal_flip=True, | |
fill_mode="nearest") | |
train_aug = os.path.join('/content/drive/My Drive/Colab Notebooks/Dataset/', 'training') | |
valid_aug = os.path.join('/content/drive/My Drive/Colab Notebooks/Dataset/', 'validation') | |
train_cln_aug = [x[1] for x in os.walk(train_aug)] | |
train_cln_aug = train_cln_aug[0] | |
valid_cln_aug = [x[1] for x in os.walk(valid_dir)] | |
valid_cln_aug = valid_cln_aug[0] | |
for tcg in train_cln_aug: | |
save_to_dir = '/content/drive/My Drive/Colab Notebooks/Dataset/training/' + tcg | |
filename = save_to_dir +'/*.jpg' | |
im=Image.open(filename) | |
image_list.append(im) | |
image = image_list[0] | |
image = img_to_array(image) | |
image = np.expand_dims(image, axis=0) | |
imageGen = aug.flow(image, batch_size=1, save_to_dir=save_to_dir ,save_prefix="image", save_format="jpg") | |
# generating 10 sample for each training image | |
total = 0 | |
final = 10 | |
for image in imageGen: | |
total += 1 | |
if total ==final : | |
break | |
for vcg in train_cln_aug: | |
save_to_dir = '/content/drive/My Drive/Colab Notebooks/Dataset/validation/' + vcg | |
filename = save_to_dir +'/*.jpg' | |
im=Image.open(filename) | |
image_list.append(im) | |
image = image_list[0] | |
image = img_to_array(image) | |
image = np.expand_dims(image, axis=0) | |
imageGen = aug.flow(image, batch_size=1, save_to_dir=save_to_dir ,save_prefix="image", save_format="jpg") | |
# generating 2 sample for each validation image | |
total = 0 | |
final = 2 | |
for image in imageGen: | |
total += 1 | |
if total ==final : | |
break |
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 tensorflow | |
from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
# Create training ImageDataGenerator object | |
train_data_gen = ImageDataGenerator(rotation_range=50, | |
width_shift_range=0.2, | |
height_shift_range=0.2, | |
zoom_range=0.3, | |
horizontal_flip=True, | |
vertical_flip=True, | |
fill_mode='constant', | |
cval=0, | |
rescale=1./255) | |
# Create validation ImageDataGenerator objects | |
valid_data_gen = ImageDataGenerator(rotation_range=45, | |
width_shift_range=0.2, | |
height_shift_range=0.2, | |
zoom_range=0.3, | |
horizontal_flip=True, | |
vertical_flip=True, | |
fill_mode='constant', | |
cval=0, | |
rescale=1./255) | |
test_data_gen = ImageDataGenerator(rescale=1./255) | |
SEED = 1234 | |
tensorflow.random.set_seed(SEED) | |
# Training | |
training_dir = os.path.join(dataset_dir, 'training') | |
train_gen = train_data_gen.flow_from_directory(training_dir, | |
target_size=(256, 256), | |
batch_size=Batch_size, | |
classes=classes, | |
class_mode='categorical', | |
shuffle=True, | |
seed=SEED) # targets are directly converted into one-hot vectors | |
# Validation | |
valid_dir = os.path.join(dataset_dir, 'valid') | |
valid_gen = valid_data_gen.flow_from_directory(valid_dir, | |
target_size=(256, 256), | |
batch_size=Batch_size, | |
classes=classes, | |
class_mode='categorical', | |
shuffle=False, | |
seed=SEED) | |
# Test | |
test_dir = os.path.join(dataset_dir, 'testing') | |
test_gen = test_data_gen.flow_from_directory(test_dir, | |
target_size=(256, 256), | |
batch_size=10, | |
shuffle=False, | |
seed=SEED, | |
class_mode=None, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment