Пример #1
0
def generate(hparams):
    input_vocab_size = 128 + 128 + 128 + 128
    target_vocab_size = 128 + 128 + 128 + 128

    dataset = tf.data.Dataset.list_files(
        '/home/big/datasets/maestro-v2.0.0/**/*.midi')

    dataset_single = pro.pipeline([
        pro.midi(),
        pro.frame(hparams['frame_size'], hparams['frame_size'], True),
        pro.unbatch(),
    ])(dataset).skip(16000).as_numpy_iterator()

    transformer = Transformer(input_vocab_size=input_vocab_size,
                              target_vocab_size=target_vocab_size,
                              pe_input=input_vocab_size,
                              pe_target=target_vocab_size,
                              hparams=hparams)

    trainer = Trainer(dataset, hparams)
    ckpt = tf.train.Checkpoint(step=trainer.step,
                               transformer=transformer,
                               optimizer=transformer.optimizer)

    trainer.init_checkpoint(ckpt)

    return generate_from_model(hparams, transformer, dataset_single)
Пример #2
0
def maestro_from_files(root_path, frame_size):
    dataset = tf.data.Dataset.list_files(os.path.join(root_path, '**/*.wav'))
    dataset = pro.pipeline([
        pro.read_file(),
        pro.decode_wav(desired_channels=1),
        pro.map_transform(lambda x: x[0]),
        pro.reshape([-1]),
        pro.frame(frame_size, frame_size),
        pro.unbatch()
    ])(dataset)

    return dataset
Пример #3
0
def start(hparams):
    dataset = tf.data.Dataset.list_files('dataset/*.midi')

    dataset = pro.pipeline([
        pro.midi(),
        pro.prefetch(),
        pro.frame(hparams['frame_size'], hparams['frame_size'], True),
        pro.unbatch(),
        # pro.map_transform(tf_serialize_example)
    ])(dataset)

    def generator():
        for features in dataset:
            yield serialize_example(features)

    serialized_features_dataset = tf.data.Dataset.from_generator(
        generator, output_types=tf.string, output_shapes=())
    filename = 'midi_dataset.tfrecord'
    writer = tf.data.experimental.TFRecordWriter(filename)
    writer.write(serialized_features_dataset)
Пример #4
0
def start(hparams):
    # Load nsynth dataset
    # dataset = tfds.load('nsynth/gansynth_subset', split='train', shuffle_files=True)
    dataset = tf.data.Dataset.list_files(
        '/home/big/datasets/maestro-v2.0.0/**/*.wav')
    dataset = pro.pipeline([
        pro.wav(),
        # pro.resample(16000, hparams['sample_rate'], tf.float32),
        pro.normalize(),
        pro.frame(hparams['window_samples'], hparams['window_samples']),
        pro.unbatch(),
        pro.set_channels(1),
        pro.dupe(),
        pro.shuffle(hparams['buffer_size']),
        pro.batch(hparams['batch_size']),
        pro.prefetch()
    ])(dataset)

    vae = VAE(hparams)

    vae.vae.summary()

    trainer = Trainer(dataset, hparams)

    ckpt = tf.train.Checkpoint(
        step=trainer.step,
        encoder=vae.encoder,
        decoder=vae.decoder,
        vae=vae.vae,
    )

    trainer.on_epoch_start = on_epoch_start
    trainer.on_step = on_step

    trainer.init_tensorboard()
    trainer.init_checkpoint(ckpt)
    trainer.set_train_step(vae.train_step)
    trainer.run()
Пример #5
0
def start(hparams):
    gc.collect()

    dataset = tf.data.Dataset.list_files(
        '/home/big/datasets/maestro-v2.0.0/**/*.midi')

    dataset_single = pro.pipeline([
        pro.midi(),
        pro.frame(hparams['frame_size'] * 2, hparams['frame_hop_len'], True),
        pro.unbatch(),
    ])(dataset)

    def _reshape(inp, tar):
        inp = tf.reshape(inp, [hparams['frame_size']])
        tar = tf.reshape(tar, [hparams['frame_size']])
        return inp, tar

    dataset = pro.pipeline([
        pro.split(2),
        #pro.batch(2, True),
        # pro.dupe(),
        pro.map_transform(_reshape),
        pro.cache(),
        pro.shuffle(hparams['buffer_size']),
        pro.batch(hparams['batch_size'], True),
        pro.prefetch(),
    ])(dataset_single)

    dataset_single = pro.shuffle(hparams['buffer_size'] // 4)(dataset_single)
    dataset_single = dataset_single.as_numpy_iterator()

    transformer = Transformer(input_vocab_size=input_vocab_size,
                              target_vocab_size=target_vocab_size,
                              pe_input=input_vocab_size,
                              pe_target=target_vocab_size,
                              hparams=hparams)

    # pop_size = 10
    # generations = 100
    # mutation_rate = 0.2
    # pool.populate(pop_size, 1)

    # for generation in range(generations):
    #     pool.evaluate(evaluate(dataset, hparams))
    #     print(f"--- GENERATION: {generation} ---")
    #     print("BEST:", pool.best, pool.fitness)
    #     pool.select(pop_size)
    #     pool.populate(pop_size, mutation_rate)
    #

    image_save_step = hparams[
        'image_save_step'] if 'image_save_step' in hparams else 2000

    def generate_image(step, tsw):
        print("Generating sample...")
        encoded, seed = generate_from_model(hparams, transformer,
                                            dataset_single)
        print("Generating sample done.")

        print("Decoding midi...")
        decoded_seed = pro.decode_midi()(seed)
        decoded = pro.decode_midi()(encoded)
        print("Decoding midi done.")

        print("Saving midi...")
        with open(f'gen_transformer_{step}.midi', 'wb') as f:
            M.write_midi(f, decoded)
        with open(f'prior_transformer_{step}.midi', 'wb') as f:
            M.write_midi(f, decoded_seed)
        print("Saving midi done.")

        print("Plotting midi...")
        plt.title('Prior')
        M.display_midi(decoded_seed)
        image_seed = util.get_plot_image()
        plt.clf()

        plt.title('Generated')
        M.display_midi(decoded)
        image = util.get_plot_image()
        plt.clf()

        image_conc = tf.concat([image_seed, image], axis=1)
        print("Plotting done.")

        with tsw.as_default():
            tf.summary.image(f'image', image_conc, step=step)
        print("Complete.")

    # This runs at every step in the training (for each batch in dataset)
    def on_step(epoch, step, stats, tsw):
        loss, tar_real, predictions = stats
        train_loss(loss)
        train_accuracy(tar_real, predictions)
        if step % 100 == 0:
            print(
                f"Epoch: {epoch}, Step: {step}, Loss: {train_loss.result()}, Accuracy: {train_accuracy.result()}"
            )
        if step % image_save_step == 0:
            generate_image(step, tsw)

        with tsw.as_default():
            tf.summary.scalar('loss', train_loss.result(), step=step)

    trainer = Trainer(dataset, hparams)
    ckpt = tf.train.Checkpoint(step=trainer.step,
                               transformer=transformer,
                               optimizer=transformer.optimizer)

    trainer.init_checkpoint(ckpt)
    trainer.init_tensorboard()
    trainer.set_train_step(transformer.train_step)
    trainer.on_epoch_start = on_epoch_start
    trainer.on_step = on_step
    trainer.on_epoch_complete = on_epoch_complete

    #generate_image(trainer.step.numpy(), trainer.train_summary_writer)

    trainer.run()