示例#1
0
def train_vae():
    print("found {} files".format(len(wav_files)))
    #     encoder = keras.models.load_model("vae/encoder-test2.h")
    #     decoder = keras.models.load_model("vae/decoder-test2.h")

    train_generator = NoteIsoSequence(train_wav_files,
                                      sample_duration=sample_duration,
                                      sample_rate=sample_rate,
                                      n_fft=n_fft,
                                      batch_size=batch_size,
                                      epsilon=epsilon,
                                      song_indices=song_indices,
                                      instr_indices=instr_indices,
                                      note_indices=note_indices)
    test_generator = NoteIsoSequence(test_wav_files,
                                     sample_duration=sample_duration,
                                     sample_rate=sample_rate,
                                     n_fft=n_fft,
                                     batch_size=batch_size,
                                     epsilon=epsilon,
                                     song_indices=song_indices,
                                     instr_indices=instr_indices,
                                     note_indices=note_indices)

    #     tb = keras.callbacks.TensorBoard(histogram_freq=0, write_grads=True)
    encoder, decoder, vae, my_vae_loss = get_vae()
    vae.summary()

    now = datetime.now()
    log_dir = "logs/vae-" + now.strftime("%Y-%m-%d-%H:%M:%S") + "/"
    callbacks = [
        TensorBoardWrapper(test_generator,
                           log_dir=log_dir,
                           nb_steps=5,
                           histogram_freq=0,
                           batch_size=batch_size,
                           write_graph=False,
                           write_grads=True,
                           write_images=False)
    ]

    vae.fit_generator(generator=train_generator,
                      validation_data=test_generator,
                      use_multiprocessing=use_multiprocessing,
                      workers=workers,
                      epochs=epochs,
                      steps_per_epoch=steps_per_epoch,
                      validation_steps=validation_steps,
                      callbacks=callbacks)

    print("saving model...")
    encoder.save("vae/encoder-test2.h")
    decoder.save("vae/decoder-test2.h")
    vae.save("vae/vae-test.h")
    print("saved vae.")
                    },
                    'ae': {
                        'time': 0,
                        'statistical': {'m(mean)': 0, 'm(std)': 0, 'm(skew)': 0, 'm(kurtosis)': 0},
                        'clustering': {'ARI': 0},
                        'classification': {'m1d1': 0, 'm2d1': 0}
                    }
                }
            pr_dat_train = preprocessed_data.iloc[train_index]
            pr_dat_test = preprocessed_data.iloc[test_index]
            dat_train = data.iloc[train_index]
            dat_test = data.iloc[test_index]

            # vae gen
            start = timer()
            gen, enc = get_vae(pr_dat_train)
            new_data = generate_data_vae(generator=gen,
                                         encoder=enc,
                                         org_data=pr_dat_train,
                                         n=1000)
            end = timer()
            time_vae = end - start
            results['vae']['time'] += time_vae

            stats_eval = evaluation.statistical(pr_dat_train,
                                                new_data,
                                                numerical_cols=numerical_cols)
            results['vae']['statistical']['m(mean)'] += stats_eval[0]
            results['vae']['statistical']['m(std)'] += stats_eval[1]
            results['vae']['statistical']['m(skew)'] += stats_eval[2]
            results['vae']['statistical']['m(kurtosis)'] += stats_eval[3]
示例#3
0
import model as model
import keras.utils as utils
import keras.datasets as datasets
import matplotlib.pyplot as plt
import numpy as np
import keras.backend as K
import utility

image_set = utility.get_MNIST()
vae_model = model.get_vae(image_set.shape[1:])

history = vae_model.fit(image_set, image_set, batch_size=128, epochs=10)

enco, deco = vae_model.layers

rand = np.random.randint(0, image_set.shape[0], (4))

for i in range(4):
    realim = image_set[rand[i]]
    predim = vae_model.predict(realim[np.newaxis, ...])
    realim, predim = [np.reshape(x, (28, 28)) for x in [realim, predim]]
    plt.subplot(4, 2, i * 2 + 1)
    plt.imshow(realim, cmap='gray')
    plt.subplot(4, 2, i * 2 + 2)
    plt.imshow(predim, cmap='gray')

n = 4
randSamples = K.eval(K.random_normal((n * n, 10)))
predic_imag = deco.predict(randSamples)
for i in range(n * n):
    plt.subplot(n, n, i + 1)