Author: Jacob Pitsenberger
Date: 12-28-23
A PyTorch project implementing neural style transfer using the VGG19 model, combining the content of one image with the style of another for artistic transformations.
Wheatfield with Crows Painting by Vincent van Gogh
This PyTorch Style Transfer project applies neural style transfer using a VGG19 model. The following steps were taken:
-
Load Pre-trained VGG Net:
- The script loads a pre-trained VGG19 model and freezes its weights to use it as a fixed feature extractor in the start of the
main
function.
- The script loads a pre-trained VGG19 model and freezes its weights to use it as a fixed feature extractor in the start of the
-
Load and Transform Images:
- Content and style images are loaded using the
load_image
function, which transforms them into PyTorch tensors.
- Content and style images are loaded using the
-
Visualize Content and Style:
- The
display_content_and_style_images
function is used to visualize the loaded content and style images side-by-side.
- The
-
Extract Features:
- The
get_features
function extracts features from the VGG model for specified layers.
- The
-
Gram Matrix Calculation:
- The
gram_matrix
function calculates the Gram Matrix for a given convolutional layer.
- The
-
Define Loss Functions:
- Content and style loss functions are defined in the
image_style_transfer
function for iteratively updating the target image.
- Content and style loss functions are defined in the
-
Perform Style Transfer:
- Style transfer is performed using the
image_style_transfer
function, iterating over a set number of steps.
- Style transfer is performed using the
-
Display Intermediate Results:
- Intermediate results are displayed at specified intervals during the optimization process as defined in the
image_style_transfer
function.
- Intermediate results are displayed at specified intervals during the optimization process as defined in the
-
Main Execution:
- The
main
function orchestrates the entire style transfer process by defining model, loading images, and initiating the style transfer.
- The
Feel free to experiment with different content and style images by adjusting the image paths in the main
function.
This project serves as a practical application and demonstration of the knowledge gained during the Udacity course "Intro to Deep Learning with PyTorch," focusing on the application of Style Transfer. The primary objectives were:
-
Understanding Neural Style Transfer: Implementing the neural style transfer technique, a fascinating application of deep learning that combines the content of one image with the style of another.
-
Utilizing Pre-trained VGG19 Model: Leveraging the VGG19 model, pre-trained on ImageNet, as a fixed feature extractor to extract content and style features from images.
-
Gram Matrix Calculation: Demonstrating the computation of the Gram Matrix, a crucial step in capturing the style representation of an image.
-
Optimizing Target Image: Iteratively updating a target image to minimize the content loss and style loss, achieving a stylized image that combines the content and style features.
-
Showcasing PyTorch Skills: Showcasing proficiency in PyTorch, including loading images, defining and training models, and using GPU acceleration for efficient computations.
This project provides hands-on experience in applying deep learning concepts to artistic image transformations, with a focus on neural style transfer techniques.
- style_transfer.py: Main module for style transfer implementation.
- images: Directory containing input images for content and style and the output target image.
-
Install required dependencies:
pip install torch torchvision Pillow matplotlib numpy
-
Run the main script:
python style_transfer.py
Load and transform an image, ensuring it is <= 400 pixels in the x-y dimensions.
Display a PyTorch tensor as an image.
Display the content and style images side-by-side.
Print the layers of the VGG model.
Run an image forward through a model and get the features for a set of layers.
Calculate the Gram Matrix of a given tensor.
Display the target image along with the content image side-by-side and save this to the images directory.
Perform style transfer on the content image using the style image.
Main function for executing the PyTorch Style Transfer project.
- Run the script
style_transfer.py
. - The script loads a pre-trained VGG19 model and freezes its parameters.
- It loads content and style images from the
images
directory. To experiment with different content and style images, modify the paths in themain
function wherecontent_path
andstyle_path
are specified. - Style transfer is performed, and intermediate results are displayed.
Feel free to customize the content_path
and style_path
variables in the main
function to point to different images and observe the style transfer outcomes.
This project is licensed under the MIT License - see the LICENSE file for details.