<jupyter_start><jupyter_text><jupyter_code>!pip install tensorflow==2.0.0rc1
import tensorflow as tf
from tensorflow import keras
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
import matplotlib.pyplot as plt
# create a grid of 3x3 images
for i in range(0,9):
  plt.subplot(330+1+i)
  plt.imshow(x_train[i], cmap=plt.get_cmap('gray'))
plt.show()
from keras_preprocessing.image import ImageDataGenerator
from keras import backend as K
#define data preparation
datagen=ImageDataGenerator(featurewise_center=True,featurewise_std_normalization=True)
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train=x_train.reshape(x_train.shape[0],1,28,28)
#fit parameters
datagen.fit(x_train,seed=10)
# configure and retrieve batches of images
for x_batch, y_batch in datagen.flow(x_train,y_train,batch_size=9,shuffle=False):
  for i in range(0,9):
    plt.subplot(330+1+i)
    plt.imshow(x_batch[i].reshape(28,28), cmap=plt.get_cmap('gray'))
  plt.show()
  break
#ZCA whitening
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train=x_train.reshape(x_train.shape[0],1,28,28)
datagen = ImageDataGenerator(zca_whitening=True)
datagen.fit(x_train)
# configure batch size and retrieve one batch of images
for X_batch, y_batch in datagen.flow(x_train, y_train, batch_size=9,shuffle=False):
# create a grid of 3x3 images
  for i in range(0, 9):
    plt.subplot(330 + 1 + i)
    plt.imshow(X_batch[i].reshape(28, 28), cmap=plt.get_cmap('gray'))
    # show the plot
  plt.show()
  break
#Random rotations
datagen = ImageDataGenerator(rotation_range=90)
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train=x_train.reshape(x_train.shape[0],1,28,28)
datagen.fit(x_train)
# configure batch size and retrieve one batch of images
for X_batch, y_batch in datagen.flow(x_train, y_train, batch_size=9,shuffle=False):
# create a grid of 3x3 images
  for i in range(0, 9):
    plt.subplot(330 + 1 + i)
    plt.imshow(X_batch[i].reshape(28, 28), cmap=plt.get_cmap('gray'))
    # show the plot
  plt.show()
  break
#Random shifts
shift = 0.2
datagen = ImageDataGenerator(width_shift_range=shift, height_shift_range=shift)
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train=x_train.reshape(x_train.shape[0],1,28,28)
datagen.fit(x_train)
# configure batch size and retrieve one batch of images
for X_batch, y_batch in datagen.flow(x_train, y_train, batch_size=9,shuffle=False):
# create a grid of 3x3 images
  for i in range(0, 9):
    plt.subplot(330 + 1 + i)
    plt.imshow(X_batch[i].reshape(28, 28), cmap=plt.get_cmap('gray'))
    # show the plot
  plt.show()
  break
#Random flips
shift = 0.2
datagen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True)
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train=x_train.reshape(x_train.shape[0],1,28,28)
datagen.fit(x_train)
# configure batch size and retrieve one batch of images
for X_batch, y_batch in datagen.flow(x_train, y_train, batch_size=9,shuffle=False):
# create a grid of 3x3 images
  for i in range(0, 9):
    plt.subplot(330 + 1 + i)
    plt.imshow(X_batch[i].reshape(28, 28), cmap=plt.get_cmap('gray'))
    # show the plot
  plt.show()
  break<jupyter_output>/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/image_data_generator.py:940: UserWarning: Expected input to be images (as Numpy array) following the data format convention "channels_last" (channels on axis 3), i.e. expected either 1, 3 or 4 channels on axis 3. However, it was passed an array with shape (60000, 1, 28, 28) (28 channels).
  ' channels).')
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/numpy_array_iterator.py:127: UserWarning: NumpyArrayIterator is set to use the data format convention "channels_last" (channels on axis 3), i.e. expected either 1, 3, or 4 channels on axis 3. However, it was passed an array with shape (60000, 1, 28, 28) (28 channels).
  str(self.x.shape[channels_axis]) + ' channels).')
{'fraction_non_alphanumeric': 0.12746942615239887, 'fraction_numerical': 0.04821260583254939, 'mean_word_length': 5.3288690476190474, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '212625', 'license': 'Apache-2.0', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1637, 'n_words': 399, 'path': 'ImageDataGenerator_Example.ipynb', 'repo_name': 'jvishnuvardhan/Keras_Examples', 'size': '4252'}