titu1994 / Neural-Style-Transfer

Keras Implementation of Neural Style Transfer from the paper "A Neural Algorithm of Artistic Style" (http://arxiv.org/abs/1508.06576) in Keras 2.0+
Apache License 2.0
2.26k stars 481 forks source link

Use int_shape to prevent type error with Tensorflow backend #36

Closed velikodniy closed 7 years ago

titu1994 commented 7 years ago

Can you describe this error ? I use Tensorflow backend currently and do not see this error.

Also, int_shape is specific to Tensorflow, whereas shape is general to all backend. I would prefer to keep shape.

velikodniy commented 7 years ago

When I try to start INetwork with --content_loss_type 1 I get this error message:

Traceback (most recent call last):
  File "INetwork.py", line 463, in <module>
    combination_features)
  File "INetwork.py", line 429, in content_loss
    multiplier = 1 / (2. * channels ** 0.5 * size ** 0.5)
TypeError: unsupported operand type(s) for ** or pow(): 'Tensor' and 'float'

I use the latest Tensorflow:

tensorflow==1.2.0
tensorflow-gpu==1.2.0

I didn't know that int_shape is specific for Keras. It is not mentioned in the documentation. But the type returned by K.shape() is different for different backends. So I don't know any simple way to fix it without using backend-specific functions.

titu1994 commented 7 years ago

There was a few other problems with the loss measure, and python 2 support for float type division was missing.

I've pushed a fix for the losses. Thanks for your help.

dribnet commented 7 years ago

Just getting started with this code, I had to back out this change because it was causing errors.

python INetwork.py \
    images/inputs/content/blue-moon-lake.jpg \
    images/inputs/style/starry_night.jpg \
    outputs/starry_moon.jpg

Model loaded.
Traceback (most recent call last):
  File "INetwork.py", line 478, in <module>
    combination_features)
  File "INetwork.py", line 440, in content_loss
    channels = K.int_shape(base)[channel_dim]
  File "/usr/local/anaconda/lib/python3.6/site-packages/keras/backend/theano_backend.py", line 250, in int_shape
    raise TypeError('Not a Keras tensor:', x)
TypeError: ('Not a Keras tensor:', Subtensor{int64, ::, ::, ::}.0)
titu1994 commented 7 years ago

@dribnet Interesting. Does the K.shape() version work ?

titu1994 commented 7 years ago

@dribnet Can you see if replacing line 440 of INetwork.py with this works ?

    try:
        channels = K.int_shape(base)[channel_dim]
    except TypeError:
        channels = K.shape(base)[channel_dim]
dribnet commented 7 years ago

Yes - things seem to be working again after commit 037a8a78, thanks!

titu1994 commented 7 years ago

That's good. Sorry for the trouble.