Skip to content

Given a content photo and a style photo,the code can transfer the style of the style photo to the content photo. Hence an Implemention of Photo Realistic Neural Style Transfer

License

sukkritsharmaofficial/NEURALFUSE

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 

Repository files navigation

                                        Python Dependencies Contributions welcome License

Basic Overview

Given a content photo and a style photo,the code can transfer the style of the style photo to the content photo.



Latest Development Changes

git clone https://github.com/sukkritsharmaofficial/NEURALFUSE.git

Download Dependencies

pip install numpy
pip install Pillow
pip install keras
pip install scipy

Code

Importing Dependencies

# Imports
import numpy as np
from PIL import Image
import requests
from io import BytesIO

from keras import backend
from keras.models import Model
from keras.applications.vgg16 import VGG16

from scipy.optimize import fmin_l_bfgs_b

Changable Hyperparameters and setting paths

# Hyperparams
ITERATIONS = 30
CHANNELS = 3
IMAGE_SIZE = 500
IMAGE_WIDTH = IMAGE_SIZE
IMAGE_HEIGHT = IMAGE_SIZE
IMAGENET_MEAN_RGB_VALUES = [123.68, 116.779, 103.939]
CONTENT_WEIGHT = 0.02
STYLE_WEIGHT = 4.5
TOTAL_VARIATION_WEIGHT = 0.995
TOTAL_VARIATION_LOSS_FACTOR = 1.25

# Paths
input_image_path = "input.png"
style_image_path = "style.png"
output_image_path = "output.png"
combined_image_path = "combined.png"

Adding links to Content and Style photo

content_image_path = "YOUR LINK GOES HERE"
style_image_path = "YOUR LINK GOES HERE"

Output

Running Model

x = np.random.uniform(0, 255, (1, IMAGE_HEIGHT, IMAGE_WIDTH, 3)) - 128.

for i in range(ITERATIONS):
    x, loss, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(), fprime=evaluator.gradients, maxfun=20)
    print("Iteration %d completed with loss %d" % (i, loss))
    
x = x.reshape((IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS))
x = x[:, :, ::-1]
x[:, :, 0] += IMAGENET_MEAN_RGB_VALUES[2]
x[:, :, 1] += IMAGENET_MEAN_RGB_VALUES[1]
x[:, :, 2] += IMAGENET_MEAN_RGB_VALUES[0]
x = np.clip(x, 0, 255).astype("uint8")
output_image = Image.fromarray(x)
output_image.save(output_image_path)

Output

Iteration 0 completed with loss 62434377728
Iteration 1 completed with loss 33507442688
Iteration 2 completed with loss 21433647104
Iteration 3 completed with loss 16948576256
Iteration 4 completed with loss 14893883392
Iteration 5 completed with loss 13701744640
.
.
.
.
Iteration 25 completed with loss 11944347648
Iteration 26 completed with loss 11941498880
Iteration 27 completed with loss 11939000320
Iteration 28 completed with loss 11936762880
Iteration 29 completed with loss 11934586880

Visualizing Combined results

combined = Image.new("RGB", (IMAGE_WIDTH*3, IMAGE_HEIGHT))
x_offset = 0
for image in map(Image.open, [input_image_path, style_image_path, output_image_path]):
    combined.paste(image, (x_offset, 0))
    x_offset += IMAGE_WIDTH
combined.save(combined_image_path)

Output

About

Given a content photo and a style photo,the code can transfer the style of the style photo to the content photo. Hence an Implemention of Photo Realistic Neural Style Transfer

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published