Esempio n. 1
0
def call_gan_training(generator, adversary):
    gan_model = VanillaGAN(
        generator=generator,
        adversary=adversary,
        z_dim=z_dim,
        x_dim=im_dim,
        folder="./veganModels/GAN",
    )
    # gan_model.summary(save=True)
    gan_model.fit(
        X_train=X_train,
        X_test=X_test,
        batch_size=batch_size,
        epochs=epochs,
        steps={"Adversary": 5},
        print_every="0.5e",
        save_model_every=None,
        save_images_every="0.5e",
        save_losses_every=1,
        enable_tensorboard=True,
    )
    samples, losses = gan_model.get_training_results(by_epoch=False)
    utils.plot_images(images=samples.reshape(-1, *samples.shape[2:]),
                      show=False)
    utils.plot_losses(losses=losses, show=True)
Esempio n. 2
0
    def _build_images(self, images, labels=None):
        """ Build matplotlib figure containing all images.

        Parameters
        ----------
        images : torch.Tensor()
            A torch Tensor containing the images to be plotted by matplotlib.

        Returns
        -------
        fig, axs: plt.Figure, np.array
            Objects containing the figure as well as all separate axis objects.
        """
        images = images.detach().cpu().numpy()
        if self.images_produced:
            fig, axs = plot_images(images=images, labels=labels, show=False)
            return fig, axs
        return None, None
Esempio n. 3
0
        gan_model.summary(save=True)
        gan_model.fit(X_train=X_train,
                      y_train=y_train,
                      X_test=X_test,
                      y_test=y_test,
                      batch_size=batch_size,
                      epochs=epochs,
                      steps=None,
                      print_every="0.2e",
                      save_model_every=None,
                      save_images_every="0.2e",
                      save_losses_every=10,
                      enable_tensorboard=False)
        samples, losses = gan_model.get_training_results(by_epoch=False)

        training_time = np.round(gan_model.total_training_time / 60, 2)
        title = "Epochs: {}, z_dim: {}, Time trained: {} minutes\nParams: {}\n\n".format(
            epochs, z_dim, training_time, gan_model.get_number_params())
        fixed_labels = np.argmax(gan_model.get_fixed_labels(), axis=1)
        fig, axs = utils.plot_images(images=samples.reshape(-1, 32, 32),
                                     labels=fixed_labels,
                                     show=False)
        fig.suptitle(title, fontsize=12)
        fig.tight_layout()
        plt.savefig(gan_model.folder + "/generated_images.png")
        fig, axs = utils.plot_losses(losses=losses, show=False)
        fig.suptitle(title, fontsize=12)
        fig.tight_layout()
        plt.savefig(gan_model.folder + "/losses.png")
        # gan_model.save()
Esempio n. 4
0
            raise NotImplementedError("{} no yet implemented in logical gate.".format(model.__name__))

        gan_model.summary(save=True)
        gan_model.fit(
            X_train=X_train,
            X_test=X_test,
            batch_size=batch_size,
            epochs=epochs,
            steps=None,
            print_every="0.2e",
            save_model_every=None,
            save_images_every="0.5e",
            save_losses_every=10,
            enable_tensorboard=True
        )
        samples, losses = gan_model.get_training_results(by_epoch=False)

        training_time = np.round(gan_model.total_training_time/60, 2)
        title = "Epochs: {}, z_dim: {}, Time trained: {} minutes\nParams: {}\n\n".format(
            epochs, z_dim, training_time, gan_model.get_number_params()
        )
        fig, axs = utils.plot_images(images=samples.reshape(-1, 32, 32), show=False)
        fig.suptitle(title, fontsize=12)
        fig.tight_layout()
        plt.savefig(gan_model.folder+"/generated_images.png")

        fig, axs = utils.plot_losses(losses=losses, show=False)
        fig.suptitle(title, fontsize=12)
        fig.tight_layout()
        plt.savefig(gan_model.folder+"/losses.png")
        # gan_model.save()
Esempio n. 5
0
    ngpu=None,  # Optional
    folder=None,  # Optional
    secure=True  # Optional
)

gan_model.summary()
gan_model.fit(
    X_train=X_train,
    y_train=y_train,
    X_test=X_test,  # Optional
    y_test=y_test,  # Optional
    batch_size=32,  # Optional
    epochs=2,  # Optional
    steps=None,  # Optional
    print_every="0.2e",  # Optional
    save_model_every=None,  # Optional
    save_images_every=None,  # Optional
    save_losses_every=10,  # Optional
    enable_tensorboard=False  # Optional
)
samples, losses = gan_model.get_training_results(by_epoch=False)

fixed_labels = np.argmax(gan_model.get_fixed_labels(), axis=1)
fig, axs = plot_images(images=samples, labels=fixed_labels, show=True)

test_labels = np.eye(N=10)
test_samples = gan_model.generate(y=test_labels)
fig, axs = plot_images(images=test_samples,
                       labels=np.argmax(test_labels, axis=1),
                       show=True)
Esempio n. 6
0
                "{} no yet implemented in logical gate.".format(
                    model.__name__))

        gan_model.summary(save=True)
        gan_model.fit(X_train=train_dataloader,
                      y_train=None,
                      X_test=None,
                      y_test=None,
                      batch_size=None,
                      epochs=epochs,
                      steps=None,
                      print_every=500,
                      save_model_every=None,
                      save_images_every="0.1e",
                      save_losses_every=10,
                      enable_tensorboard=False)
        samples, losses = gan_model.get_training_results(by_epoch=False)

        training_time = np.round(gan_model.total_training_time / 60, 2)
        title = "Epochs: {}, z_dim: {}, Time trained: {} minutes\nParams: {}\n\n".format(
            epochs, z_dim, training_time, gan_model.get_number_params())
        fig, axs = utils.plot_images(images=samples, show=False)
        fig.suptitle(title, fontsize=12)
        fig.tight_layout()
        plt.savefig(gan_model.folder + "/generated_images.png")

        fig, axs = utils.plot_losses(losses=losses, show=False)
        fig.suptitle(title, fontsize=12)
        fig.tight_layout()
        plt.savefig(gan_model.folder + "/losses.png")
        # gan_model.save()
Esempio n. 7
0
        gan_model.summary(save=True)
        gan_model.fit(X_train=X_train,
                      y_train=y_train,
                      X_test=X_test,
                      y_test=y_test,
                      batch_size=batch_size,
                      epochs=epochs,
                      steps=None,
                      print_every="0.2e",
                      save_model_every=None,
                      save_images_every="0.2e",
                      save_losses_every=10,
                      enable_tensorboard=False)
        samples, losses = gan_model.get_training_results(by_epoch=False)

        training_time = np.round(gan_model.total_training_time / 60, 2)
        title = "Epochs: {}, z_dim: {}, Time trained: {} minutes\nParams: {}\n\n".format(
            epochs, z_dim, training_time, gan_model.get_number_params())
        fixed_labels = np.argmax(gan_model.get_fixed_labels(), axis=1)
        fig, axs = utils.plot_images(images=samples,
                                     labels=fixed_labels,
                                     show=False)
        fig.suptitle(title, fontsize=12)
        fig.tight_layout()
        plt.savefig(gan_model.folder + "/generated_images.png")
        fig, axs = utils.plot_losses(losses=losses, show=False)
        fig.suptitle(title, fontsize=12)
        fig.tight_layout()
        plt.savefig(gan_model.folder + "/losses.png")
        # gan_model.save()
Esempio n. 8
0
    generator = loader.load_generator(x_dim=x_dim, z_dim=z_dim, y_dim=None)
    discriminator = loader.load_adversary(x_dim=x_dim, y_dim=None)

    gan = VanillaGAN(generator=generator,
                     adversary=discriminator,
                     z_dim=z_dim,
                     x_dim=x_dim,
                     folder=None)
    gan.summary()  # optional, shows architecture

    # Training
    gan.fit(X_train[:300], enable_tensorboard=False)

    # Vizualise results
    images, losses = gan.get_training_results()
    utils.plot_images(images)
    utils.plot_losses(losses)

    # Sample new images, you can also pass a specific noise vector
    samples = gan.generate(n=36)
    utils.plot_images(samples)

elif mode == "supervised":
    import torch
    import numpy as np
    import vegans.utils as utils
    import vegans.utils.loading as loading
    from vegans.GAN import ConditionalVanillaGAN

    # Data preparation (Load your own data or example MNIST)
    loader = loading.MNISTLoader()