1

I want to create a Keras model consisting of a CNN => RNN => FCN.

The model must be able to drop the elements of each sequence that are padded as zero 2D arrays.

# Create model
model = Sequential()
input_shape = (n_tel, img_w, img_h)

# mask the inputs that correspond to padding
model.add(Masking(mask_value=0., input_shape=input_shape))

# Add CNN feature extractor
model.add(TimeDistributed(
        Conv2D(
                filters=16, 
                kernel_size=(3,3), 
                padding='same',
                activation='relu'
                )
        ))
model.add(TimeDistributed(
        Conv2D(
                filters=32, 
                kernel_size=(3,3), 
                padding='same',
                activation='relu'
                )
        ))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2,2), strides=None)))

# Add LSTM feature extractor
model.add(LSTM(units=100))

model.add(Dense(100))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))

The above code returns an error that triggers on the first Conv2D layer:

IndexError: list index out of range

What is going on?


EDIT: Full traceback

  Traceback (most recent call last):

  File "<ipython-input-10-7253565e1e8b>", line 1, in <module>
    runfile('/home/jsevillamol/Documentos/ctlearn_keras/models/build_cnn_rnn.py', wdir='/home/jsevillamol/Documentos/ctlearn_keras/models')

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/jsevillamol/Documentos/ctlearn_keras/models/build_cnn_rnn.py", line 78, in <module>
    model = build_cnn_rnn(args.n_tel, args.img_w, args.img_h)

  File "/home/jsevillamol/Documentos/ctlearn_keras/models/build_cnn_rnn.py", line 27, in build_cnn_rnn
    activation='relu'

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 364, in _method_wrapper
    method(self, *args, **kwargs)

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 184, in add
    output_tensor = layer(self.outputs[0])

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 728, in __call__
    self.build(input_shapes)

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/keras/layers/wrappers.py", line 213, in build
    self.layer.build(tuple(child_input_shape))

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 182, in build
    self.rank + 2))

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 828, in __init__
    input_channels_dim = input_shape[num_spatial_dims + 1]

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 615, in __getitem__
    return self._dims[key]

IndexError: list index out of range
Jsevillamol
  • 2,425
  • 2
  • 23
  • 46
  • Please post the full error stack trace. – today Oct 28 '18 at 13:06
  • @today the post now has the full traceback :) – Jsevillamol Oct 28 '18 at 13:12
  • Each input sample must be a 4D array. Change the input shape and add the number of channels (i.e. last axis) to it. – today Oct 28 '18 at 13:20
  • @today thank you! After adding the channel dimension I still get an error `TypeError: Layer conv2d_7 does not support masking, but was passed an input_mask: Tensor("time_distributed_6/Reshape_2:0", shape=(?, 1000, 1000), dtype=bool)` – Jsevillamol Oct 28 '18 at 13:27
  • It is self-explanatory: convolution layers does not support masking. – today Oct 28 '18 at 13:30
  • 1
    As far as I know, masking is usually used for sequence data. The issue you are referring to concerns Conv1D layers which is used for processing sequence data and as you can see it is a stale issue. You are using Conv2D layers and I am not sure how masking is done in this context. I don't know about the specific problem you are working on but maybe you can leave zero padding as it is and let the model learn to ignore them (without any explicit masking mechanism). [This answer](https://stackoverflow.com/a/52570297/2099607) might help as well. – today Oct 28 '18 at 13:44

0 Answers0