Exemplo n.º 1
0
    def visualize_generator(self,
                            epoch,
                            to_mlflow=False,
                            is_remote=False,
                            vmin=0,
                            vmax=1,
                            *args,
                            **kwargs):
        # Check how the generator is doing by saving G's output on fixed_noise
        path = TMP_IMAGES_DIR
        # Evaluation mode
        self.generator.eval()

        with torch.no_grad():
            fake = self.generator(self.fixed_noise)[0].detach().cpu()
            img = vutils.make_grid(fake,
                                   padding=20,
                                   normalize=False,
                                   range=(vmin, vmax))
            path = f'{path}/epoch{epoch}.png'
            vutils.save_image(img, path)

            if to_mlflow:
                log_artifact(path, 'images', is_remote=is_remote)
                os.remove(path)
Exemplo n.º 2
0
    def forward_and_save_one_image(self, inp_image, label, epoch, path=TMP_IMAGES_DIR, to_mlflow=False,
                                   is_remote=False, vmin=0, vmax=1):
        """
        Reconstructs one image and writes two images (original and reconstructed) in one figure to :param path.
        :param inp_image: Image for evaluation
        :param label: Label of image
        :param epoch: Epoch
        :param path: Path to save image to
        """
        # Evaluation mode
        self.generator.eval()
        self.encoder.eval()
        with torch.no_grad():
            # Format input batch
            inp = inp_image.to(self.device)

            # Forward pass
            z_mean, z_logvar, _, _ = self.encoder(inp)
            # z_hat = z_mean + z_logvar * torch.randn(z_mean.size()).to(self.device)
            if len(z_mean.size()) == 1:
                z_mean = z_mean.view(1, z_mean.size(0))
            x_rec, _, _ = self.generator(z_mean)

            inp_image = inp_image.to('cpu')
            x_rec = x_rec.to('cpu')
            fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
            ax[0].imshow(inp_image.numpy()[0, 0, :, :], cmap='gray', vmin=vmin, vmax=vmax)
            ax[1].imshow(x_rec.numpy()[0, 0, :, :], cmap='gray', vmin=vmin, vmax=vmax)
            path = f'{path}/epoch{epoch}_label{int(label)}.png'
            plt.savefig(path)
            plt.close(fig)

            if to_mlflow:
                log_artifact(path, 'images', is_remote=is_remote)
                os.remove(path)
Exemplo n.º 3
0
    def forward_and_save_one_image(self,
                                   inp_image,
                                   label,
                                   epoch,
                                   path=TMP_IMAGES_DIR,
                                   to_mlflow=False,
                                   is_remote=False):
        """
        Reconstructs one image and writes two images (original and reconstructed) in one figure to :param path.
        :param is_remote:
        :param to_mlflow:
        :param inp_image: Image for evaluation
        :param label: Label of image
        :param epoch: Epoch
        :param path: Path to save image to
        """
        # Evaluation mode
        self.eval()
        with torch.no_grad():
            # Format input batch
            inp = inp_image.to(self.device)

            # Forward pass
            output = self(inp)
            output_img = output.to('cpu')

            fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
            ax[0].imshow(inp_image.numpy()[0, 0, :, :],
                         cmap='gray',
                         vmin=0,
                         vmax=1)
            ax[1].imshow(output_img.numpy()[0, 0, :, :],
                         cmap='gray',
                         vmin=0,
                         vmax=1)
            path = f'{path}/epoch{epoch}_label{int(label)}.png'
            plt.savefig(path)
            plt.close(fig)

            if to_mlflow:
                log_artifact(path, 'images', is_remote=is_remote)
                os.remove(path)
Exemplo n.º 4
0
    def forward_and_save_one_image(self,
                                   inp_image,
                                   label,
                                   epoch,
                                   path=TMP_IMAGES_DIR,
                                   to_mlflow=False,
                                   is_remote=False):
        """
        Save random sample of original and reconstructed image
        :param inp_image: input original image
        :param label: label of image
        :param epoch: number of epoch
        :param device: device type: CPU or CUDA GPU
        :param path: path to save
        """
        self.eval()
        with torch.no_grad():
            inp = inp_image.to(self.device)
            output, _, _ = self(inp)
            output_img = output.to('cpu')

            fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
            ax[0].imshow(inp_image.numpy()[0, 0, :, :],
                         cmap='gray',
                         vmin=0,
                         vmax=1)
            ax[1].imshow(output_img.numpy()[0, 0, :, :],
                         cmap='gray',
                         vmin=0,
                         vmax=1)
            path = f'{path}/epoch{epoch}_label{int(label)}.png'
            plt.savefig(path)
            plt.close(fig)

            if to_mlflow:
                log_artifact(path, 'images', is_remote=is_remote)
                os.remove(path)