Neural Style Transfer: Unveiling the Art of Digital Transformation

Allan Kouidri
-
11/28/2023
Neural Style Transfer on train image

How does Neural Style Transfer work?

Neural Style Transfer (NST) is a computational technique that leverages deep neural networks to blend two images—a content image and a style image—to generate a third image that mirrors the content of the first in the style of the second. 

Neural Style Transfer Arc de Triomphe

The core of NST lies in its ability to differentiate between the content and style representations of an image using a deep learning model.

High-level architecture of Neural Style Transfer

Neural Style Transfer (NST) integrates several crucial components within its architecture to achieve the blending of artistic styles:

  • Content image: This is the image that serves as the base for style transfer. The content, or the subject matter of this image, is retained in the final output.
  • Style image: The source of the artistic style to be applied. This image's stylistic elements are extracted and applied to the content image.
  • Generated image: This is the outcome of the NST process. It starts as a copy of the content image and is iteratively modified to incorporate the style of the style image. This image represents the only variable that is trained and adjusted throughout the NST process.

NST typically employs pretrained Convolutional Neural Network (CNN) models, such as VGG-16. The focus is on leveraging the initial layers of these networks, which are adept at capturing the stylistic features necessary for the transfer. 

The overarching goal of the model architecture is to seamlessly integrate the artistic style of one image with the content of another, resulting in visually compelling and artistically enriched outputs.

Neural Style Transfer basic structure
Neural Style Transfer basic structure [1]

Neural Style Transfer process

The process of Neural Style Transfer involves several key steps, each contributing to the transformation of an ordinary image into a unique fusion of art and content:

Neural Style Transfer model architecture
Neural Style Transfer Model Architecture [2]

  1. (1) Selection of images: Choose a content image, which provides the subject or scene, and a style image, which offers the artistic style to be applied.

  1. (2) Loading a pre-trained CNN: Utilize a pre-trained Convolutional Neural Network like VGG-16. This network has been extensively trained on diverse image sets and is adept at extracting both the content features from the content image and the style features from the style image.

  1. (3) Initialization of the generated image: Start with an image initialized with the content image's pixel values. This image will be transformed over the course of the training.

  1. (4) Defining loss functions: Two distinct loss functions need to be established:

            - Content Loss: This captures the root mean squared error between the activations produced by the generated image and the content image. The idea is that similar activations in higher layers indicate similar content. It ensures that the content of the original image is preserved in the newly generated image.

            - Style Loss: This is more complex and uses the Gram matrix, which captures the distribution of features across feature maps in a layer. The style loss is essentially the mean squared error between the Gram matrices of the generated and style images. The aim is to transfer the artistic style of the style image to the generated image.

  1. (5) Defining total loss: Introduce the total loss, a critical component in NST. It is the weighted sum of content loss and style loss, formulated as α  × Content Loss  +  β × Style Loss . The weights α (for content) and β (for style) are adjustable parameters that balance the influence of content and style in the final image.

  1. (6) Optimization process: Use an optimization algorithm, such as Adam, to minimize a combined loss function, which is a weighted sum of the content loss and the style loss. This optimization process alters the initial content-matching generated image to incorporate the style of the style image.

  1. (7) Iterative refinement: Repeat the optimization process for multiple epochs. In each epoch, the generated image is incrementally adjusted to better represent the style of the style image while maintaining the content of the content image.

  1. (8) Final output: Continue the iterative process until the generated image satisfactorily reflects the style of the style image along with the content of the content image, resulting in the final stylized output.

By methodically balancing the content and style through the total loss function, Neural Style Transfer successfully blends the essence of the original image with the chosen artistic style, resulting in a harmonious and aesthetically pleasing piece of art.

Applications and impact

Neural Style Transfer has had a significant impact across various fields, offering innovative applications and creative opportunities:

Artistic creation

NST has revolutionized digital art by enabling artists and enthusiasts to blend diverse artistic styles with their own digital creations. This fusion of art and technology allows for the generation of unique and captivating artwork, where traditional photographs or digital works are transformed with the styles of famous paintings or unique artistic techniques.

Media and entertainment

The entertainment industry has embraced NST, particularly in areas such as video gaming, film production, and virtual reality. In gaming, NST can be used to dynamically stylize game environments, creating unique visual experiences.

Movie producers use NST to add artistic effects to scenes, enhancing visual storytelling. In virtual reality, NST helps in creating immersive and stylistically varied environments, offering users a more engaging experience.

Fashion and design

NST is becoming an invaluable tool in the fashion sector, assisting designers in conceptualizing new designs and patterns. By applying various artistic styles to existing garment designs or textiles, designers can explore new and innovative combinations of patterns and textures, pushing the boundaries of traditional fashion design.

Educational tools

In the realm of education, particularly in subjects like computer science and digital arts, NST serves as a practical and interactive tool. It helps students grasp complex concepts in deep learning and artificial intelligence by providing a visually engaging and hands-on experience.

Through NST, learners can see the immediate effects of algorithmic changes, making abstract concepts more tangible and understandable.

Easily apply Style Transfer to an image

With the Ikomia API, you can effortlessly use Neural Style Transfer algorithm with just a few lines of code.

Setup

To get started, you need to install the API in a virtual environment [3].


pip install ikomia

Run NST with a few lines of code

You can also directly charge the notebook we have prepared.


from ikomia.dataprocess.workflow import Workflow
from ikomia.utils.displayIO import display
from ikomia.utils import ik


# Init your workflow
wf = Workflow()

# Add algorithm
nst = wf.add_task(ik.infer_neural_style_transfer(model_name="candy"), auto_connect=True)

# Run on your image  
wf.run_on(url="https://github.com/Ikomia-dev/notebooks/blob/main/examples/img/img_train.jpg?raw=true")

# Display transferred style
display(nst.get_output(1).get_image())

# Display result
display(nst.get_output(0).get_image())

Neural Style Transfer on a train
  • Model names available:

            - the_wave

            - la_muse

            - composition_vii

            - starry_night

            - instance_norm

            - candy

            - mosaic

            - the_scream

            - udnie

            - feathers

Exploring artistic possibilities with Ikomia tools

  • Neural Style Transfer: Ikomia API simplifies Neural Style Transfer (NST), a deep neural network application that artistically merges two images. Users can effortlessly apply NST using the Ikomia API, with a range of style models like 'the_wave' and 'candy', facilitating easy artistic experimentation.

  • Resourceful Documentation: The Ikomia documentation provides detailed guidance for maximizing the API's potential in creative projects.

  • Ikomia HUB: Explore a vast array of cutting-edge algorithms on Ikomia HUB, broadening the scope for creative and technical experimentation in image processing.

  • Ikomia STUDIO: This desktop app offers an intuitive interface for a more visual approach to image processing, retaining the API's functionalities.
Neural Style Transfer with Ikomia STUDIO app
Running NST with no code with the Ikomia STUDIO app

References

[1]  Perceptual Losses for Real-Time Style Transfer and Super-Resolution

‍[2]  Neural Style Transfer: Everything You Need to Know

[3] How to create a virtual environment in Python

Arrow
Arrow
No items found.