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:
- Why does
TimeDistributed(Flatten())fail here and why does thetf.Reshapeworkaround succeed? - Furthermore, if it is so, is there a safer or more appropriate way to flatten the output of
Conv3D+MaxPool3Dfor RNN input??