Menu iconMenu iconGenerative Deep Learning Updated Edition
Generative Deep Learning Updated Edition

Chapter 10: Project: Image Generation with Diffusion Models

10.4 Generating New Images

Once the diffusion model is trained, we can use it to generate new images from random noise. This process involves starting with a noise vector and iteratively applying the model to remove noise, transforming the random noise into coherent and structured images. In this section, we will detail the steps to generate new images using the trained diffusion model, providing example codes to illustrate each step.

10.4.1 Initializing Random Noise

The first step in generating new images is to initialize a batch of random noise vectors. These noise vectors will serve as the starting point for the diffusion model to transform into images.

Example: Initializing Random Noise

import numpy as np

# Define the shape of the noise vectors
noise_shape = (32, 32, 3)
batch_size = 10

# Generate a batch of random noise vectors
random_noise = np.random.normal(size=(batch_size, *noise_shape))

# Print the shape of the noise vectors
print(f"Random noise shape: {random_noise.shape}")

The code is creating the batch of random noise vectors. It first imports the numpy library. Then, it defines the shape of the noise vectors as a tuple (32, 32, 3) and the batch size as 10.

After that, it uses numpy's random.normal function to generate a batch of random noise vectors, where the size of the generated array is (batch_size, *noise_shape). The asterisk before noise_shape is used to unpack the values in the noise_shape tuple. Finally, it prints the shape of the generated random noise batch.

10.4.2 Iterative Denoising Process

The core of the image generation process involves iteratively applying the trained diffusion model to the noise vectors. At each step, the model predicts and removes a portion of the noise, gradually transforming the random noise into structured images. We also use step encodings to guide the model through the diffusion steps.

Example: Iterative Denoising Process

def generate_images(model, noise_vectors, num_steps, d_model):
    """
    Generates images by iteratively applying the diffusion model.

    Parameters:
    - model: The trained diffusion model.
    - noise_vectors: Batch of random noise vectors.
    - num_steps: Number of diffusion steps.
    - d_model: Dimensionality of the step encoding.

    Returns:
    - Generated images.
    """
    generated_images = noise_vectors.copy()
    for step in range(num_steps):
        step_encodings = sinusoidal_step_encoding(np.full((batch_size, 1), step), d_model)
        generated_images = model.predict([generated_images, step_encodings])
    return generated_images

# Example usage
num_steps = 10
generated_images = generate_images(diffusion_model, random_noise, num_steps, d_model)

# Plot the generated images
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
for i in range(batch_size):
    plt.subplot(2, 5, i + 1)
    plt.imshow((generated_images[i] * 0.5) + 0.5)
    plt.axis('off')
plt.suptitle('Generated Images')
plt.show()

The function generate_images applies the diffusion model to noise vectors for a specified number of steps. It also uses sinusoidal step encoding during each step.

The function takes four parameters: the trained diffusion model, the noise vectors, the number of diffusion steps, and the dimensionality of the step encoding. It returns the generated images.

An example of how to use the function is also provided. It demonstrates generating images with the diffusion model using random noise and plotting the generated images using matplotlib.

10.4.3 Enhancing Image Quality

To further enhance the quality of the generated images, we can apply post-processing techniques such as image filtering and sharpening. These techniques can help improve the visual appeal of the images and remove any remaining artifacts.

Example: Enhancing Image Quality

from skimage.filters import unsharp_mask

def enhance_images(images):
    """
    Enhances the quality of generated images using image filtering.

    Parameters:
    - images: Batch of generated images.

    Returns:
    - Enhanced images.
    """
    enhanced_images = []
    for img in images:
        enhanced_img = unsharp_mask((img * 0.5) + 0.5, radius=1.0, amount=1.0)
        enhanced_images.append(enhanced_img)
    return np.array(enhanced_images)

# Example usage
enhanced_images = enhance_images(generated_images)

# Plot the enhanced images
plt.figure(figsize=(12, 4))
for i in range(batch_size):
    plt.subplot(2, 5, i + 1)
    plt.imshow(enhanced_images[i])
    plt.axis('off')
plt.suptitle('Enhanced Images')
plt.show()

This code enhances the quality of images using a technique called unsharp masking. The function 'enhance_images' takes in a batch of images as input, applies unsharp masking to each image using the 'unsharp_mask' function from the 'skimage.filters' library, and returns the enhanced images.

After the images are enhanced, the code also includes an example of how to use this function and how to plot these images using matplotlib's pyplot library. The plotting section of the code shows each enhanced image in a separate subplot without any axes, and gives the overall plot a title of 'Enhanced Images'.

10.4.4 Saving the Generated Images

After generating and enhancing the images, it is important to save them for future use or evaluation. We can save the images in a format such as PNG or JPEG.

Example: Saving the Generated Images

import os
from skimage.io import imsave

def save_images(images, directory, prefix="generated_image"):
    """
    Saves generated images to the specified directory.

    Parameters:
    - images: Batch of generated images.
    - directory: Directory to save the images.
    - prefix: Prefix for the image filenames.
    """
    if not os.path.exists(directory):
        os.makedirs(directory)
    for i, img in enumerate(images):
        filename = os.path.join(directory, f"{prefix}_{i + 1}.png")
        imsave(filename, (img * 255).astype(np.uint8))

# Example usage
save_images(enhanced_images, "generated_images")

This script is a function to save the batch of images to a specified directory. The function takes as arguments images (the batch of images to be saved), directory (the location where the images will be saved), and an optional prefix (the beginning part of the saved file's name). If the specified directory does not exist, it will be created. The images are saved in PNG format with the filename being a combination of the prefix and their position in the batch.

Summary

In this section, we covered the detailed process of generating new images using the trained diffusion model. We started by initializing random noise vectors, which serve as the starting point for image generation. We then applied the iterative denoising process, where the model predicts and removes noise from the images step-by-step, transforming the noise into coherent and structured images.

To further enhance the quality of the generated images, we applied post-processing techniques such as image filtering and sharpening. Finally, we saved the generated and enhanced images for future use or evaluation.

By following these steps, you can effectively use a trained diffusion model to generate high-quality images from random noise. In the next sections, we will evaluate the performance of the generated images and explore further enhancements and applications of diffusion models in image generation tasks.

10.4 Generating New Images

Once the diffusion model is trained, we can use it to generate new images from random noise. This process involves starting with a noise vector and iteratively applying the model to remove noise, transforming the random noise into coherent and structured images. In this section, we will detail the steps to generate new images using the trained diffusion model, providing example codes to illustrate each step.

10.4.1 Initializing Random Noise

The first step in generating new images is to initialize a batch of random noise vectors. These noise vectors will serve as the starting point for the diffusion model to transform into images.

Example: Initializing Random Noise

import numpy as np

# Define the shape of the noise vectors
noise_shape = (32, 32, 3)
batch_size = 10

# Generate a batch of random noise vectors
random_noise = np.random.normal(size=(batch_size, *noise_shape))

# Print the shape of the noise vectors
print(f"Random noise shape: {random_noise.shape}")

The code is creating the batch of random noise vectors. It first imports the numpy library. Then, it defines the shape of the noise vectors as a tuple (32, 32, 3) and the batch size as 10.

After that, it uses numpy's random.normal function to generate a batch of random noise vectors, where the size of the generated array is (batch_size, *noise_shape). The asterisk before noise_shape is used to unpack the values in the noise_shape tuple. Finally, it prints the shape of the generated random noise batch.

10.4.2 Iterative Denoising Process

The core of the image generation process involves iteratively applying the trained diffusion model to the noise vectors. At each step, the model predicts and removes a portion of the noise, gradually transforming the random noise into structured images. We also use step encodings to guide the model through the diffusion steps.

Example: Iterative Denoising Process

def generate_images(model, noise_vectors, num_steps, d_model):
    """
    Generates images by iteratively applying the diffusion model.

    Parameters:
    - model: The trained diffusion model.
    - noise_vectors: Batch of random noise vectors.
    - num_steps: Number of diffusion steps.
    - d_model: Dimensionality of the step encoding.

    Returns:
    - Generated images.
    """
    generated_images = noise_vectors.copy()
    for step in range(num_steps):
        step_encodings = sinusoidal_step_encoding(np.full((batch_size, 1), step), d_model)
        generated_images = model.predict([generated_images, step_encodings])
    return generated_images

# Example usage
num_steps = 10
generated_images = generate_images(diffusion_model, random_noise, num_steps, d_model)

# Plot the generated images
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
for i in range(batch_size):
    plt.subplot(2, 5, i + 1)
    plt.imshow((generated_images[i] * 0.5) + 0.5)
    plt.axis('off')
plt.suptitle('Generated Images')
plt.show()

The function generate_images applies the diffusion model to noise vectors for a specified number of steps. It also uses sinusoidal step encoding during each step.

The function takes four parameters: the trained diffusion model, the noise vectors, the number of diffusion steps, and the dimensionality of the step encoding. It returns the generated images.

An example of how to use the function is also provided. It demonstrates generating images with the diffusion model using random noise and plotting the generated images using matplotlib.

10.4.3 Enhancing Image Quality

To further enhance the quality of the generated images, we can apply post-processing techniques such as image filtering and sharpening. These techniques can help improve the visual appeal of the images and remove any remaining artifacts.

Example: Enhancing Image Quality

from skimage.filters import unsharp_mask

def enhance_images(images):
    """
    Enhances the quality of generated images using image filtering.

    Parameters:
    - images: Batch of generated images.

    Returns:
    - Enhanced images.
    """
    enhanced_images = []
    for img in images:
        enhanced_img = unsharp_mask((img * 0.5) + 0.5, radius=1.0, amount=1.0)
        enhanced_images.append(enhanced_img)
    return np.array(enhanced_images)

# Example usage
enhanced_images = enhance_images(generated_images)

# Plot the enhanced images
plt.figure(figsize=(12, 4))
for i in range(batch_size):
    plt.subplot(2, 5, i + 1)
    plt.imshow(enhanced_images[i])
    plt.axis('off')
plt.suptitle('Enhanced Images')
plt.show()

This code enhances the quality of images using a technique called unsharp masking. The function 'enhance_images' takes in a batch of images as input, applies unsharp masking to each image using the 'unsharp_mask' function from the 'skimage.filters' library, and returns the enhanced images.

After the images are enhanced, the code also includes an example of how to use this function and how to plot these images using matplotlib's pyplot library. The plotting section of the code shows each enhanced image in a separate subplot without any axes, and gives the overall plot a title of 'Enhanced Images'.

10.4.4 Saving the Generated Images

After generating and enhancing the images, it is important to save them for future use or evaluation. We can save the images in a format such as PNG or JPEG.

Example: Saving the Generated Images

import os
from skimage.io import imsave

def save_images(images, directory, prefix="generated_image"):
    """
    Saves generated images to the specified directory.

    Parameters:
    - images: Batch of generated images.
    - directory: Directory to save the images.
    - prefix: Prefix for the image filenames.
    """
    if not os.path.exists(directory):
        os.makedirs(directory)
    for i, img in enumerate(images):
        filename = os.path.join(directory, f"{prefix}_{i + 1}.png")
        imsave(filename, (img * 255).astype(np.uint8))

# Example usage
save_images(enhanced_images, "generated_images")

This script is a function to save the batch of images to a specified directory. The function takes as arguments images (the batch of images to be saved), directory (the location where the images will be saved), and an optional prefix (the beginning part of the saved file's name). If the specified directory does not exist, it will be created. The images are saved in PNG format with the filename being a combination of the prefix and their position in the batch.

Summary

In this section, we covered the detailed process of generating new images using the trained diffusion model. We started by initializing random noise vectors, which serve as the starting point for image generation. We then applied the iterative denoising process, where the model predicts and removes noise from the images step-by-step, transforming the noise into coherent and structured images.

To further enhance the quality of the generated images, we applied post-processing techniques such as image filtering and sharpening. Finally, we saved the generated and enhanced images for future use or evaluation.

By following these steps, you can effectively use a trained diffusion model to generate high-quality images from random noise. In the next sections, we will evaluate the performance of the generated images and explore further enhancements and applications of diffusion models in image generation tasks.

10.4 Generating New Images

Once the diffusion model is trained, we can use it to generate new images from random noise. This process involves starting with a noise vector and iteratively applying the model to remove noise, transforming the random noise into coherent and structured images. In this section, we will detail the steps to generate new images using the trained diffusion model, providing example codes to illustrate each step.

10.4.1 Initializing Random Noise

The first step in generating new images is to initialize a batch of random noise vectors. These noise vectors will serve as the starting point for the diffusion model to transform into images.

Example: Initializing Random Noise

import numpy as np

# Define the shape of the noise vectors
noise_shape = (32, 32, 3)
batch_size = 10

# Generate a batch of random noise vectors
random_noise = np.random.normal(size=(batch_size, *noise_shape))

# Print the shape of the noise vectors
print(f"Random noise shape: {random_noise.shape}")

The code is creating the batch of random noise vectors. It first imports the numpy library. Then, it defines the shape of the noise vectors as a tuple (32, 32, 3) and the batch size as 10.

After that, it uses numpy's random.normal function to generate a batch of random noise vectors, where the size of the generated array is (batch_size, *noise_shape). The asterisk before noise_shape is used to unpack the values in the noise_shape tuple. Finally, it prints the shape of the generated random noise batch.

10.4.2 Iterative Denoising Process

The core of the image generation process involves iteratively applying the trained diffusion model to the noise vectors. At each step, the model predicts and removes a portion of the noise, gradually transforming the random noise into structured images. We also use step encodings to guide the model through the diffusion steps.

Example: Iterative Denoising Process

def generate_images(model, noise_vectors, num_steps, d_model):
    """
    Generates images by iteratively applying the diffusion model.

    Parameters:
    - model: The trained diffusion model.
    - noise_vectors: Batch of random noise vectors.
    - num_steps: Number of diffusion steps.
    - d_model: Dimensionality of the step encoding.

    Returns:
    - Generated images.
    """
    generated_images = noise_vectors.copy()
    for step in range(num_steps):
        step_encodings = sinusoidal_step_encoding(np.full((batch_size, 1), step), d_model)
        generated_images = model.predict([generated_images, step_encodings])
    return generated_images

# Example usage
num_steps = 10
generated_images = generate_images(diffusion_model, random_noise, num_steps, d_model)

# Plot the generated images
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
for i in range(batch_size):
    plt.subplot(2, 5, i + 1)
    plt.imshow((generated_images[i] * 0.5) + 0.5)
    plt.axis('off')
plt.suptitle('Generated Images')
plt.show()

The function generate_images applies the diffusion model to noise vectors for a specified number of steps. It also uses sinusoidal step encoding during each step.

The function takes four parameters: the trained diffusion model, the noise vectors, the number of diffusion steps, and the dimensionality of the step encoding. It returns the generated images.

An example of how to use the function is also provided. It demonstrates generating images with the diffusion model using random noise and plotting the generated images using matplotlib.

10.4.3 Enhancing Image Quality

To further enhance the quality of the generated images, we can apply post-processing techniques such as image filtering and sharpening. These techniques can help improve the visual appeal of the images and remove any remaining artifacts.

Example: Enhancing Image Quality

from skimage.filters import unsharp_mask

def enhance_images(images):
    """
    Enhances the quality of generated images using image filtering.

    Parameters:
    - images: Batch of generated images.

    Returns:
    - Enhanced images.
    """
    enhanced_images = []
    for img in images:
        enhanced_img = unsharp_mask((img * 0.5) + 0.5, radius=1.0, amount=1.0)
        enhanced_images.append(enhanced_img)
    return np.array(enhanced_images)

# Example usage
enhanced_images = enhance_images(generated_images)

# Plot the enhanced images
plt.figure(figsize=(12, 4))
for i in range(batch_size):
    plt.subplot(2, 5, i + 1)
    plt.imshow(enhanced_images[i])
    plt.axis('off')
plt.suptitle('Enhanced Images')
plt.show()

This code enhances the quality of images using a technique called unsharp masking. The function 'enhance_images' takes in a batch of images as input, applies unsharp masking to each image using the 'unsharp_mask' function from the 'skimage.filters' library, and returns the enhanced images.

After the images are enhanced, the code also includes an example of how to use this function and how to plot these images using matplotlib's pyplot library. The plotting section of the code shows each enhanced image in a separate subplot without any axes, and gives the overall plot a title of 'Enhanced Images'.

10.4.4 Saving the Generated Images

After generating and enhancing the images, it is important to save them for future use or evaluation. We can save the images in a format such as PNG or JPEG.

Example: Saving the Generated Images

import os
from skimage.io import imsave

def save_images(images, directory, prefix="generated_image"):
    """
    Saves generated images to the specified directory.

    Parameters:
    - images: Batch of generated images.
    - directory: Directory to save the images.
    - prefix: Prefix for the image filenames.
    """
    if not os.path.exists(directory):
        os.makedirs(directory)
    for i, img in enumerate(images):
        filename = os.path.join(directory, f"{prefix}_{i + 1}.png")
        imsave(filename, (img * 255).astype(np.uint8))

# Example usage
save_images(enhanced_images, "generated_images")

This script is a function to save the batch of images to a specified directory. The function takes as arguments images (the batch of images to be saved), directory (the location where the images will be saved), and an optional prefix (the beginning part of the saved file's name). If the specified directory does not exist, it will be created. The images are saved in PNG format with the filename being a combination of the prefix and their position in the batch.

Summary

In this section, we covered the detailed process of generating new images using the trained diffusion model. We started by initializing random noise vectors, which serve as the starting point for image generation. We then applied the iterative denoising process, where the model predicts and removes noise from the images step-by-step, transforming the noise into coherent and structured images.

To further enhance the quality of the generated images, we applied post-processing techniques such as image filtering and sharpening. Finally, we saved the generated and enhanced images for future use or evaluation.

By following these steps, you can effectively use a trained diffusion model to generate high-quality images from random noise. In the next sections, we will evaluate the performance of the generated images and explore further enhancements and applications of diffusion models in image generation tasks.

10.4 Generating New Images

Once the diffusion model is trained, we can use it to generate new images from random noise. This process involves starting with a noise vector and iteratively applying the model to remove noise, transforming the random noise into coherent and structured images. In this section, we will detail the steps to generate new images using the trained diffusion model, providing example codes to illustrate each step.

10.4.1 Initializing Random Noise

The first step in generating new images is to initialize a batch of random noise vectors. These noise vectors will serve as the starting point for the diffusion model to transform into images.

Example: Initializing Random Noise

import numpy as np

# Define the shape of the noise vectors
noise_shape = (32, 32, 3)
batch_size = 10

# Generate a batch of random noise vectors
random_noise = np.random.normal(size=(batch_size, *noise_shape))

# Print the shape of the noise vectors
print(f"Random noise shape: {random_noise.shape}")

The code is creating the batch of random noise vectors. It first imports the numpy library. Then, it defines the shape of the noise vectors as a tuple (32, 32, 3) and the batch size as 10.

After that, it uses numpy's random.normal function to generate a batch of random noise vectors, where the size of the generated array is (batch_size, *noise_shape). The asterisk before noise_shape is used to unpack the values in the noise_shape tuple. Finally, it prints the shape of the generated random noise batch.

10.4.2 Iterative Denoising Process

The core of the image generation process involves iteratively applying the trained diffusion model to the noise vectors. At each step, the model predicts and removes a portion of the noise, gradually transforming the random noise into structured images. We also use step encodings to guide the model through the diffusion steps.

Example: Iterative Denoising Process

def generate_images(model, noise_vectors, num_steps, d_model):
    """
    Generates images by iteratively applying the diffusion model.

    Parameters:
    - model: The trained diffusion model.
    - noise_vectors: Batch of random noise vectors.
    - num_steps: Number of diffusion steps.
    - d_model: Dimensionality of the step encoding.

    Returns:
    - Generated images.
    """
    generated_images = noise_vectors.copy()
    for step in range(num_steps):
        step_encodings = sinusoidal_step_encoding(np.full((batch_size, 1), step), d_model)
        generated_images = model.predict([generated_images, step_encodings])
    return generated_images

# Example usage
num_steps = 10
generated_images = generate_images(diffusion_model, random_noise, num_steps, d_model)

# Plot the generated images
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
for i in range(batch_size):
    plt.subplot(2, 5, i + 1)
    plt.imshow((generated_images[i] * 0.5) + 0.5)
    plt.axis('off')
plt.suptitle('Generated Images')
plt.show()

The function generate_images applies the diffusion model to noise vectors for a specified number of steps. It also uses sinusoidal step encoding during each step.

The function takes four parameters: the trained diffusion model, the noise vectors, the number of diffusion steps, and the dimensionality of the step encoding. It returns the generated images.

An example of how to use the function is also provided. It demonstrates generating images with the diffusion model using random noise and plotting the generated images using matplotlib.

10.4.3 Enhancing Image Quality

To further enhance the quality of the generated images, we can apply post-processing techniques such as image filtering and sharpening. These techniques can help improve the visual appeal of the images and remove any remaining artifacts.

Example: Enhancing Image Quality

from skimage.filters import unsharp_mask

def enhance_images(images):
    """
    Enhances the quality of generated images using image filtering.

    Parameters:
    - images: Batch of generated images.

    Returns:
    - Enhanced images.
    """
    enhanced_images = []
    for img in images:
        enhanced_img = unsharp_mask((img * 0.5) + 0.5, radius=1.0, amount=1.0)
        enhanced_images.append(enhanced_img)
    return np.array(enhanced_images)

# Example usage
enhanced_images = enhance_images(generated_images)

# Plot the enhanced images
plt.figure(figsize=(12, 4))
for i in range(batch_size):
    plt.subplot(2, 5, i + 1)
    plt.imshow(enhanced_images[i])
    plt.axis('off')
plt.suptitle('Enhanced Images')
plt.show()

This code enhances the quality of images using a technique called unsharp masking. The function 'enhance_images' takes in a batch of images as input, applies unsharp masking to each image using the 'unsharp_mask' function from the 'skimage.filters' library, and returns the enhanced images.

After the images are enhanced, the code also includes an example of how to use this function and how to plot these images using matplotlib's pyplot library. The plotting section of the code shows each enhanced image in a separate subplot without any axes, and gives the overall plot a title of 'Enhanced Images'.

10.4.4 Saving the Generated Images

After generating and enhancing the images, it is important to save them for future use or evaluation. We can save the images in a format such as PNG or JPEG.

Example: Saving the Generated Images

import os
from skimage.io import imsave

def save_images(images, directory, prefix="generated_image"):
    """
    Saves generated images to the specified directory.

    Parameters:
    - images: Batch of generated images.
    - directory: Directory to save the images.
    - prefix: Prefix for the image filenames.
    """
    if not os.path.exists(directory):
        os.makedirs(directory)
    for i, img in enumerate(images):
        filename = os.path.join(directory, f"{prefix}_{i + 1}.png")
        imsave(filename, (img * 255).astype(np.uint8))

# Example usage
save_images(enhanced_images, "generated_images")

This script is a function to save the batch of images to a specified directory. The function takes as arguments images (the batch of images to be saved), directory (the location where the images will be saved), and an optional prefix (the beginning part of the saved file's name). If the specified directory does not exist, it will be created. The images are saved in PNG format with the filename being a combination of the prefix and their position in the batch.

Summary

In this section, we covered the detailed process of generating new images using the trained diffusion model. We started by initializing random noise vectors, which serve as the starting point for image generation. We then applied the iterative denoising process, where the model predicts and removes noise from the images step-by-step, transforming the noise into coherent and structured images.

To further enhance the quality of the generated images, we applied post-processing techniques such as image filtering and sharpening. Finally, we saved the generated and enhanced images for future use or evaluation.

By following these steps, you can effectively use a trained diffusion model to generate high-quality images from random noise. In the next sections, we will evaluate the performance of the generated images and explore further enhancements and applications of diffusion models in image generation tasks.