Reputation: 1217
I am working with MNIST images and I want to perform some data augmentation techniques using Keras ImageDataGenerator
.
I m wondering if I can get the original images alongside with the transformed ones. Here is the code so far. Actually, I don't know how to recuperate the original images corresponding to the transformed ones.
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
def load_mnist():
# the data, shuffled and split between train and test sets
from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x = np.concatenate((x_train, x_test))
y = np.concatenate((y_train, y_test))
x = x.reshape([-1, 28, 28, 1]) / 255.0
print('MNIST samples', x.shape)
return x, y
def show_images(X_original, X_transformed, nb_images=50, img_h=28, img_w=28):
plt.figure(figsize=(40, 4))
for i in range(nb_images):
# display original
ax = plt.subplot(2, nb_images, i + 1)
plt.imshow(X_original[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display reconstruction
ax = plt.subplot(2, nb_images, i + 1 + nb_images)
plt.imshow(X_transformed[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
datagen = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, rotation_range=10, zoom_range=[0.8, 1.2])
X, Y = load_mnist()
gen0 = datagen.flow(X, Y, shuffle=True, batch_size=256)
X1, Y1 = gen0.next()
show_images(X_original=?, X_transformed=X1, nb_images=50, img_h=28, img_w=28)
Upvotes: 2
Views: 78
Reputation: 3989
To obtain the original images alongside with the transformed ones you can instantiate a new ImageDataGenerator
without any arguments, and when using the respective flow
for each ImageDataGenerator
pass the same seed for both functions.
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
def load_mnist():
# the data, shuffled and split between train and test sets
...
return x, y
def show_images(X_original, X_transformed, nb_images=50, img_h=28, img_w=28):
plt.figure(figsize=(40, 4))
for i in range(nb_images):
...
...
plt.show()
datagen = ImageDataGenerator(width_shift_range=0.4, height_shift_range=0.4,\
rotation_range=50, zoom_range=[0.8, 1.2])
datagen_original = ImageDataGenerator() # new generator
X, Y = load_mnist()
gen0 = datagen.flow(X, Y, shuffle=True, batch_size=256, seed=42) # same seed here
gen1 = datagen_original.flow(X, Y, shuffle=True, batch_size=256, seed=42) # and here
X1, Y1 = gen0.next()
X1_original, Y1_original = gen1.next()
show_images(X_original=X1_original, X_transformed=X1, nb_images=50, img_h=28, img_w=28)
Upvotes: 1