{
  "source_file": "0930",
  "source_platform": "Medium",
  "author": "Muhammad Ardi Putra",
  "title_listed": "SPP-Net Paper Walkthrough: Breaking the Fixed-Size Constraint",
  "keywords": "",
  "domain": "图像处理",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://towardsdatascience.com/spp-net-paper-walkthrough-breaking-the-fixed-size-constraint/",
  "publish_date": "2026-08-10",
  "corpus_stage": "",
  "notes": "",
  "id": "465855dd76aa",
  "collected_at": "2026-10-02T02:12:26",
  "title": "SPP-Net Paper Walkthrough: Breaking the Fixed-Size Constraint | Towards Data Science",
  "body_markdown": "![Revisit consent button](../images/body-df29beacd7496220cc94.svg)\nWe value your privacy\nWe use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking \"Accept All\", you consent to our use of cookies.\nCustomise Reject All Accept All\nCustomise Consent Preferences ![](../images/body-2159bb67f3de8ad870eb.svg)\nWe use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.\nThe cookies that are categorised as \"Necessary\" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... Show more\nNecessaryAlways Active\nNecessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.\n  * Cookie\nBCTempID\n  * Duration\n10 minutes\n  * Description\nNo description available.\n\n\n  * Cookie\n__cf_bm\n  * Duration\n1 hour\n  * Description\nThis cookie, set by Cloudflare, is used to support Cloudflare Bot Management. \n\n\n  * Cookie\nAWSALBCORS\n  * Duration\n7 days\n  * Description\nAmazon Web Services set this cookie for load balancing.\n\n\n  * Cookie\n_cfuvid\n  * Duration\nsession\n  * Description\nCloudflare sets this cookie to track users across sessions to optimize user experience by maintaining session consistency and providing personalized services\n\n\n  * Cookie\nli_gc\n  * Duration\n6 months\n  * Description\nLinkedin set this cookie for storing visitor's consent regarding using cookies for non-essential purposes.\n\n\n  * Cookie\n__hssrc\n  * Duration\nsession\n  * Description\nThis cookie is set by Hubspot whenever it changes the session cookie. The __hssrc cookie set to 1 indicates that the user has restarted the browser, and if the cookie does not exist, it is assumed to be a new session.\n\n\n  * Cookie\n__hssc\n  * Duration\n1 hour\n  * Description\nHubSpot sets this cookie to keep track of sessions and to determine if HubSpot should increment the session number and timestamps in the __hstc cookie.\n\n\n  * Cookie\nwpEmojiSettingsSupports\n  * Duration\nsession\n  * Description\nWordPress sets this cookie when a user interacts with emojis on a WordPress site. It helps determine if the user's browser can display emojis properly.\n\n\n  * Cookie\nBCSessionID\n  * Duration\n1 year 1 month 4 days\n  * Description\nBlueconic sets this cookie as a unique identifier for the BlueConic profile.\n\n\n  * Cookie\n_octo\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nlogged_in\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\n__Secure-YEC\n  * Duration\npast\n  * Description\nYouTube sets this cookie to stores the user's video player preferences using embedded YouTube video\n\n\n  * Cookie\n__eoi\n  * Duration\n6 months\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nAWSALBTGCORS\n  * Duration\n7 days\n  * Description\nNo description available.\n\n\n  * Cookie\nlogin-status-p\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nAWSALBTG\n  * Duration\n7 days\n  * Description\nNo description available.\n\n\n  * Cookie\ncsrf_token\n  * Duration\nsession\n  * Description\nNo description available.\n\n\n  * Cookie\ntoken_v2\n  * Duration\n1 day\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nD\n  * Duration\n1 year\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nPHPSESSID\n  * Duration\nsession\n  * Description\nThis cookie is native to PHP applications. The cookie stores and identifies a user's unique session ID to manage user sessions on the website. The cookie is a session cookie and will be deleted when all the browser windows are closed.\n\n\n  * Cookie\nVISITOR_PRIVACY_METADATA\n  * Duration\n6 months\n  * Description\nYouTube sets this cookie to store the user's cookie consent state for the current domain. \n\n\n  * Cookie\ncookietest\n  * Duration\nsession\n  * Description\nThe cookietest cookie is typically used to determine whether the user's browser accepts cookies, essential for website functionality and user experience.\n\n\n  * Cookie\n__Host-airtable-session\n  * Duration\n1 year\n  * Description\nThis cookie is used to enable us to integrate the services of Airtable.\n\n\n  * Cookie\n__Host-airtable-session.sig\n  * Duration\n1 year\n  * Description\nThis cookie is used to enable us to integrate the services of Airtable.\n\n\n  * Cookie\nm\n  * Duration\n1 year 1 month 4 days\n  * Description\nStripe sets this cookie for fraud prevention purposes. It identifies the device used to access the website, allowing the website to be formatted accordingly.\n\n\n  * Cookie\nBIGipServer*\n  * Duration\nsession\n  * Description\nMarketo sets this cookie to collect information about the user's online activity and build a profile about their interests to provide advertisements relevant to the user. \n\n\n  * Cookie\n__cfruid\n  * Duration\nsession\n  * Description\nCloudflare sets this cookie to identify trusted web traffic.\n\n\n  * Cookie\n_GRECAPTCHA\n  * Duration\n6 months\n  * Description\nGoogle Recaptcha service sets this cookie to identify bots to protect the website against malicious spam attacks.\n\n\n  * Cookie\n__Secure-YNID\n  * Duration\n6 months\n  * Description\nGoogle cookie used to protect user security and prevent fraud, especially during the login process.\n\n\n  * Cookie\ncookieyes-consent\n  * Duration\n1 year\n  * Description\nCookieYes sets this cookie to remember users' consent preferences so that their preferences are respected on subsequent visits to this site. It does not collect or store any personal information about the site visitors.\n\n\nFunctional\nFunctional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.\n  * Cookie\nlidc\n  * Duration\n1 day\n  * Description\nLinkedIn sets the lidc cookie to facilitate data center selection.\n\n\n  * Cookie\nbrw\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nbrwConsent\n  * Duration\n5 minutes\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nWMF-Uniq\n  * Duration\n1 year\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nloom_anon_comment\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nloom_referral_video\n  * Duration\nsession\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nVISITOR_INFO1_LIVE\n  * Duration\n6 months\n  * Description\nA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.\n\n\n  * Cookie\nyt-remote-connected-devices\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to store the user's video preferences using embedded YouTube videos.\n\n\n  * Cookie\nytidb::LAST_RESULT_ENTRY_KEY\n  * Duration\nNever Expires\n  * Description\nThe cookie ytidb::LAST_RESULT_ENTRY_KEY is used by YouTube to store the last search result entry that was clicked by the user. This information is used to improve the user experience by providing more relevant search results in the future.\n\n\n  * Cookie\nyt-remote-device-id\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to store the user's video preferences using embedded YouTube videos.\n\n\n  * Cookie\nyt-remote-session-name\n  * Duration\nsession\n  * Description\nThe yt-remote-session-name cookie is used by YouTube to store the user's video player preferences using embedded YouTube video.\n\n\n  * Cookie\nyt-remote-fast-check-period\n  * Duration\nsession\n  * Description\nThe yt-remote-fast-check-period cookie is used by YouTube to store the user's video player preferences for embedded YouTube videos.\n\n\n  * Cookie\nyt-remote-session-app\n  * Duration\nsession\n  * Description\nThe yt-remote-session-app cookie is used by YouTube to store user preferences and information about the interface of the embedded YouTube video player.\n\n\n  * Cookie\nyt-remote-cast-available\n  * Duration\nsession\n  * Description\nThe yt-remote-cast-available cookie is used to store the user's preferences regarding whether casting is available on their YouTube video player.\n\n\n  * Cookie\nyt-remote-cast-installed\n  * Duration\nsession\n  * Description\nThe yt-remote-cast-installed cookie is used to store the user's video player preferences using embedded YouTube video.\n\n\n  * Cookie\ncp_session\n  * Duration\n3 months\n  * Description\nCodepen sets this cookie for Help systems found in the website.\n\n\n  * Cookie\nloid\n  * Duration\n1 year 1 month 4 days\n  * Description\nThis cookie is set by the Reddit. The cookie enables the sharing of content from the website onto the social media platform.\n\n\nAnalytics\nAnalytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.\n  * Cookie\n__hstc\n  * Duration\n6 months\n  * Description\nHubspot set this main cookie for tracking visitors. It contains the domain, initial timestamp (first visit), last timestamp (last visit), current timestamp (this visit), and session number (increments for each subsequent session).\n\n\n  * Cookie\nhubspotutk\n  * Duration\n6 months\n  * Description\nHubSpot sets this cookie to keep track of the visitors to the website. This cookie is passed to HubSpot on form submission and used when deduplicating contacts.\n\n\n  * Cookie\n_ga\n  * Duration\n1 year 1 month 4 days\n  * Description\nGoogle Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors.\n\n\n  * Cookie\n_ga_*\n  * Duration\n1 year 1 month 4 days\n  * Description\nGoogle Analytics sets this cookie to store and count page views.\n\n\n  * Cookie\n__Host-psifi.analyticsTrace\n  * Duration\n6 hours\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\n__Host-psifi.analyticsTraceV2\n  * Duration\n6 hours\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\n_gh_sess\n  * Duration\nsession\n  * Description\nGitHub sets this cookie for temporary application and framework state between pages like what step the user is on in a multiple step form.\n\n\n  * Cookie\nYSC\n  * Duration\nsession\n  * Description\nYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.\n\n\n  * Cookie\najs_anonymous_id\n  * Duration\n1 year\n  * Description\nThis cookie is set by Segment to count the number of people who visit a certain site by tracking if they have visited before.\n\n\n  * Cookie\nvuid\n  * Duration\n1 year 1 month 4 days\n  * Description\nVimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. \n\n\nPerformance\nPerformance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.\n  * Cookie\nAWSALB\n  * Duration\n7 days\n  * Description\nAWSALB is an application load balancer cookie set by Amazon Web Services to map the session to the target.\n\n\n  * Cookie\nacq\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nacq.sig\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nptc\n  * Duration\n2 years\n  * Description\nNo description available.\n\n\nAdvertisement\nAdvertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.\n  * Cookie\nmuc_ads\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to collect user behaviour and interaction data to optimize the website.\n\n\n  * Cookie\nguest_id_marketing\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor.\n\n\n  * Cookie\nguest_id_ads\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor.\n\n\n  * Cookie\npersonalization_id\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to integrate and share features for social media and also store information about how the user uses the website, for tracking and targeting.\n\n\n  * Cookie\nguest_id\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor. It registers if a user is signed in to the Twitter platform and collects information about ad preferences.\n\n\n  * Cookie\nbcookie\n  * Duration\n1 year\n  * Description\nLinkedIn sets this cookie from LinkedIn share buttons and ad tags to recognize browser IDs.\n\n\n  * Cookie\n__Secure-ROLLOUT_TOKEN\n  * Duration\n6 months\n  * Description\nYouTube sets this cookie to manage feature rollout and experimentation. It helps Google control which new features or interface changes are shown to users as part of testing and staged rollouts, ensuring consistent experience for a given user during an experiment.\n\n\n  * Cookie\nyt.innertube::nextId\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.\n\n\n  * Cookie\nyt.innertube::requests\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.\n\n\n  * Cookie\nsession_tracker\n  * Duration\nsession\n  * Description\nThis cookie is set by the Reddit. This cookie is used to identify trusted web traffic. It also helps in adverstising on the website.\n\n\n  * Cookie\nedgebucket\n  * Duration\nsession\n  * Description\nReddit sets this cookie to save the information about a log-on Reddit user, for the purpose of advertisement recommendations and updating the content.\n\n\n  * Cookie\ndid\n  * Duration\n1 year\n  * Description\nArbor sets this cookie to show targeted ads to site visitors.This cookie expires after 2 months or 1 year.\n\n\nUncategorised\nOther uncategorised cookies are those that are being analysed and have not been classified into a category as yet.\nNo cookies to display.\nReject All Save My Preferences Accept All\nToggle Search[](https://towardsdatascience.com/)Toggle Mobile Navigation\n[](https://towardsdatascience.com/)\nPublish AI, ML & data-science insights to a global community of data professionals.\nSign in[Submit an Article](https://write.insightmediagroup.io)\n[Latest](https://towardsdatascience.com/latest/)[Editor's Picks](https://towardsdatascience.com/tag/editors-pick/)[Newsletter](https://towardsdatascience.com/tag/the-variable/)[ShipAI](https://towardsdatascience.com/shipai/)\n* * *\n[Write For TDS](https://towardsdatascience.com/submissions/)\n[LinkedIn](https://www.linkedin.com/company/towards-data-science/)[X](https://x.com/TDataScience)Toggle Search\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\n# SPP-Net Paper Walkthrough: Breaking the Fixed-Size Constraint\nLearn how Spatial Pyramid Pooling enables CNNs to handle any image size, with a from-scratch PyTorch implementation\n[Muhammad Ardi Putra](https://towardsdatascience.com/author/muhammad_ardi/)\nAugust 10, 202618 min read\n![](../images/000.jpg)\nPhoto by [Viktor Theo](https://unsplash.com/@viktortheo?utm_source=medium&utm_medium=referral) on Unsplash\nIn the past, a CNN model needed to have a fixed input dimension. This was actually a problem because objects in our surroundings have varying dimensions. With humans and cars, for example, we cannot use the exact same bounding box to approximate them since humans tend to have a vertical shape while it is more appropriate to approximate a car with a horizontal bounding box. The easiest way to address this issue is by using either cropping or warping, and by doing so we can set an image to have a specific size. However, if the image is cropped, the cropped area might cut off part of the object, while if we warp the image, we will have the object distorted. These transformations clearly cause the visual appearance of the objects to change weirdly, thus it might potentially reduce model accuracy if we were to use them to train a classification model. Take a look at Figure 1 below to better understand what I mean.\n![](../images/001.png)\nFigure 1. Examples of how cropping (left) and warping (right) respectively truncate and distort image content [1].\nIn this article we are going to talk about SPP-Net, a CNN-based model that can accept images of varying sizes so that we can completely avoid using cropping or warping mechanism. Not only that, here I will also try to implement it from scratch with PyTorch so that you can get a better understanding about the detailed architecture.\n···\nLearn this step by step with the interactive [Machine Learning](https://roadmap.sh/machine-learning) roadmap.\n## A Brief History of SPP-Net\nSPP-Net was introduced in a paper titled “ _Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition_ ” written by He _et al._ back in June 2014 [1]. It is important to note that SPP itself already existed long before this paper. So, what this paper essentially proposed was SPP-Net, which is the very first model to integrate the idea of SPP on a CNN. \n### CNN vs SPP-Net\n![](../images/002.png)\nFigure 2. The flow of the conventional CNN architecture (top) vs the flow of a CNN with SPP layer (bottom) [1].\nThe upper flowchart in Figure 2 displays the workflow of the conventional CNN model. This process requires us to perform cropping or warping to make the images in the dataset have uniform size before passing them through the model. Meanwhile, the lower flowchart in the figure shows the flow when we use SPP layer in the CNN. Here you can see that we don’t need the cropping or warping step since we already have the SPP layer placed between the convolution layers and the fully-connected layers within the network.\nIf we take a closer look at the CNN architecture, what basically makes the conventional CNN able to only accept a fixed-size tensor is its fully-connected layer. The convolution layers themselves can naturally accept input of varying sizes due to the shared weights across the spatial dimension of the image. For instance, if we have a conv layer with a 3×3 kernel, then we will have 9 trainable weights (or 10 if we include the bias term) regardless of the size of the image passed through the network. This property is different from a fully-connected layer, where every single element in the tensor needs to have its corresponding weight. For example, if we have an image of size 10×10, we need to flatten it into a single-dimensional tensor of length 100, and thus we will also need that same number of weights (or 101 with the bias term). This essentially means that if the shape of the input tensor changes, then we will require a different number of weights as well. This behavior makes an FC layer unsuitable for processing images of varying resolutions.\nWith this problem in mind, the authors of SPP-Net proposed a mechanism where we can still utilize the property of CNN in handling varying input sizes while adapting the extracted feature map to a fixed-size vector required by the fully-connected layer. This is essentially the reason why in Figure 2 they placed the SPP layer between the stack of convolutions and the FC layers.\n···\n## How SPP Layer Works\nFigure 3 below displays the detailed steps of how SPP-Net processes an image. In accordance with the lower flowchart in Figure 2, we initially pass the raw image through a stack of convolution layers. In one of their experiments, they used a network of 5 convolutions which they refer to as _conv₁_ , _conv₂_ , _conv₃_ , _conv₄_ and _conv₅_. For the sake of simplicity, the figure below only displays the tensor produced by the last convolution, which is the one to be forwarded to the SPP layer.\n![](../images/body-08abb7ffe39c229ee0f4.png)\nFigure 3. The process within the SPP layer [1].\nInside the SPP layer, what we do first is to divide the spatial dimension of the tensor into grids and take the maximum value within each grid — just like a typical maxpooling operation. There are 3 grid arrangements we use here: 4×4, 2×2, and 1×1, in which we will apply each of them to the same tensor. Finally, these grid cells of maximum values are flattened and concatenated, ready to be forwarded to the fully-connected part of the network.\nThe grid arrangements are created independently of the spatial resolution of the image. This means that we will always have the same number of grid cells regardless of the input tensor shape, which also implies that at the end of the process we will obtain a fixed-length vector. Let’s take a look at the examples in Figure 4 and 5 below to prove this.\n![](../images/body-103f93dc7cd031ba8db5.png)\nFigure 4. How SPP with 4×4, 2×2, and 1×1 grid arrangements works on an 8×8 feature map [2].\nFigure 4 above displays a case where our input feature map has the resolution of 8×8. It is mentioned in the paper that we can find the resulting feature vector length by computing _Mk_ , where _M_ is the number of spatial bins (the grids) and _k_ is the number of channels of the tensor. In this case, we assume that our tensor only has a single channel, so we can simply compute _M_ and multiply everything with 1. With the above configuration, we will get a feature vector with a length of (4×4 + 2×2 + 1×1) × 1 = 21. Now if we try to use the same configuration on different image size, say 12×8, the SPP layer will still produce the exact same vector length as it automatically adjusts the window size and the stride. You can see in Figure 5 below that the pooling window becomes rectangle.\n![](../images/body-548c8fac76f8eae68d69.png)\nFigure 5. How the same SPP grid arrangements work on a 12×8 feature map. This one also produces a feature vector of length 21 [2].\n···\n## Advantages of SPP-Net\nThe way SPP works discussed above allows SPP-Net to be more robust against overfitting. In fact, what makes the conventional CNN prone to overfit is the flatten operation done on the feature map produced by the last convolution layer. This is essentially because flattening destroys the spatial structure of the feature map, forcing the FC layer to learn position-specific features, which breaks the translation-invariant property that the convolutional layers provide. By using SPP, we basically still preserve the spatial information through the smaller pooling window (the 4×4 grid) while capturing the holistic view of the image through the large pooling window (the 1×1 grid). This implies that the part of the feature vector highlighted in blue in Figures 4 and 5 stores more detailed spatial information, while the one highlighted in gray stores the general information of that image.\nThe reason that SPP-Net is less prone to overfitting is because of the invariance of the 1D feature vector against object deformation and spatial layout. We can think of the _object deformation invariance_ like this: suppose in the training set we got a bunch of images of a person standing — SPP allows our model to predict the person even if they are sitting since our feature vector is now less rigid to such subtle changes. Meanwhile, _spatial layout invariance_ refers to a condition where our model is trained with images of a person located at the center of the image yet is tested on images where the person is not located exactly in the middle. SPP allows the model to predict it correctly since the pixels are now grouped into larger spatial bins, making slight shifts in the object location undetected.\nAnother advantage we get by using SPP-Net is that we can train the model with images of varying resolutions and aspect ratios, which again, allows the model to be even better to handle overfitting. It is explained in the paper that the authors decided to randomly resize the input image between the range of 180×180 and 224×224 after completing each epoch.\n···\n## Experimental Results\nTalking about the experimental results, it is shown in Figure 6 below that implementing SPP allows a model to achieve lower classification loss. Moreover, we can decrease this loss value even further by training the model with varying image scales. This trend can actually be observed in all backbone models the authors experimented with. They therefore explicitly mention in the paper that the advantages of SPP are orthogonal to specific CNN designs — meaning that we can improve the performance of any backbone model simply by attaching an SPP layer to it.\n![](../images/body-4999ea689efec3a5460e.png)\nFigure 6. Classification result on ImageNet 2012 dataset [1].\n### Experiments on Detection Task\nNot only on classification task, but the authors also tried to use SPP-Net to perform object detection, which some of their experimental results are summarized in Figure 7 below.\n![](../images/body-4376bb3f8e27f2afb0b1.png)\nFigure 7. Experimental results on object detection task [1].\nRemember that back in 2014 R-CNN was the state-of-the-art model for object detection, which unfortunately was extremely slow. You can see in the table above that SPP-based approach performed 38× faster than R-CNN while maintaining the exact same mAP (59.2). We can actually make the SPP-Net even faster by performing prediction on a single scale only, which they found that it could boost the speed over 100× faster than R-CNN by slightly sacrificing the mAP.\nTalking more specifically about the underlying algorithm, R-CNN is very slow because it needs to classify approximately 2000 bounding box candidates by passing each of them through the entire CNN model one by one. This process is modified by SPP-Net, where instead of passing each object candidate through the CNN, we can just pass the entire image through it and then classify the bounding box candidates from the deeper feature map using the SPP and the FC layers (see Figure 8). By doing so, we essentially avoid performing the repeating feature extraction process done by the CNN. Instead, we can now just perform the feature extraction once and do the ~2000 classifications based on the 1-dimensional feature vectors produced by the SPP layer. And so, it makes a perfect sense that SPP-based object detection method can massively outperform R-CNN in terms of the processing time. Additionally, it might be worth noting that the varying bounding box candidate sizes won’t be a problem since SPP can easily convert them into uniform-sized vectors.\n![](../images/body-2871ab67d4b2abc8d714.png)\nFigure 8. How SPP layer works on object detection task [1].\n···\n## SPP-Net from Scratch\nAs we have understood the underlying theory of SPP-Net, we can now try to implement it from scratch. And in fact, the implementation is considerably easy.\nThe authors used several base models in their experiments: ZF-5, Convnet-5, Overfeat-5 and Overfeat-7. In this article I’m going to implement the ZF-5, which I think is the simplest one. I’ll leave the remaining models as an exercise for you to practice. The table given in Figure 9 below shows the architectural details of these models. It is important to note that this table only provides the CNN part. According to the paper, this part will then be connected to the SPP layer followed by two FC layers and the classification layer.\n![](../images/body-6a7cc44de5f61c6a001a.png)\nFigure 9. The backbone models where the authors implement SPP layer on [1].\nThe very first thing we do in the code is to import the required modules. Here we also initialize the `GRIDS` variable, which is used to determine the grid arrangements we are about to use in the SPP layer. In this case, I set the values to 4, 2, and 1, indicating that we will perform pooling operation within each 4×4, 2×2, and 1×1 grid cells, following the example in Figure 3.\npython\n\n```\n# Codeblock 1\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nGRIDS = [4, 2, 1]\n```\n\n### SPP Layer\nWhat we do next is to implement the SPP layer. Take a look at Codeblock 2 below to see how I do that. If you’re familiar with PyTorch models, you will find this `SPP` class quite unique as it does not have the `__init__()` method. However, this class is valid because we indeed want to define a _parameterless flow_. Well, this is actually not a standard convention in deep learning, but I use this term to emphasize that this module does not have any trainable weights or biases. All it does is just transform the input tensor based on the predefined grid cell arrangements.\npython\n\n```\n# Codeblock 2\nclass SPP(nn.Module):\n    def forward(self, x):\n\n        pooled_outputs = []      #(1)\n        for grid in GRIDS:\n            pooled = F.adaptive_max_pool2d(x, output_size=(grid,grid))  #(2)\n            print(f'after pool\\t\\t: {pooled.size()}')\n\n            pooled = torch.flatten(pooled, start_dim=1)    #(3)\n            print(f'after flatten\\t\\t: {pooled.size()}\\n')\n\n            pooled_outputs.append(pooled)                  #(4)\n\n        concatenated = torch.cat(pooled_outputs, dim=1)    #(5)\n        print(f'after concatenate\\t: {concatenated.size()}\\n')\n\n        return concatenated\n```\n\nInside the `forward()` method, what we do first is to initialize an empty list as written at line `#(1)`. This list will then be used to store the pooling results. Next, for each grid arrangement, we will perform pooling operation using `F.adaptive_max_pool2d()` (`#(2)`). Adaptive pooling mechanism used here is different from a standard pooling, where in this case we can just specify the output dimension we want and let the function automatically determine the kernel size and the stride. Afterwards, we flatten the pooling result (`#(3)`) and append it to the `pooled_output` list (`#(4)`) we initialized earlier. Once the iteration is complete, we then concatenate all the elements, forming a long single-dimensional tensor (`#(5)`).\nIn order to check if our `SPP` class above works properly, we can test it using the following code. The `x` tensor I use below is essentially the one I displayed in Figure 4 (`#(1)`). Here I am going to pass this tensor through the SPP layer (`#(2)`) and see if the resulting output is correct.\npython\n\n```\n# Codeblock 3\nspp = SPP()\n\nx = torch.tensor([[[[5, 4, 4, 5, 3, 2, 6, 5],\n                    [2, 8, 6, 3, 1, 5, 8, 1],\n                    [4, 3, 7, 5, 2, 4, 7, 3],\n                    [2, 1, 3, 2, 5, 5, 4, 2],\n                    [4, 6, 7, 9, 5, 6, 6, 7],\n                    [6, 8, 5, 9, 4, 9, 4, 1],\n                    [3, 7, 4, 6, 8, 4, 4, 9],\n                    [2, 4, 4, 3, 8, 2, 9, 2]]]], dtype=torch.float32)  #(1)\n\nout = spp(x)    #(2)\n```\n\nAnd here’s what the output looks like. You can see below that our pooling operation correctly produces 4×4, 2×2, and 1×1 tensors within each iteration, which results in three 1D vectors of length 16, 4, and 1 after being flattened. Then, these feature vectors are concatenated, forming a new vector of length 21.\nmarkup\n\n```\n# Codeblock 3 Outputafter pool        : torch.Size([1, 1, 4, 4])after flatten     : torch.Size([1, 16])after pool        : torch.Size([1, 1, 2, 2])after flatten     : torch.Size([1, 4])after pool        : torch.Size([1, 1, 1, 1])after flatten     : torch.Size([1, 1])after concatenate : torch.Size([1, 21])\n```\n\nWe can also print out the values inside the `out` tensor as follows. You’ll be surprised that these values are exactly the same as the one in Figure 4.\npython\n\n```\n# Codeblock 4\nprint(out)\n```\n\nmarkup\n\n```\n# Codeblock 4 Outputtensor([[8., 6., 5., 8., 4., 7., 5., 7., 8., 9., 9., 7., 7., 6., 8., 9., 8., 8.,         9., 9., 9.]])\n```\n\nWe can do the exact same thing to the 12×8 tensor in Figure 5. You can see in the output of the codeblock below that despite this difference in the input size, the SPP layer still produces a feature vector of the same length since we don’t change the values in the `GRIDS` list.\npython\n\n```\n# Codeblock 5\nspp = SPP()\n\nx = torch.tensor([[[4, 3, 7, 5, 2, 4, 7, 3],\n                   [2, 1, 3, 2, 5, 5, 4, 2],\n                   [5, 4, 4, 5, 3, 2, 6, 5],\n                   [2, 8, 6, 3, 1, 5, 8, 1],\n                   [4, 3, 7, 5, 2, 4, 7, 3],\n                   [2, 1, 3, 2, 5, 5, 4, 2],\n                   [4, 6, 7, 9, 5, 6, 6, 7],\n                   [6, 8, 5, 9, 4, 9, 4, 1],\n                   [3, 7, 4, 6, 8, 4, 4, 9],\n                   [2, 4, 4, 3, 8, 2, 9, 2], \n                   [6, 8, 5, 9, 4, 9, 4, 1],\n                   [3, 7, 4, 6, 8, 4, 4, 9]]], dtype=torch.float32)\n\nout = spp(x)\n```\n\nmarkup\n\n```\n# Codeblock 5 Outputafter pool        : torch.Size([1, 4, 4])after flatten     : torch.Size([1, 16])after pool        : torch.Size([1, 2, 2])after flatten     : torch.Size([1, 4])after pool        : torch.Size([1, 1, 1])after flatten     : torch.Size([1, 1])after concatenate : torch.Size([1, 21])\n```\n\nAgain, if we print out the actual values contained in the resulting tensor, we can see that it has the exact same values as the one in Figure 5 (see Codeblock 6). These two test cases essentially confirm that our SPP implementation is correct, thus ready to actually be attached to the backbone model.\npython\n\n```\n# Codeblock 6\nprint(out)\n```\n\nmarkup\n\n```\n# Codeblock 6 Outputtensor([[5., 7., 5., 7., 8., 7., 5., 8., 8., 9., 9., 9., 8., 9., 9., 9., 8., 8.,         9., 9., 9.]])\n```\n\n···\n### ZF-5 Backbone with SPP\nAs I’ve mentioned earlier, in this demonstration I am going to use ZF-5 as the backbone model. Here I name the class `ZF5_SPPNet` since we want to integrate SPP into this model. The code implementation is pretty long, so I break it down into two codeblocks: 7a and 7b. Just ensure that these two codeblocks are placed inside the same notebook cell if you want to run it on your own.\npython\n\n```\n# Codeblock 7a\nclass ZF5_SPPNet(nn.Module):\n    def __init__(self):\n        super().__init__()\n        \n        self.relu = nn.ReLU()\n        \n        \n        self.conv1 = nn.Conv2d(in_channels=3, \n                               out_channels=96, \n                               kernel_size=7, \n                               stride=2, \n                               padding=0)    #(1)\n        self.norm1 = nn.LocalResponseNorm(size=5)\n        self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)    #(2)\n        \n        \n        self.conv2 = nn.Conv2d(in_channels=96, \n                               out_channels=256, \n                               kernel_size=5, \n                               stride=2, \n                               padding=1)    #(3)\n        self.norm2 = nn.LocalResponseNorm(size=5)\n        self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=0)    #(4)\n        \n        \n        self.conv3 = nn.Conv2d(in_channels=256, \n                               out_channels=384, \n                               kernel_size=3, \n                               stride=1, \n                               padding=1)\n        \n        self.conv4 = nn.Conv2d(in_channels=384, \n                               out_channels=384, \n                               kernel_size=3, \n                               stride=1, \n                               padding=1)\n        \n        self.conv5 = nn.Conv2d(in_channels=384, \n                               out_channels=256, \n                               kernel_size=3, \n                               stride=1, \n                               padding=1)\n        \n        self.spp = SPP()    #(4)\n        spp_out_size = 256 * sum([grid**2 for grid in GRIDS])    #(5)\n        \n        self.fc6 = nn.Linear(in_features=spp_out_size, out_features=4096)\n        self.dropout6 = nn.Dropout(p=0.5)    #(6)\n        \n        self.fc7 = nn.Linear(in_features=4096, out_features=4096)\n        self.dropout7 = nn.Dropout(p=0.5)    #(7)\n        \n        self.fc8 = nn.Linear(in_features=4096, out_features=1000)\n```\n\nNow let’s talk about the Codeblock 7a first, which contains the entire `__init__()` method. According to Figure 9, the first two convolution layers of ZF-5 also have a _local response norm_ layer and a pooling layer associated with each. It might be worth noting that local response norm was the state-of-the-art normalization mechanism at that time as batch normalization layer was only introduced a year after this paper.\nOne thing that makes this implementation a bit challenging is the padding of the first two convolutions and their corresponding maxpooling layers. We typically set the padding to 3 for 7×7 kernel, padding 2 for 5×5 kernel, padding 1 for 3×3 kernel, and padding 0 for 1×1 kernel so that the spatial dimension of the tensor is preserved. However, when I tried to use that convention, the resulting tensor shapes do not match with the paper, so I tweaked them manually and came up with padding values that appear to be somewhat arbitrary (see lines `#(1,2,3,4)`). \nAs the first two convolution layers are done, we can now move on to the `conv3`, `conv4`, and `conv5` layers. These remaining convolutions are easy since we only need to stack them sequentially by setting the number of input and output channels according to the guide in Figure 9. In this case we don’t need to search for the padding since all of them use 3×3 kernels, hence we can just set it to 1 as we won’t change the spatial dimension until we reach the SPP layer.\nThe SPP layer itself is initialized at line `#(4)`. Don’t forget to calculate the output shape of this layer because we want to use it to specify the feature vector length accepted by the subsequent FC layer (`#(5)`). The stack of FC layers is also straightforward to implement, where it consists of two hidden layers of 4096 neurons (`fc6` and `fc7`). The `fc7` layer is then followed by the final output layer `fc8`, which returns the classification tensor of length 1000, matching the number of classes available in the ImageNet 2012 dataset. Additionally, here we also initialize two dropout layers (`#(6,7)`) to be placed after each of the two hidden FC layers.\nAs the `__init__()` method is completed, we will now move on to the `forward()` method which I write in Codeblock 7b below. I am not going to explain this code though, because what we basically do here is just to connect the layers one by one.\npython\n\n```\n# Codeblock 7b\n    def forward(self, x):\n        print(f'original\\t\\t: {x.size()}')\n        \n        x = self.norm1(self.relu(self.conv1(x)))\n        print(f'after conv1\\t\\t: {x.size()}')\n        x = self.pool1(x)\n        print(f'after pool1\\t\\t: {x.size()}')\n        \n        x = self.norm2(self.relu(self.conv2(x)))\n        print(f'after conv2\\t\\t: {x.size()}')\n        x = self.pool2(x)\n        print(f'after pool2\\t\\t: {x.size()}')\n        \n        x = self.relu(self.conv3(x))\n        print(f'after conv3\\t\\t: {x.size()}')\n        \n        x = self.relu(self.conv4(x))\n        print(f'after conv4\\t\\t: {x.size()}')\n        \n        x = self.relu(self.conv5(x))\n        print(f'after conv5\\t\\t: {x.size()}\\n')\n        \n        x = self.spp(x)\n        print(f'after spp\\t\\t: {x.size()}\\n')\n        \n        x = self.dropout6(self.relu(self.fc6(x)))\n        print(f'after fc6\\t\\t: {x.size()}')\n        \n        x = self.dropout7(self.relu(self.fc7(x)))\n        print(f'after fc7\\t\\t: {x.size()}')\n        \n        x = self.fc8(x)\n        print(f'after fc8\\t\\t: {x.size()}')\n        \n        return x\n```\n\nNow let’s check if our `ZF5_SPPNet` class works properly by running the following codeblock. Here I am trying to pass the dummy tensor `x` through the network, which simulates an RGB image of size 224×224.\npython\n\n```\n# Codeblock 8\nzf5sppnet = ZF5_SPPNet()\n\nx = torch.randn(1, 3, 224, 224)\nout = zf5sppnet(x)\n```\n\nYou can see in the resulting output below that our 224×224 tensor becomes 55×55 after being processed by the first convolution and its corresponding maxpooling layer (`#(1)`). Next, the second conv-pooling stack downsamples it further to 13×13 (`#(3)`). If you go back to Figure 9, you will notice the table is a bit imprecise since the feature map size of 27×27 written in the _conv2_ column should be the output of the second convolution (`#(2)`), not the output of the pooling layer (`#(3)`). The subsequent `conv3`, `conv4`, and `conv5` layers (`#(4–5)`) are quite simple, where what they essentially do are just changing the number of layers from 256 to 384 and back to 256 again while preserving the spatial dimension. The resulting 256×13×13 tensor is then forwarded to the SPP layer. Remember that since our `GRIDS` list has 3 values, then our SPP will produce 3 feature vectors as well, each having the length of 4096, 1024, and 256 as shown at lines `#(6)`, `#(7)`, and `#(8)`. These layers are then concantenated, forming a long one-dimensional vector of length 5376 (`#(9)`). Afterwards, we can feed this feature vector into the FC layers until we eventually got the 1000-class prediction vector (`#(10)`).\nmarkup\n\n```\n# Codeblock 8 Outputoriginal          : torch.Size([1, 3, 224, 224])after conv1       : torch.Size([1, 96, 109, 109])after pool1       : torch.Size([1, 96, 55, 55])    #(1) after conv2       : torch.Size([1, 256, 27, 27])   #(2)after pool2       : torch.Size([1, 256, 13, 13])   #(3)after conv3       : torch.Size([1, 384, 13, 13])   #(4)after conv4       : torch.Size([1, 384, 13, 13])after conv5       : torch.Size([1, 256, 13, 13])   #(5)after pool        : torch.Size([1, 256, 4, 4])after flatten     : torch.Size([1, 4096])          #(6)after pool        : torch.Size([1, 256, 2, 2])after flatten     : torch.Size([1, 1024])          #(7)after pool        : torch.Size([1, 256, 1, 1])after flatten     : torch.Size([1, 256])           #(8)after concatenate : torch.Size([1, 5376])          #(9)after spp         : torch.Size([1, 5376])after fc6         : torch.Size([1, 4096])after fc7         : torch.Size([1, 4096])after fc8         : torch.Size([1, 1000])          #(10)\n```\n\nAt this point we have verified the forward pass of this network. Thus, we can say that this model is now ready to be trained.\n···\n## Ending\nAnd that’s everything about the theory and the implementation of SPP-Net. Here I challenge you to try implementing SPP layer on modern architectures like ResNet, ConvNeXt, etc. I believe you can do that pretty easily as what you need to do is just to copy-paste the `SPP` class in Codeblock 2 and connect it to any CNN-based architectures. You can visit my profile [3] if you need more references on implementing deep learning architectures from scratch. \nI hope you learn something new today, feel free to reach me if you spot any mistakes in my explanation or in the code. By the way you can also access the code in my GitHub repository [4]. Thanks for reading!\n···\n## References\n[1] Kaiming He _et al_. Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition. Arxiv. <https://arxiv.org/abs/1406.4729> [Accessed October 17, 2025].\n[2] Image created originally by author.\n[3] Muhammad Ardi. Medium. <https://medium.com/@muhammad_ardi> [Accessed October 17, 2025].\n[4] MuhammadArdiPutra. SPP-Net. GitHub. <https://github.com/MuhammadArdiPutra/medium_articles/blob/main/Deep%20Learning%20From%20Scratch/SPP-Net.ipynb> [Accessed October 18, 2025].\n## Register Now to Continue Reading\n### Unlock Towards Data Science with a Free Account\nAll articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more.\nRegister for freeSign in to your account\n* * *\n![](../images/body-59e1c17daab1fcb582c5.jpeg)\nWritten By\n## [Muhammad Ardi Putra](https://towardsdatascience.com/author/muhammad_ardi/)\nDeep learning storyteller\n[See all from Muhammad Ardi Putra](https://towardsdatascience.com/author/muhammad_ardi/)\n[Deep Dives](https://towardsdatascience.com/tag/deep-dives/)[Artificial Intelligence](https://towardsdatascience.com/tag/artificial-intelligence/)[Cnn](https://towardsdatascience.com/tag/cnn/)[Machine Learning](https://towardsdatascience.com/tag/machine-learning/)[Object Detection](https://towardsdatascience.com/tag/object-detection/)\nShare This Article\n  * [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Ftowardsdatascience.com%2Fspp-net-paper-walkthrough-breaking-the-fixed-size-constraint%2F&title=SPP-Net%20Paper%20Walkthrough%3A%20Breaking%20the%20Fixed-Size%20Constraint)\n  * [](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Ftowardsdatascience.com%2Fspp-net-paper-walkthrough-breaking-the-fixed-size-constraint%2F&title=SPP-Net%20Paper%20Walkthrough%3A%20Breaking%20the%20Fixed-Size%20Constraint)\n  * [](https://x.com/share?url=https%3A%2F%2Ftowardsdatascience.com%2Fspp-net-paper-walkthrough-breaking-the-fixed-size-constraint%2F&text=SPP-Net%20Paper%20Walkthrough%3A%20Breaking%20the%20Fixed-Size%20Constraint)\n\n\nTowards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.\n[Write for TDS](https://towardsdatascience.com/submissions/)\n## Related Articles\n  * [![](../images/003.jpg)](https://towardsdatascience.com/pytorch-shap-explainable-convolutional-neural-networks-ece5f04c374f/)\n## [PyTorch + SHAP = Explainable Convolutional Neural Networks](https://towardsdatascience.com/pytorch-shap-explainable-convolutional-neural-networks-ece5f04c374f/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nLearn how to explain predictions of convolutional neural networks with PyTorch and SHAP\n[Dario Radečić](https://towardsdatascience.com/author/radecicdario/)\nFebruary 1, 20214 min read\n  * [![](../images/004.jpg)](https://towardsdatascience.com/how-to-boost-pytorch-dataset-using-memory-mapped-files-6893bff27b99/)\n## [How to boost PyTorch Dataset using memory-mapped files](https://towardsdatascience.com/how-to-boost-pytorch-dataset-using-memory-mapped-files-6893bff27b99/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nThis article will discuss the reasoning and the steps of implementing a PyTorch dataset that uses memory-mapped files\n[Tudor Surdoiu](https://towardsdatascience.com/author/dacus-augustus/)\nJuly 25, 20224 min read\n  * [![](../images/005.jpeg)](https://towardsdatascience.com/top-3-resources-to-learn-machine-learning-a2adcda40fd/)\n## [Top 3 Resources to Learn Machine Learning](https://towardsdatascience.com/top-3-resources-to-learn-machine-learning-a2adcda40fd/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nLooking for a course in applied machine learning? Or something bit more theoretical? Start here.\n[Dario Radečić](https://towardsdatascience.com/author/radecicdario/)\nJanuary 13, 20205 min read\n  * [![](../images/body-63beb50f664a055ed739.jpg)](https://towardsdatascience.com/how-the-pytorch-convolutions-work-or-how-to-collapse-two-convolutions-into-one-6dc810489d79/)\n## [How the PyTorch convolutions work or how to collapse two convolutions into one](https://towardsdatascience.com/how-the-pytorch-convolutions-work-or-how-to-collapse-two-convolutions-into-one-6dc810489d79/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nOr closer look at convolution for deep learning engineers\n[Anton Lebedev](https://towardsdatascience.com/author/antoleb/)\nAugust 30, 20226 min read\n  * [![](../images/007.jpeg)](https://towardsdatascience.com/if-you-work-with-textual-data-learn-these-pandas-methods-3f224122ebaf/)\n## [If You Work With Textual Data, Learn These Pandas Methods](https://towardsdatascience.com/if-you-work-with-textual-data-learn-these-pandas-methods-3f224122ebaf/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nTextual data carries more information than numbers\n[Soner Yıldırım](https://towardsdatascience.com/author/sonery/)\nApril 7, 20226 min read\n  * [![](../images/008.jpg)](https://towardsdatascience.com/exploring-object-detection-with-r-cnn-models-a-comprehensive-beginners-guide-part-2-685bc89775e2/)\n## [Exploring Object Detection with R-CNN Models - A Comprehensive Beginner's Guide (Part 2)](https://towardsdatascience.com/exploring-object-detection-with-r-cnn-models-a-comprehensive-beginners-guide-part-2-685bc89775e2/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nObject Detection Models\n[Raghav Bali](https://towardsdatascience.com/author/rghv_bali/)\nFebruary 17, 20247 min read\n  * [![](../images/009.jpg)](https://towardsdatascience.com/how-i-built-a-lo-fi-music-web-player-with-ai-generated-tracks-36f3915e39f8/)\n## [How I Built a Lo-fi Music Web Player with AI-Generated Tracks](https://towardsdatascience.com/how-i-built-a-lo-fi-music-web-player-with-ai-generated-tracks-36f3915e39f8/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nMade with Tone.js and LSTM Model\n[Aleksandra Ma](https://towardsdatascience.com/author/leksa_86/)\nJanuary 12, 202311 min read\n  * [![](../images/010.jpg)](https://towardsdatascience.com/why-i-use-weights-biases-for-my-machine-learning-ph-d-research-11ab2fe16956/)\n## [Why I Use Weights & Biases for My Machine Learning Ph.D. Research](https://towardsdatascience.com/why-i-use-weights-biases-for-my-machine-learning-ph-d-research-11ab2fe16956/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nExperiment tracking, hyperparameter optimization, private hosting\n[Pascal Janetzky](https://towardsdatascience.com/author/pascaljanetzky/)\nSeptember 26, 20227 min read\n  * [![](../images/011.jpg)](https://towardsdatascience.com/optimizing-pytorch-model-inference-on-aws-graviton/)\n## [Optimizing PyTorch Model Inference on AWS Graviton](https://towardsdatascience.com/optimizing-pytorch-model-inference-on-aws-graviton/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nTips for accelerating AI/ML on CPU — Part 2\n[Chaim Rand](https://towardsdatascience.com/author/chaimrand/)\nDecember 10, 20259 min read\n\n\n  * [YouTube](https://www.youtube.com/c/TowardsDataScience)\n  * [X](https://x.com/TDataScience)\n  * [LinkedIn](https://www.linkedin.com/company/towards-data-science/)\n  * [Threads](https://www.threads.net/@towardsdatascience)\n  * [Bluesky](https://bsky.app/profile/towardsdatascience.com)\n\n\n[](https://towardsdatascience.com/)\nYour home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.\nSubscribe to Our Newsletter[Write For TDS](https://towardsdatascience.com/submissions/)•[About](https://towardsdatascience.com/about-towards-data-science-d691af11cc2f/)•[Advertise](https://contact.towardsdatascience.com/advertise-with-towards-data-science)•[Privacy Policy](https://towardsdatascience.com/privacy-policy/)•[Terms of Use](https://towardsdatascience.com/website-terms-of-use/)\n© Insight Media Group, LLC 2026•[The New Stack](https://thenewstack.io)•[roadmap.sh](https://roadmap.sh)•[Creator Network](https://insightmediagroup.io/creator-network)\n####  Recommended Articles \nClose\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607686/image?etag=1782161464448&width=500&height=500) How Relevance Models Foreshadowed Transformers for NLP ](https://towardsdatascience.com/how-relevance-models-foreshadowed-transformers-for-nlp/ \"How Relevance Models Foreshadowed Transformers for NLP\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607639/image?etag=1781250125848&width=500&height=500) Javascript Fatigue: HTMX Is All You Need to Build ChatGPT — Part 1 ](https://towardsdatascience.com/javascript-fatigue-you-dont-need-js-to-build-chatgpt/ \"Javascript Fatigue: HTMX Is All You Need to Build ChatGPT — Part 1\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607930/image?etag=1782124566054&width=500&height=500) The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs ](https://towardsdatascience.com/subset-sum-problem-solved-in-linear-time-for-dense-enough-inputs/ \"The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/3660/image?etag=1780176246070&width=500&height=500) Does Your Company Have a Data Strategy? ](https://towardsdatascience.com/does-your-company-have-a-data-strategy-e8944dbec4ae/ \"Does Your Company Have a Data Strategy?\")\n\n\n",
  "metadata": {
    "title": "SPP-Net Paper Walkthrough: Breaking the Fixed-Size Constraint | Towards Data Science",
    "description": "Learn how Spatial Pyramid Pooling enables CNNs to handle any image size, with a from-scratch PyTorch implementation",
    "keywords": null,
    "author": "Muhammad Ardi Putra",
    "og:locale": "en_US",
    "og:type": "article",
    "og:title": "SPP-Net Paper Walkthrough: Breaking the Fixed-Size Constraint",
    "og:description": "Learn how Spatial Pyramid Pooling enables CNNs to handle any image size, with a from-scratch PyTorch implementation",
    "og:url": "https://towardsdatascience.com/spp-net-paper-walkthrough-breaking-the-fixed-size-constraint/",
    "og:site_name": "Towards Data Science",
    "og:image": "https://assets.insightmediagroup.io/media/wp-content/uploads/2026/08/0_1b-saeAeKEc7qkSF.jpg",
    "og:image:width": "960",
    "og:image:height": "618",
    "og:image:type": "image/jpeg",
    "twitter:card": "summary_large_image",
    "twitter:site": "@TDataScience",
    "twitter:creator": "@TDataScience",
    "twitter:label1": "Written by",
    "twitter:data1": "Muhammad Ardi Putra",
    "twitter:label2": "Est. reading time",
    "twitter:data2": "18 min read",
    "article:published_time": "2026-08-10T06:00:07.000Z",
    "article:modified_time": "2026-08-10T06:00:07.000Z",
    "article:author": "https://towardsdatascience.com/author/muhammad_ardi/"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 202,
    "error": ""
  },
  "code_blocks": [
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_000.txt",
      "chars": 100
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_001.txt",
      "chars": 605
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_002.txt",
      "chars": 447
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_003.txt",
      "chars": 307
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_004.txt",
      "chars": 25
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_005.txt",
      "chars": 123
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_006.txt",
      "chars": 605
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_007.txt",
      "chars": 298
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_008.txt",
      "chars": 25
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_009.txt",
      "chars": 123
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_010.txt",
      "chars": 2069
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_011.txt",
      "chars": 1102
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_012.txt",
      "chars": 91
    },
    {
      "language": "txt",
      "file": "attachments/465855dd76aa/code/block_013.txt",
      "chars": 979
    }
  ],
  "images": [
    {
      "status": "http_404",
      "url": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607686/image?etag=1782161464448&width=500&height=500",
      "src": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607686/image?etag=1782161464448&width=500&height=500",
      "alt": "",
      "downloaded": false,
      "discovery": "saved_markdown",
      "retried_at": "2026-10-20T17:37:17Z"
    },
    {
      "status": "http_404",
      "url": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607639/image?etag=1781250125848&width=500&height=500",
      "src": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607639/image?etag=1781250125848&width=500&height=500",
      "alt": "",
      "downloaded": false,
      "discovery": "saved_markdown",
      "retried_at": "2026-10-20T17:37:17Z"
    },
    {
      "status": "http_404",
      "url": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607930/image?etag=1782124566054&width=500&height=500",
      "src": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607930/image?etag=1782124566054&width=500&height=500",
      "alt": "",
      "downloaded": false,
      "discovery": "saved_markdown",
      "retried_at": "2026-10-20T17:37:17Z"
    },
    {
      "status": "http_404",
      "url": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/3660/image?etag=1780176246070&width=500&height=500",
      "src": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/3660/image?etag=1780176246070&width=500&height=500",
      "alt": "",
      "downloaded": false,
      "discovery": "saved_markdown",
      "retried_at": "2026-10-20T17:37:17Z"
    }
  ],
  "audio": [],
  "videos": [
    {
      "url": "https://www.youtube.com/c/TowardsDataScience",
      "kind": "channel_reference",
      "status": "reference_only"
    }
  ],
  "links": [
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/latest/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/editors-pick/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/the-variable/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/shipai/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/submissions/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/category/artificial-intelligence/deep-learning/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/muhammad_ardi/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/deep-dives/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/artificial-intelligence/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/cnn/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/machine-learning/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/object-detection/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/pytorch-shap-explainable-convolutional-neural-networks-ece5f04c374f/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/radecicdario/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/how-to-boost-pytorch-dataset-using-memory-mapped-files-6893bff27b99/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/dacus-augustus/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/top-3-resources-to-learn-machine-learning-a2adcda40fd/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/category/artificial-intelligence/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/how-the-pytorch-convolutions-work-or-how-to-collapse-two-convolutions-into-one-6dc810489d79/"
    },
    {
      "type": "external",
      "href": "https://write.insightmediagroup.io"
    },
    {
      "type": "external",
      "href": "https://www.linkedin.com/company/towards-data-science/"
    },
    {
      "type": "external",
      "href": "https://x.com/TDataScience"
    },
    {
      "type": "external",
      "href": "https://unsplash.com/@viktortheo"
    },
    {
      "type": "external",
      "href": "https://roadmap.sh/machine-learning"
    },
    {
      "type": "external",
      "href": "https://arxiv.org/abs/1406.4729"
    },
    {
      "type": "external",
      "href": "https://medium.com/@muhammad_ardi"
    },
    {
      "type": "external",
      "href": "https://github.com/MuhammadArdiPutra/medium_articles/blob/main/Deep%20Learning%20From%20Scratch/SPP-Net.ipynb"
    },
    {
      "type": "external",
      "href": "https://www.facebook.com/sharer/sharer.php?title=SPP-Net+Paper+Walkthrough%3A+Breaking+the+Fixed-Size+Constraint&u=https%3A%2F%2Ftowardsdatascience.com%2Fspp-net-paper-walkthrough-breaking-the-fixed-size-constraint%2F"
    },
    {
      "type": "external",
      "href": "https://www.linkedin.com/shareArticle?mini=true&title=SPP-Net+Paper+Walkthrough%3A+Breaking+the+Fixed-Size+Constraint&url=https%3A%2F%2Ftowardsdatascience.com%2Fspp-net-paper-walkthrough-breaking-the-fixed-size-constraint%2F"
    },
    {
      "type": "external",
      "href": "https://x.com/share?text=SPP-Net+Paper+Walkthrough%3A+Breaking+the+Fixed-Size+Constraint&url=https%3A%2F%2Ftowardsdatascience.com%2Fspp-net-paper-walkthrough-breaking-the-fixed-size-constraint%2F"
    },
    {
      "type": "external",
      "href": "https://www.youtube.com/c/TowardsDataScience"
    },
    {
      "type": "external",
      "href": "https://www.threads.net/@towardsdatascience"
    },
    {
      "type": "external",
      "href": "https://bsky.app/profile/towardsdatascience.com"
    },
    {
      "type": "external",
      "href": "https://thenewstack.io"
    },
    {
      "type": "external",
      "href": "https://roadmap.sh"
    },
    {
      "type": "external",
      "href": "https://insightmediagroup.io/creator-network"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-20T17:23:10Z",
  "source_url": "https://towardsdatascience.com/spp-net-paper-walkthrough-breaking-the-fixed-size-constraint/",
  "delivery_status": "unverified",
  "discovery_status": "http_403",
  "missing_reason": "源页面未重新核验:http_403；已按保留正文补采图片",
  "repair_error": "",
  "actual_video_files": [],
  "expected_saved_body_images": 4,
  "image_discovery": "saved_markdown",
  "source_body_markdown": "![Revisit consent button](https://cdn-cookieyes.com/assets/images/revisit.svg)\nWe value your privacy\nWe use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking \"Accept All\", you consent to our use of cookies.\nCustomise Reject All Accept All\nCustomise Consent Preferences ![](https://cdn-cookieyes.com/assets/images/close.svg)\nWe use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.\nThe cookies that are categorised as \"Necessary\" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... Show more\nNecessaryAlways Active\nNecessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.\n  * Cookie\nBCTempID\n  * Duration\n10 minutes\n  * Description\nNo description available.\n\n\n  * Cookie\n__cf_bm\n  * Duration\n1 hour\n  * Description\nThis cookie, set by Cloudflare, is used to support Cloudflare Bot Management. \n\n\n  * Cookie\nAWSALBCORS\n  * Duration\n7 days\n  * Description\nAmazon Web Services set this cookie for load balancing.\n\n\n  * Cookie\n_cfuvid\n  * Duration\nsession\n  * Description\nCloudflare sets this cookie to track users across sessions to optimize user experience by maintaining session consistency and providing personalized services\n\n\n  * Cookie\nli_gc\n  * Duration\n6 months\n  * Description\nLinkedin set this cookie for storing visitor's consent regarding using cookies for non-essential purposes.\n\n\n  * Cookie\n__hssrc\n  * Duration\nsession\n  * Description\nThis cookie is set by Hubspot whenever it changes the session cookie. The __hssrc cookie set to 1 indicates that the user has restarted the browser, and if the cookie does not exist, it is assumed to be a new session.\n\n\n  * Cookie\n__hssc\n  * Duration\n1 hour\n  * Description\nHubSpot sets this cookie to keep track of sessions and to determine if HubSpot should increment the session number and timestamps in the __hstc cookie.\n\n\n  * Cookie\nwpEmojiSettingsSupports\n  * Duration\nsession\n  * Description\nWordPress sets this cookie when a user interacts with emojis on a WordPress site. It helps determine if the user's browser can display emojis properly.\n\n\n  * Cookie\nBCSessionID\n  * Duration\n1 year 1 month 4 days\n  * Description\nBlueconic sets this cookie as a unique identifier for the BlueConic profile.\n\n\n  * Cookie\n_octo\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nlogged_in\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\n__Secure-YEC\n  * Duration\npast\n  * Description\nYouTube sets this cookie to stores the user's video player preferences using embedded YouTube video\n\n\n  * Cookie\n__eoi\n  * Duration\n6 months\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nAWSALBTGCORS\n  * Duration\n7 days\n  * Description\nNo description available.\n\n\n  * Cookie\nlogin-status-p\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nAWSALBTG\n  * Duration\n7 days\n  * Description\nNo description available.\n\n\n  * Cookie\ncsrf_token\n  * Duration\nsession\n  * Description\nNo description available.\n\n\n  * Cookie\ntoken_v2\n  * Duration\n1 day\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nD\n  * Duration\n1 year\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nPHPSESSID\n  * Duration\nsession\n  * Description\nThis cookie is native to PHP applications. The cookie stores and identifies a user's unique session ID to manage user sessions on the website. The cookie is a session cookie and will be deleted when all the browser windows are closed.\n\n\n  * Cookie\nVISITOR_PRIVACY_METADATA\n  * Duration\n6 months\n  * Description\nYouTube sets this cookie to store the user's cookie consent state for the current domain. \n\n\n  * Cookie\ncookietest\n  * Duration\nsession\n  * Description\nThe cookietest cookie is typically used to determine whether the user's browser accepts cookies, essential for website functionality and user experience.\n\n\n  * Cookie\n__Host-airtable-session\n  * Duration\n1 year\n  * Description\nThis cookie is used to enable us to integrate the services of Airtable.\n\n\n  * Cookie\n__Host-airtable-session.sig\n  * Duration\n1 year\n  * Description\nThis cookie is used to enable us to integrate the services of Airtable.\n\n\n  * Cookie\nm\n  * Duration\n1 year 1 month 4 days\n  * Description\nStripe sets this cookie for fraud prevention purposes. It identifies the device used to access the website, allowing the website to be formatted accordingly.\n\n\n  * Cookie\nBIGipServer*\n  * Duration\nsession\n  * Description\nMarketo sets this cookie to collect information about the user's online activity and build a profile about their interests to provide advertisements relevant to the user. \n\n\n  * Cookie\n__cfruid\n  * Duration\nsession\n  * Description\nCloudflare sets this cookie to identify trusted web traffic.\n\n\n  * Cookie\n_GRECAPTCHA\n  * Duration\n6 months\n  * Description\nGoogle Recaptcha service sets this cookie to identify bots to protect the website against malicious spam attacks.\n\n\n  * Cookie\n__Secure-YNID\n  * Duration\n6 months\n  * Description\nGoogle cookie used to protect user security and prevent fraud, especially during the login process.\n\n\n  * Cookie\ncookieyes-consent\n  * Duration\n1 year\n  * Description\nCookieYes sets this cookie to remember users' consent preferences so that their preferences are respected on subsequent visits to this site. It does not collect or store any personal information about the site visitors.\n\n\nFunctional\nFunctional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.\n  * Cookie\nlidc\n  * Duration\n1 day\n  * Description\nLinkedIn sets the lidc cookie to facilitate data center selection.\n\n\n  * Cookie\nbrw\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nbrwConsent\n  * Duration\n5 minutes\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nWMF-Uniq\n  * Duration\n1 year\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nloom_anon_comment\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nloom_referral_video\n  * Duration\nsession\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nVISITOR_INFO1_LIVE\n  * Duration\n6 months\n  * Description\nA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.\n\n\n  * Cookie\nyt-remote-connected-devices\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to store the user's video preferences using embedded YouTube videos.\n\n\n  * Cookie\nytidb::LAST_RESULT_ENTRY_KEY\n  * Duration\nNever Expires\n  * Description\nThe cookie ytidb::LAST_RESULT_ENTRY_KEY is used by YouTube to store the last search result entry that was clicked by the user. This information is used to improve the user experience by providing more relevant search results in the future.\n\n\n  * Cookie\nyt-remote-device-id\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to store the user's video preferences using embedded YouTube videos.\n\n\n  * Cookie\nyt-remote-session-name\n  * Duration\nsession\n  * Description\nThe yt-remote-session-name cookie is used by YouTube to store the user's video player preferences using embedded YouTube video.\n\n\n  * Cookie\nyt-remote-fast-check-period\n  * Duration\nsession\n  * Description\nThe yt-remote-fast-check-period cookie is used by YouTube to store the user's video player preferences for embedded YouTube videos.\n\n\n  * Cookie\nyt-remote-session-app\n  * Duration\nsession\n  * Description\nThe yt-remote-session-app cookie is used by YouTube to store user preferences and information about the interface of the embedded YouTube video player.\n\n\n  * Cookie\nyt-remote-cast-available\n  * Duration\nsession\n  * Description\nThe yt-remote-cast-available cookie is used to store the user's preferences regarding whether casting is available on their YouTube video player.\n\n\n  * Cookie\nyt-remote-cast-installed\n  * Duration\nsession\n  * Description\nThe yt-remote-cast-installed cookie is used to store the user's video player preferences using embedded YouTube video.\n\n\n  * Cookie\ncp_session\n  * Duration\n3 months\n  * Description\nCodepen sets this cookie for Help systems found in the website.\n\n\n  * Cookie\nloid\n  * Duration\n1 year 1 month 4 days\n  * Description\nThis cookie is set by the Reddit. The cookie enables the sharing of content from the website onto the social media platform.\n\n\nAnalytics\nAnalytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.\n  * Cookie\n__hstc\n  * Duration\n6 months\n  * Description\nHubspot set this main cookie for tracking visitors. It contains the domain, initial timestamp (first visit), last timestamp (last visit), current timestamp (this visit), and session number (increments for each subsequent session).\n\n\n  * Cookie\nhubspotutk\n  * Duration\n6 months\n  * Description\nHubSpot sets this cookie to keep track of the visitors to the website. This cookie is passed to HubSpot on form submission and used when deduplicating contacts.\n\n\n  * Cookie\n_ga\n  * Duration\n1 year 1 month 4 days\n  * Description\nGoogle Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors.\n\n\n  * Cookie\n_ga_*\n  * Duration\n1 year 1 month 4 days\n  * Description\nGoogle Analytics sets this cookie to store and count page views.\n\n\n  * Cookie\n__Host-psifi.analyticsTrace\n  * Duration\n6 hours\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\n__Host-psifi.analyticsTraceV2\n  * Duration\n6 hours\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\n_gh_sess\n  * Duration\nsession\n  * Description\nGitHub sets this cookie for temporary application and framework state between pages like what step the user is on in a multiple step form.\n\n\n  * Cookie\nYSC\n  * Duration\nsession\n  * Description\nYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.\n\n\n  * Cookie\najs_anonymous_id\n  * Duration\n1 year\n  * Description\nThis cookie is set by Segment to count the number of people who visit a certain site by tracking if they have visited before.\n\n\n  * Cookie\nvuid\n  * Duration\n1 year 1 month 4 days\n  * Description\nVimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. \n\n\nPerformance\nPerformance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.\n  * Cookie\nAWSALB\n  * Duration\n7 days\n  * Description\nAWSALB is an application load balancer cookie set by Amazon Web Services to map the session to the target.\n\n\n  * Cookie\nacq\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nacq.sig\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nptc\n  * Duration\n2 years\n  * Description\nNo description available.\n\n\nAdvertisement\nAdvertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.\n  * Cookie\nmuc_ads\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to collect user behaviour and interaction data to optimize the website.\n\n\n  * Cookie\nguest_id_marketing\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor.\n\n\n  * Cookie\nguest_id_ads\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor.\n\n\n  * Cookie\npersonalization_id\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to integrate and share features for social media and also store information about how the user uses the website, for tracking and targeting.\n\n\n  * Cookie\nguest_id\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor. It registers if a user is signed in to the Twitter platform and collects information about ad preferences.\n\n\n  * Cookie\nbcookie\n  * Duration\n1 year\n  * Description\nLinkedIn sets this cookie from LinkedIn share buttons and ad tags to recognize browser IDs.\n\n\n  * Cookie\n__Secure-ROLLOUT_TOKEN\n  * Duration\n6 months\n  * Description\nYouTube sets this cookie to manage feature rollout and experimentation. It helps Google control which new features or interface changes are shown to users as part of testing and staged rollouts, ensuring consistent experience for a given user during an experiment.\n\n\n  * Cookie\nyt.innertube::nextId\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.\n\n\n  * Cookie\nyt.innertube::requests\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.\n\n\n  * Cookie\nsession_tracker\n  * Duration\nsession\n  * Description\nThis cookie is set by the Reddit. This cookie is used to identify trusted web traffic. It also helps in adverstising on the website.\n\n\n  * Cookie\nedgebucket\n  * Duration\nsession\n  * Description\nReddit sets this cookie to save the information about a log-on Reddit user, for the purpose of advertisement recommendations and updating the content.\n\n\n  * Cookie\ndid\n  * Duration\n1 year\n  * Description\nArbor sets this cookie to show targeted ads to site visitors.This cookie expires after 2 months or 1 year.\n\n\nUncategorised\nOther uncategorised cookies are those that are being analysed and have not been classified into a category as yet.\nNo cookies to display.\nReject All Save My Preferences Accept All\nToggle Search[](https://towardsdatascience.com/)Toggle Mobile Navigation\n[](https://towardsdatascience.com/)\nPublish AI, ML & data-science insights to a global community of data professionals.\nSign in[Submit an Article](https://write.insightmediagroup.io)\n[Latest](https://towardsdatascience.com/latest/)[Editor's Picks](https://towardsdatascience.com/tag/editors-pick/)[Newsletter](https://towardsdatascience.com/tag/the-variable/)[ShipAI](https://towardsdatascience.com/shipai/)\n* * *\n[Write For TDS](https://towardsdatascience.com/submissions/)\n[LinkedIn](https://www.linkedin.com/company/towards-data-science/)[X](https://x.com/TDataScience)Toggle Search\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\n# SPP-Net Paper Walkthrough: Breaking the Fixed-Size Constraint\nLearn how Spatial Pyramid Pooling enables CNNs to handle any image size, with a from-scratch PyTorch implementation\n[Muhammad Ardi Putra](https://towardsdatascience.com/author/muhammad_ardi/)\nAugust 10, 202618 min read\n![](https://assets.insightmediagroup.io/media/wp-content/uploads/2026/08/0_1b-saeAeKEc7qkSF.jpg)\nPhoto by [Viktor Theo](https://unsplash.com/@viktortheo?utm_source=medium&utm_medium=referral) on Unsplash\nIn the past, a CNN model needed to have a fixed input dimension. This was actually a problem because objects in our surroundings have varying dimensions. With humans and cars, for example, we cannot use the exact same bounding box to approximate them since humans tend to have a vertical shape while it is more appropriate to approximate a car with a horizontal bounding box. The easiest way to address this issue is by using either cropping or warping, and by doing so we can set an image to have a specific size. However, if the image is cropped, the cropped area might cut off part of the object, while if we warp the image, we will have the object distorted. These transformations clearly cause the visual appearance of the objects to change weirdly, thus it might potentially reduce model accuracy if we were to use them to train a classification model. Take a look at Figure 1 below to better understand what I mean.\n![](https://assets.insightmediagroup.io/media/max/800/1*xo558qbzQ6FMfJuvzpeFow.png)\nFigure 1. Examples of how cropping (left) and warping (right) respectively truncate and distort image content [1].\nIn this article we are going to talk about SPP-Net, a CNN-based model that can accept images of varying sizes so that we can completely avoid using cropping or warping mechanism. Not only that, here I will also try to implement it from scratch with PyTorch so that you can get a better understanding about the detailed architecture.\n···\nLearn this step by step with the interactive [Machine Learning](https://roadmap.sh/machine-learning) roadmap.\n## A Brief History of SPP-Net\nSPP-Net was introduced in a paper titled “ _Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition_ ” written by He _et al._ back in June 2014 [1]. It is important to note that SPP itself already existed long before this paper. So, what this paper essentially proposed was SPP-Net, which is the very first model to integrate the idea of SPP on a CNN. \n### CNN vs SPP-Net\n![](https://assets.insightmediagroup.io/media/max/800/1*3IRot9WT5g2Tluhg87YFUA.png)\nFigure 2. The flow of the conventional CNN architecture (top) vs the flow of a CNN with SPP layer (bottom) [1].\nThe upper flowchart in Figure 2 displays the workflow of the conventional CNN model. This process requires us to perform cropping or warping to make the images in the dataset have uniform size before passing them through the model. Meanwhile, the lower flowchart in the figure shows the flow when we use SPP layer in the CNN. Here you can see that we don’t need the cropping or warping step since we already have the SPP layer placed between the convolution layers and the fully-connected layers within the network.\nIf we take a closer look at the CNN architecture, what basically makes the conventional CNN able to only accept a fixed-size tensor is its fully-connected layer. The convolution layers themselves can naturally accept input of varying sizes due to the shared weights across the spatial dimension of the image. For instance, if we have a conv layer with a 3×3 kernel, then we will have 9 trainable weights (or 10 if we include the bias term) regardless of the size of the image passed through the network. This property is different from a fully-connected layer, where every single element in the tensor needs to have its corresponding weight. For example, if we have an image of size 10×10, we need to flatten it into a single-dimensional tensor of length 100, and thus we will also need that same number of weights (or 101 with the bias term). This essentially means that if the shape of the input tensor changes, then we will require a different number of weights as well. This behavior makes an FC layer unsuitable for processing images of varying resolutions.\nWith this problem in mind, the authors of SPP-Net proposed a mechanism where we can still utilize the property of CNN in handling varying input sizes while adapting the extracted feature map to a fixed-size vector required by the fully-connected layer. This is essentially the reason why in Figure 2 they placed the SPP layer between the stack of convolutions and the FC layers.\n···\n## How SPP Layer Works\nFigure 3 below displays the detailed steps of how SPP-Net processes an image. In accordance with the lower flowchart in Figure 2, we initially pass the raw image through a stack of convolution layers. In one of their experiments, they used a network of 5 convolutions which they refer to as _conv₁_ , _conv₂_ , _conv₃_ , _conv₄_ and _conv₅_. For the sake of simplicity, the figure below only displays the tensor produced by the last convolution, which is the one to be forwarded to the SPP layer.\n![](https://assets.insightmediagroup.io/media/max/800/1*fiEw4PtWqo-hgwteioWw6A.png)\nFigure 3. The process within the SPP layer [1].\nInside the SPP layer, what we do first is to divide the spatial dimension of the tensor into grids and take the maximum value within each grid — just like a typical maxpooling operation. There are 3 grid arrangements we use here: 4×4, 2×2, and 1×1, in which we will apply each of them to the same tensor. Finally, these grid cells of maximum values are flattened and concatenated, ready to be forwarded to the fully-connected part of the network.\nThe grid arrangements are created independently of the spatial resolution of the image. This means that we will always have the same number of grid cells regardless of the input tensor shape, which also implies that at the end of the process we will obtain a fixed-length vector. Let’s take a look at the examples in Figure 4 and 5 below to prove this.\n![](https://assets.insightmediagroup.io/media/max/1200/1*MLlnI2Z-48Gz8bmB9Ef9rg.png)\nFigure 4. How SPP with 4×4, 2×2, and 1×1 grid arrangements works on an 8×8 feature map [2].\nFigure 4 above displays a case where our input feature map has the resolution of 8×8. It is mentioned in the paper that we can find the resulting feature vector length by computing _Mk_ , where _M_ is the number of spatial bins (the grids) and _k_ is the number of channels of the tensor. In this case, we assume that our tensor only has a single channel, so we can simply compute _M_ and multiply everything with 1. With the above configuration, we will get a feature vector with a length of (4×4 + 2×2 + 1×1) × 1 = 21. Now if we try to use the same configuration on different image size, say 12×8, the SPP layer will still produce the exact same vector length as it automatically adjusts the window size and the stride. You can see in Figure 5 below that the pooling window becomes rectangle.\n![](https://assets.insightmediagroup.io/media/max/1200/1*QHZXjNHEgBraX9nDf8kqbA.png)\nFigure 5. How the same SPP grid arrangements work on a 12×8 feature map. This one also produces a feature vector of length 21 [2].\n···\n## Advantages of SPP-Net\nThe way SPP works discussed above allows SPP-Net to be more robust against overfitting. In fact, what makes the conventional CNN prone to overfit is the flatten operation done on the feature map produced by the last convolution layer. This is essentially because flattening destroys the spatial structure of the feature map, forcing the FC layer to learn position-specific features, which breaks the translation-invariant property that the convolutional layers provide. By using SPP, we basically still preserve the spatial information through the smaller pooling window (the 4×4 grid) while capturing the holistic view of the image through the large pooling window (the 1×1 grid). This implies that the part of the feature vector highlighted in blue in Figures 4 and 5 stores more detailed spatial information, while the one highlighted in gray stores the general information of that image.\nThe reason that SPP-Net is less prone to overfitting is because of the invariance of the 1D feature vector against object deformation and spatial layout. We can think of the _object deformation invariance_ like this: suppose in the training set we got a bunch of images of a person standing — SPP allows our model to predict the person even if they are sitting since our feature vector is now less rigid to such subtle changes. Meanwhile, _spatial layout invariance_ refers to a condition where our model is trained with images of a person located at the center of the image yet is tested on images where the person is not located exactly in the middle. SPP allows the model to predict it correctly since the pixels are now grouped into larger spatial bins, making slight shifts in the object location undetected.\nAnother advantage we get by using SPP-Net is that we can train the model with images of varying resolutions and aspect ratios, which again, allows the model to be even better to handle overfitting. It is explained in the paper that the authors decided to randomly resize the input image between the range of 180×180 and 224×224 after completing each epoch.\n···\n## Experimental Results\nTalking about the experimental results, it is shown in Figure 6 below that implementing SPP allows a model to achieve lower classification loss. Moreover, we can decrease this loss value even further by training the model with varying image scales. This trend can actually be observed in all backbone models the authors experimented with. They therefore explicitly mention in the paper that the advantages of SPP are orthogonal to specific CNN designs — meaning that we can improve the performance of any backbone model simply by attaching an SPP layer to it.\n![](https://assets.insightmediagroup.io/media/max/800/1*CjPfwX0H0CloRQW8XjQhzQ.png)\nFigure 6. Classification result on ImageNet 2012 dataset [1].\n### Experiments on Detection Task\nNot only on classification task, but the authors also tried to use SPP-Net to perform object detection, which some of their experimental results are summarized in Figure 7 below.\n![](https://assets.insightmediagroup.io/media/max/800/1*G2FhmkpL_rBUeIXxA239QA.png)\nFigure 7. Experimental results on object detection task [1].\nRemember that back in 2014 R-CNN was the state-of-the-art model for object detection, which unfortunately was extremely slow. You can see in the table above that SPP-based approach performed 38× faster than R-CNN while maintaining the exact same mAP (59.2). We can actually make the SPP-Net even faster by performing prediction on a single scale only, which they found that it could boost the speed over 100× faster than R-CNN by slightly sacrificing the mAP.\nTalking more specifically about the underlying algorithm, R-CNN is very slow because it needs to classify approximately 2000 bounding box candidates by passing each of them through the entire CNN model one by one. This process is modified by SPP-Net, where instead of passing each object candidate through the CNN, we can just pass the entire image through it and then classify the bounding box candidates from the deeper feature map using the SPP and the FC layers (see Figure 8). By doing so, we essentially avoid performing the repeating feature extraction process done by the CNN. Instead, we can now just perform the feature extraction once and do the ~2000 classifications based on the 1-dimensional feature vectors produced by the SPP layer. And so, it makes a perfect sense that SPP-based object detection method can massively outperform R-CNN in terms of the processing time. Additionally, it might be worth noting that the varying bounding box candidate sizes won’t be a problem since SPP can easily convert them into uniform-sized vectors.\n![](https://assets.insightmediagroup.io/media/max/800/1*ajOfUZc-GtJ7PWF3MYnUQQ.png)\nFigure 8. How SPP layer works on object detection task [1].\n···\n## SPP-Net from Scratch\nAs we have understood the underlying theory of SPP-Net, we can now try to implement it from scratch. And in fact, the implementation is considerably easy.\nThe authors used several base models in their experiments: ZF-5, Convnet-5, Overfeat-5 and Overfeat-7. In this article I’m going to implement the ZF-5, which I think is the simplest one. I’ll leave the remaining models as an exercise for you to practice. The table given in Figure 9 below shows the architectural details of these models. It is important to note that this table only provides the CNN part. According to the paper, this part will then be connected to the SPP layer followed by two FC layers and the classification layer.\n![](https://assets.insightmediagroup.io/media/max/1200/1*Wrka8_QhVz5z7xWPbfjLKA.png)\nFigure 9. The backbone models where the authors implement SPP layer on [1].\nThe very first thing we do in the code is to import the required modules. Here we also initialize the `GRIDS` variable, which is used to determine the grid arrangements we are about to use in the SPP layer. In this case, I set the values to 4, 2, and 1, indicating that we will perform pooling operation within each 4×4, 2×2, and 1×1 grid cells, following the example in Figure 3.\npython\n\n```\n# Codeblock 1\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nGRIDS = [4, 2, 1]\n```\n\n### SPP Layer\nWhat we do next is to implement the SPP layer. Take a look at Codeblock 2 below to see how I do that. If you’re familiar with PyTorch models, you will find this `SPP` class quite unique as it does not have the `__init__()` method. However, this class is valid because we indeed want to define a _parameterless flow_. Well, this is actually not a standard convention in deep learning, but I use this term to emphasize that this module does not have any trainable weights or biases. All it does is just transform the input tensor based on the predefined grid cell arrangements.\npython\n\n```\n# Codeblock 2\nclass SPP(nn.Module):\n    def forward(self, x):\n\n        pooled_outputs = []      #(1)\n        for grid in GRIDS:\n            pooled = F.adaptive_max_pool2d(x, output_size=(grid,grid))  #(2)\n            print(f'after pool\\t\\t: {pooled.size()}')\n\n            pooled = torch.flatten(pooled, start_dim=1)    #(3)\n            print(f'after flatten\\t\\t: {pooled.size()}\\n')\n\n            pooled_outputs.append(pooled)                  #(4)\n\n        concatenated = torch.cat(pooled_outputs, dim=1)    #(5)\n        print(f'after concatenate\\t: {concatenated.size()}\\n')\n\n        return concatenated\n```\n\nInside the `forward()` method, what we do first is to initialize an empty list as written at line `#(1)`. This list will then be used to store the pooling results. Next, for each grid arrangement, we will perform pooling operation using `F.adaptive_max_pool2d()` (`#(2)`). Adaptive pooling mechanism used here is different from a standard pooling, where in this case we can just specify the output dimension we want and let the function automatically determine the kernel size and the stride. Afterwards, we flatten the pooling result (`#(3)`) and append it to the `pooled_output` list (`#(4)`) we initialized earlier. Once the iteration is complete, we then concatenate all the elements, forming a long single-dimensional tensor (`#(5)`).\nIn order to check if our `SPP` class above works properly, we can test it using the following code. The `x` tensor I use below is essentially the one I displayed in Figure 4 (`#(1)`). Here I am going to pass this tensor through the SPP layer (`#(2)`) and see if the resulting output is correct.\npython\n\n```\n# Codeblock 3\nspp = SPP()\n\nx = torch.tensor([[[[5, 4, 4, 5, 3, 2, 6, 5],\n                    [2, 8, 6, 3, 1, 5, 8, 1],\n                    [4, 3, 7, 5, 2, 4, 7, 3],\n                    [2, 1, 3, 2, 5, 5, 4, 2],\n                    [4, 6, 7, 9, 5, 6, 6, 7],\n                    [6, 8, 5, 9, 4, 9, 4, 1],\n                    [3, 7, 4, 6, 8, 4, 4, 9],\n                    [2, 4, 4, 3, 8, 2, 9, 2]]]], dtype=torch.float32)  #(1)\n\nout = spp(x)    #(2)\n```\n\nAnd here’s what the output looks like. You can see below that our pooling operation correctly produces 4×4, 2×2, and 1×1 tensors within each iteration, which results in three 1D vectors of length 16, 4, and 1 after being flattened. Then, these feature vectors are concatenated, forming a new vector of length 21.\nmarkup\n\n```\n# Codeblock 3 Outputafter pool        : torch.Size([1, 1, 4, 4])after flatten     : torch.Size([1, 16])after pool        : torch.Size([1, 1, 2, 2])after flatten     : torch.Size([1, 4])after pool        : torch.Size([1, 1, 1, 1])after flatten     : torch.Size([1, 1])after concatenate : torch.Size([1, 21])\n```\n\nWe can also print out the values inside the `out` tensor as follows. You’ll be surprised that these values are exactly the same as the one in Figure 4.\npython\n\n```\n# Codeblock 4\nprint(out)\n```\n\nmarkup\n\n```\n# Codeblock 4 Outputtensor([[8., 6., 5., 8., 4., 7., 5., 7., 8., 9., 9., 7., 7., 6., 8., 9., 8., 8.,         9., 9., 9.]])\n```\n\nWe can do the exact same thing to the 12×8 tensor in Figure 5. You can see in the output of the codeblock below that despite this difference in the input size, the SPP layer still produces a feature vector of the same length since we don’t change the values in the `GRIDS` list.\npython\n\n```\n# Codeblock 5\nspp = SPP()\n\nx = torch.tensor([[[4, 3, 7, 5, 2, 4, 7, 3],\n                   [2, 1, 3, 2, 5, 5, 4, 2],\n                   [5, 4, 4, 5, 3, 2, 6, 5],\n                   [2, 8, 6, 3, 1, 5, 8, 1],\n                   [4, 3, 7, 5, 2, 4, 7, 3],\n                   [2, 1, 3, 2, 5, 5, 4, 2],\n                   [4, 6, 7, 9, 5, 6, 6, 7],\n                   [6, 8, 5, 9, 4, 9, 4, 1],\n                   [3, 7, 4, 6, 8, 4, 4, 9],\n                   [2, 4, 4, 3, 8, 2, 9, 2], \n                   [6, 8, 5, 9, 4, 9, 4, 1],\n                   [3, 7, 4, 6, 8, 4, 4, 9]]], dtype=torch.float32)\n\nout = spp(x)\n```\n\nmarkup\n\n```\n# Codeblock 5 Outputafter pool        : torch.Size([1, 4, 4])after flatten     : torch.Size([1, 16])after pool        : torch.Size([1, 2, 2])after flatten     : torch.Size([1, 4])after pool        : torch.Size([1, 1, 1])after flatten     : torch.Size([1, 1])after concatenate : torch.Size([1, 21])\n```\n\nAgain, if we print out the actual values contained in the resulting tensor, we can see that it has the exact same values as the one in Figure 5 (see Codeblock 6). These two test cases essentially confirm that our SPP implementation is correct, thus ready to actually be attached to the backbone model.\npython\n\n```\n# Codeblock 6\nprint(out)\n```\n\nmarkup\n\n```\n# Codeblock 6 Outputtensor([[5., 7., 5., 7., 8., 7., 5., 8., 8., 9., 9., 9., 8., 9., 9., 9., 8., 8.,         9., 9., 9.]])\n```\n\n···\n### ZF-5 Backbone with SPP\nAs I’ve mentioned earlier, in this demonstration I am going to use ZF-5 as the backbone model. Here I name the class `ZF5_SPPNet` since we want to integrate SPP into this model. The code implementation is pretty long, so I break it down into two codeblocks: 7a and 7b. Just ensure that these two codeblocks are placed inside the same notebook cell if you want to run it on your own.\npython\n\n```\n# Codeblock 7a\nclass ZF5_SPPNet(nn.Module):\n    def __init__(self):\n        super().__init__()\n        \n        self.relu = nn.ReLU()\n        \n        \n        self.conv1 = nn.Conv2d(in_channels=3, \n                               out_channels=96, \n                               kernel_size=7, \n                               stride=2, \n                               padding=0)    #(1)\n        self.norm1 = nn.LocalResponseNorm(size=5)\n        self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)    #(2)\n        \n        \n        self.conv2 = nn.Conv2d(in_channels=96, \n                               out_channels=256, \n                               kernel_size=5, \n                               stride=2, \n                               padding=1)    #(3)\n        self.norm2 = nn.LocalResponseNorm(size=5)\n        self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=0)    #(4)\n        \n        \n        self.conv3 = nn.Conv2d(in_channels=256, \n                               out_channels=384, \n                               kernel_size=3, \n                               stride=1, \n                               padding=1)\n        \n        self.conv4 = nn.Conv2d(in_channels=384, \n                               out_channels=384, \n                               kernel_size=3, \n                               stride=1, \n                               padding=1)\n        \n        self.conv5 = nn.Conv2d(in_channels=384, \n                               out_channels=256, \n                               kernel_size=3, \n                               stride=1, \n                               padding=1)\n        \n        self.spp = SPP()    #(4)\n        spp_out_size = 256 * sum([grid**2 for grid in GRIDS])    #(5)\n        \n        self.fc6 = nn.Linear(in_features=spp_out_size, out_features=4096)\n        self.dropout6 = nn.Dropout(p=0.5)    #(6)\n        \n        self.fc7 = nn.Linear(in_features=4096, out_features=4096)\n        self.dropout7 = nn.Dropout(p=0.5)    #(7)\n        \n        self.fc8 = nn.Linear(in_features=4096, out_features=1000)\n```\n\nNow let’s talk about the Codeblock 7a first, which contains the entire `__init__()` method. According to Figure 9, the first two convolution layers of ZF-5 also have a _local response norm_ layer and a pooling layer associated with each. It might be worth noting that local response norm was the state-of-the-art normalization mechanism at that time as batch normalization layer was only introduced a year after this paper.\nOne thing that makes this implementation a bit challenging is the padding of the first two convolutions and their corresponding maxpooling layers. We typically set the padding to 3 for 7×7 kernel, padding 2 for 5×5 kernel, padding 1 for 3×3 kernel, and padding 0 for 1×1 kernel so that the spatial dimension of the tensor is preserved. However, when I tried to use that convention, the resulting tensor shapes do not match with the paper, so I tweaked them manually and came up with padding values that appear to be somewhat arbitrary (see lines `#(1,2,3,4)`). \nAs the first two convolution layers are done, we can now move on to the `conv3`, `conv4`, and `conv5` layers. These remaining convolutions are easy since we only need to stack them sequentially by setting the number of input and output channels according to the guide in Figure 9. In this case we don’t need to search for the padding since all of them use 3×3 kernels, hence we can just set it to 1 as we won’t change the spatial dimension until we reach the SPP layer.\nThe SPP layer itself is initialized at line `#(4)`. Don’t forget to calculate the output shape of this layer because we want to use it to specify the feature vector length accepted by the subsequent FC layer (`#(5)`). The stack of FC layers is also straightforward to implement, where it consists of two hidden layers of 4096 neurons (`fc6` and `fc7`). The `fc7` layer is then followed by the final output layer `fc8`, which returns the classification tensor of length 1000, matching the number of classes available in the ImageNet 2012 dataset. Additionally, here we also initialize two dropout layers (`#(6,7)`) to be placed after each of the two hidden FC layers.\nAs the `__init__()` method is completed, we will now move on to the `forward()` method which I write in Codeblock 7b below. I am not going to explain this code though, because what we basically do here is just to connect the layers one by one.\npython\n\n```\n# Codeblock 7b\n    def forward(self, x):\n        print(f'original\\t\\t: {x.size()}')\n        \n        x = self.norm1(self.relu(self.conv1(x)))\n        print(f'after conv1\\t\\t: {x.size()}')\n        x = self.pool1(x)\n        print(f'after pool1\\t\\t: {x.size()}')\n        \n        x = self.norm2(self.relu(self.conv2(x)))\n        print(f'after conv2\\t\\t: {x.size()}')\n        x = self.pool2(x)\n        print(f'after pool2\\t\\t: {x.size()}')\n        \n        x = self.relu(self.conv3(x))\n        print(f'after conv3\\t\\t: {x.size()}')\n        \n        x = self.relu(self.conv4(x))\n        print(f'after conv4\\t\\t: {x.size()}')\n        \n        x = self.relu(self.conv5(x))\n        print(f'after conv5\\t\\t: {x.size()}\\n')\n        \n        x = self.spp(x)\n        print(f'after spp\\t\\t: {x.size()}\\n')\n        \n        x = self.dropout6(self.relu(self.fc6(x)))\n        print(f'after fc6\\t\\t: {x.size()}')\n        \n        x = self.dropout7(self.relu(self.fc7(x)))\n        print(f'after fc7\\t\\t: {x.size()}')\n        \n        x = self.fc8(x)\n        print(f'after fc8\\t\\t: {x.size()}')\n        \n        return x\n```\n\nNow let’s check if our `ZF5_SPPNet` class works properly by running the following codeblock. Here I am trying to pass the dummy tensor `x` through the network, which simulates an RGB image of size 224×224.\npython\n\n```\n# Codeblock 8\nzf5sppnet = ZF5_SPPNet()\n\nx = torch.randn(1, 3, 224, 224)\nout = zf5sppnet(x)\n```\n\nYou can see in the resulting output below that our 224×224 tensor becomes 55×55 after being processed by the first convolution and its corresponding maxpooling layer (`#(1)`). Next, the second conv-pooling stack downsamples it further to 13×13 (`#(3)`). If you go back to Figure 9, you will notice the table is a bit imprecise since the feature map size of 27×27 written in the _conv2_ column should be the output of the second convolution (`#(2)`), not the output of the pooling layer (`#(3)`). The subsequent `conv3`, `conv4`, and `conv5` layers (`#(4–5)`) are quite simple, where what they essentially do are just changing the number of layers from 256 to 384 and back to 256 again while preserving the spatial dimension. The resulting 256×13×13 tensor is then forwarded to the SPP layer. Remember that since our `GRIDS` list has 3 values, then our SPP will produce 3 feature vectors as well, each having the length of 4096, 1024, and 256 as shown at lines `#(6)`, `#(7)`, and `#(8)`. These layers are then concantenated, forming a long one-dimensional vector of length 5376 (`#(9)`). Afterwards, we can feed this feature vector into the FC layers until we eventually got the 1000-class prediction vector (`#(10)`).\nmarkup\n\n```\n# Codeblock 8 Outputoriginal          : torch.Size([1, 3, 224, 224])after conv1       : torch.Size([1, 96, 109, 109])after pool1       : torch.Size([1, 96, 55, 55])    #(1) after conv2       : torch.Size([1, 256, 27, 27])   #(2)after pool2       : torch.Size([1, 256, 13, 13])   #(3)after conv3       : torch.Size([1, 384, 13, 13])   #(4)after conv4       : torch.Size([1, 384, 13, 13])after conv5       : torch.Size([1, 256, 13, 13])   #(5)after pool        : torch.Size([1, 256, 4, 4])after flatten     : torch.Size([1, 4096])          #(6)after pool        : torch.Size([1, 256, 2, 2])after flatten     : torch.Size([1, 1024])          #(7)after pool        : torch.Size([1, 256, 1, 1])after flatten     : torch.Size([1, 256])           #(8)after concatenate : torch.Size([1, 5376])          #(9)after spp         : torch.Size([1, 5376])after fc6         : torch.Size([1, 4096])after fc7         : torch.Size([1, 4096])after fc8         : torch.Size([1, 1000])          #(10)\n```\n\nAt this point we have verified the forward pass of this network. Thus, we can say that this model is now ready to be trained.\n···\n## Ending\nAnd that’s everything about the theory and the implementation of SPP-Net. Here I challenge you to try implementing SPP layer on modern architectures like ResNet, ConvNeXt, etc. I believe you can do that pretty easily as what you need to do is just to copy-paste the `SPP` class in Codeblock 2 and connect it to any CNN-based architectures. You can visit my profile [3] if you need more references on implementing deep learning architectures from scratch. \nI hope you learn something new today, feel free to reach me if you spot any mistakes in my explanation or in the code. By the way you can also access the code in my GitHub repository [4]. Thanks for reading!\n···\n## References\n[1] Kaiming He _et al_. Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition. Arxiv. <https://arxiv.org/abs/1406.4729> [Accessed October 17, 2025].\n[2] Image created originally by author.\n[3] Muhammad Ardi. Medium. <https://medium.com/@muhammad_ardi> [Accessed October 17, 2025].\n[4] MuhammadArdiPutra. SPP-Net. GitHub. <https://github.com/MuhammadArdiPutra/medium_articles/blob/main/Deep%20Learning%20From%20Scratch/SPP-Net.ipynb> [Accessed October 18, 2025].\n## Register Now to Continue Reading\n### Unlock Towards Data Science with a Free Account\nAll articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more.\nRegister for freeSign in to your account\n* * *\n![](https://assets.insightmediagroup.io/media/1787657598287_taxhya.jpeg)\nWritten By\n## [Muhammad Ardi Putra](https://towardsdatascience.com/author/muhammad_ardi/)\nDeep learning storyteller\n[See all from Muhammad Ardi Putra](https://towardsdatascience.com/author/muhammad_ardi/)\n[Deep Dives](https://towardsdatascience.com/tag/deep-dives/)[Artificial Intelligence](https://towardsdatascience.com/tag/artificial-intelligence/)[Cnn](https://towardsdatascience.com/tag/cnn/)[Machine Learning](https://towardsdatascience.com/tag/machine-learning/)[Object Detection](https://towardsdatascience.com/tag/object-detection/)\nShare This Article\n  * [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Ftowardsdatascience.com%2Fspp-net-paper-walkthrough-breaking-the-fixed-size-constraint%2F&title=SPP-Net%20Paper%20Walkthrough%3A%20Breaking%20the%20Fixed-Size%20Constraint)\n  * [](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Ftowardsdatascience.com%2Fspp-net-paper-walkthrough-breaking-the-fixed-size-constraint%2F&title=SPP-Net%20Paper%20Walkthrough%3A%20Breaking%20the%20Fixed-Size%20Constraint)\n  * [](https://x.com/share?url=https%3A%2F%2Ftowardsdatascience.com%2Fspp-net-paper-walkthrough-breaking-the-fixed-size-constraint%2F&text=SPP-Net%20Paper%20Walkthrough%3A%20Breaking%20the%20Fixed-Size%20Constraint)\n\n\nTowards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.\n[Write for TDS](https://towardsdatascience.com/submissions/)\n## Related Articles\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2021/02/03zgD0Xk3prwEh_PG.jpg)](https://towardsdatascience.com/pytorch-shap-explainable-convolutional-neural-networks-ece5f04c374f/)\n## [PyTorch + SHAP = Explainable Convolutional Neural Networks](https://towardsdatascience.com/pytorch-shap-explainable-convolutional-neural-networks-ece5f04c374f/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nLearn how to explain predictions of convolutional neural networks with PyTorch and SHAP\n[Dario Radečić](https://towardsdatascience.com/author/radecicdario/)\nFebruary 1, 20214 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2022/07/0EhS1QEjjwP1bjzLY-scaled.jpg)](https://towardsdatascience.com/how-to-boost-pytorch-dataset-using-memory-mapped-files-6893bff27b99/)\n## [How to boost PyTorch Dataset using memory-mapped files](https://towardsdatascience.com/how-to-boost-pytorch-dataset-using-memory-mapped-files-6893bff27b99/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nThis article will discuss the reasoning and the steps of implementing a PyTorch dataset that uses memory-mapped files\n[Tudor Surdoiu](https://towardsdatascience.com/author/dacus-augustus/)\nJuly 25, 20224 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2020/01/1QjeHDF_YBNkMUDjy_wtj-Q.jpeg)](https://towardsdatascience.com/top-3-resources-to-learn-machine-learning-a2adcda40fd/)\n## [Top 3 Resources to Learn Machine Learning](https://towardsdatascience.com/top-3-resources-to-learn-machine-learning-a2adcda40fd/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nLooking for a course in applied machine learning? Or something bit more theoretical? Start here.\n[Dario Radečić](https://towardsdatascience.com/author/radecicdario/)\nJanuary 13, 20205 min read\n  * [![](https://towardsdatascience.com/images/fallback-image.jpg)](https://towardsdatascience.com/how-the-pytorch-convolutions-work-or-how-to-collapse-two-convolutions-into-one-6dc810489d79/)\n## [How the PyTorch convolutions work or how to collapse two convolutions into one](https://towardsdatascience.com/how-the-pytorch-convolutions-work-or-how-to-collapse-two-convolutions-into-one-6dc810489d79/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nOr closer look at convolution for deep learning engineers\n[Anton Lebedev](https://towardsdatascience.com/author/antoleb/)\nAugust 30, 20226 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2022/04/1S_FScuRVWTITaOvFg0FdsA-scaled.jpeg)](https://towardsdatascience.com/if-you-work-with-textual-data-learn-these-pandas-methods-3f224122ebaf/)\n## [If You Work With Textual Data, Learn These Pandas Methods](https://towardsdatascience.com/if-you-work-with-textual-data-learn-these-pandas-methods-3f224122ebaf/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nTextual data carries more information than numbers\n[Soner Yıldırım](https://towardsdatascience.com/author/sonery/)\nApril 7, 20226 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/02/0iS8U_4PDeGERdC7A-scaled.jpg)](https://towardsdatascience.com/exploring-object-detection-with-r-cnn-models-a-comprehensive-beginners-guide-part-2-685bc89775e2/)\n## [Exploring Object Detection with R-CNN Models - A Comprehensive Beginner's Guide (Part 2)](https://towardsdatascience.com/exploring-object-detection-with-r-cnn-models-a-comprehensive-beginners-guide-part-2-685bc89775e2/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nObject Detection Models\n[Raghav Bali](https://towardsdatascience.com/author/rghv_bali/)\nFebruary 17, 20247 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2023/01/0F7Ln3dhjETAIb7Y_-scaled.jpg)](https://towardsdatascience.com/how-i-built-a-lo-fi-music-web-player-with-ai-generated-tracks-36f3915e39f8/)\n## [How I Built a Lo-fi Music Web Player with AI-Generated Tracks](https://towardsdatascience.com/how-i-built-a-lo-fi-music-web-player-with-ai-generated-tracks-36f3915e39f8/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nMade with Tone.js and LSTM Model\n[Aleksandra Ma](https://towardsdatascience.com/author/leksa_86/)\nJanuary 12, 202311 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2022/09/0T8T_lJVYHPP7mx5e-scaled.jpg)](https://towardsdatascience.com/why-i-use-weights-biases-for-my-machine-learning-ph-d-research-11ab2fe16956/)\n## [Why I Use Weights & Biases for My Machine Learning Ph.D. Research](https://towardsdatascience.com/why-i-use-weights-biases-for-my-machine-learning-ph-d-research-11ab2fe16956/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nExperiment tracking, hyperparameter optimization, private hosting\n[Pascal Janetzky](https://towardsdatascience.com/author/pascaljanetzky/)\nSeptember 26, 20227 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2025/12/1-4-scaled-copy.jpg)](https://towardsdatascience.com/optimizing-pytorch-model-inference-on-aws-graviton/)\n## [Optimizing PyTorch Model Inference on AWS Graviton](https://towardsdatascience.com/optimizing-pytorch-model-inference-on-aws-graviton/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nTips for accelerating AI/ML on CPU — Part 2\n[Chaim Rand](https://towardsdatascience.com/author/chaimrand/)\nDecember 10, 20259 min read\n\n\n  * [YouTube](https://www.youtube.com/c/TowardsDataScience)\n  * [X](https://x.com/TDataScience)\n  * [LinkedIn](https://www.linkedin.com/company/towards-data-science/)\n  * [Threads](https://www.threads.net/@towardsdatascience)\n  * [Bluesky](https://bsky.app/profile/towardsdatascience.com)\n\n\n[](https://towardsdatascience.com/)\nYour home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.\nSubscribe to Our Newsletter[Write For TDS](https://towardsdatascience.com/submissions/)•[About](https://towardsdatascience.com/about-towards-data-science-d691af11cc2f/)•[Advertise](https://contact.towardsdatascience.com/advertise-with-towards-data-science)•[Privacy Policy](https://towardsdatascience.com/privacy-policy/)•[Terms of Use](https://towardsdatascience.com/website-terms-of-use/)\n© Insight Media Group, LLC 2026•[The New Stack](https://thenewstack.io)•[roadmap.sh](https://roadmap.sh)•[Creator Network](https://insightmediagroup.io/creator-network)\n####  Recommended Articles \nClose\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607686/image?etag=1782161464448&width=500&height=500) How Relevance Models Foreshadowed Transformers for NLP ](https://towardsdatascience.com/how-relevance-models-foreshadowed-transformers-for-nlp/ \"How Relevance Models Foreshadowed Transformers for NLP\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607639/image?etag=1781250125848&width=500&height=500) Javascript Fatigue: HTMX Is All You Need to Build ChatGPT — Part 1 ](https://towardsdatascience.com/javascript-fatigue-you-dont-need-js-to-build-chatgpt/ \"Javascript Fatigue: HTMX Is All You Need to Build ChatGPT — Part 1\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607930/image?etag=1782124566054&width=500&height=500) The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs ](https://towardsdatascience.com/subset-sum-problem-solved-in-linear-time-for-dense-enough-inputs/ \"The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/3660/image?etag=1780176246070&width=500&height=500) Does Your Company Have a Data Strategy? ](https://towardsdatascience.com/does-your-company-have-a-data-strategy-e8944dbec4ae/ \"Does Your Company Have a Data Strategy?\")\n\n\n",
  "body_base_path": "attachments/465855dd76aa/pages/",
  "body_markdown_path": "attachments/465855dd76aa/pages/archived-body.md",
  "local_html": "attachments/465855dd76aa/pages/archived-body.html",
  "body_source": "prior_capture"
}