Search code examples
keraskeras-layer

Verifying the output of an isolated layer in keras


Is it possible to verify the output of

Cropping2D(cropping=((22, 0), (0, 0)), 
                     input_shape=resized_image.shape)

Without constructing and training a model? I.e. I just want to pass and image to Cropping and get and display the output image.


Solution

  • Yes, this is described in the Keras FAQ. To quote:

    from keras import backend as K
    
    # with a Sequential model
    get_3rd_layer_output = K.function([model.layers[0].input],
                                      [model.layers[3].output])
    layer_output = get_3rd_layer_output([X])[0]
    

    This example assumes that your Cropping layer has index 3. You need to substitute this index with the correct number according to your model.