image_output_1.png
from keras.datasets import cifar10

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, BatchNormalization, Activation

from keras.models import Model

import keras

import matplotlib.pyplot as plt

def load_images():
    (x_train, _), (x_test, _) = cifar10.load_data()
    return (x_train, x_test)

def test_plot(x):
    pass

def normalize(x_train, x_test):
    x_train = keras.utils.normalize(x_train)
    x_test = keras.utils.normalize(x_test)
    return (x_train, x_test)

input_shape = x_train.shape[1:]
receptive_field = (3, 3)
pooling_field = (2, 2)

def CONVautoencoder(x_train_re, x_test_re, epochs=200):
    input_img = Input(shape=(32, 32, 3))
    x = Conv2D(64, (3, 3), padding='same')(input_img)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(32, (3, 3), padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(16, (3, 3), padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    encoded = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(16, (3, 3), padding='same')(encoded)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(32, (3, 3), padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(64, (3, 3), padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(3, (3, 3), padding='same')(x)
    x = BatchNormalization()(x)
    decoded = Activation('sigmoid')(x)
    autoencoder = Model(input_img, decoded)
    encoder = Model(input_img, encoded)
    autoencoder.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    autoencoder.fit(x_train_re, x_train_re, epochs=epochs, batch_size=250, validation_data=(x_test_re, x_test_re))
    encoded_imgs = encoder.predict(x_test_re)
    predicted = autoencoder.predict(x_test_re)
    return (encoded_imgs, predicted)
    return (encoded_imgs, predicted)

def plotting(x_test,encoded_imgs,predicted):
  plt.figure(figsize=(40, 4))
  for i in range(10):
      # display original images
      ax = plt.subplot(3, 20, i + 1)
      plt.imshow(x_test[i].reshape(32, 32,3))
      #plt.gray()
      ax.get_xaxis().set_visible(False)
      ax.get_yaxis().set_visible(False)

  # display reconstructed images
      ax = plt.subplot(3, 20, 2*20 +i+ 1)
      plt.imshow(predicted[i].reshape(32, 32,3))
      #plt.gray()
      ax.get_xaxis().set_visible(False)
      ax.get_yaxis().set_visible(False)

def main():
    x_train, x_test = load_images()
    x_train, x_test = normalize(x_train, x_test)
    encoded_imgs, predicted = CONVautoencoder(x_train_re, x_test_re)

main()