Spaces:
Runtime error
Runtime error
Added multiple models
Browse files- .gitattributes +1 -0
- app.py +288 -82
- examples/content_1.jpg +3 -0
- examples/content_2.jpg +3 -0
- examples/content_3.jpg +3 -0
- examples/style_1.jpg +3 -0
- examples/style_2.jpg +3 -0
- examples/style_3.jpg +3 -0
- flagged/content/tmpu5ej5fhy.jpg +0 -0
- flagged/style/tmpesg98402.jpg +0 -0
- model.py +21 -94
.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
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
model = NeuralStyleTransfer(
|
11 |
-
style_image=
|
12 |
-
content_image=
|
13 |
extractor=extractor,
|
14 |
-
n_content_layers=n_content_layers,
|
15 |
n_style_layers=n_style_layers,
|
|
|
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 |
-
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
|
examples/content_2.jpg
ADDED
Git LFS Details
|
examples/content_3.jpg
ADDED
Git LFS Details
|
examples/style_1.jpg
ADDED
Git LFS Details
|
examples/style_2.jpg
ADDED
Git LFS Details
|
examples/style_3.jpg
ADDED
Git LFS Details
|
flagged/content/tmpu5ej5fhy.jpg
CHANGED
Git LFS Details
|
flagged/style/tmpesg98402.jpg
CHANGED
Git LFS Details
|
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
|
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 |
-
|
|
|
|
|
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
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|