Generating latent vectors using diffusers
In this section, we are going to use a pre-trained Stable Diffusion model to encode an image into latent space so that we have a concrete impression of what a latent vector looks and feels like. Then, we will decode the latent vector back into an image. This operation will also establish the foundation for building the image-to-image custom pipeline:
- Load an image: We can use the
load_image
function fromdiffusers
to load an image from local storage or a URL. In the following code, we load an image nameddog.png
from the same directory of the current program:from diffusers.utils import load_image
image = load_image("dog.png")
display(image)
- Pre-process the image: Each pixel of the loaded image is represented by a number ranging from 0 to 255. The image encoder from the Stable Diffusion process handles image data ranging from -1.0 to 1.0. So, we first need to make the data range conversion:
import numpy as np
# convert image...