0

I'm building a lip-reading model using tensorflow.keras and getting an error when applying TimeDistributed(Flatten()) after my Conv3D layers. Here's a simplified version of my model architecture:

CODE

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv3D, MaxPool3D, Activation, TimeDistributed, Flatten, Bidirectional, LSTM, Dropout, Dense, Lambda
import tensorflow as tf
model = Sequential()
model.add(Conv3D(128, 3, input_shape=(75, 70, 180, 1), padding='same'))    
model.add(Activation('relu'))   
model.add(MaxPool3D((1, 2, 2)))
model.add(Conv3D(256, 3, padding='same'))    
model.add(Activation('relu'))   
model.add(MaxPool3D((1, 2, 2)))
model.add(Conv3D(75, 3, padding='same'))    
model.add(Activation('relu'))    
model.add(MaxPool3D((1, 2, 2)))    
model.add(TimeDistributed(Flatten()))    
model.add(Bidirectional(LSTM(128, kernel_initializer='Orthogonal', return_sequences=True)))`    
model.add(Dropout(0.5))    
model.add(Bidirectional(LSTM(128, kernel_initializer='Orthogonal', return_sequences=True)))`    
model.add(Dropout(0.5))    
model.add(Dense(char_to_num.vocabulary_size() + 1, kernel_initializer='he_normal', activation='softmax'))

The line causing an error:

model.add(TimeDistributed(Flatten()))

What works instead:

model.add(TimeDistributed(Reshape(x, (-1,)))))
When I use TimeDistributed(Flatten()), I get the following error during model.fit():

Error when i use Flatten in my architecture:

InvalidArgumentError: Graph execution error: Detected at node sequential_1/time_distributed_1/Reshape ...

However, if I replace TimeDistributed(Flatten()) with TimeDistributed(Reshape((-1,))), the error goes away.

My questions is:

  1. Why does TimeDistributed(Flatten()) fail here and why does the tf.Reshape workaround succeed?
  2. Furthermore, if it is so, is there a safer or more appropriate way to flatten the output of Conv3D + MaxPool3D for RNN input??
1
  • You need to post the full error traceback, as it stands this is not very informative. Commented Apr 23 at 4:37

1 Answer 1

0

The error you are encountering stems from TensorFlow’s handling of dynamic shapes in the Flatten layer. When both the batch_size and some other dimensions are dynamic, TensorFlow’s reshape operation fails, as it cannot infer two unknown dimensions at the same time.

However, Reshape([-1]) works because it ensures that only one dimension is being inferred, resolving the error.

This is a known issue with Keras(TensorFlow backend) that has been addressed in this PR which adds support for multiple dynamic dimensions in the Flatten layer, Please try upgrading to the latest version of TensorFlow/Keras and check if the issue still persists. You can also refer to this related issue for more clarity. Thanks!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.