last latent

  • image output
  • image input
  • image init_latents
  • image

py

        def image_grid(imgs, rows, cols):
            assert len(imgs) == rows*cols

            w, h = imgs[0].size
            grid = PIL.Image.new('L', size=(cols*w, rows*h))
            grid_w, grid_h = grid.size
            
            for i, img in enumerate(imgs):
                grid.paste(img, box=(i%cols*w, i//cols*h, i%cols*w + w, i//cols*h + h))
            return grid

        def save_latent(x, name):
            x = torch.clamp((x[0] + 1.0) / 2.0, min=0.0, max=1.0)
            x = x.cpu().numpy()
            x = x * 255.
            imgs = []
            for i, y in enumerate(x):
                from PIL import Image
                img = Image.fromarray(y.astype(np.uint8))
                imgs.append(img)
            image_grid(imgs, 2, 2).save(name)

        save_latent(init_latents, "init_latents.png")

This page is auto-translated from [/nishio/Stable Diffusion Latent Space Visualization](https://scrapbox.io/nishio/Stable Diffusion Latent Space Visualization) using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I’m very happy to spread my thought to non-Japanese readers.