I have a model with an input (batch of images w/ shape (height, width, time)) that has a dynamically sized dimension (time), which is only determined at runtime. However, the Dense
layer requires fully defined spatial dimensions. Code snippet example:
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Input
# Define an input with an undefined dimension (None)
input_tensor = Input(shape=(None, 256, 256, None, 13))
# Apply a Dense layer (which expects a fully defined shape)
x = Flatten()(input_tensor)
x = Dense(10)(x)
# Build the model
model = tf.keras.models.Model(inputs=input_tensor, outputs=x)
model.summary()
This raises the error:
ValueError: The last dimension of the inputs to a Dense layer should be defined. Found None.
How can I make it work using Flatten
instead of alternatives like GlobalAveragePooling3D
? Essentially, I’m looking for a way to create a 1D array with the original pixel values, but compatible with the Dense
layer.
Flatten()
but with the Dense layer after it, which needs a fixed input size. So you have to get rid of the dynamic size, plain and simple. Also, you are clearly getting rid of pixel-level information anyway by flattening and applying a Dense layer, so that doesn't seem so important after all...