Keras

Reproducible Results

import random as rn
import numpy as np
import tensorflow as tf

import os
os.environ['PYTHONHASHSEED'] = '0'

# Setting the seed for numpy-generated random seed
np.randon.sedd(27)

# Setting the seed for python-generated random seed
rn.seed(123)

# Setting the seed for tensorflow-generated random seed
tf.set_random_seed(42)

# Force tensorflow to use a single thread
from keras import backend as K

sees = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sees)

Data Augmentation

gen = ImageDataGenerator(....)

image_path = 'image_file.jpeg'

# Obtain image
image = np.expand_dims(ndimage.imread(image_path), 0)
plt.imshow(image[0])

# Generate batches of augentated images from given image
aug_ter = gen.flow(image)

# Get 10 samplese of augentated images
aug_img = [next(aug_iter)[0].astype(np.uint8) for i in range(10)]

# Plot augmentated images (see helper function in notebook)
plots(aug_img, figsize=(12, 8), rows=2) 

Last updated