SKT27182 commited on
Commit
280a2a5
1 Parent(s): d8e53c5

Added multiple models

Browse files
.gitattributes CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ *.jpg filter=lfs diff=lfs merge=lfs -text
app.py CHANGED
@@ -1,101 +1,307 @@
1
  import gradio as gr
2
  from model import NeuralStyleTransfer
3
  import tensorflow as tf
 
 
4
 
5
 
6
- def model_fn(
7
- style, content, extractor="inception_v3", n_content_layers=3, n_style_layers=2,
8
- epochs=4, learning_rate=60.0, steps_per_epoch=100, style_weight=1e-2,
 
 
 
 
 
 
9
  ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  model = NeuralStyleTransfer(
11
- style_image=style,
12
- content_image=content,
13
  extractor=extractor,
14
- n_content_layers=n_content_layers,
15
  n_style_layers=n_style_layers,
 
16
  )
17
 
18
- return model.fit_style_transfer(
19
- epochs=10,
20
- learning_rate=80.0,
21
- steps_per_epoch=100,
22
- style_weight=1e-2,
23
- content_weight=1e-4,
24
- show_image=True,
25
- show_interval=90,
26
- var_weight=1e-12,
27
- terminal=False,
 
 
 
 
 
 
28
  )
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- def hugging_face():
32
- demo = gr.Interface(
33
- fn=model_fn,
34
- inputs=[
35
- "image",
36
- "image",
37
- gr.Dropdown(
38
- ["inception_v3", "vgg19", "resnet50", "mobilenet_v2"],
39
- label="extractor",
40
- default="inception_v3",
41
- info="Feature extractor to use.",
42
- ),
43
- gr.Slider(
44
- 1,
45
- 5,
46
- value=3,
47
- label="n_content_layers",
48
- info="Number of content layers to use.",
49
- ),
50
- gr.Slider(
51
- 1,
52
- 5,
53
- value=2,
54
- label="n_style_layers",
55
- info="Number of style layers to use.",
56
- ),
57
- gr.Slider(
58
- 2, 20, value=4, label="epochs", info="Number of epochs to train for."
59
- ),
60
- gr.Slider(
61
- 1, 100, value=60, label="learning_rate", info="Initial Learning rate."
62
- ),
63
- gr.Slider(
64
- 1,
65
- 100,
66
- value=100,
67
- label="steps_per_epoch",
68
- info="Number of steps per epoch.",
69
- ),
70
- gr.Slider(
71
- 1e-4,
72
- 1e-2,
73
- value=1e-2,
74
- label="style_weight",
75
- info="Weight of style loss.",
76
- ),
77
- gr.Slider(
78
- 1e-4,
79
- 1e-2,
80
- value=1e-4,
81
- label="content_weight",
82
- info="Weight of content loss.",
83
- ),
84
- gr.Slider(
85
- 1e-12,
86
- 1e-9,
87
- value=1e-12,
88
- label="var_weight",
89
- info="Weight of total variation loss.",
90
- ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  ],
92
- outputs="image",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  )
94
 
95
- return demo
96
 
97
 
98
- if __name__ == "__main__":
99
- demo = hugging_face()
100
- demo.launch( )
101
-
 
1
  import gradio as gr
2
  from model import NeuralStyleTransfer
3
  import tensorflow as tf
4
+ from keras import backend as K
5
+ import numpy as np
6
 
7
 
8
+ def change_dtype_inputs(
9
+ n_style_layers,
10
+ n_content_layers,
11
+ epochs,
12
+ learning_rate,
13
+ steps_per_epoch,
14
+ style_weight,
15
+ content_weight,
16
+ var_weight,
17
  ):
18
+ return (
19
+ int(n_style_layers),
20
+ int(n_content_layers),
21
+ int(epochs),
22
+ float(learning_rate),
23
+ int(steps_per_epoch),
24
+ float(style_weight),
25
+ float(content_weight),
26
+ float(var_weight),
27
+ )
28
+
29
+
30
+ def fit_style_transfer(
31
+ style_image,
32
+ content_image,
33
+ extractor="inception_v3",
34
+ n_style_layers=2,
35
+ n_content_layers=3,
36
+ epochs=4,
37
+ learning_rate=60.0,
38
+ steps_per_epoch=100,
39
+ style_weight=1e-2,
40
+ content_weight=1e-4,
41
+ var_weight=1e-12,
42
+ ):
43
+ """
44
+ Fit the style transfer model to the content and style images.
45
+
46
+ Parameters
47
+ ----------
48
+
49
+ style_image: str
50
+ The path to the style image.
51
+
52
+ content_image: str
53
+ The path to the content image.
54
+
55
+ extractor: str
56
+ The name of the feature extractor to use. Options are
57
+ "inception_v3", "vgg19", "resnet50", and "mobilenet_v2".
58
+
59
+ n_style_layers: int
60
+ The number of layers to use for the style loss.
61
+
62
+ n_content_layers: int
63
+ The number of layers to use for the content loss.
64
+
65
+ epochs: int
66
+ The number of epochs to train the model for.
67
+
68
+ learning_rate: float
69
+ The learning rate to use for the Adam optimizer.
70
+
71
+ steps_per_epoch: int
72
+ The number of steps to take per epoch.
73
+
74
+ style_weight: float
75
+ The weight to use for the style loss.
76
+
77
+ content_weight: float
78
+ The weight to use for the content loss.
79
+
80
+ var_weight: float
81
+ The weight to use for the total variation loss.
82
+
83
+ Returns
84
+ -------
85
+ display_image: np.array
86
+ """
87
+
88
+ (
89
+ n_style_layers,
90
+ n_content_layers,
91
+ epochs,
92
+ learning_rate,
93
+ steps_per_epoch,
94
+ style_weight,
95
+ content_weight,
96
+ var_weight,
97
+ ) = change_dtype_inputs(
98
+ n_style_layers,
99
+ n_content_layers,
100
+ epochs,
101
+ learning_rate,
102
+ steps_per_epoch,
103
+ style_weight,
104
+ content_weight,
105
+ var_weight,
106
+ )
107
+
108
  model = NeuralStyleTransfer(
109
+ style_image=style_image,
110
+ content_image=content_image,
111
  extractor=extractor,
 
112
  n_style_layers=n_style_layers,
113
+ n_content_layers=n_content_layers,
114
  )
115
 
116
+ style_image = model.style_image
117
+ content_image = model.content_image
118
+
119
+ content_and_style_layers = model.get_output_layers()
120
+
121
+ # build the model with the layers we need to extract the features from
122
+ K.clear_session()
123
+ model.build(content_and_style_layers)
124
+
125
+ style_features = model.get_features(style_image, type="style")
126
+ content_features = model.get_features(content_image, type="content")
127
+
128
+ optimizer = tf.optimizers.Adam(
129
+ tf.keras.optimizers.schedules.ExponentialDecay(
130
+ initial_learning_rate=learning_rate, decay_steps=100, decay_rate=0.80
131
+ )
132
  )
133
 
134
+ generated_image = tf.cast(content_image, tf.float32)
135
+ generated_image = tf.Variable(generated_image)
136
+
137
+ step = 0
138
+
139
+ for epoch in range(epochs):
140
+ for step in range(steps_per_epoch):
141
+ losses = model._update_image_with_style(
142
+ generated_image,
143
+ style_features,
144
+ content_features,
145
+ style_weight,
146
+ content_weight,
147
+ optimizer,
148
+ var_weight,
149
+ )
150
+
151
+ display_image = model.tensor_to_image(generated_image)
152
+
153
+ step += 1
154
+
155
+ style_loss, content_loss, var_loss = losses
156
+
157
+ yield np.array(display_image), style_loss, content_loss, var_loss, epoch, step
158
+
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"],
167
+ label="Feature Extractor",
168
+ value="inception_v3",
169
+
170
+ )
171
+
172
+ n_content_layers = gr.Slider(
173
+ 1,
174
+ 5,
175
+ value=3,
176
+ step=1,
177
+ label="Content Layers",
178
+ )
179
+
180
+ n_style_layers = gr.Slider(
181
+ 1,
182
+ 5,
183
+ value=2,
184
+ step=1,
185
+ label="Style Layers",
186
+ )
187
+
188
+ epochs = gr.Slider(2, 20, value=4, step=1, label="Epochs")
189
+
190
+ learning_rate = gr.Slider(1, 100, value=60, step=1, label="Learning Rate")
191
+
192
+ steps_per_epoch = gr.Slider(
193
+ 1,
194
+ 100,
195
+ value=80,
196
+ step=1,
197
+ label="Steps Per Epoch",
198
+ )
199
+
200
+ style_weight = gr.Slider(
201
+ 1e-4,
202
+ 0.5,
203
+ value=1e-1,
204
+ step=1e-4,
205
+ label="Style Weight",
206
+ )
207
+
208
+ content_weight = gr.Slider(
209
+ 1e-3,
210
+ 0.5,
211
+ value=0.3,
212
+ step=1e-4,
213
+ label="Content Weight",
214
+ )
215
+
216
+ var_weight = gr.Slider(
217
+ 0,
218
+ 1e-1,
219
+ value=1e-5,
220
+ step=1e-12,
221
+ label="Total Variation Weight",
222
+ )
223
+
224
+ inputs = [
225
+ style_image,
226
+ content_image,
227
+ extractor,
228
+ n_style_layers,
229
+ n_content_layers,
230
+ epochs,
231
+ learning_rate,
232
+ steps_per_epoch,
233
+ style_weight,
234
+ content_weight,
235
+ var_weight,
236
+ ]
237
+
238
+ examples = [
239
+ [
240
+ "examples/style_1.jpg",
241
+ "examples/content_1.jpg",
242
+ "inception_v3",
243
+ 3,
244
+ 2,
245
+ 4,
246
+ 60,
247
+ 100,
248
+ 1e-2,
249
+ 1e-2,
250
+ 1e-11,
251
  ],
252
+ [
253
+ "examples/style_2.jpg",
254
+ "examples/content_2.jpg",
255
+ "inception_v3",
256
+ 3,
257
+ 2,
258
+ 4,
259
+ 60,
260
+ 100,
261
+ 1e-2,
262
+ 1e-2,
263
+ 1e-11,
264
+ ],
265
+ [
266
+ "examples/style_3.jpg",
267
+ "examples/content_3.jpg",
268
+ "inception_v3",
269
+ 3,
270
+ 2,
271
+ 4,
272
+ 60,
273
+ 100,
274
+ 1e-2,
275
+ 1e-2,
276
+ 1e-11,
277
+ ]
278
+
279
+ ]
280
+
281
+ output_image = gr.Image(type="numpy", label="Output Image", shape=(512, 512))
282
+
283
+ style_loss = gr.Number(label="Current Style Loss")
284
+
285
+ content_loss = gr.Number(label="Current Content Loss")
286
+
287
+ var_loss = gr.Number(label="Current Total Variation Loss")
288
+
289
+ curr_epoch = gr.Number(label="Current Epoch")
290
+
291
+ curr_step = gr.Number(label="Current Step")
292
+
293
+
294
+
295
+ outputs = [output_image, style_loss, content_loss, var_loss, curr_epoch, curr_step]
296
+
297
+ interface = gr.Interface(
298
+ fn=fit_style_transfer,
299
+ inputs=inputs,
300
+ outputs=outputs,
301
+ examples=examples
302
  )
303
 
304
+ interface.queue().launch(debug=True)
305
 
306
 
307
+ main()
 
 
 
examples/content_1.jpg ADDED

Git LFS Details

  • SHA256: 86b8d00d3303cb3525db04e57f7d23853f5607ebed85561e43ed3a3f61d95176
  • Pointer size: 132 Bytes
  • Size of remote file: 2.36 MB
examples/content_2.jpg ADDED

Git LFS Details

  • SHA256: a04d1bb8ae37a6d6b5d7058a067d4b471d9c9461ce20d5b48fa8390b5afbfa17
  • Pointer size: 132 Bytes
  • Size of remote file: 1.96 MB
examples/content_3.jpg ADDED

Git LFS Details

  • SHA256: 16dfe55991e70e355ac95927d1ad9fa461c58e555d013ad84deb717a89e26da5
  • Pointer size: 132 Bytes
  • Size of remote file: 3.1 MB
examples/style_1.jpg ADDED

Git LFS Details

  • SHA256: 0982c578317c170d444599632c1556f2492b51fa75a2caad9012755893159c52
  • Pointer size: 132 Bytes
  • Size of remote file: 2.59 MB
examples/style_2.jpg ADDED

Git LFS Details

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

Git LFS Details

  • SHA256: 749549277a70212a842011a60228ae91d17026ecac8aecc3aab90799b6eed6a2
  • Pointer size: 132 Bytes
  • Size of remote file: 4.41 MB
flagged/content/tmpu5ej5fhy.jpg CHANGED

Git LFS Details

  • SHA256: 57f197c752613c13f5bdf182cd6213fe8feb9c3711b972e407fa62a314672e13
  • Pointer size: 131 Bytes
  • Size of remote file: 241 kB
flagged/style/tmpesg98402.jpg CHANGED

Git LFS Details

  • SHA256: 57f197c752613c13f5bdf182cd6213fe8feb9c3711b972e407fa62a314672e13
  • Pointer size: 131 Bytes
  • Size of remote file: 241 kB
model.py CHANGED
@@ -5,12 +5,24 @@ from keras import backend as K
5
 
6
 
7
  class NeuralStyleTransfer:
8
- def __init__(self, style_image, content_image, extractor, n_style_layers=5, n_content_layers=5, display=True):
9
  # load the model
10
  if extractor == "inception_v3":
11
  self.feature_extractor = tf.keras.applications.InceptionV3(
12
  include_top=False, weights="imagenet"
13
  )
 
 
 
 
 
 
 
 
 
 
 
 
14
  elif isinstance(extractor, tf.keras.Model):
15
  self.feature_extractor = extractor
16
  else:
@@ -26,13 +38,6 @@ class NeuralStyleTransfer:
26
  self.style_image = self._load_img(style_image)
27
  self.content_image = self._load_img(content_image)
28
 
29
-
30
- if display:
31
- self.show_images_with_objects(
32
- [self.style_image, self.content_image],
33
- ["Style Image", "Content Image"],
34
- )
35
-
36
  def tensor_to_image(self, tensor):
37
  """converts a tensor to an image"""
38
  tensor_shape = tf.shape(tensor)
@@ -215,7 +220,7 @@ class NeuralStyleTransfer:
215
  ]
216
  )
217
  total_loss = style_loss + content_loss
218
- return total_loss
219
 
220
  def _grad_loss(
221
  self,
@@ -237,7 +242,7 @@ class NeuralStyleTransfer:
237
  with tf.GradientTape() as tape:
238
  style_features = self.get_features(generated_image, type="style")
239
  content_features = self.get_features(generated_image, type="content")
240
- loss = self._style_content_loss(
241
  style_target,
242
  style_features,
243
  content_target,
@@ -246,9 +251,11 @@ class NeuralStyleTransfer:
246
  content_weight,
247
  )
248
 
249
- loss += var_weight*tf.image.total_variation(generated_image)
 
 
250
  grads = tape.gradient(loss, generated_image)
251
- return grads, loss
252
 
253
  def _update_image_with_style(
254
  self,
@@ -260,7 +267,7 @@ class NeuralStyleTransfer:
260
  optimizer,
261
  var_weight,
262
  ):
263
- grads, loss = self._grad_loss(
264
  generated_image, style_target, content_target, style_weight, content_weight, var_weight
265
  )
266
 
@@ -269,84 +276,4 @@ class NeuralStyleTransfer:
269
  generated_image.assign(
270
  tf.clip_by_value(generated_image, clip_value_min=0.0, clip_value_max=255.0)
271
  )
272
- return loss
273
-
274
- def fit_style_transfer(
275
- self,
276
- epochs=10,
277
- learning_rate=80,
278
- steps_per_epoch=100,
279
- style_weight=1e-2,
280
- content_weight=1e-4,
281
- show_interval=10,
282
- var_weight=0.0,
283
- ):
284
- """
285
- epochs:
286
- the number of epochs to train the model for
287
-
288
- learning_rate:
289
- the initial learning rate of the optimizer (default: 80)
290
-
291
- steps_per_epoch:
292
- the number of steps to train the model for per epoch
293
-
294
- style_weight:
295
- the weight of the style loss
296
-
297
- content_weight:
298
- the weight of the content loss
299
-
300
- show_image:
301
- whether to save the generated image after each epoch
302
-
303
- show_interval:
304
- the interval at which to save the generated image
305
-
306
- var_weight:
307
- the weight of the total variation loss
308
-
309
- """
310
-
311
- style_image = self.style_image
312
- content_image = self.content_image
313
-
314
- content_and_style_layers = self.get_output_layers()
315
-
316
- # build the model with the layers we need to extract the features from
317
- K.clear_session()
318
- self.build(content_and_style_layers)
319
-
320
- style_features = self.get_features(style_image, type="style")
321
- content_features = self.get_features(content_image, type="content")
322
-
323
- optimizer = tf.optimizers.Adam(
324
- tf.keras.optimizers.schedules.ExponentialDecay(
325
- initial_learning_rate=learning_rate, decay_steps=100, decay_rate=0.80
326
- )
327
- )
328
-
329
- generated_image = tf.cast(content_image, tf.float32)
330
- generated_image = tf.Variable(generated_image)
331
-
332
- step = 0
333
- images = []
334
-
335
- img = None
336
-
337
- for epoch in range(epochs):
338
- for step in range(steps_per_epoch):
339
- loss = self._update_image_with_style(
340
- generated_image,
341
- style_features,
342
- content_features,
343
- style_weight,
344
- content_weight,
345
- optimizer,
346
- var_weight,
347
- )
348
-
349
- display_image = self.tensor_to_image(generated_image)
350
-
351
-
352
-
 
5
 
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(
12
  include_top=False, weights="imagenet"
13
  )
14
+ elif extractor == "vgg19":
15
+ self.feature_extractor = tf.keras.applications.VGG19(
16
+ include_top=False, weights="imagenet"
17
+ )
18
+ elif extractor == "resnet50":
19
+ self.feature_extractor = tf.keras.applications.ResNet50(
20
+ include_top=False, weights="imagenet"
21
+ )
22
+ elif extractor == "mobilenet_v2":
23
+ self.feature_extractor = tf.keras.applications.MobileNetV2(
24
+ include_top=False, weights="imagenet"
25
+ )
26
  elif isinstance(extractor, tf.keras.Model):
27
  self.feature_extractor = extractor
28
  else:
 
38
  self.style_image = self._load_img(style_image)
39
  self.content_image = self._load_img(content_image)
40
 
 
 
 
 
 
 
 
41
  def tensor_to_image(self, tensor):
42
  """converts a tensor to an image"""
43
  tensor_shape = tf.shape(tensor)
 
220
  ]
221
  )
222
  total_loss = style_loss + content_loss
223
+ return total_loss, style_loss, content_loss
224
 
225
  def _grad_loss(
226
  self,
 
242
  with tf.GradientTape() as tape:
243
  style_features = self.get_features(generated_image, type="style")
244
  content_features = self.get_features(generated_image, type="content")
245
+ loss, style_loss, content_loss = self._style_content_loss(
246
  style_target,
247
  style_features,
248
  content_target,
 
251
  content_weight,
252
  )
253
 
254
+ variational_loss= var_weight*tf.image.total_variation(generated_image)
255
+
256
+ loss += variational_loss
257
  grads = tape.gradient(loss, generated_image)
258
+ return grads, loss, [style_loss, content_loss, variational_loss]
259
 
260
  def _update_image_with_style(
261
  self,
 
267
  optimizer,
268
  var_weight,
269
  ):
270
+ grads, loss, loss_list = self._grad_loss(
271
  generated_image, style_target, content_target, style_weight, content_weight, var_weight
272
  )
273
 
 
276
  generated_image.assign(
277
  tf.clip_by_value(generated_image, clip_value_min=0.0, clip_value_max=255.0)
278
  )
279
+ return loss_list