Keras: Flatten() doenst work with Tensorflow

Created on 22 Nov 2016  路  20Comments  路  Source: keras-team/keras

Flatten() does not work with tensorflow backend.

stale

Most helpful comment

@isaacgerg
I tried your example on keras 1.1.1, and it didn't work too.
I'm assuming the problem comes from the output shape becoming (None, None) after the Flatten layer.

import keras.backend as K
...
m = keras.layers.Convolution2D(64,3,3, border_mode='same')(m)
print K.int_shape(m)
m = Flatten()(m)  # This line is the error, replacing it with the next line fixes it.
print K.int_shape(m)
...

>> (None, 6, 6, 64)
>> (None, None)

I'm personally curious why the Flatten layer's output shape becomes (None, None)... shouldn't the layer specify its output shape?

All 20 comments

It does works with both Tensorflow and Theano backends. You might want to be more specific about what you are trying to do, where it didn't worked or post code snippet so that someone can help you.

@varun-bankiti Here's an example showing it doesn't work in tf. Can you confirm?

This code runs fine with theano backend.

    input = keras.layers.Input(shape=(400,400, 1))
    x = AveragePooling2D(pool_size=(2,2))(input)
    x = Flatten()(x)  # This flatten work fine.
    x = keras.layers.RepeatVector(3)(x)
    x = Reshape((200, 200, 3))(x)

    y = AveragePooling2D(pool_size=(4,4))(input)
    y = Flatten()(y)  # This flatten works fine.
    y = keras.layers.RepeatVector(3)(y)
    y = Reshape((100, 100, 3))(y)

    vgg1 = keras.applications.vgg16.VGG16(include_top=False)
    vgg1.trainable = False    # Doesnt work
    x = vgg1(x)

    vgg2 = keras.applications.vgg16.VGG16(include_top=False)
    vgg2.trainable = False  # Doesnt work
    y = vgg2(y)
    yUp  = keras.layers.UpSampling2D((2,2))(y)

    m = keras.layers.merge([x,yUp], mode='sum')
    m = keras.layers.Convolution2D(64,3,3, border_mode='same')(m)
    m = keras.layers.Flatten()(m)  # This line is the error, replacing it with the next line fixes it.
    #m = keras.layers.Reshape((6*6*64,))(m)
    m = keras.layers.MaxoutDense(8)(m)
    m = keras.layers.Dropout(0.5)(m)
    m = keras.layers.MaxoutDense(4)(m)
    m = keras.layers.Dropout(0.5)(m)
    m = keras.layers.Dense(2, activation='softmax')(m)

    model = keras.models.Model(input, m)
    return model

yields the error:
Expected binary or unicode string, got -1

@varun-bankiti Does the example I posted work for you?

@isaacgerg
I tried your example on keras 1.1.1, and it didn't work too.
I'm assuming the problem comes from the output shape becoming (None, None) after the Flatten layer.

import keras.backend as K
...
m = keras.layers.Convolution2D(64,3,3, border_mode='same')(m)
print K.int_shape(m)
m = Flatten()(m)  # This line is the error, replacing it with the next line fixes it.
print K.int_shape(m)
...

>> (None, 6, 6, 64)
>> (None, None)

I'm personally curious why the Flatten layer's output shape becomes (None, None)... shouldn't the layer specify its output shape?

I use keras as api for tensorflow, and met the same problem:

    y = Dropout(0.25)(y)
    y = Flatten()(y)

and the corresponding shape is:

(?, 6, 6, 2)
(?, ?)

Can any one explain this? Or just a bug. However, it seems that you can use reshape layer to replace it, eg
y = Reshape((24,))(y)

@linkoffate Since the vgg model with args include_top=False will make both height and width None.

@joelthchao Right, but it works fine in theano. Tf is what causes the issue. Since the keras front it is to abstract the backends, the behavior should be the with tf and theano.

@isaacgerg Tensorflow has lots of problem in reshape and I think the problem comes with tf's shape inference algorithm. (ref)

@joelthchao That's fine, but the keras backend needs to work that out so the abstraction is correct.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

With the latest keras 2.0.8 I am still facing the problem described here. The flatten() layer works fine using the theano backend, but not using tensorflow. Are there any plans to fix this or is this a tensorflow and not a keras issue?

Why a tensor with shape (None, 6, 6, 64) was flatten to a shape: (None, None) ?
I think it should be (None, 2304).

Same problem happened in Keras 2.2.0

Same probem in Keras.
Did somebody have solved it?
This problem also exist in LSTM layer.
For example, when setting LSTM(..., return_sequences=True), it doent's return tensor shape (?, timestep, units), but returns (?, ?, units).
I think these problems might due to same reason.

does anyone find an answer for none dimensions after flatten? can we use Reshape instead of Flatten?

same problem. Using Keras Flatten after dropout layer yields (?,?) size. Any solution

It seems this problem is related to use tensorflow backend...
I didn't find any solution to make Flatten directly output the correct shape yet...
But I use tensor.set_shape to reshape the output, so the output can get the correct shape, and It seems to work so far.

same problem when I want to flatten a tensor input ??? any solution

Does this problem occur when using tf.keras?

I'm having the same problem

Was this page helpful?
0 / 5 - 0 ratings

Related issues

braingineer picture braingineer  路  3Comments

harishkrishnav picture harishkrishnav  路  3Comments

fredtcaroli picture fredtcaroli  路  3Comments

somewacko picture somewacko  路  3Comments

anjishnu picture anjishnu  路  3Comments