Dohn Joe
Dohn Joe

Reputation: 37

Keras: input_shape=train_data.shape produces "list index out of range"

I want to use Keras to build a CNN-LSTM network. However, I have trouble finding the right shape for the first layer's input_shape parameter.

My train_data is a ndarray of the shape (1433, 32, 32); 1433 pictures of size 32x32.

As found in this example, I tried using input_shape=train_data.shape[1:], which results in the same error as input_shape=train_data.shape:

IndexError: list index out of range

The relevant code is:

train_data, train_labels = get_training_data()
# train_data = train_data.reshape(train_data.shape + (1,))
model = Sequential()
model.add(TimeDistributed(Conv2D(
      CONV_FILTER_SIZE[0],
      CONV_KERNEL_SIZE,
      activation="relu",
      padding="same"),
   input_shape=train_data.shape[1:]))

All the results I found for this error were produced under different dircumstances; not through input_shape. So how do I have to shape my Input? Do I have to look for the error somewhere completely different?

Update: Complete error:

Traceback (most recent call last):
File "trajecgen_keras.py", line 131, in <module>
    tf.app.run()
File "/home/.../lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 124, in run
    _sys.exit(main(argv))
File "trajecgen_keras.py", line 85, in main
    input_shape=train_data.shape))
File "/home/.../lib/python3.5/site-packages/keras/models.py", line 467, in add
    layer(x)
File "/home/.../lib/python3.5/site-packages/keras/engine/topology.py", line 619, in __call__
    output = self.call(inputs, **kwargs)
File "/home/.../lib/python3.5/site-packages/keras/layers/wrappers.py", line 211, in call
    y = self.layer.call(inputs, **kwargs)
File "/home/.../lib/python3.5/site-packages/keras/layers/convolutional.py", line 168, in call
    dilation_rate=self.dilation_rate)
File "/home/.../lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 3335, in conv2d
    data_format=tf_data_format)
File "/home/.../lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 753, in convolution
    name=name, data_format=data_format)
File "/home/.../lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 799, in __init__
    input_channels_dim = input_shape[num_spatial_dims + 1]
File "/home/../lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 521, in __getitem__
    return self._dims[key]
IndexError: list index out of range

Upvotes: 3

Views: 4272

Answers (1)

kluu
kluu

Reputation: 2995

When using a TimeDistributed layer combined with a Conv2D layer, it seems that input_shape requires a tuple of length 4 at least: input_shape = (number_of_timesteps, height, width, number_of_channels).

You could try to modify your code like this for example:

model = Sequential()
model.add(TimeDistributed(Conv2D(
      CONV_FILTER_SIZE[0],
      CONV_KERNEL_SIZE,
      activation="relu",
      padding="same"),
    input_shape=(None, 32, 32, 1))

More info here.

Upvotes: 4

Related Questions