0
0
PyTorchml~5 mins

Image generation basics in PyTorch

Choose your learning style9 modes available
Introduction

Image generation helps computers create new pictures from scratch. It is useful for art, design, and learning how images work.

Creating new artwork or designs automatically.
Generating images for games or virtual worlds.
Helping computers understand how images are made.
Making variations of photos for data augmentation.
Building fun apps that draw pictures from text or noise.
Syntax
PyTorch
import torch
import torch.nn as nn

class SimpleGenerator(nn.Module):
    def __init__(self, input_dim, output_dim):
        super(SimpleGenerator, self).__init__()
        self.model = nn.Sequential(
            nn.Linear(input_dim, 128),
            nn.ReLU(),
            nn.Linear(128, 256),
            nn.ReLU(),
            nn.Linear(256, output_dim),
            nn.Tanh()
        )

    def forward(self, x):
        return self.model(x)

The generator takes random noise as input and creates an image-like output.

Use nn.Tanh() at the end to keep output values between -1 and 1, which is common for images.

Examples
Create a random noise vector of size 100, then generate a fake 28x28 image flattened to 784 pixels.
PyTorch
noise = torch.randn(1, 100)
generator = SimpleGenerator(100, 784)  # 28x28 image flattened
fake_image = generator(noise)
Generate a batch of 16 fake images at once from random noise.
PyTorch
batch_noise = torch.randn(16, 100)
fake_images = generator(batch_noise)
Sample Model

This program creates a simple generator model that turns random noise into a fake 28x28 image. It prints the shape and pixel value range of the generated image.

PyTorch
import torch
import torch.nn as nn

class SimpleGenerator(nn.Module):
    def __init__(self, input_dim, output_dim):
        super(SimpleGenerator, self).__init__()
        self.model = nn.Sequential(
            nn.Linear(input_dim, 128),
            nn.ReLU(),
            nn.Linear(128, 256),
            nn.ReLU(),
            nn.Linear(256, output_dim),
            nn.Tanh()
        )

    def forward(self, x):
        return self.model(x)

# Set random seed for reproducibility
torch.manual_seed(42)

# Create generator instance
input_dim = 100
output_dim = 28 * 28  # 784 pixels for 28x28 image
generator = SimpleGenerator(input_dim, output_dim)

# Generate one fake image
noise = torch.randn(1, input_dim)
fake_image = generator(noise)

# Check output shape and value range
print(f"Fake image shape: {fake_image.shape}")
print(f"Min pixel value: {fake_image.min().item():.4f}")
print(f"Max pixel value: {fake_image.max().item():.4f}")
OutputSuccess
Important Notes

Image generation models often start from random noise to create new images.

Output values are usually scaled between -1 and 1 for easier training and normalization.

This example is very simple; real image generators use more layers and special blocks like convolution.

Summary

Image generation creates new pictures from random inputs.

Generators are neural networks that learn to make images from noise.

Output images are often flattened and scaled between -1 and 1.