Home

Awesome

<img src="img/demo_readme.jpg" width="800">

receptivefield

Gradient based receptive field estimation for Convolutional Neural Networks. receptivefield uses backpropagation of the gradients from output of selected feature maps to the input image in order to estimate the size (width, height), stride and offset of resulting receptive field. Numerical estimation of receptive field can be useful when dealing with more complicated neural networks like ResNet, Inception (see notebooks) where analytical approach of computing receptive fields cannot be used.

Build Status

Installation & supported APIs

Some remarks

Limitations

Supported APIs

Currently only Keras, Tensorflow and Pytorch API are supported. However, it should be possible to extend receptivefield functionality by deriving abstract class ReceptiveField in the base.py file.

How does it work?

This is description of a general approach which is almost the same for other APIs.

  1. Define build_function which returns model (here Keras model)

    def model_build_func(input_shape=[224, 224, 3]):
        ...
        return Model(input, output)
    
  2. Compute receptive field parameters with KerasReceptiveField

    from receptivefield.keras import KerasReceptiveField
    rf_params = KerasReceptiveField(model_build_func).compute(
        input_shape=[224, 224, 3], # this will be passed to model_build_func
        input_layer='input_image', # must exist - usually input image layer
        output_layers=['feature_map'] # for example last conv layer
    )
    
  3. The rf_params is a list of FeatureMapDescription. Here we selected only single feature map output_layers=['feature_map'] and the rf_params will contain

    rf_params = [
       FeatureMapDescription(
           size=Size(w=60, h=60), 
           rf=ReceptiveFieldDescription(
               offset=(2.5, 2.5), 
               stride=(1.0, 1.0), 
               size=Size(w=9, h=9))
           )
    ]
    

    Explanation of FeatureMapDescription fields:

    • size - defined the spatial dimensions of the feature map i.e. the width and height of the feature map grid.
    • rf is an instance of ReceptiveFieldDescription:
      • offset - defines location of the first left-top anchor in the image coordinates (defined in pixels).
      • stride - defines how much RF of the network moves w.r.t unit displacement in the feature_map tensor.
      • size - defines the effective area in the input image which one point in the feature_map tensor is seeing.

Keras minimal - copy/paste example

Pytorch minimal - copy/paste example

Keras more detailed example

Here we show, how to estimate effective receptive field of any Keras model.