保留正文与补采附件。源页面当前未重新核验;具体限制见交付清单。
We value your privacy
We 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.
Customise Reject All Accept All
Customise Consent Preferences
We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.
The 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
NecessaryAlways Active
Necessary 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.
Cookie BCTempID
Duration 10 minutes
Description No description available.
Cookie __cf_bm
Duration 1 hour
Description This cookie, set by Cloudflare, is used to support Cloudflare Bot Management.
Cookie AWSALBCORS
Duration 7 days
Description Amazon Web Services set this cookie for load balancing.
Cookie _cfuvid
Duration session
Description Cloudflare sets this cookie to track users across sessions to optimize user experience by maintaining session consistency and providing personalized services
Cookie li_gc
Duration 6 months
Description Linkedin set this cookie for storing visitor's consent regarding using cookies for non-essential purposes.
Cookie __hssrc
Duration session
Description This 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.
Cookie __hssc
Duration 1 hour
Description HubSpot sets this cookie to keep track of sessions and to determine if HubSpot should increment the session number and timestamps in the __hstc cookie.
Cookie wpEmojiSettingsSupports
Duration session
Description WordPress 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.
Cookie BCSessionID
Duration 1 year 1 month 4 days
Description Blueconic sets this cookie as a unique identifier for the BlueConic profile.
Cookie _octo
Duration 1 year
Description No description available.
Cookie logged_in
Duration 1 year
Description No description available.
Cookie __Secure-YEC
Duration past
Description YouTube sets this cookie to stores the user's video player preferences using embedded YouTube video
Cookie __eoi
Duration 6 months
Description Description is currently not available.
Cookie AWSALBTGCORS
Duration 7 days
Description No description available.
Cookie login-status-p
Duration past
Description Description is currently not available.
Cookie AWSALBTG
Duration 7 days
Description No description available.
Cookie csrf_token
Duration session
Description No description available.
Cookie token_v2
Duration 1 day
Description Description is currently not available.
Cookie D
Duration 1 year
Description Description is currently not available.
Cookie PHPSESSID
Duration session
Description This 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.
Cookie VISITOR_PRIVACY_METADATA
Duration 6 months
Description YouTube sets this cookie to store the user's cookie consent state for the current domain.
Cookie cookietest
Duration session
Description The cookietest cookie is typically used to determine whether the user's browser accepts cookies, essential for website functionality and user experience.
Cookie __Host-airtable-session
Duration 1 year
Description This cookie is used to enable us to integrate the services of Airtable.
Cookie __Host-airtable-session.sig
Duration 1 year
Description This cookie is used to enable us to integrate the services of Airtable.
Cookie m
Duration 1 year 1 month 4 days
Description Stripe sets this cookie for fraud prevention purposes. It identifies the device used to access the website, allowing the website to be formatted accordingly.
Cookie BIGipServer*
Duration session
Description Marketo 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.
Cookie __cfruid
Duration session
Description Cloudflare sets this cookie to identify trusted web traffic.
Cookie _GRECAPTCHA
Duration 6 months
Description Google Recaptcha service sets this cookie to identify bots to protect the website against malicious spam attacks.
Cookie __Secure-YNID
Duration 6 months
Description Google cookie used to protect user security and prevent fraud, especially during the login process.
Cookie cookieyes-consent
Duration 1 year
Description CookieYes 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.
Functional Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.
Cookie lidc
Duration 1 day
Description LinkedIn sets the lidc cookie to facilitate data center selection.
Cookie brw
Duration 1 year
Description No description available.
Cookie brwConsent
Duration 5 minutes
Description Description is currently not available.
Cookie WMF-Uniq
Duration 1 year
Description Description is currently not available.
Cookie loom_anon_comment
Duration 1 year
Description No description available.
Cookie loom_referral_video
Duration session
Description Description is currently not available.
Cookie VISITOR_INFO1_LIVE
Duration 6 months
Description A cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.
Cookie yt-remote-connected-devices
Duration Never Expires
Description YouTube sets this cookie to store the user's video preferences using embedded YouTube videos.
Cookie ytidb::LAST_RESULT_ENTRY_KEY
Duration Never Expires
Description The 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.
Cookie yt-remote-device-id
Duration Never Expires
Description YouTube sets this cookie to store the user's video preferences using embedded YouTube videos.
Cookie yt-remote-session-name
Duration session
Description The yt-remote-session-name cookie is used by YouTube to store the user's video player preferences using embedded YouTube video.
Cookie yt-remote-fast-check-period
Duration session
Description The yt-remote-fast-check-period cookie is used by YouTube to store the user's video player preferences for embedded YouTube videos.
Cookie yt-remote-session-app
Duration session
Description The yt-remote-session-app cookie is used by YouTube to store user preferences and information about the interface of the embedded YouTube video player.
Cookie yt-remote-cast-available
Duration session
Description The yt-remote-cast-available cookie is used to store the user's preferences regarding whether casting is available on their YouTube video player.
Cookie yt-remote-cast-installed
Duration session
Description The yt-remote-cast-installed cookie is used to store the user's video player preferences using embedded YouTube video.
Cookie cp_session
Duration 3 months
Description Codepen sets this cookie for Help systems found in the website.
Cookie loid
Duration 1 year 1 month 4 days
Description This cookie is set by the Reddit. The cookie enables the sharing of content from the website onto the social media platform.
Analytics Analytical 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.
Cookie __hstc
Duration 6 months
Description Hubspot 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).
Cookie hubspotutk
Duration 6 months
Description HubSpot 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.
Cookie _ga
Duration 1 year 1 month 4 days
Description Google 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.
Cookie ga*
Duration 1 year 1 month 4 days
Description Google Analytics sets this cookie to store and count page views.
Cookie __Host-psifi.analyticsTrace
Duration 6 hours
Description Description is currently not available.
Cookie __Host-psifi.analyticsTraceV2
Duration 6 hours
Description Description is currently not available.
Cookie _gh_sess
Duration session
Description GitHub sets this cookie for temporary application and framework state between pages like what step the user is on in a multiple step form.
Cookie YSC
Duration session
Description YSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.
Cookie ajs_anonymous_id
Duration 1 year
Description This cookie is set by Segment to count the number of people who visit a certain site by tracking if they have visited before.
Cookie vuid
Duration 1 year 1 month 4 days
Description Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website.
Performance Performance 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.
Cookie AWSALB
Duration 7 days
Description AWSALB is an application load balancer cookie set by Amazon Web Services to map the session to the target.
Cookie acq
Duration past
Description Description is currently not available.
Cookie acq.sig
Duration past
Description Description is currently not available.
Cookie ptc
Duration 2 years
Description No description available.
Advertisement Advertisement 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.
Cookie muc_ads
Duration 1 year 1 month 4 days
Description Twitter sets this cookie to collect user behaviour and interaction data to optimize the website.
Cookie guest_id_marketing
Duration 1 year 1 month 4 days
Description Twitter sets this cookie to identify and track the website visitor.
Cookie guest_id_ads
Duration 1 year 1 month 4 days
Description Twitter sets this cookie to identify and track the website visitor.
Cookie personalization_id
Duration 1 year 1 month 4 days
Description Twitter 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.
Cookie guest_id
Duration 1 year 1 month 4 days
Description Twitter 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.
Cookie bcookie
Duration 1 year
Description LinkedIn sets this cookie from LinkedIn share buttons and ad tags to recognize browser IDs.
Cookie __Secure-ROLLOUT_TOKEN
Duration 6 months
Description YouTube 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.
Cookie yt.innertube::nextId
Duration Never Expires
Description YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.
Cookie yt.innertube::requests
Duration Never Expires
Description YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.
Cookie session_tracker
Duration session
Description This cookie is set by the Reddit. This cookie is used to identify trusted web traffic. It also helps in adverstising on the website.
Cookie edgebucket
Duration session
Description Reddit sets this cookie to save the information about a log-on Reddit user, for the purpose of advertisement recommendations and updating the content.
Cookie did
Duration 1 year
Description Arbor sets this cookie to show targeted ads to site visitors.This cookie expires after 2 months or 1 year.
Uncategorised Other uncategorised cookies are those that are being analysed and have not been classified into a category as yet. No cookies to display. Reject All Save My Preferences Accept All Toggle SearchToggle Mobile Navigation Publish AI, ML & data-science insights to a global community of data professionals. Sign inSubmit an Article LatestEditor's PicksNewsletterShipAI
Write For TDS LinkedInXToggle Search Deep Learning
Learn how Spatial Pyramid Pooling enables CNNs to handle any image size, with a from-scratch PyTorch implementation
Muhammad Ardi Putra
August 10, 202618 min read
Photo by Viktor Theo on Unsplash
In 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.
Figure 1. Examples of how cropping (left) and warping (right) respectively truncate and distort image content [1].
In 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.
···
Learn this step by step with the interactive Machine Learning roadmap.
SPP-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.
Figure 2. The flow of the conventional CNN architecture (top) vs the flow of a CNN with SPP layer (bottom) [1].
The 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.
If 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.
With 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.
···
Figure 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.
Figure 3. The process within the SPP layer [1].
Inside 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.
The 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.
Figure 4. How SPP with 4×4, 2×2, and 1×1 grid arrangements works on an 8×8 feature map [2].
Figure 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.
Figure 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].
···
The 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. The 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. Another 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. ···
Talking 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.
Figure 6. Classification result on ImageNet 2012 dataset [1].
Not 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.
Figure 7. Experimental results on object detection task [1].
Remember 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.
Talking 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.
Figure 8. How SPP layer works on object detection task [1].
···
As 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.
The 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.
Figure 9. The backbone models where the authors implement SPP layer on [1].
The 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.
python
# Codeblock 1
import torch
import torch.nn as nn
import torch.nn.functional as F
GRIDS = [4, 2, 1]
What 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.
python
# Codeblock 2
class SPP(nn.Module):
def forward(self, x):
pooled_outputs = [] #(1)
for grid in GRIDS:
pooled = F.adaptive_max_pool2d(x, output_size=(grid,grid)) #(2)
print(f'after pool\t\t: {pooled.size()}')
pooled = torch.flatten(pooled, start_dim=1) #(3)
print(f'after flatten\t\t: {pooled.size()}\n')
pooled_outputs.append(pooled) #(4)
concatenated = torch.cat(pooled_outputs, dim=1) #(5)
print(f'after concatenate\t: {concatenated.size()}\n')
return concatenated
Inside 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)).
In 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.
python
# Codeblock 3
spp = SPP()
x = torch.tensor([[[[5, 4, 4, 5, 3, 2, 6, 5],
[2, 8, 6, 3, 1, 5, 8, 1],
[4, 3, 7, 5, 2, 4, 7, 3],
[2, 1, 3, 2, 5, 5, 4, 2],
[4, 6, 7, 9, 5, 6, 6, 7],
[6, 8, 5, 9, 4, 9, 4, 1],
[3, 7, 4, 6, 8, 4, 4, 9],
[2, 4, 4, 3, 8, 2, 9, 2]]]], dtype=torch.float32) #(1)
out = spp(x) #(2)
And 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. markup
# 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])
We 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.
python
# Codeblock 4
print(out)
markup
# Codeblock 4 Outputtensor([[8., 6., 5., 8., 4., 7., 5., 7., 8., 9., 9., 7., 7., 6., 8., 9., 8., 8., 9., 9., 9.]])
We 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.
python
# Codeblock 5
spp = SPP()
x = torch.tensor([[[4, 3, 7, 5, 2, 4, 7, 3],
[2, 1, 3, 2, 5, 5, 4, 2],
[5, 4, 4, 5, 3, 2, 6, 5],
[2, 8, 6, 3, 1, 5, 8, 1],
[4, 3, 7, 5, 2, 4, 7, 3],
[2, 1, 3, 2, 5, 5, 4, 2],
[4, 6, 7, 9, 5, 6, 6, 7],
[6, 8, 5, 9, 4, 9, 4, 1],
[3, 7, 4, 6, 8, 4, 4, 9],
[2, 4, 4, 3, 8, 2, 9, 2],
[6, 8, 5, 9, 4, 9, 4, 1],
[3, 7, 4, 6, 8, 4, 4, 9]]], dtype=torch.float32)
out = spp(x)
markup
# 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])
Again, 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. python
# Codeblock 6
print(out)
markup
# Codeblock 6 Outputtensor([[5., 7., 5., 7., 8., 7., 5., 8., 8., 9., 9., 9., 8., 9., 9., 9., 8., 8., 9., 9., 9.]])
···
As 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.
python
# Codeblock 7a
class ZF5_SPPNet(nn.Module):
def __init__(self):
super().__init__()
self.relu = nn.ReLU()
self.conv1 = nn.Conv2d(in_channels=3,
out_channels=96,
kernel_size=7,
stride=2,
padding=0) #(1)
self.norm1 = nn.LocalResponseNorm(size=5)
self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) #(2)
self.conv2 = nn.Conv2d(in_channels=96,
out_channels=256,
kernel_size=5,
stride=2,
padding=1) #(3)
self.norm2 = nn.LocalResponseNorm(size=5)
self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=0) #(4)
self.conv3 = nn.Conv2d(in_channels=256,
out_channels=384,
kernel_size=3,
stride=1,
padding=1)
self.conv4 = nn.Conv2d(in_channels=384,
out_channels=384,
kernel_size=3,
stride=1,
padding=1)
self.conv5 = nn.Conv2d(in_channels=384,
out_channels=256,
kernel_size=3,
stride=1,
padding=1)
self.spp = SPP() #(4)
spp_out_size = 256 * sum([grid**2 for grid in GRIDS]) #(5)
self.fc6 = nn.Linear(in_features=spp_out_size, out_features=4096)
self.dropout6 = nn.Dropout(p=0.5) #(6)
self.fc7 = nn.Linear(in_features=4096, out_features=4096)
self.dropout7 = nn.Dropout(p=0.5) #(7)
self.fc8 = nn.Linear(in_features=4096, out_features=1000)
Now 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.
One 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)).
As 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.
The 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.
As 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.
python
# Codeblock 7b
def forward(self, x):
print(f'original\t\t: {x.size()}')
x = self.norm1(self.relu(self.conv1(x)))
print(f'after conv1\t\t: {x.size()}')
x = self.pool1(x)
print(f'after pool1\t\t: {x.size()}')
x = self.norm2(self.relu(self.conv2(x)))
print(f'after conv2\t\t: {x.size()}')
x = self.pool2(x)
print(f'after pool2\t\t: {x.size()}')
x = self.relu(self.conv3(x))
print(f'after conv3\t\t: {x.size()}')
x = self.relu(self.conv4(x))
print(f'after conv4\t\t: {x.size()}')
x = self.relu(self.conv5(x))
print(f'after conv5\t\t: {x.size()}\n')
x = self.spp(x)
print(f'after spp\t\t: {x.size()}\n')
x = self.dropout6(self.relu(self.fc6(x)))
print(f'after fc6\t\t: {x.size()}')
x = self.dropout7(self.relu(self.fc7(x)))
print(f'after fc7\t\t: {x.size()}')
x = self.fc8(x)
print(f'after fc8\t\t: {x.size()}')
return x
Now 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.
python
# Codeblock 8
zf5sppnet = ZF5_SPPNet()
x = torch.randn(1, 3, 224, 224)
out = zf5sppnet(x)
You 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)).
markup
# 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)
At this point we have verified the forward pass of this network. Thus, we can say that this model is now ready to be trained. ···
And 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.
I 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!
···
[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]. [2] Image created originally by author. [3] Muhammad Ardi. Medium. https://medium.com/@muhammad_ardi [Accessed October 17, 2025]. [4] MuhammadArdiPutra. SPP-Net. GitHub. https://github.com/MuhammadArdiPutra/medium_articles/blob/main/Deep Learning From Scratch/SPP-Net.ipynb [Accessed October 18, 2025].
All articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more. Register for freeSign in to your account
Written By
Deep learning storyteller See all from Muhammad Ardi Putra Deep DivesArtificial IntelligenceCnnMachine LearningObject Detection Share This Article
Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program. Write for TDS
Deep Learning Learn how to explain predictions of convolutional neural networks with PyTorch and SHAP Dario Radečić February 1, 20214 min read
Deep Learning This article will discuss the reasoning and the steps of implementing a PyTorch dataset that uses memory-mapped files Tudor Surdoiu July 25, 20224 min read
Artificial Intelligence Looking for a course in applied machine learning? Or something bit more theoretical? Start here. Dario Radečić January 13, 20205 min read
Deep Learning Or closer look at convolution for deep learning engineers Anton Lebedev August 30, 20226 min read
Artificial Intelligence Textual data carries more information than numbers Soner Yıldırım April 7, 20226 min read
Deep Learning Object Detection Models Raghav Bali February 17, 20247 min read
Deep Learning Made with Tone.js and LSTM Model Aleksandra Ma January 12, 202311 min read
Deep Learning Experiment tracking, hyperparameter optimization, private hosting Pascal Janetzky September 26, 20227 min read
Deep Learning Tips for accelerating AI/ML on CPU — Part 2 Chaim Rand December 10, 20259 min read
Your home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals. Subscribe to Our NewsletterWrite For TDS•About•Advertise•Privacy Policy•Terms of Use © Insight Media Group, LLC 2026•The New Stack•roadmap.sh•Creator Network
Close