예제 #1
0
def main():
    SECTION = 'vae'
    RUN_ID = '0002'
    DATA_NAME = 'digits'
    RUN_FOLDER = 'run/{}/'.format(SECTION)
    RUN_FOLDER += '_'.join([RUN_ID, DATA_NAME])

    print('RUN_FOLDER: ', RUN_FOLDER)

    if not os.path.exists(RUN_FOLDER):
        os.makedirs(RUN_FOLDER)
        os.makedirs(os.path.join(RUN_FOLDER, 'viz'))
        os.makedirs(os.path.join(RUN_FOLDER, 'images'))
        os.makedirs(os.path.join(RUN_FOLDER, 'weights'))

    mode = 'build'  #'load' #

    (x_train, y_train), (x_test, y_test) = load_mnist()

    vae = VariationalAutoencoder(input_dim=(28, 28, 1),
                                 encoder_conv_filters=[32, 64, 64, 64],
                                 encoder_conv_kernel_size=[3, 3, 3, 3],
                                 encoder_conv_strides=[1, 2, 2, 1],
                                 decoder_conv_t_filters=[64, 64, 32, 1],
                                 decoder_conv_t_kernel_size=[3, 3, 3, 3],
                                 decoder_conv_t_strides=[1, 2, 2, 1],
                                 z_dim=2)

    if mode == 'build':
        vae.save(RUN_FOLDER)
    else:
        vae.load_weights(os.path.join(RUN_FOLDER, 'weights/weights.h5'))

    vae.encoder.summary()

    vae.decoder.summary()

    LEARNING_RATE = 0.0005
    R_LOSS_FACTOR = 1000
    vae.compile(LEARNING_RATE, R_LOSS_FACTOR)

    BATCH_SIZE = 32
    EPOCHS = 200
    PRINT_EVERY_N_BATCHES = 100
    INITIAL_EPOCH = 0

    vae.train(x_train,
              batch_size=BATCH_SIZE,
              epochs=EPOCHS,
              run_folder=RUN_FOLDER,
              print_every_n_batches=PRINT_EVERY_N_BATCHES,
              initial_epoch=INITIAL_EPOCH)

    print('Done!')
예제 #2
0
def main():
    SECTION = 'vae'
    RUN_ID = '0001'
    DATA_NAME = 'digits'
    RUN_FOLDER = 'run/{}/'.format(SECTION)
    RUN_FOLDER += '_'.join([RUN_ID, DATA_NAME])

    print('RUN_FOLDER: ', RUN_FOLDER)

    (x_train, y_train), (x_test, y_test) = load_mnist()

    AE = load_model(Autoencoder, RUN_FOLDER)
    reconstruct(AE, x_test)
    coder_wall(AE, x_test, y_test)
예제 #3
0
def main():

    SECTION = 'vae'
    RUN_ID = '0001'
    DATA_NAME = 'digits'
    RUN_FOLDER = './run/{}/'.format(SECTION)
    RUN_FOLDER += '_'.join([RUN_ID, DATA_NAME])

    if not os.path.exists(RUN_FOLDER):
        os.makedirs(RUN_FOLDER)
        os.makedirs(os.path.join(RUN_FOLDER, 'viz'))
        os.makedirs(os.path.join(RUN_FOLDER, 'images'))
        os.makedirs(os.path.join(RUN_FOLDER, 'weights'))

    MODE = 'build'  # 'load' #

    (x_train, y_train), (x_test, y_test) = load_mnist()

    AE = Autoencoder(input_dim=(28, 28, 1),
                     encoder_conv_filters=[32, 64, 64, 64],
                     encoder_conv_kernel_size=[3, 3, 3, 3],
                     encoder_conv_strides=[1, 2, 2, 1],
                     decoder_conv_t_filters=[64, 64, 32, 1],
                     decoder_conv_t_kernel_size=[3, 3, 3, 3],
                     decoder_conv_t_strides=[1, 2, 2, 1],
                     z_dim=2)

    if MODE == 'build':
        AE.save(RUN_FOLDER)
    else:
        AE.load_weights(os.path.join(RUN_FOLDER, 'weights/weights.h5'))

    AE.encoder.summary()

    AE.decoder.summary()

    LEARNING_RATE = 0.0005
    BATCH_SIZE = 32
    INITIAL_EPOCH = 0

    AE.compile(LEARNING_RATE)

    AE.train(x_train[:1000],
             batch_size=BATCH_SIZE,
             epochs=200,
             run_folder=RUN_FOLDER,
             initial_epoch=INITIAL_EPOCH)

    print("Done")
import numpy as np
import os
from scipy.stats import norm

from models.AE import Autoencoder
from utils.loaders import load_mnist, load_model

# %%
SECTION = 'vae'
RUN_ID = '0001'
DATA_NAME = 'digits'
RUN_FOLDER = f'run/{SECTION}/'
RUN_FOLDER += '_'.join([RUN_ID, DATA_NAME])

# %%
(x_train, y_train), (x_test, y_test) = load_mnist()

# %%
# Load the model architecture
AE = load_model(Autoencoder, RUN_FOLDER)

# reconstructing original paintings
n_to_show = 10
example_idx = np.random.choice(range(len(x_test)), n_to_show)
example_images = x_test[example_idx]

z_points = AE.encoder.predict(example_images)

reconst_images = AE.decoder.predict(z_points)

fig = plt.figure(figsize=(15, 3))