Skip to content

Instantly share code, notes, and snippets.

View dhanushkamath's full-sized avatar

Dhanush Kamath dhanushkamath

View GitHub Profile
vae_decoder_input, vae_decoder_output, vae_decoder = build_decoder(input_dim = Z_DIM,
shape_before_flattening = shape_before_flattening,
conv_filters = [64,64,32,3],
conv_kernel_size = [3,3,3,3],
conv_strides = [2,2,2,2]
)
# ENCODER
def build_vae_encoder(input_dim, output_dim, conv_filters, conv_kernel_size,
conv_strides, use_batch_norm = False, use_dropout = False):
# Clear tensorflow session to reset layer index numbers to 0 for LeakyRelu,
# BatchNormalization and Dropout.
# Otherwise, the names of above mentioned layers in the model
# would be inconsistent
global K
K.clear_session()
def generate_images_from_noise(n_to_show = 10):
reconst_images = decoder.predict(np.random.normal(0,1,size=(n_to_show,Z_DIM)))
fig = plt.figure(figsize=(15, 3))
fig.subplots_adjust(hspace=0.4, wspace=0.4)
for i in range(n_to_show):
img = reconst_images[i].squeeze()
sub = fig.add_subplot(2, n_to_show, i+1)
sub.axis('off')
plot_compare(images = example_images, add_noise = True)
import matplotlib.pyplot as plt
example_batch = next(data_flow)
example_batch = example_batch[0]
example_images = example_batch[:10]
def plot_compare(images=None, add_noise=False):
if images is None:
example_batch = next(data_flow)
LEARNING_RATE = 0.0005
N_EPOCHS = 10
optimizer = Adam(lr = LEARNING_RATE)
def r_loss(y_true, y_pred):
return K.mean(K.square(y_true - y_pred), axis = [1,2,3])
simple_autoencoder.compile(optimizer=optimizer, loss = r_loss)
# The input to the model will be the image fed to the encoder.
simple_autoencoder_input = encoder_input
# The output will be the output of the decoder. The term - decoder(encoder_output)
# combines the model by passing the encoder output to the input of the decoder.
simple_autoencoder_output = decoder(encoder_output)
# Input to the combined model will be the input to the encoder.
# Output of the combined model will be the output of the decoder.
simple_autoencoder = Model(simple_autoencoder_input, simple_autoencoder_output)
# Decoder
def build_decoder(input_dim, shape_before_flattening, conv_filters, conv_kernel_size,
conv_strides):
# Number of Conv layers
n_layers = len(conv_filters)
# Define model input
decoder_input = Input(shape = (input_dim,) , name = 'decoder_input')
# ENCODER
def build_encoder(input_dim, output_dim, conv_filters, conv_kernel_size,
conv_strides):
# Clear tensorflow session to reset layer index numbers to 0 for LeakyRelu,
# BatchNormalization and Dropout.
# Otherwise, the names of above mentioned layers in the model
# would be inconsistent
global K
K.clear_session()
filenames = np.array(glob(os.path.join(DATA_FOLDER, '*/*.jpg')))
NUM_IMAGES = len(filenames)
print("Total number of images : " + str(NUM_IMAGES))
# prints : Total number of images : 202599
INPUT_DIM = (128,128,3) # Image dimension
BATCH_SIZE = 512
Z_DIM = 200 # Dimension of the latent vector (z)