Neural Style Transfer Your Own Art

Yudi Xu
Just another data scientist
4 min readOct 25, 2018

Most of the computer science guys or data scientist guy know little about art, so do I. And I can tell you that the last place I would go when visit a city is going to an art museum.

Not to mention to create your own paintings… kill me pls…

However, your girlfriend or boyfriend may fond of those boring things… what if you can code to create some special art pieces? have you ever thought about using that to surprise your sweetheart(s)?

Okay, I am not saying you will make her/his day by using what I will share with you here today, but no harm to give it a try.

I give you a flower, you marry me, ok? lol

I am not creating this from scratch, I am actually re-using the work from some brilliant guys, have to acknowledge them here: Lawrence Francis, this is his Github, the code is from his creation.

Stating coding

first we need to clone the git code from Lawrence to our Colab.

# this line actually check if the folder exist, it will remove it if it's already there.
!rm -r ComputerVision NeuralStyleTransfer
# this line clone the repository
!git clone https://github.com/ldfrancis/ComputerVision.git
#copy the directly into the current working directly, it's imporant, because in colab, the change directory command you use will only happen in that cell.
!cp -r ComputerVision/NeuralStyleTransfer .

Then we upload two images, when you run the following command in Colab, it will pop up a upload button, select the pictures from your laptop one at a time, and click upload.

from google.colab import files 
files.upload()

after upload the pic, we need to set the size of the pic so that our model can understand. Basically, the content image and the style image must be in the same dimension. Note: when you click run, you have to be quick, otherwise uploading won’t work, weird from Colab!

CONTENT_IMAGE = "content.jpg" 
STYLE_IMAGE = "style.jpg"
IMAGE_HEIGHT = 400
IMAGE_WIDTH = 300
ITERATION = 800

then we set the path of the images we adjusted, we put it in a folder called /content/ for later use.

path_to_content_image = "/content/"+CONTENT_IMAGE
path_to_style_image = "/content/"+STYLE_IMAGE

Now we can take a look at the image we uploaded:

#import matplotlib for ploting
import matplotlib.pyplot as plt
c_image = plt.imread(path_to_content_image)
s_image = plt.imread(path_to_style_image)

print out the image, content image :

print("Content Image of size (height, width) => {0}".format(c_image.shape[:-1]))
plt.imshow(c_image)
me in Nationale Park De Hoge Veluwe

and style image:

print("Style Image of size (height, width) => {0}".format(s_image.shape[:-1]))
plt.imshow(s_image)
Van Gogh Starry night

Train the model!

from NeuralStyleTransfer import implementNTS as NSTNST.setImageDim(IMAGE_WIDTH,IMAGE_HEIGHT)NST.run(ITERATION, style_image=path_to_style_image,content_image=path_to_content_image)

list the image we generated in folder

!ls NeuralStyleTransfer/output/

View generated image!

generated_image_path = "/content/NeuralStyleTransfer/output/generated_image.jpg"
image = plt.imread(generated_image_path)
plt.imshow(image)
iteration 800 (end results):

if you want to download the image to your laptop, use the script below:

files.download("NeuralStyleTransfer/output/generated_image.jpg")

I also tried something else, to change the iteration, you will have different results!

Different iterations:

iteration 100:
iteration 300:
iteration 500:
iteration 700:
iteration 300 but with a 150*200 dimension, which mean the pic has less pixels. that’s why the huge difference of the results!
London
starry London, lol
London eye
starry London eye

Ok, now create something to showoff to your girls or boys :-)

Last but not least, I should not forget Colab notebook is here:

Author: Yudi Xu

CTO Consultant for Emerging digital technology

Project manager and expert in IoT

Former data scientist in Shell

Linkedin: https://www.linkedin.com/in/xuyudi/

--

--

Yudi Xu
Just another data scientist

Not really a data scientist, but I want to become just another data scientist (JADS), that’s why the project together with my best data scientist friends:)