Reconstructs a grayscale image using a combination of 2D Gaussians. Initializes a set of Gaussians over the image, then refines their parameters (e.g., position, spread, orientation, color) to best match the original image.
Original Image:
Gaussians after 1 iteration:
Gaussians after 300 iterations:
import torch
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import imageio
from IPython.display import Video, display
Defines a 2D Gaussian function with parameters for position, spread, intensity, and rotation.
def gaussian_2d(x, y, mu_x, mu_y, sigma_x, sigma_y, color_intensity, rotation_angle):
Defines a Mean Squared Error (MSE) loss function to measure the difference between the original image and the Gaussian-based reconstruction.
def mse_loss(params):
Uses the Adam optimizer to refine the Gaussians' parameters for better image reconstruction. Also employs a learning rate scheduler for better convergence.
optimizer = optim.Adam([params], lr=0.5)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.7)