1

I am trying to see what my data looks like after going through a convolutional layer in my Keras model. I am using the Theano backend. The code I have has been cobbled together from the Keras Github:

def get_layer0_outputs(model, test_data):
    output = model.layers[0].output
    inputs = [K.learning_phase()] + model.inputs
    func = K.function(inputs, [output])
    return func([0] + [test_data])

What I'm trying to do here is compile a function for the first layer (a Conv2D layer) in my network. The test_data argument is an np.ndarray. My model is loaded correctly, and I have already trained it with decent accuracy.

However, when I call this function, I get a cryptic stacktrace:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/function_module.py", line 884, in __call__
    self.fn() if output_subset is None else\
  File "/usr/local/lib/python3.5/dist-packages/theano/gof/op.py", line 872, in rval
    r = p(n, [x[0] for x in i], o)
  File "/usr/local/lib/python3.5/dist-packages/theano/tensor/nnet/abstract_conv.py", line 1626, in perform
    conv_out = self.conv(img, kern, mode="valid", dilation=self.filter_dilation)
  File "/usr/local/lib/python3.5/dist-packages/theano/tensor/nnet/abstract_conv.py", line 1531, in conv
    dilated_kern[n, im0, ...],
IndexError: index 1 is out of bounds for axis 1 with size 1

What does this mean? Am I calling my function incorrectly?

1 Answer 1

0

Your function works for me using the following model:

a = Input(shape=(224,224,3))
b = Conv2D(8, 3, strides=(2,2))(a)
model = Model(inputs=a, outputs=b)
model.compile(optimizer='sgd', loss='mse')

def get_layer0_outputs(model, test_data):
    output = model.layers[0].output
    inputs = [K.learning_phase()] + model.inputs
    func = K.function(inputs, [output])
    return func([0] + [test_data])

print get_layer0_outputs(model, np.zeros((1, 224, 224, 3)))[0].shape

Note that layer 0 is an Input layer not a Conv2D, but the code also works for layer 1. I'm using the tensorflow backend so I don't know if the difference is your model or the theano backend.

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

3 Comments

Interesting. I'll try with tensorflow.
What versions of Keras and TensorFlow are you using? Are you using Python 2 or 3?
Keraas 2.0.5, TensorFlow 1.2.1, Python 2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.