数据扩充:将转换后的原始图像恢复到原始数据中

时间:2018-10-02 18:06:09

标签: python tensorflow keras

我正在使用MNIST图像,我想使用Keras ImageDataGenerator执行一些数据增强技术。

我想知道我能否同时获得原始图像和转换后的图像。 这是到目前为止的代码。实际上,我不知道该如何恢复与转换后的图像相对应的原始图像。

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)

0 个答案:

没有答案
相关问题