Coders Packet

Neural Style Transfer: Exploring Artistic Expression through Deep Learning in Python

By Amanpreet Singh

The project focuses on generating art images from content images and stylized images.

In this project, we will discuss how can we generate an Art Image based on sample images and style images.


Neural Style Transfer is a deep learning technology that allows creative styles to be transferred from one image to another. This is accomplished by training a neural network to create a picture that combines the content of one image with the aesthetics of another.

The texture, color, and other visual components included in the style picture determine the style. The created picture's structure, topic matter, and general context are provided by the content image. The output is a new image that aesthetically integrates the information and style of the input photos. This method has been used for a variety of purposes, including creative expression, design, and picture modification.

Neural Style Transfer is an enthralling branch of deep learning that blends computer vision, image processing, and generative models to generate fresh and distinctive visuals.

Typically, the architecture is built on a convolutional neural network (CNN) trained for image classification tasks, such as VGG-19.

When content and style representations are extracted, then the model employs an optimizer, such as L-BFGS or Adam, to minimize the difference between the representations of the content image and the produced picture. At the same time, the model minimizes the disparity between the style picture's style representation and the produced image. This procedure creates a new image by combining the information and style of the input photos.

By altering the weights of the content and style loss functions, as well as the optimizer's hyperparameters, the Neural Style Transfer architecture may be fine-tuned to create pictures with varied styles and variants.



In this project, we are using Google Collab IDE with Python version 3.9. In our IDE we need to install certain libraries like :

pip install gradio

pip install tensorflow

pip install tensorflow-hub

pip install Pillow

pip install numpy


Import Libraries

Let's import all the required libraries which we installed earlier.

import tensorflow as tf
import tensorflow_hub as hub
import gradio as gr
import numpy as np
from PIL import Image

Load Model & Build Function

Let's create a function that accepts two input pictures, "normal img" and "style img," and generates a new image via style transfer.

First, the input pictures are transformed into tensors and their values are normalized by dividing them by 255. Normalization is required since the values of the pixel intensities should be in the range of 0 to 1 for the model's input.

The "model" variable is then loaded via the TensorFlow Hub's "hub.load" method. This loads a model that has been pre-trained for arbitrary picture stylization. The model receives two pictures as inputs: the regular image and the style image, and produces the styled image.

Below is the code for how can we do it:

def style(normal_img,style_img):
    normal_img=tf.convert_to_tensor(normal_img,np.float32)[tf.newaxis, ...] /255.
    style_img=tf.convert_to_tensor(style_img,np.float32)[tf.newaxis,...] /255.
    return Image.fromarray(np.uint8(res[0]*255))

Built Interface with Gradio

Let's build an Interface that takes input as an image and displays output as text using Gradio.

  • Call the style() function which we created earlier.
  • Make 2 input boxes that upload a content image and a style image.
  • Define the title="Neural Style Transfer".
  • Define description in Interface.
  • Launch the webserver.


normal_image_input = gr.inputs.Image(label="Content Image")
style_image_input = gr.inputs.Image(shape=(256, 256), label="Style Image")
# Examples
golden_gate = ["samples/golden_gate_bridge.jpeg", "samples/the_great_wave.jpeg"]
joshua_tree = ["samples/joshua_tree.jpeg", "samples/starry_night.jpeg"]
glacier = ["samples/glacier_national_park.jpeg", "samples/the_scream.jpg"]

app_interface = gr.Interface(fn=style,
                             inputs=[normal_image_input, style_image_input],
                             title="Fast Neural Style Transfer",
                             description="Gradio demo for Fast Neural Style Transfer using a pretrained Image Stylization model from TensorFlow Hub. To use it, simply upload a content image and style image, or click one of the examples to load them. To learn more about the project, please find the references listed below.",
                             examples=[glacier, golden_gate, joshua_tree],
                             "1. Tutorial to implement Fast Neural Style Transfer using the pretrained model from TensorFlow Hub  \n"
                             "2. The idea to build a neural style transfer application was inspired from this Hugging Face Space ")



Upload here content image and stylized image then hit on submit button.

neural out

Download Complete Code


No comments yet

Download Packet

Reviews Report

Submitted by Amanpreet Singh (Aman9868)

Download packets of source code on Coders Packet