text_plain_output_1.png
from PIL import Image

from keras import optimizers

from keras.layers import Input, Dropout, Dense, Conv2D, MaxPooling2D, UpSampling2D

from keras.layers.noise import GaussianNoise, GaussianDropout

from keras.layers.normalization import BatchNormalization

from keras.models import Model

from keras.regularizers import l1,l2,l1_l2

import glob

import matplotlib.pyplot as plt

import numpy as np

import os

import xml.etree.ElementTree as ET

root_images = '../input/all-dogs/all-dogs/'
root_annots = '../input/annotation/Annotation/'
all_images = os.listdir('../input/all-dogs/all-dogs/')
breeds = glob.glob('../input/annotation/Annotation/*')
annotation = []
for b in breeds:
    annotation += glob.glob(b + '/*')
breed_map = {}
for annot in annotation:
    breed = annot.split('/')[-2]
    index = breed.split('-')[0]
    breed_map.setdefault(index, breed)

def bounding_box(image):
    bpath = root_annots + str(breed_map[image.split('_')[0]]) + '/' + str(image.split('.')[0])
    tree = ET.parse(bpath)
    root = tree.getroot()
    objects = root.findall('object')
    for o in objects:
        bndbox = o.find('bndbox')
        xmin = int(bndbox.find('xmin').text)
        ymin = int(bndbox.find('ymin').text)
        xmax = int(bndbox.find('xmax').text)
        ymax = int(bndbox.find('ymax').text)
    return (xmin, ymin, xmax, ymax)
num_rows = 5
num_cols = 9
num_images_to_show = num_rows * num_cols
selected_images = np.random.choice(all_images, size=num_images_to_show, replace=False)
for k, image_filename in enumerate(selected_images):
    bbox = bounding_box(image_filename)
    orig_image = Image.open(os.path.join(root_images, image_filename))
    cropped_image = orig_image.crop(bbox)
    plt.axis('off')

num_images_in_dataset = 16384
image_dimention = 64
resize_shape = (image_dimention, image_dimention)
selected_images = np.random.choice(all_images, size=num_images_in_dataset, replace=False)
image_dataset_4D_matrix = np.zeros((image_dimention, image_dimention, 3, num_images_in_dataset), dtype=np.uint8)
for k, image_filename in enumerate(selected_images):
    bbox = bounding_box(image_filename)
    orig_image = Image.open(os.path.join(root_images, image_filename))
    cropped_image = orig_image.crop(bbox)
    resized_image = tform.resize(np.array(cropped_image), resize_shape, preserve_range=True).astype(np.uint8)
    image_dataset_4D_matrix[:, :, :, k] = resized_image

from keras.models import Model
from keras.layers import Input, Dropout, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
from keras.layers.normalization import BatchNormalization
from keras.layers.noise import GaussianNoise, GaussianDropout
from keras.regularizers import l1, l2, l1_l2
from keras import optimizers
kernel_reg = 1e-07
activity_reg = 1e-07
input_image = Input(shape=(64, 64, 3), name='input_image')
x = Conv2D(32, (3, 3), activation='relu', padding='valid', name='encoder_conv1')(input_image)
x = MaxPooling2D((2, 2), padding='same', name='encoder_pool1')(x)
x = BatchNormalization(name='batchnorm_2')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='valid', name='encoder_conv2')(x)
x = MaxPooling2D((2, 2), padding='same', name='encoder_pool2')(x)
x = BatchNormalization(name='batchnorm_3')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='valid', kernel_regularizer=l2(kernel_reg), name='encoder_conv3')(x)
x = MaxPooling2D((2, 2), padding='same', name='encoder_pool3')(x)
x = BatchNormalization(name='batchnorm_4')(x)
x = Conv2D(1024, (3, 3), activation='relu', padding='valid', kernel_regularizer=l2(kernel_reg), name='encoder_conv4')(x)
x = MaxPooling2D((2, 2), padding='same', name='encoder_pool4')(x)
x = BatchNormalization(name='batchnorm_5')(x)
x = Conv2D(2048, (3, 3), activation='relu', padding='valid', kernel_regularizer=l2(kernel_reg), name='encoder_conv5')(x)
x = MaxPooling2D((3, 3), padding='same', name='encoder_pool5')(x)
x = BatchNormalization(name='batchnorm_6')(x)
image_representation = Conv2D(2048, (1, 1), activation='relu', kernel_regularizer=l2(kernel_reg), activity_regularizer=l1(activity_reg), name='encoder_fc6')(x)
image_representation = GaussianDropout(rate=0.1)(image_representation)
image_representation = GaussianNoise(stddev=0.1)(image_representation)
encoder = Model(input_image, image_representation, name='encoder')
external_image_representation = Input(shape=(1, 1, 2048), name='extenral_image_rep')
x = UpSampling2D((5, 5))(external_image_representation)
x = BatchNormalization(name='decoder_BN_1')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='valid', kernel_regularizer=l2(kernel_reg), name='decoder_conv1')(x)
x = UpSampling2D((3, 3), interpolation='bilinear')(x)
x = BatchNormalization(name='decoder_BN_2')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_regularizer=l2(kernel_reg), name='decoder_conv2')(x)
x = UpSampling2D((2, 2), interpolation='bilinear')(x)
x = BatchNormalization(name='decoder_BN_3')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='valid', kernel_regularizer=l2(kernel_reg), name='decoder_conv3')(x)
x = UpSampling2D((2, 2), interpolation='bilinear')(x)
x = BatchNormalization(name='decoder_BN_4')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_regularizer=l2(kernel_reg), name='decoder_conv4')(x)
x = UpSampling2D((2, 2), interpolation='bilinear')(x)
x = BatchNormalization(name='decoder_BN_5')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_regularizer=l2(kernel_reg), name='decoder_conv5')(x)
output_image = Conv2D(3, (3, 3), activation='linear', padding='same', name='generated_image')(x)
decoder = Model(external_image_representation, output_image, name='decoder')
autoencoder = Model(input_image, decoder(encoder(input_image)))
autoencoder.summary()

image_dataset_4D_matrix
normlized_X = image_dataset_4D_matrix.astype(np.float32)
normlized_X = normlized_X - normlized_X.mean(axis=3, keepdims=True)
normlized_X = normlized_X / normlized_X.std(axis=3, keepdims=True)
normlized_X = np.transpose(normlized_X, [3, 0, 1, 2])

num_epochs = 64
batch_size = 32
learning_rate = 0.0003
loss_to_use = 'mse'
optimizer_to_use = optimizers.Nadam(lr=learning_rate)
autoencoder.compile(optimizer=optimizer_to_use, loss=loss_to_use)
history = autoencoder.fit(x=normlized_X, y=normlized_X, batch_size=batch_size, epochs=num_epochs, validation_split=0.125)

epoch_number = np.arange(1, num_epochs + 1)
plt.figure(figsize=(12, 6))
plt.plot(epoch_number, history.history['loss'], 'b')
plt.plot(epoch_number, history.history['val_loss'], 'g')
plt.legend(['train', 'valid'], fontsize=18)