'Learnable Cropping Layer in Keras

Inside a keras model, a Cropping2D layer can be used to crop images, which is this:

tf.keras.layers.Cropping2D(
    cropping=((2, 2), (4, 4)), data_format=None, **kwargs
)

Here, the dimensions has to be mentioned and so my question is:
How do make this a learnable layer, which automatically finds out the dimension and then crops it?

The aim of my model is to classify images of birds to 20 classes.
The problem is that some images have birds that are very far away, and some are enough close. So I want to crop the images to the part of the image where the bird is present and then classify it.
I cannot use techniques like YOLO because I don't have the bounding box labels. That's why I choose this way of classification:

  1. Input image of 1024x1024x3
  2. Use CNN to extract features and then some fully connected layers to find the bounding box and then resize the image to 128x128x3
  3. Use Cropping2D layer to crop the image using the calculated bounding box.
  4. Use a CNN to extract features from the cropped image and then an some fully connected layers to classify the image
  5. Use categorical_crossentropy to calculate the overall loss of the model and update accordingly.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source