-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__helpers__.py
More file actions
351 lines (250 loc) · 10.2 KB
/
__helpers__.py
File metadata and controls
351 lines (250 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import os
# To suppress all the Tensorflow warning while importing
import logging
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # FATAL
logging.getLogger('tensorflow').setLevel(logging.FATAL)
import tensorflow as tf
import keras.backend as K
from keras.applications.vgg19 import VGG19, preprocess_input
from tensorflow.python.keras import models
from keras.models import Model
from keras.preprocessing import image
import requests
from PIL import Image
import numpy as np
import cv2
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")
# Defining the Feature Layers we need respectively
styleLayers = ['block1_conv2',
'block2_conv2',
'block3_conv3',
'block4_conv3',
'block5_conv3']
contentLayer = ['block3_conv2']
numContentLayers = len(contentLayer) # Number of Content Layers
numStyleLayers = len(styleLayers) # Number of Style Layers
# Defining Function to import model (VGG19)
def getModel():
# Loading Model from tf
model = tf.keras.applications.vgg19.VGG19(include_top=False, weights='imagenet')
model.trainable = False # Freezing to parameters
# Features of the Respective Layers
contentFeatures = [model.get_layer(name).output for name in contentLayer]
styleFeatures = [model.get_layer(name).output for name in styleLayers]
modelOutput = contentFeatures + styleFeatures
return models.Model(model.input, modelOutput)
# Defining GRAM MATRIX
def gram(x):
# number of channels
channels = int(x.shape[-1])
# reshaping to channel first
a = tf.reshape(x, [-1, channels])
n = tf.shape(a)[0]
# 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))
# Defining STYLE COST
def styleCost(styleFeatures, generateFeatures):
styleGram = gram(styleFeatures)
return tf.reduce_mean(tf.square(styleGram - generateFeatures))
def getFeatures(content, style, model):
# Defining the respective outputs from our model
contentOutputs = model(content)
styleOutputs = model(style)
# 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]]
return contentFeatures, styleFeatures
def loadImage(path_to_img):
max_dim = 512
if type(path_to_img) == str:
img2show = Image.open(path_to_img)
else:
img2show = path_to_img
# long = max(img2show.size)
long = (img2show.size)
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)
return img2show, img
def urlToImage(url):
resp = urllib.request.urlopen(url)
img = np.asarray(bytearray(resp.read()), dtype='uint8')
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
img2show = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.expand_dims(img2show, axis=0)
return img2show, img
def inputImageAndPreprocess(path):
# Loading the image and reshaping it according to VGG19 requirements.
if path[:4]=='http':
# print("Loading Image from Internet...")
img2show, img = urlToImage(path)
else:
# 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
# Deprocessing Image to save locally
def deprocessImage(processed_img):
x = processed_img.copy()
if len(x.shape) == 4:
x = np.squeeze(x, 0)
assert len(x.shape) == 3, ("Input to deprocess image must be an image of "
"dimension [1, height, width, channel] or [height, width, channel]")
if len(x.shape) != 3:
raise ValueError("Invalid input to deprocessing image")
# perform the inverse of the preprocessiing step
x[:, :, 0] += 103.939
x[:, :, 1] += 116.779
x[:, :, 2] += 123.68
x = x[:, :, ::-1]
x = np.clip(x, 0, 255).astype('uint8')
return x
def totalLoss(model, lossWeights, generateImage, contentFeatures, styleFeatures):
# Extracting the respective weights
contentWeight, styleWeight = lossWeights
# Extracting the generate image features from the model
modelOutputs = model(generateImage)
# Splitting the generate Features into different categories
contentGenerateFeatures = modelOutputs[numStyleLayers:]
styleGenerateFeatures = modelOutputs[:numStyleLayers]
# Initializing all costs with 0
contentCostValue, styleCostValue = 0, 0
# Defining partial weights
contentWeightPerLayer = 1.0 / float(numContentLayers)
styleWeightPerLayer = 1.0 / float(numStyleLayers)
# Computing Content Cost
for generateContent, combinationContent in zip(contentFeatures, contentGenerateFeatures):
contentCostValue += contentWeightPerLayer * contentCost(combinationContent[0], generateContent)
# 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
def computeGrads(config):
with tf.GradientTape() as tape:
allLoss = totalLoss(**config)
loss = allLoss[0]
return tape.gradient(loss, config['generateImage']), allLoss
def extract_frames_out_of_the_video(vid_path):
cam = cv2.VideoCapture(vid_path)
currentframe = 1
frames = []
while(True):
# reading from frame
ret, frame = cam.read()
if ret:
frame = np.expand_dims(frame, axis=0)
frames.append(frame.astype('float32'))
print(currentframe, end='\r')
currentframe += 1
else:
break
print('Frames generated..!')
return frames
def skip_frame_every(fps_quality):
if fps_quality == 'high':
skip_frame_every = 1
elif fps_quality == 'medium':
skip_frame_every = 3
elif fps_quality == 'low':
skip_frame_every = 6
else:
skip_frame_every = 10
return skip_frame_every
# Defining the MAIN TRAINING FUNCTION
def runStyleTransfer(contentPath,
stylePath,
iterations = 1000,
SAVE_EVERY = 0,
contentWeight = 1e3,
styleWeight = 1e-2,
output_dirName = None):
# Importing the Model
model = getModel()
for layer in model.layers:
layer.trainable = False
if type(contentPath) == str:
_ , contentImage = inputImageAndPreprocess(contentPath)
else:
contentImage = contentPath
_ , styleImage = inputImageAndPreprocess(stylePath)
# Extracting out the respective features from the model
contentFeatures, styleFeatures = getFeatures(contentImage, styleImage, model)
styleFeatures = [gram(styleFeature) for styleFeature in styleFeatures]
# Creating the Generate Image
generateImage = contentImage
generateImage = tf.Variable(generateImage, dtype=tf.float32)
# Defining the Adam Optimizer
optimizer = tf.keras.optimizers.Adam(learning_rate=5, epsilon=1e-3)
# Storing the best Image and Loss
bestLoss, bestImage = float('inf'), None
# Zipping the Weights
lossWeights = (contentWeight, styleWeight)
# Defining the Config File
config = {
'model': model,
'lossWeights': lossWeights,
'generateImage': generateImage,
'contentFeatures': contentFeatures,
'styleFeatures': styleFeatures
}
normMeans = np.array([103.939, 116.779, 123.68])
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)
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
grads, allLoss = computeGrads(config)
# Extracting different kinds of Losses
loss, contentLoss, styleLoss = allLoss
# Saving the respective losses in respective lists for plotting
contentCostLog.append(contentLoss)
styleCostLog.append(styleLoss)
totalCostLog.append(loss)
# Applying gradients to Generate Image
optimizer.apply_gradients([(grads, generateImage)])
# Clipping the values of Generate Image from (-255, 255)
clipped = tf.clip_by_value(generateImage, minVals, maxVals)
generateImage.assign(clipped)
# Updating the Best Image and Loss
if loss < bestLoss:
bestLoss = loss
bestImage = deprocessImage(generateImage.numpy())
# Saving the Generate Image
if SAVE_EVERY:
if iter % SAVE_EVERY == 0:
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)
return bestImage, (contentCostLog, styleCostLog, totalCostLog)