Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
bob
bob.learn.tensorflow
Commits
ee40233a
Commit
ee40233a
authored
Jun 25, 2019
by
Tiago de Freitas Pereira
Browse files
Merge branch 'fix-style-transfer' into 'master'
[style] Fixed issues with style-transfer Closes
#82
See merge request
!80
parents
7e18ff07
c38c3ef6
Pipeline
#32531
failed with stages
in 174 minutes and 10 seconds
Changes
3
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
bob/learn/tensorflow/loss/StyleLoss.py
View file @
ee40233a
...
...
@@ -5,6 +5,7 @@
import
logging
import
tensorflow
as
tf
logger
=
logging
.
getLogger
(
"bob.learn.tensorflow"
)
import
functools
def
content_loss
(
noises
,
content_features
):
...
...
@@ -33,7 +34,7 @@ def content_loss(noises, content_features):
content_losses
=
[]
for
n
,
c
in
zip
(
noises
,
content_features
):
content_losses
.
append
((
2
*
tf
.
nn
.
l2_loss
(
n
-
c
)
/
c
.
size
))
return
reduce
(
tf
.
add
,
content_losses
)
return
functools
.
reduce
(
tf
.
add
,
content_losses
)
def
linear_gram_style_loss
(
noises
,
gram_style_features
):
...
...
@@ -63,7 +64,7 @@ def linear_gram_style_loss(noises, gram_style_features):
for
n
,
s
in
zip
(
noises
,
gram_style_features
):
style_losses
.
append
((
2
*
tf
.
nn
.
l2_loss
(
n
-
s
))
/
s
.
size
)
return
reduce
(
tf
.
add
,
style_losses
)
return
functools
.
reduce
(
tf
.
add
,
style_losses
)
...
...
@@ -82,7 +83,7 @@ def denoising_loss(noise):
"""
def
_tensor_size
(
tensor
):
from
operator
import
mul
return
reduce
(
mul
,
(
d
.
value
for
d
in
tensor
.
get_shape
()),
1
)
return
functools
.
reduce
(
mul
,
(
d
.
value
for
d
in
tensor
.
get_shape
()),
1
)
shape
=
noise
.
get_shape
().
as_list
()
...
...
bob/learn/tensorflow/script/style_transfer.py
View file @
ee40233a
...
...
@@ -49,7 +49,7 @@ logger = logging.getLogger(__name__)
help
=
'Number of iterations to generate the image'
,
default
=
1000
)
@
click
.
option
(
'--learning_rate'
,
'-
i
'
,
'-
r
'
,
type
=
click
.
types
.
FLOAT
,
help
=
'Learning rate.'
,
default
=
1.
)
...
...
bob/learn/tensorflow/style_transfer/neural_transfer.py
View file @
ee40233a
...
...
@@ -163,6 +163,7 @@ def do_style_transfer(content_image, style_images,
# Reshaping to NxWxHxC and converting to the tensorflow format
# content
original_image
=
content_image
content_image
=
bob
.
io
.
image
.
to_matplotlib
(
content_image
).
astype
(
"float32"
)
content_image
=
numpy
.
reshape
(
content_image
,
wise_shape
(
content_image
.
shape
))
...
...
@@ -253,11 +254,12 @@ def do_style_transfer(content_image, style_images,
if
normalized_style_image
.
shape
[
0
]
==
1
:
normalized_style_image_yuv
=
bob
.
ip
.
color
.
rgb_to_yuv
(
bob
.
ip
.
color
.
gray_to_rgb
(
normalized_style_image
[
0
,:,:]))
# Loading the content image and clipping from 0-255 in case is in another scale
scaled_content_image
=
normalize4save
(
bob
.
io
.
base
.
load
(
content_image_path
).
astype
(
"float32"
)).
astype
(
"float64"
)
#scaled_content_image = normalize4save(bob.io.base.load(content_image_path).astype("float32")).astype("float64")
scaled_content_image
=
original_image
.
astype
(
"float64"
)
content_image_yuv
=
bob
.
ip
.
color
.
rgb_to_yuv
(
bob
.
ip
.
color
.
gray_to_rgb
(
scaled_content_image
))
else
:
normalized_style_image_yuv
=
bob
.
ip
.
color
.
rgb_to_yuv
(
bob
.
ip
.
color
.
gray_to_rgb
(
bob
.
ip
.
color
.
rgb_to_gray
(
normalized_style_image
)))
content_image_yuv
=
bob
.
ip
.
color
.
rgb_to_yuv
(
bob
.
io
.
base
.
load
(
content_image_path
)
)
content_image_yuv
=
bob
.
ip
.
color
.
rgb_to_yuv
(
original_image
)
output_image
=
numpy
.
zeros
(
shape
=
content_image_yuv
.
shape
,
dtype
=
"uint8"
)
output_image
[
0
,:,:]
=
normalized_style_image_yuv
[
0
,:,:]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment