naman.mistry commited on
Commit
5620c0c
1 Parent(s): be66856

Initial Commit

Browse files
app.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ from flask import Flask,request,render_template
4
+ import tensorflow as tf
5
+ from PIL import Image
6
+ from tensorflow.keras.applications import vgg19
7
+
8
+ from tensorflow.python.keras import models
9
+ import IPython.display
10
+ import numpy as np
11
+ from tensorflow import keras
12
+ from werkzeug.utils import secure_filename
13
+ import os
14
+ from flask import send_file
15
+
16
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
17
+
18
+ img_nrows = 256
19
+ img_ncols = 300
20
+
21
+ def img_preprocess(image_path):
22
+ # Util function to open, resize and format pictures into appropriate tensors
23
+ # img = keras.preprocessing.image.load_img(
24
+ # image_path, target_size=(img_nrows, img_ncols)
25
+ # )
26
+ img = keras.preprocessing.image.img_to_array(image_path)
27
+ img = np.expand_dims(img, axis=0)
28
+ img = vgg19.preprocess_input(img)
29
+ return tf.convert_to_tensor(img)
30
+
31
+ def deprocess_img(processed_img):
32
+ x = processed_img.copy()
33
+ if len(x.shape) == 4:
34
+ x = np.squeeze(x, 0)
35
+ assert len(x.shape) == 3 #Input dimension must be [1, height, width, channel] or [height, width, channel]
36
+
37
+
38
+ # perform the inverse of the preprocessing step
39
+ x[:, :, 0] += 103.939
40
+ x[:, :, 1] += 116.779
41
+ x[:, :, 2] += 123.68
42
+ x = x[:, :, ::-1] # converting BGR to RGB channel
43
+
44
+ x = np.clip(x, 0, 255).astype('uint8')
45
+ return x
46
+
47
+ content_layers = ['block5_conv2']
48
+ style_layers = ['block1_conv1',
49
+ 'block2_conv1',
50
+ 'block3_conv1',
51
+ 'block4_conv1',
52
+ 'block5_conv1']
53
+ number_content=len(content_layers)
54
+ number_style =len(style_layers)
55
+ def get_model():
56
+
57
+ vgg=tf.keras.applications.vgg19.VGG19(include_top=False,weights='imagenet')
58
+ vgg.trainable=False
59
+ content_output=[vgg.get_layer(layer).output for layer in content_layers]
60
+ style_output=[vgg.get_layer(layer).output for layer in style_layers]
61
+ model_output= style_output+content_output
62
+ return models.Model(vgg.input,model_output)
63
+
64
+ def get_content_loss(noise,target):
65
+ loss = tf.reduce_mean(tf.square(noise-target))
66
+ return loss
67
+
68
+ def gram_matrix(tensor):
69
+ channels=int(tensor.shape[-1])
70
+ vector=tf.reshape(tensor,[-1,channels])
71
+ n=tf.shape(vector)[0]
72
+ gram_matrix=tf.matmul(vector,vector,transpose_a=True)
73
+ return gram_matrix/tf.cast(n,tf.float32)
74
+
75
+ def get_style_loss(noise,target):
76
+ gram_noise=gram_matrix(noise)
77
+ #gram_target=gram_matrix(target)
78
+ loss=tf.reduce_mean(tf.square(target-gram_noise))
79
+ return loss
80
+
81
+ def get_features(model,content_path,style_path):
82
+ content_img=img_preprocess(content_path)
83
+ style_image=img_preprocess(style_path)
84
+
85
+ content_output=model(content_img)
86
+ style_output=model(style_image)
87
+
88
+ content_feature = [layer[0] for layer in content_output[number_style:]]
89
+ style_feature = [layer[0] for layer in style_output[:number_style]]
90
+ return content_feature,style_feature
91
+
92
+ def compute_loss(model, loss_weights,image, gram_style_features, content_features):
93
+ style_weight,content_weight = loss_weights #style weight and content weight are user given parameters
94
+ #that define what percentage of content and/or style will be preserved in the generated image
95
+
96
+ output=model(image)
97
+ content_loss=0
98
+ style_loss=0
99
+
100
+ noise_style_features = output[:number_style]
101
+ noise_content_feature = output[number_style:]
102
+
103
+ weight_per_layer = 1.0/float(number_style)
104
+ for a,b in zip(gram_style_features,noise_style_features):
105
+ style_loss+=weight_per_layer*get_style_loss(b[0],a)
106
+
107
+
108
+ weight_per_layer =1.0/ float(number_content)
109
+ for a,b in zip(noise_content_feature,content_features):
110
+ content_loss+=weight_per_layer*get_content_loss(a[0],b)
111
+
112
+ style_loss *= style_weight
113
+ content_loss *= content_weight
114
+
115
+ total_loss = content_loss + style_loss
116
+
117
+
118
+ return total_loss,style_loss,content_loss
119
+
120
+ def compute_grads(dictionary):
121
+ with tf.GradientTape() as tape:
122
+ all_loss=compute_loss(**dictionary)
123
+
124
+ total_loss=all_loss[0]
125
+ return tape.gradient(total_loss,dictionary['image']),all_loss
126
+
127
+ def run_style_transfer(content_path,style_path,epochs=20,content_weight=1e3, style_weight=1e-2):
128
+
129
+ model=get_model()
130
+
131
+ for layer in model.layers:
132
+ layer.trainable = False
133
+
134
+ content_feature,style_feature = get_features(model,content_path,style_path)
135
+ style_gram_matrix=[gram_matrix(feature) for feature in style_feature]
136
+
137
+ noise = img_preprocess(content_path)
138
+ noise=tf.Variable(noise,dtype=tf.float32)
139
+
140
+ optimizer = tf.keras.optimizers.Adam(learning_rate=5, beta_1=0.99, epsilon=1e-1)
141
+
142
+ best_loss,best_img=float('inf'),None
143
+
144
+ loss_weights = (style_weight, content_weight)
145
+ dictionary={'model':model,
146
+ 'loss_weights':loss_weights,
147
+ 'image':noise,
148
+ 'gram_style_features':style_gram_matrix,
149
+ 'content_features':content_feature}
150
+
151
+ norm_means = np.array([103.939, 116.779, 123.68])
152
+ min_vals = -norm_means
153
+ max_vals = 255 - norm_means
154
+
155
+ imgs = []
156
+ for i in range(1,epochs+1):
157
+ grad,all_loss=compute_grads(dictionary)
158
+ total_loss,style_loss,content_loss=all_loss
159
+ optimizer.apply_gradients([(grad,noise)])
160
+ clipped=tf.clip_by_value(noise,min_vals,max_vals)
161
+ noise.assign(clipped)
162
+
163
+ if total_loss<best_loss:
164
+ best_loss = total_loss
165
+ best_img = deprocess_img(noise.numpy())
166
+
167
+ #for visualization
168
+
169
+ if i%1==0:
170
+ plot_img = noise.numpy()
171
+ plot_img = deprocess_img(plot_img)
172
+ imgs.append(plot_img)
173
+ IPython.display.clear_output(wait=True)
174
+ IPython.display.display_png(Image.fromarray(plot_img))
175
+
176
+
177
+ IPython.display.clear_output(wait=True)
178
+
179
+
180
+ return best_img,best_loss,imgs
181
+
182
+ content_path = "3.jpg"
183
+ style_path = "4.jpg"
184
+ def predict(image1_input, image2_input):
185
+
186
+ return run_style_transfer(image1_input,image2_input,epochs=60)[0]
187
+
188
+
189
+ image1_input = gr.inputs.Image(label="Image 1")
190
+ image2_input = gr.inputs.Image(label="Image 2")
191
+ output_image = gr.outputs.Image(label="Merged Image", type="filepath")
192
+
193
+ title = "Image Merger"
194
+ description = "Merge two input images"
195
+
196
+ gr.Interface(fn=predict, inputs=[image1_input, image2_input], outputs=output_image, title=title, description=description).launch()
flagged/Image 1/tmp8a57zh_3.jpg ADDED
flagged/Image 2/tmprweh2324.jpg ADDED
flagged/log.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Image 1,Image 2,Merged Image,flag,username,timestamp
2
+ D:\folders\projects\projects 2\Machine Learning Projects\style transfer\gradio\flagged\Image 1\tmp8a57zh_3.jpg,D:\folders\projects\projects 2\Machine Learning Projects\style transfer\gradio\flagged\Image 2\tmprweh2324.jpg,,,,2023-05-26 16:42:08.057633