{"id":17982203,"url":"https://github.com/kmkolasinski/receptivefield","last_synced_at":"2025-10-12T20:05:53.199Z","repository":{"id":88309212,"uuid":"111994546","full_name":"kmkolasinski/receptivefield","owner":"kmkolasinski","description":"Gradient based receptive field estimation for Convolutional Neural Networks","archived":false,"fork":false,"pushed_at":"2017-11-25T21:57:03.000Z","size":663,"stargazers_count":14,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-12T20:05:24.806Z","etag":null,"topics":["cnn","keras","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kmkolasinski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-25T10:16:47.000Z","updated_at":"2021-06-15T09:16:59.000Z","dependencies_parsed_at":"2023-04-27T21:32:47.596Z","dependency_job_id":null,"html_url":"https://github.com/kmkolasinski/receptivefield","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kmkolasinski/receptivefield","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Freceptivefield","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Freceptivefield/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Freceptivefield/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Freceptivefield/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kmkolasinski","download_url":"https://codeload.github.com/kmkolasinski/receptivefield/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Freceptivefield/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279012791,"owners_count":26085187,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cnn","keras","tensorflow"],"created_at":"2024-10-29T18:13:26.843Z","updated_at":"2025-10-12T20:05:53.179Z","avatar_url":"https://github.com/kmkolasinski.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# receptivefield\n\nGradient based receptive field estimation for Convolutional \nNeural Networks. **receptivefield** uses backpropagation of \nthe gradients from output feature map to input image in order to\nestimate the size (width, height), stride and offset of resulting\nreceptive field. Numerical estimation of receptive field can be \nuseful when dealing with more complicated neural networks like\nResNet, Inception (see notebooks) where analytical approach of \ncomputing receptive fields cannot be used.\n\n# Installation\n\n* `pip install receptivefield` inside the project.\n\n# Some remarks\n\n* In order to get better results or even avoid NaNs in the \nestimated receptive field parameters, it is suggested to \nuse `Linear` (instead `Relu`) activation and `AvgPool2D` instead of `MaxPool2D`.\nThis improves gradient flow in the network and hence better signal\nin the input image. Note, that this is required only for RF estimation.\n\n* Additionally, one may even initialize network with constant \npositive values in all weights (positive if max pooling is used)\nand set biases to zero. In case of Keras API this can be obtained by setting `init_weight=True` \nin the `KerasReceptiveField(init_weight=True)` constructor.\n\n# Keras Example\n\nCurrently only Keras API is supported. However it should be\npossible to extend **receptivefield** functionality by deriving\nabstract class **ReceptiveField** in base.py file. Here we show\nhow to estimate effective receptive field of any Keras model.\n\n* Create model build_function which returns model. This function\nshould accept one parameter `input_shape`.\n\n```python\nfrom keras.layers import Conv2D, Input\nfrom keras.layers import AvgPool2D\nfrom keras.models import Model\n\ndef model_build_func(input_shape):\n    activation = 'linear'\n    padding='valid'\n    \n    inp = Input(shape=input_shape, name='input_image')\n    x = Conv2D(32, (5, 5), padding=padding, activation=activation)(inp)\n    x = Conv2D(32, (3, 3), padding=padding, activation=activation)(x)\n    x = AvgPool2D()(x)\n    x = Conv2D(64, (3, 3), activation=activation, padding=padding)(x)\n    x = Conv2D(64, (3, 3), activation=activation, padding=padding)(x)\n    x = AvgPool2D()(x)\n    x = Conv2D(128, (3, 3), activation=activation, padding=padding)(x)\n    x = Conv2D(128, (3, 3), activation=activation, padding=padding, name='feature_grid')(x)\n\n    model = Model(inp, x)\n    return model\n```\n\n* Check if model is building properly:\n```python\nmodel = model_build_func(input_shape=(96, 96, 3))\nmodel.summary()\n```\n\n```txt\n_________________________________________________________________\nLayer (type)                 Output Shape              Param #   \n=================================================================\ninput_image (InputLayer)     (None, 96, 96, 3)         0         \n_________________________________________________________________\nconv2d_1 (Conv2D)            (None, 92, 92, 32)        2432      \n_________________________________________________________________\nconv2d_2 (Conv2D)            (None, 90, 90, 32)        9248      \n_________________________________________________________________\naverage_pooling2d_1 (Average (None, 45, 45, 32)        0         \n_________________________________________________________________\nconv2d_3 (Conv2D)            (None, 43, 43, 64)        18496     \n_________________________________________________________________\nconv2d_4 (Conv2D)            (None, 41, 41, 64)        36928     \n_________________________________________________________________\naverage_pooling2d_2 (Average (None, 20, 20, 64)        0         \n_________________________________________________________________\nconv2d_5 (Conv2D)            (None, 18, 18, 128)       73856     \n_________________________________________________________________\nfeature_grid (Conv2D)        (None, 16, 16, 128)       147584    \n=================================================================\nTotal params: 288,544\nTrainable params: 288,544\nNon-trainable params: 0\n```\n\n* This step is not required but it is useful to plot results in the\nexample image. For instance you would like to see what is the size\nof network receptive field in comparision to some objects you\nwish detect (or localize) by this network.\n\n```python\nfrom receptivefield.image import get_default_image\nimport matplotlib.pyplot as plt\n# Load sample image of `Lena`.\nimage = get_default_image(shape=(32, 32), tile_factor=1)\nplt.imshow(image)\n```\n\n\u003cimg src=\"img/demo_keras_lena.jpg\" width=\"512\"\u003e\n\n* Compute receptive field of the network by calling `rf.compute`\n\n```python\nfrom receptivefield.keras import KerasReceptiveField\n\nrf = KerasReceptiveField(model_build_func, init_weights=False)\n\nrf_params = rf.compute(\n    input_shape=image.shape, \n    input_layer='input_image', \n    output_layer='feature_grid'\n)\nprint(rf_params)\n\n```\n\n* The resulting receptive field is:\n\n```txt\nReceptiveFieldDescription(offset=(17.0, 17.0), stride=(4.0, 4.0), size=Size(w=34, h=34))\n```\n\n* Input shape: `rf.input_shape==GridShape(n=None, w=96, h=96, c=3)`\n* Output feature map shape: `rf.output_shape==GridShape(n=None, w=16, h=16, c=1)`.\n Note, that number of channels in the output feature map is set to 1 but this is\n used internally by `receptivefield`.\n \n* You may want to see how gradients backpropagate to the input image. Here\n`point=(8, 8)` refers to the (W, H) position of the source signal\nfrom the output grid.\n\n```python\n\nrf.plot_gradient_at(point=(8, 8), image=None, figsize=(7, 7))\n```\n\n\u003cimg src=\"img/demo_keras_response.jpg\" width=\"512\"\u003e\n\n* Or even plot whole receptive field grid:\n\n```python\nrf.plot_rf_grid(custom_image=image, figsize=(6, 6))\n```\n\n\u003cimg src=\"img/demo_keras_rf_debug.jpg\" width=\"512\"\u003e\n\n* In the above, the red rectangle corresponds to the area which top-left\ngrid point is seeing in the input image. Blue rectangle corresponds\nto the central grid point, green to the bottom-right point. Green dots\nshow the position of the centers of the grid anchors in the source\nimage. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmkolasinski%2Freceptivefield","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkmkolasinski%2Freceptivefield","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmkolasinski%2Freceptivefield/lists"}