Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions __helpers__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
import urllib
from tqdm import tqdm


if tf.__version__[0] == '1':
tf.enable_eager_execution()
# print("Eager Execution Initialized:",tf.executing_eagerly())


# Starting session to download Images from URL if fed.
s = requests.Session()
s.proxies = {"http": "http://61.233.25.166:80"}
r = s.get("http://www.google.com")
# s = requests.Session()
# s.proxies = {"http": "http://61.233.25.166:80"}
# r = s.get("http://www.google.com")


# Defining the Feature Layers we need respectively
Expand Down Expand Up @@ -60,8 +61,7 @@ def getModel():
return models.Model(model.input, modelOutput)


# Defining GRAM MATRIX

# Defining GRAM MATRIX
def gram(x):

# number of channels
Expand All @@ -73,9 +73,9 @@ def gram(x):

# gram matrix
gram = tf.matmul(a, a, transpose_a=True)

return gram / tf.cast(n, tf.float32)


# Defining CONTENT COST
def contentCost(contentFeatures, generateFeatures):
return tf.reduce_mean(tf.square(contentFeatures-generateFeatures))
Expand All @@ -94,13 +94,13 @@ def getFeatures(content, style, model):

# Extracting out the different features from the model output
contentFeatures = [contentFeature[0] for contentFeature in contentOutputs[numStyleLayers:]]
styleFeatures = [styleFeature[0] for styleFeature in styleOutputs[:numStyleLayers]]

styleFeatures = [styleFeature[0] for styleFeature in styleOutputs[:numStyleLayers]]
return contentFeatures, styleFeatures


def loadImage(path_to_img):
max_dim = 512
# import pdb; pdb.set_trace()
if type(path_to_img) == str:
img2show = Image.open(path_to_img)
else:
Expand All @@ -109,17 +109,19 @@ def loadImage(path_to_img):
# long = max(img2show.size)
long = (img2show.size)

long = long[0]
scale = max_dim/long
img = img2show.resize((round(img2show.size[0]*scale), round(img2show.size[1]*scale)), Image.ANTIALIAS)

img = image.img_to_array(img)

# We need to broadcast the image array such that it has a batch dimension
img = np.expand_dims(img2show, axis=0)
img = np.expand_dims(img, axis=0)
return img2show, img


def urlToImage(url):
import pdb; pdb.set_trace()
resp = urllib.request.urlopen(url)
img = np.asarray(bytearray(resp.read()), dtype='uint8')
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
Expand All @@ -132,15 +134,14 @@ def urlToImage(url):
def inputImageAndPreprocess(path):
# Loading the image and reshaping it according to VGG19 requirements.
if path[:4]=='http':
# print("Loading Image from Internet...")
print("Loading Image from Internet...")
img2show, img = urlToImage(path)
else:
# print("Loading Image from Local...")
print("Loading Image from Local...")
img2show, img = loadImage(path)

# Preprocessing the img according to VGG19 requirements
img = tf.keras.applications.vgg19.preprocess_input(img)

return img2show, img


Expand Down Expand Up @@ -190,15 +191,13 @@ def totalLoss(model, lossWeights, generateImage, contentFeatures, styleFeatures)
# Computing Style Cost for every layer
for generateStyle, combinationStyle in zip(styleFeatures, styleGenerateFeatures):
styleCostValue += styleWeightPerLayer * styleCost(combinationStyle[0], generateStyle)


# Assigning the weights
contentCostValue *= contentWeight
styleCostValue *= styleWeight

# Computing the Total Loss
totalLossValue = contentCostValue + styleCostValue

return totalLossValue, contentCostValue, styleCostValue


Expand All @@ -207,7 +206,6 @@ def computeGrads(config):
allLoss = totalLoss(**config)

loss = allLoss[0]

return tape.gradient(loss, config['generateImage']), allLoss


Expand Down Expand Up @@ -252,7 +250,8 @@ def runStyleTransfer(contentPath,
SAVE_EVERY = 0,
contentWeight = 1e3,
styleWeight = 1e-2,
output_dirName = None):
output_dirName = None,
save_stats = False):

# Importing the Model
model = getModel()
Expand Down Expand Up @@ -294,23 +293,22 @@ def runStyleTransfer(contentPath,


normMeans = np.array([103.939, 116.779, 123.68])
minVals = -normMeans
maxVals = 255 - normMeans
minVals = -normMeans
maxVals = 255 - normMeans

# Creating Logs to use for Plotting Later
# global contentCostLog, styleCostLog, totalCostLog
contentCostLog, styleCostLog, totalCostLog = [], [], []


if output_dirName:
PATH = os.path.join(os.curdir, 'outputs', output_dirName)
PATH = os.path.join(os.curdir, output_dirName)
if not os.path.isdir(PATH):
os.mkdir(PATH)
os.chdir(PATH)

for iter in tqdm(range(iterations), leave=False):

# Computing the Grads and Loss
for iter in tqdm(range(iterations), leave=False):
# for iter in range(iterations):
grads, allLoss = computeGrads(config)

# Extracting different kinds of Losses
Expand Down Expand Up @@ -339,13 +337,13 @@ def runStyleTransfer(contentPath,
new = deprocessImage(generateImage.numpy())
cv2.imwrite(f'generateImage_{iter+1}.jpg', cv2.cvtColor(new, cv2.COLOR_BGR2RGB))



bestImage = deprocessImage(generateImage.numpy())
cv2.imwrite(f'FINAL_OUTPUT.jpg', cv2.cvtColor(bestImage, cv2.COLOR_BGR2RGB))
# Saving the numpy Arrays to plot later
np.save('contentLoss.npy', contentCostLog)
np.save('styleLoss.npy', styleCostLog)
np.save('totalCostLoss.npy', totalCostLog)
cv2.imwrite(f'output.jpg', cv2.cvtColor(bestImage, cv2.COLOR_BGR2RGB))

if save_stats:
# Saving the numpy Arrays to plot later
np.save('contentLoss.npy', contentCostLog)
np.save('styleLoss.npy', styleCostLog)
np.save('totalCostLoss.npy', totalCostLog)

return bestImage, (contentCostLog, styleCostLog, totalCostLog)
88 changes: 88 additions & 0 deletions application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from flask import Flask, render_template, request, redirect
import numpy as np
import joblib
import os, time, random
from PIL import Image
import numpy as np

from __helpers__ import *


app = Flask(__name__)

# Creating a user_file folder if not present in the working dir
if 'temp_user_files' not in os.listdir():
os.mkdir('temp_user_files')
# temp_user_file : It'll contain the images uploaded by the user temporarily

app.config["IMAGE_UPLOADS"] = os.path.join(os.getcwd(), 'temp_user_files')
home_dir = os.getcwd()

# To DISABLE Caching
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r

# Defining the Home page
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template('index.html')


# Defining the Prediction page
@app.route('/output', methods=['GET', 'POST'])
def output():

if request.method == 'POST':
if request.form['submit'] == 'Do the magic!':

# # Reading the POST request
content_img = request.files['img1name']
style_img = request.files['img2name']
iterations = int(request.form['iterations'])
print('Iterations :',iterations)

# # Saving the Image file in local env
content_path = os.path.join(app.config["IMAGE_UPLOADS"], 'content_img.jpg')
style_path = os.path.join(app.config["IMAGE_UPLOADS"], 'style_img.jpg')
content_img.save(content_path)
style_img.save(style_path)

# # Running Style Transfer
print('[INFO] Style Transfer Intializing..')
generatedImage, losses = runStyleTransfer(content_path,
style_path,
iterations = iterations,
SAVE_EVERY = 0,
contentWeight = 1,
styleWeight = 0.8,
output_dirName = 'static',
save_stats = False)


# Deleting the file from the database
os.remove(content_path)
os.remove(style_path)

# Switching back to the main dir because Current dir is changed internally
os.chdir(home_dir)
print('[INFO] Output generated.')
return render_template('output.html')

print('[INFO] Rendering Output')
return render_template('index.html')



if __name__ == '__main__':
app.run(port=5555, debug=True)

# int(random.random()*10000)
Loading