Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Neural Style Transfer using a simple python library
Latest   Machine Learning

Neural Style Transfer using a simple python library

Last Updated on July 26, 2023 by Editorial Team

Author(s): Divy Shah

Originally published on Towards AI.

Computer Vision

A guide to neural style transfer using the amazing neural-style-transfer python library with few lines of code.

In this tutorial, we will learn how to use the neural-style-transfer library to compose images in the style of other images using just a few lines of code.

Neural Style Transfer is basically an optimization technique that takes 2 pictures as an input, one image reference style image, and the other image is input image which you want to style β€” model blends these two image together and produce a transformed image that looks like the given input (content) but painted in the given referenced style image.

looks interesting right.. but it’s required lots of stuff which includes image pre-processing code, model training code, optimization functions, and lots of other small functions, but to get rid of all these things we have an amazing library called neural-style-transfer which does all this stuff for us in just a few lines of code so in this blog will show you how someone can use this beautiful library.

First, install the library

pip install neural-style-transfer

Import the library and create a class object

from neuralstyletransfer.style_transfer import NeuralStyleTransfernst = NeuralStyleTransfer()

Now we need to load content and reference style images using the given command.

here I used an image from an external URL you can also load images directly from the local machine by providing an absolute path and pathType=’local’.

content_url = 'https://i.ibb.co/6mVpxGW/content.png'
style_url = 'https://i.ibb.co/30nz9Lc/style.jpg'
nst.LoadContentImage(content_url, pathType='url')
nst.LoadStyleImage(style_url, pathType='url')

Content image

Content image

Style image

Great! now we are all set to start model training..

No need to write any long code you just need to call apply function and model start optimization and give the final result as output.

output = nst.apply(contentWeight=1000, styleWeight=0.01, epochs=600)

output variable contains the final transformed image you can save the output using the Pillow library.

from PIL import Image
output.save('output.jpg')

this process takes some time based on your machine’s hardware I suggest using a GPU machine to make the process much faster.

here in the apply function, you can provide different hyperparameters based on your requirement.

  • contentWeight β†’Using this parameter we can specify the weight of the content image if it is set to a low value than in the transformed (result) image it does not appear clearly, to make a content image sharper you can increase the value of the content weight.
  • styleWeight β†’Using this parameter we can specify the weight of the style image usually it is set to 0.01 but if you want to make it lighter you can decrease the value based on your requirement.
  • epochs β†’Using this parameter you can set the number of epoch for model training, as it is an optimization problem you can probably set this to a higher value like 500 or 600 or even more than that if you want your image more clear, basically higher the numbers of epoch image is more optimized, during the training time it prints the loss of content and style image so based on that you can decide your epoch for optimal result.

Transformed image (Output)

Output image

Wow.. we are done above is our transformed image looks like

checkout this colab-notebook for demo.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓