Search code examples
keraskeras-layer

Keras how to slice output of a layer and then add it to the model


I have a layer which outputs [N,100,100,2]

I want to use only the [N,100,100,0], I have used slice(layer,[0,0,0,0],[-1,-1,-1,1] to get this, however I am unable to add this sliced output to a model as it is not a layer.

How is it done.

Can some one provide a code snippet.

c3 = Convolution2D(2, (1, 1),strides=(1,1),padding='same',use_bias=True,activation='softmax')
output = (c3)(output)
slicedoutput = slice(output,[0,0,0,0],[-1,-1,-1,1])
classifier = Model(inputs=[image_a],outputs=[slicedoutput,outputboxes])

Solution

  • There is no inbuilt slice layer in Keras. But you can use Lambda layer for your purpose.

    In your case instead of using slice from keras.backend you can use Lambda layer

    slicedoutput = Lambda(lambda x: x[:,:,:,0])(output)