SKT27182 commited on
Commit
9317f0e
Β·
1 Parent(s): 700a2d4

Tunend intital values and also the range

Browse files
app.py CHANGED
@@ -159,8 +159,8 @@ def fit_style_transfer(
159
 
160
 
161
  def main():
162
- content_image = gr.Image(type="filepath", label="Content Image", shape=(512, 512))
163
- style_image = gr.Image(type="filepath", label="Style Image", shape=(512, 512))
164
 
165
  extractor = gr.Dropdown(
166
  ["inception_v3", "vgg19", "resnet50", "mobilenet_v2"],
@@ -180,7 +180,7 @@ def main():
180
  n_style_layers = gr.Slider(
181
  1,
182
  5,
183
- value=2,
184
  step=1,
185
  label="Style Layers",
186
  )
@@ -191,34 +191,31 @@ def main():
191
 
192
  steps_per_epoch = gr.Slider(
193
  1,
194
- 10,
195
  value=5,
196
  step=1,
197
  label="Steps Per Epoch",
198
  )
199
 
200
  style_weight = gr.Slider(
201
- 1e-4,
202
- 1e-1,
203
- value=5e-2,
204
- step=1e-4,
205
  label="Style Weight",
206
  )
207
 
208
  content_weight = gr.Slider(
209
- 1e-3,
210
- 1e-1,
211
- value=3e-2,
212
- step=1e-4,
213
  label="Content Weight",
214
  )
215
 
216
  var_weight = gr.Slider(
217
  0,
218
- 3,
219
- value=1.5,
220
- step=0.2,
221
- label="Total Variation Weight",
222
  )
223
 
224
  inputs = [
@@ -237,49 +234,34 @@ def main():
237
 
238
  examples = [
239
  [
240
- "examples/style_1.jpg",
241
- "examples/content_1.jpg",
242
  "inception_v3",
243
- 3,
244
- 1,
245
  5,
246
- 80,
247
  10,
248
- 2e-2,
249
- 1e-2,
250
- 1.5,
251
- ],
252
- [
253
- "examples/style_2.jpg",
254
- "examples/content_2.jpg",
255
- "inception_v3",
256
- 3,
257
- 2,
258
- 4,
259
- 60,
260
  10,
261
- 5e-2,
262
- 3e-2,
263
- 1.5,
264
  ],
265
-
266
  [
267
- "examples/style_3.jpg",
268
- "examples/content_3.jpg",
269
- "inception_v3",
270
- 3,
271
- 1,
272
- 4,
273
- 60,
274
  10,
275
- 5e-2,
276
- 3e-2,
277
- 1.5,
 
 
278
  ]
279
-
280
  ]
281
 
282
- output_image = gr.Image(type="numpy", label="Output Image", shape=(512, 512))
283
 
284
  style_loss = gr.Number(label="Current Style Loss")
285
 
@@ -291,6 +273,10 @@ def main():
291
 
292
  curr_step = gr.Number(label="Current Step")
293
 
 
 
 
 
294
 
295
 
296
  outputs = [output_image, style_loss, content_loss, var_loss, curr_epoch, curr_step]
@@ -299,7 +285,10 @@ def main():
299
  fn=fit_style_transfer,
300
  inputs=inputs,
301
  outputs=outputs,
 
 
302
  examples=examples,
 
303
 
304
  )
305
 
 
159
 
160
 
161
  def main():
162
+ content_image = gr.Image(type="filepath", label="Content Image")
163
+ style_image = gr.Image(type="filepath", label="Style Image")
164
 
165
  extractor = gr.Dropdown(
166
  ["inception_v3", "vgg19", "resnet50", "mobilenet_v2"],
 
180
  n_style_layers = gr.Slider(
181
  1,
182
  5,
183
+ value=3,
184
  step=1,
185
  label="Style Layers",
186
  )
 
191
 
192
  steps_per_epoch = gr.Slider(
193
  1,
194
+ 20,
195
  value=5,
196
  step=1,
197
  label="Steps Per Epoch",
198
  )
199
 
200
  style_weight = gr.Slider(
201
+ 0,
202
+ 1,
203
+ value=0.1,
 
204
  label="Style Weight",
205
  )
206
 
207
  content_weight = gr.Slider(
208
+ 0,
209
+ 1,
210
+ value=1,
 
211
  label="Content Weight",
212
  )
213
 
214
  var_weight = gr.Slider(
215
  0,
216
+ 0.8,
217
+ value=0,
218
+ label="Variation Weight",
 
219
  )
220
 
221
  inputs = [
 
234
 
235
  examples = [
236
  [
237
+ "examples/van_gogh.jpg",
238
+ "examples/scene.jpg",
239
  "inception_v3",
 
 
240
  5,
241
+ 5,
242
  10,
243
+ 40,
 
 
 
 
 
 
 
 
 
 
 
244
  10,
245
+ 0.1,
246
+ 1,
247
+ 0.01,
248
  ],
 
249
  [
250
+ "examples/painting.jpg",
251
+ "examples/swan.jpg",
252
+ "vgg19",
253
+ 5,
254
+ 5,
 
 
255
  10,
256
+ 40,
257
+ 10,
258
+ 0.1,
259
+ 1,
260
+ 0.01,
261
  ]
 
262
  ]
263
 
264
+ output_image = gr.Image(type="numpy", label="Output Image")
265
 
266
  style_loss = gr.Number(label="Current Style Loss")
267
 
 
273
 
274
  curr_step = gr.Number(label="Current Step")
275
 
276
+ title = "Neural Style Transfer"
277
+
278
+ description = """### This app uses a neural network to transfer the style of one image to another. \n### The `style image` is the image whose style you want to transfer, and the `content image` is the image you want to transfer the style to. \n### The `feature extractor` is the neural network used to extract the features from the images. \n### The number of `style layers` and `content layers` are the number of layers in the feature extractor used to extract the style and content features respectively. \n### The `epochs`, `learning_rate`, `steps_per_epoch`, `style_weight`, `content_weight`, and `total_variation_weight` are all **hyperparameters** that affect the style transfer process. \n### The style weight controls how much the style image affects the output image, the content weight controls how much the content image affects the output image, and the variation weight controls how much the total variation of the output image affects the output image. The total variation of an image is the sum of the absolute differences for neighboring pixel-values in the image. The total variation loss is used to smooth the output image. The higher the variation weight, the smoother the output image will be."""
279
+
280
 
281
 
282
  outputs = [output_image, style_loss, content_loss, var_loss, curr_epoch, curr_step]
 
285
  fn=fit_style_transfer,
286
  inputs=inputs,
287
  outputs=outputs,
288
+ title=title,
289
+ description=description,
290
  examples=examples,
291
+ theme='gradio/monochrome'
292
 
293
  )
294
 
examples/{content_1.jpg β†’ painting.jpg} RENAMED
File without changes
examples/{content_2.jpg β†’ scene.jpg} RENAMED
File without changes
examples/style_2.jpg DELETED

Git LFS Details

  • SHA256: d25bb1f00ca850cab0710ac98414a0de63dd9e49f9abc96ee9415cbdf7e4540a
  • Pointer size: 133 Bytes
  • Size of remote file: 14.3 MB
examples/style_3.jpg DELETED

Git LFS Details

  • SHA256: 749549277a70212a842011a60228ae91d17026ecac8aecc3aab90799b6eed6a2
  • Pointer size: 132 Bytes
  • Size of remote file: 4.41 MB
examples/{content_3.jpg β†’ swan.jpg} RENAMED
File without changes
examples/{style_1.jpg β†’ van_gogh.jpg} RENAMED
File without changes
model.py CHANGED
@@ -6,6 +6,8 @@ from keras import backend as K
6
 
7
  class NeuralStyleTransfer:
8
  def __init__(self, style_image, content_image, extractor, n_style_layers=5, n_content_layers=5):
 
 
9
  # load the model
10
  if extractor == "inception_v3":
11
  self.feature_extractor = tf.keras.applications.InceptionV3(
@@ -69,31 +71,19 @@ class NeuralStyleTransfer:
69
 
70
  return image
71
 
72
- def imshow(self, image, title=None):
73
- """displays an image with a corresponding title"""
74
- if len(image.shape) > 3:
75
- image = tf.squeeze(image, axis=0)
76
-
77
- plt.imshow(image)
78
- if title:
79
- plt.title(title)
80
-
81
- def show_images_with_objects(self, images, titles=[]):
82
- """displays a row of images with corresponding titles"""
83
- if len(images) != len(titles):
84
- return
85
-
86
- plt.figure(figsize=(20, 12))
87
- for idx, (image, title) in enumerate(zip(images, titles)):
88
- plt.subplot(1, len(images), idx + 1)
89
- plt.xticks([])
90
- plt.yticks([])
91
- self.imshow(image, title)
92
 
93
  def _preprocess_image(self, image):
94
  image = tf.cast(image, dtype=tf.float32)
95
- image = (image / 127.5) - 1.0
96
-
 
 
 
 
 
 
 
 
97
  return image
98
 
99
  def get_output_layers(self):
@@ -219,6 +209,11 @@ class NeuralStyleTransfer:
219
  )
220
  ]
221
  )
 
 
 
 
 
222
  total_loss = style_loss + content_loss
223
  return total_loss, style_loss, content_loss
224
 
 
6
 
7
  class NeuralStyleTransfer:
8
  def __init__(self, style_image, content_image, extractor, n_style_layers=5, n_content_layers=5):
9
+
10
+ self.extractor = extractor
11
  # load the model
12
  if extractor == "inception_v3":
13
  self.feature_extractor = tf.keras.applications.InceptionV3(
 
71
 
72
  return image
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  def _preprocess_image(self, image):
76
  image = tf.cast(image, dtype=tf.float32)
77
+
78
+ if self.extractor == "inception_v3":
79
+ image = image / 127.5 - 1
80
+ elif self.extractor == "vgg19":
81
+ image = tf.keras.applications.vgg19.preprocess_input(image)
82
+ elif self.extractor == "resnet50":
83
+ image = tf.keras.applications.resnet50.preprocess_input(image)
84
+ elif self.extractor == "mobilenet_v2":
85
+ # scale pixel between -1 and 1
86
+ image = image / 127.5 - 1
87
  return image
88
 
89
  def get_output_layers(self):
 
209
  )
210
  ]
211
  )
212
+
213
+ style_loss /= self.n_style_layers
214
+ content_loss /= self.n_content_layers
215
+
216
+
217
  total_loss = style_loss + content_loss
218
  return total_loss, style_loss, content_loss
219