[Python] How do I display the images generated by an autoencoder?

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I created an autoencoder using python, with no errors. However, I do not know the code for how do display the generated images from the autoencoder. The code of the autoencoder is shown below:

    import numpy as np
    import matplotlib.pyplot as plt

    from tensorflow.keras import layers, models, datasets, callbacks
    import tensorflow.keras.backend as K

    #NOT IN THE TEXT FOR SOME odd REASON
    from keras.models import Model


    #IMPORT THE DATA INTO TRAIN AND TEST SETS
    from tensorflow.keras import datasets
    (x_train,y_train), (x_test,y_test) = datasets.fashion_mnist.load_data()

    #scale the images
    def preprocess(imgs):
    imgs = imgs.astype("float32") / 255.0
    imgs = np.pad(imgs, ((0, 0), (2, 2), (2, 2)), constant_values=0.0)
    imgs = np.expand_dims(imgs, -1)
    return imgs
    x_train = preprocess(x_train)
    x_test = preprocess(x_test)

    #THE ENCODER
    encoder_input = layers.Input(
    shape=(32, 32, 1), name = "encoder_input"
    )
    x = layers.Conv2D(32, (3, 3), strides = 2, activation = 'relu', padding="same")(
    encoder_input
    )
    x = layers.Conv2D(64, (3, 3), strides = 2, activation = 'relu', padding="same")(x)
    x = layers.Conv2D(128, (3, 3), strides = 2, activation = 'relu', padding="same")(x)
    shape_before_flattening = K.int_shape(x)[1:]
    x = layers.Flatten()(x)
    encoder_output = layers.Dense(2, name="encoder_output")(x)
    encoder = models.Model(encoder_input, encoder_output)

    #THE DECODER
    decoder_input = layers.Input(shape=(2,), name="decoder_input")
    x = layers.Dense(np.prod(shape_before_flattening))(decoder_input)
    x = layers.Reshape(shape_before_flattening)(x)
    x = layers.Conv2DTranspose(
    128, (3, 3), strides=2, activation = 'relu', padding="same"
    )(x)
    x = layers.Conv2DTranspose(
    64, (3, 3), strides=2, activation = 'relu', padding="same"
    )(x)
    x = layers.Conv2DTranspose(
    32, (3, 3), strides=2, activation = 'relu', padding="same"
    )(x)
    decoder_output = layers.Conv2D(
    1,
    (3, 3),
    strides = 1,
    activation="sigmoid",
    padding="same",
    name="decoder_output"
    )(x)
    decoder = models.Model(decoder_input, decoder_output)

    # JOIN THE ENCODER WITH THE DECODER
    autoencoder = Model(encoder_input, decoder(encoder_output))

    # Compile the autoencoder
    autoencoder.compile(optimizer="adam", loss="binary_crossentropy")
    # Train the autoencoder by passing in the input images as both the input and output
    # with one epoch

    autoencoder.fit(
    x_train,
    x_train,
    epochs=1,
    batch_size=100,
    shuffle=True,
    validation_data=(x_test, x_test),
    )

    #Reconstruct the images
    example_images = x_test[:50]
    predictions = autoencoder.predict(example_images)


    I tried using plt.imshow as shown below. I expected to see 10 images generated by the autoencoder. however it is not working. I'm really not sure how to use it:

    for i in range(10):
    plt.figure(figsize=(20,3))
    plt.imshow(predictions.astype("float32"), cmap="gray_r")
    plt.show()

    Continue reading...

Compartilhe esta Página