In [1]:
%matplotlib inline

In [2]:
import numpy as np

from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot as plt
from keras import backend as K
K.set_image_dim_ordering('th')


Using Theano backend.

原始图片


In [3]:
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape is needed for the Theano backend 
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

In [4]:
X_small = X_train[:9]
y_small = y_train[:9]

In [5]:
def plot_digits(gen):
    gen.fit(X_train)
    for X_batch, y_batch in gen.flow(X_small, y_small, batch_size=9, shuffle=False, seed=42):
        for i in range(0, 9):
            plt.subplot(330 + 1 + i)
            plt.imshow(X_batch[i].reshape(28, 28))
        plt.show()
        break

In [6]:
gen=plot_digits(ImageDataGenerator(featurewise_center=False))



In [7]:
gen=plot_digits(ImageDataGenerator(featurewise_center=True))



In [8]:
plot_digits(ImageDataGenerator(samplewise_std_normalization=True))



In [9]:
plot_digits(ImageDataGenerator(zca_whitening=True))



In [10]:
plot_digits(ImageDataGenerator(rotation_range=60))



In [11]:
plot_digits(ImageDataGenerator(width_shift_range=0.3))



In [12]:
plot_digits(ImageDataGenerator(height_shift_range=0.3))



In [13]:
plot_digits(ImageDataGenerator(shear_range=np.pi))



In [14]:
plot_digits(ImageDataGenerator(zoom_range=1))



In [15]:
plot_digits(ImageDataGenerator(channel_shift_range=200))


水平和垂直翻转


In [16]:
plot_digits(ImageDataGenerator(horizontal_flip=True))



In [17]:
plot_digits(ImageDataGenerator(vertical_flip=True))


fill_mode 填充模式


In [18]:
plot_digits(ImageDataGenerator(zoom_range=2, fill_mode="nearest"))



In [19]:
plot_digits(ImageDataGenerator(zoom_range=2, fill_mode="reflect"))



In [20]:
plot_digits(ImageDataGenerator(zoom_range=2, fill_mode="wrap"))



In [21]:
plot_digits(ImageDataGenerator(zoom_range=2, cval=255, fill_mode="constant"))



In [22]:
%cd data/dogscats/


/home/leo/src/fastai-courses/deeplearning1/nbs/data/dogscats

In [23]:
import os
gen = ImageDataGenerator()
X_fit_batch, y_fit_batch =gen.flow_from_directory('train', batch_size=500, shuffle=False, seed=42).next()


Found 23000 images belonging to 2 classes.

In [24]:
def plot_cats(gen):
    gen.fit(X_fit_batch)
    for X_batch, y_batch in gen.flow_from_directory('train', batch_size=9, shuffle=True, seed=42):
        for i in range(0, 9):
            plt.subplot(3, 3, 1 + i)
            plt.imshow(X_batch[i].transpose(1, 2, 0).astype(np.uint8))
        plt.show()
        break

In [25]:
plot_cats(ImageDataGenerator())


Found 23000 images belonging to 2 classes.

In [26]:
plot_cats(ImageDataGenerator(featurewise_center=True))


Found 23000 images belonging to 2 classes.

In [27]:
plot_cats(ImageDataGenerator(samplewise_center=True))


Found 23000 images belonging to 2 classes.

In [28]:
plot_cats(ImageDataGenerator(featurewise_std_normalization=True))


Found 23000 images belonging to 2 classes.

In [29]:
plot_cats(ImageDataGenerator(samplewise_std_normalization=True))


Found 23000 images belonging to 2 classes.