I wrote in regards to the FPN (Characteristic Pyramid Community) structure [1], which is likely one of the most influential necks we are able to apply to a spine mannequin. FPN was first launched to reinforce the potential of an object detection mannequin to detect small objects. Nonetheless, Liu et al. in 2018 discovered that the data movement of FPN had room for enchancment. So, they determined to deal with this hole by proposing PANet of their analysis paper titled “Path Aggregation Community for Occasion Segmentation” [2], which we’re going to focus on on this article.
A Little Bit About FPN
PANet is constructed on high of the FPN structure, so I believe it will be a good suggestion to speak slightly bit about how FPN works upfront. In a CNN-based spine mannequin, the deeper function maps have completely different traits from the shallower ones. The function maps from deeper layers are likely to have excessive semantic data, but it doesn’t have that a lot quantity of spatial data. Conversely, function maps from shallower layers comprise extra spatial data however have much less semantic data. These information basically inform us that we should always use the deeper function maps if we had been to foretell massive objects and use the shallower function maps for predicting small objects.
Nonetheless, the authors of PANet discovered that it’s not fairly applicable to straight take data from shallower function maps to detect small objects. It’s because these function maps comprise minimal quantity of semantic data, which basically implies that they don’t have an excellent understanding in regards to the content material of the picture. FPN solves this difficulty by combining function maps from the deeper layers and the shallower ones. By doing so, we are able to mainly inject semantic data into the shallower function maps that they beforehand didn’t have.
What FPN Solves
Now check out the illustration in Determine 1 beneath to raised perceive this concept. The upward movement on the left is the principle spine mannequin, whereas the one going downwards on the suitable (a.ok.a. the top-down pathway) is the FPN. The horizontal arrows that go on to the FPN half (known as lateral connections) are the tensors to be mixed with the one from the deeper layers. On this illustration, the FPN produces three function maps the place the detection head will make the bounding field predictions from.

The blue borderlines within the illustration above point out the quantity of semantic data contained throughout the corresponding function map. You’ll be able to see within the authentic spine that the borderline will get thicker as we get deeper into the community, which means that the function map that has the best semantic data is the one produced by the deepest layer. Through the use of function map mixture mechanism proposed by FPN, we are able to see that the shallower function maps at the moment are wealthy of semantic data which makes them appropriate for detecting small objects precisely.
What FPN Doesn’t Remedy
At this level we’d assume that FPN is simply fantastic because it is ready to permit a mannequin to detect small objects higher in comparison with the plain spine mannequin. In reality, the authors of PANet nonetheless see one thing that isn’t solved by FPN simply but, which as I’ve talked about earlier is said to the data movement drawback. Now let’s check out Determine 2 beneath.

On this determine, the part known as (a) is the spine mannequin that has been outfitted with FPN, which is mainly the identical construction I confirmed you in Determine 1. In FPN, function maps P₅, P₄, P₃, and P₂ change into the premise of the detection head to make predictions, the place P₂ has a excessive semantic data because of the movement within the top-down pathway. And right here’s the issue FPN doesn’t remedy: whereas FPN successfully enriches function maps in shallower layers, the deeper function maps nonetheless lack spatial data. To start with of this text I mentioned that the deeper and the shallower function maps in a spine have their very own benefit and downside, and FPN solely solves the downside of the shallower function maps whereas nonetheless leaving the issue of the deeper function maps untouched. Based mostly on this notion, the authors of PANet thought that we might nonetheless enhance the flexibility of an object detection mannequin in detecting massive objects by injecting spatial data to the deeper function maps, which additionally doubtlessly permits the general accuracy of the detection mannequin to get even increased.
It’s positively regular for a deeper layer to lose spatial data because of the downsampling mechanism within the spine. Nonetheless, that is really not solely attributable to the downsampling itself. As a substitute, the lengthy data journey within the stack of convolution layers additionally causes spatial data to degrade. So, if we can’t omit the downsampling operation throughout the spine, we are able to nonetheless decrease this spatial data degradation by creating shortcut paths — similar to the skip-connections in ResNet.
How PANet Solves the Drawback Unsolved by FPN
The authors of PANet proposed a intelligent thought, the place they added a stack of a number of convolution layers positioned on high of the prevailing FPN alongside the lateral connections (the half known as (b) in Determine 2). It may appear counterintuitive at look — how can including extra layers permit us to get shorter paths as a substitute? In reality, this method is legitimate because it actually permits the shallower function maps to ship spatial data to the deeper layers extra seamlessly.
Now let’s return to Determine 2 and take note of the dashed arrows. Each arrows basically denote the movement of the C₂ tensor (the spine function on the similar spatial decision as P₂ and N₂) to the deepest layer. The purple one is after we solely use the top-down pathway of FPN, whereas the inexperienced one is after we additionally use the bottom-up path augmentation of PANet.
If we don’t make the most of PANet, the one method for the C₂ tensor to get into the final layer P₅ is by touring up by means of the complete spine one after the other, which may really be over 100 layers in whole. And as we mentioned earlier, this causes degradation within the spatial data. If we evaluate this with the movement after we use PANet, it basically permits us to make use of the trail traced by the inexperienced arrow. Understand that the variety of layers throughout the top-down and the bottom-up pathways are a lot lower than the one within the spine, therefore decreasing the trail from 100+ layers to round 10 layers to succeed in N₅ (which is in the identical degree as P₅). By doing this, we are able to positively protect way more spatial data. And so, by combining FPN and PANet, we get bidirectional data movement that enriches shallower layers with semantic data and deeper layers with spatial data.
By the way in which, on this article we are going to concentrate on how we are able to combine the (a) and (b) components within the structure, which is the core contribution that has been extensively adopted in trendy architectures. The complete PANet paper really additionally contains the adaptive pooling mechanism (c), bounding field prediction for object detection (d), and fully-connected function fusion for segmentation (e), that are past our scope right here.
The Detailed PANet Structure
In FPN, the P₅, P₄, P₃, and P₂ tensors will straight be forwarded to the detection head, whereas in PANet, these 4 tensors might be transferred to the bottom-up path augmentation which then produces the N₅, N₄, N₃, and N₂ tensors. These N tensors are those for use by the detection head as the premise of the article detection prediction.
Typically talking, the way in which PANet performs function mixture is much like FPN but is finished in reversed method. Check out Determine 3 beneath to see the detailed course of.

What we initially do within the above constructing block is to course of the Nᵢ tensor with a 3×3 convolution. The stride of this conv layer is ready to 2 since we would like it to scale back the spatial dimension by half. Subsequent, we mix the Pᵢ₊₁ tensor and the downsampled Nᵢ tensor utilizing element-wise summation. This mixed tensor is then processed by one other 3×3 convolution layer. This time we don’t need this layer to scale back the spatial dimension any additional, so we have to set the stride to 1. Lastly, as all processes above are carried out, we can have the Nᵢ₊₁ tensor, which is able to be processed once more with the identical process till we finally get all of the N₅, N₄, N₃, and N₂ tensors.
There are literally a number of belongings you want to remember right here. First, since N₂ is the very first tensor within the bottom-up path augmentation, there’s really nothing to mix to acquire this tensor. Thus, we are able to merely understand P₂ because the N₂ tensor itself. Second, right here we have to put a ReLU activation operate after every convolution layer. In reality, that is completely different from FPN which utterly omits the activation capabilities. So, later within the implementation, our top-down pathway won’t use any activation operate in any respect, but we can have all of the convs within the bottom-up path augmentation outfitted with ReLU. And third, all tensors all through the complete bottom-up path augmentation have precisely 256 channels, which means that the convolution layers right here don’t change the variety of channels in any respect.
That’s just about all in regards to the detailed movement of the PANet structure. Within the subsequent part I’m going to show methods to implement it from scratch.
PANet From Scratch
The thought of our implementation right here is that we’ll create a dummy spine with an FPN and place the PANet layers on high of it. As standard, what we have to do first within the code is to import the required modules. See Codeblock 1 beneath.
# Codeblock 1
import torch
import torch.nn as nn
CNN Implementation
What I really imply by a dummy spine is a quite simple CNN mannequin. In reality the CNN class in Codeblock 2 beneath is precisely the identical because the one in my FPN article [1]. Simply remember that the precise spine used within the paper is ResNet, however because the focus of this text is PANet, I’ll use a typical CNN as a substitute. (By the way in which, I really received a separate article about ResNet. Test that out on the hyperlink in reference [4] in case you’re to implement FPN on ResNet from scratch.)
# Codeblock 2
class CNN(nn.Module):
def __init__(self):
tremendous().__init__()
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, padding=1)
self.conv3 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1)
self.conv4 = nn.Conv2d(in_channels=512, out_channels=1024, kernel_size=3, padding=1)
self.conv5 = nn.Conv2d(in_channels=1024, out_channels=2048, kernel_size=3, padding=1)
def ahead(self, x):
print(f'originalt: {x.dimension()}n')
x = self.relu(self.conv1(x))
print(f'after conv1t: {x.dimension()}')
x = self.maxpool(x)
print(f'after poolt: {x.dimension()}n')
x = self.relu(self.conv2(x))
print(f'after conv2t: {x.dimension()}')
x = self.maxpool(x)
print(f'after pool (c2)t: {x.dimension()}n')
c2 = x.clone()
x = self.relu(self.conv3(x))
print(f'after conv3t: {x.dimension()}')
x = self.maxpool(x)
print(f'after pool (c3)t: {x.dimension()}n')
c3 = x.clone()
x = self.relu(self.conv4(x))
print(f'after conv4t: {x.dimension()}')
x = self.maxpool(x)
print(f'after pool (c4)t: {x.dimension()}n')
c4 = x.clone()
x = self.relu(self.conv5(x))
print(f'after conv5t: {x.dimension()}')
c5 = self.maxpool(x)
print(f'after pool (c5)t: {c5.dimension()}n')
return c2, c3, c4, c5
The CNN class above returns a number of outputs: c2, c3, c4 and c5, the place c5 is the tensor from the deepest layer. So as to verify if this class works correctly, we are going to check it by passing a tensor by means of it. The dummy tensor I initialize in Codeblock 3 beneath has the dimension of three×224×224, which matches the enter form of the ResNet mannequin.
# Codeblock 3
cnn = CNN()
x = torch.randn(1, 3, 224, 224)
out_cnn = cnn(x)
c2, c3, c4, c5 = out_cnn
Beneath you may see how the spine mannequin transforms our enter tensor all through the community. In a while, all of the C tensors might be used because the enter of our FPN.
# Codeblock 3 Output
authentic : torch.Measurement([1, 3, 224, 224])
after conv1 : torch.Measurement([1, 64, 224, 224])
after pool : torch.Measurement([1, 64, 112, 112])
after conv2 : torch.Measurement([1, 256, 112, 112])
after pool (c2) : torch.Measurement([1, 256, 56, 56])
after conv3 : torch.Measurement([1, 512, 56, 56])
after pool (c3) : torch.Measurement([1, 512, 28, 28])
after conv4 : torch.Measurement([1, 1024, 28, 28])
after pool (c4) : torch.Measurement([1, 1024, 14, 14])
after conv5 : torch.Measurement([1, 2048, 14, 14])
after pool (c5) : torch.Measurement([1, 2048, 7, 7])
FPN Implementation
And right here’s what the FPN class appears to be like like. I’m not going to get very deep into the movement of this community since I’ve coated it totally in my earlier article [1].
# Codeblock 4
class FPN(nn.Module):
def __init__(self):
tremendous().__init__()
self.upsample = nn.Upsample(scale_factor=2, mode='nearest') #(1)
self.lateral_c5 = nn.Conv2d(in_channels=2048, out_channels=256, kernel_size=1)
self.lateral_c4 = nn.Conv2d(in_channels=1024, out_channels=256, kernel_size=1)
self.lateral_c3 = nn.Conv2d(in_channels=512, out_channels=256, kernel_size=1)
self.lateral_c2 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=1)
self.smooth_m4 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.smooth_m3 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.smooth_m2 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
def ahead(self, c2, c3, c4, c5):
m5 = self.lateral_c5(c5)
p5 = m5
m4 = self.upsample(m5) + self.lateral_c4(c4)
p4 = self.smooth_m4(m4)
m3 = self.upsample(m4) + self.lateral_c3(c3)
p3 = self.smooth_m3(m3)
m2 = self.upsample(m3) + self.lateral_c2(c2)
p2 = self.smooth_m2(m2)
return p2, p3, p4, p5
For now, let’s simply concentrate on the returned P tensors which we are going to use because the enter of our PANet. You’ll be able to see within the output of the code beneath that each one these P tensors have 256 channels, which I consider is why the authors finally ended up with this variety of channels for the complete PANet construction.
# Codeblock 5
fpn = FPN()
out_fpn = fpn(c2, c3, c4, c5)
p2, p3, p4, p5 = out_fpn
print(f'p2: {p2.dimension()}')
print(f'p3: {p3.dimension()}')
print(f'p4: {p4.dimension()}')
print(f'p5: {p5.dimension()}')
# Codeblock 5 Output
p2: torch.Measurement([1, 256, 56, 56])
p3: torch.Measurement([1, 256, 28, 28])
p4: torch.Measurement([1, 256, 14, 14])
p5: torch.Measurement([1, 256, 7, 7])
PANet Implementation
And at last, it’s time to implement the PANet itself. In Codeblock 6 beneath, what we do first is to initialize the downsample_n{2,3,4} layers. Because the identify suggests, these layers are chargeable for performing spatial downsampling, which is the rationale that we set the stride parameter of those layers to 2. Subsequent, we initialize conv_{n2down_p3, n3down_p4, n4down_p5}. These three convolution layers are used to course of the mixed tensors. As an example, the layer with suffix n2down_p3 will take the mixed downsampled N₂ and the P₃ tensors for the enter. Don’t neglect to initialize the ReLU activation operate as we are going to use them after every convolution layer.
# Codeblock 6
class PANet(nn.Module):
def __init__(self):
tremendous().__init__()
self.downsample_n2 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=2, padding=1)
self.downsample_n3 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=2, padding=1)
self.downsample_n4 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=2, padding=1)
self.conv_n2down_p3 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.conv_n3down_p4 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.conv_n4down_p5 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.relu = nn.ReLU()
def ahead(self, p2, p3, p4, p5):
n2 = p2 #(1)
print(f'n2tt: {n2.dimension()}n')
#######################################
n2down = self.relu(self.downsample_n2(n2)) #(2)
print(f'n2 downsampledt: {n2down.dimension()}')
n2down_p3 = n2down + p3 #(3)
print(f'after sumt: {n2down_p3.dimension()}')
n3 = self.relu(self.conv_n2down_p3(n2down_p3)) #(4)
print(f'n3tt: {n3.dimension()}n')
#######################################
n3down = self.relu(self.downsample_n3(n3))
print(f'n3 downsampledt: {n3down.dimension()}')
n3down_p4 = n3down + p4
print(f'after sumt: {n3down_p4.dimension()}')
n4 = self.relu(self.conv_n3down_p4(n3down_p4))
print(f'n4tt: {n4.dimension()}n')
#######################################
n4down = self.relu(self.downsample_n4(n4))
print(f'n4 downsampledt: {n4down.dimension()}')
n4down_p5 = n4down + p5
print(f'after sumt: {n4down_p5.dimension()}')
n5 = self.relu(self.conv_n4down_p5(n4down_p5))
print(f'n5tt: {n5.dimension()}')
return n2, n3, n4, n5
Now let’s transfer on to the ahead() technique. You’ll be able to see right here that we settle for all P tensors from FPN because the enter. As I’ve talked about earlier, we don’t have to do something with the P₂ tensor since its corresponding N tensor is the very first one within the bottom-up path augmentation (#(1)). Subsequent, on the line marked with #(2) we downsample the n2 tensor such that its spatial dimension halves. This downsampling course of is vital since we would like the n2 tensor to have the very same dimension as p3 to permit element-wise summation to be carried out (#(3)). Afterwards, this mixed tensor is processed additional by one other convolution layer (#(4)). These steps are repeated a number of instances till we finally attain n5.
Now let’s check our code above by initializing a PANet occasion and cross the P tensors we obtained from our earlier output by means of the community.
# Codeblock 7
panet = PANet()
out_panet = panet(p2, p3, p4, p5)
n2, n3, n4, n5 = out_panet
And beneath is what the output appears to be like like. It looks like our PANet works correctly because it efficiently passes the enter tensor all the way in which to the top of the community. It is usually seen right here that whereas the spatial dimension will get smaller, the variety of function maps in every step stays the identical, which is precisely what we would like.
# Codeblock 7 Output
n2 : torch.Measurement([1, 256, 56, 56])
n2 downsampled : torch.Measurement([1, 256, 28, 28])
after sum : torch.Measurement([1, 256, 28, 28])
n3 : torch.Measurement([1, 256, 28, 28])
n3 downsampled : torch.Measurement([1, 256, 14, 14])
after sum : torch.Measurement([1, 256, 14, 14])
n4 : torch.Measurement([1, 256, 14, 14])
n4 downsampled : torch.Measurement([1, 256, 7, 7])
after sum : torch.Measurement([1, 256, 7, 7])
n5 : torch.Measurement([1, 256, 7, 7])
All the pieces in a Single Go
Lastly, let’s attempt to wrap every part in a single class which I check with as BackboneNeck. The way in which to do this can be very easy, all we have to do within the __init__() technique is simply to initialize the dummy CNN, FPN, and the PANet we created earlier. Subsequent, within the ahead() technique, we are able to simply cross the tensors sequentially. Right here I additionally print out the form of the tensors within the intermediate steps so as to clearly see how the tensors rework after every a part of the community. The outputs of this BackboneNeck are the N tensros, that are able to be forwarded to a detection head.
# Codeblock 8
class BackboneNeck(nn.Module):
def __init__(self):
tremendous().__init__()
self.cnn = CNN()
self.fpn = FPN()
self.panet = PANet()
def ahead(self, x):
c2, c3, c4, c5 = self.cnn(x)
print(f'c2: {c2.dimension()}')
print(f'c3: {c3.dimension()}')
print(f'c4: {c4.dimension()}')
print(f'c5: {c5.dimension()}n')
p2, p3, p4, p5 = self.fpn(c2, c3, c4, c5)
print(f'p2: {p2.dimension()}')
print(f'p3: {p3.dimension()}')
print(f'p4: {p4.dimension()}')
print(f'p5: {p5.dimension()}n')
n2, n3, n4, n5 = self.panet(p2, p3, p4, p5)
print(f'n2: {n2.dimension()}')
print(f'n3: {n3.dimension()}')
print(f'n4: {n4.dimension()}')
print(f'n5: {n5.dimension()}')
return n2, n3, n4, n5
Now let’s check our BackboneNeck class above utilizing the next testing code.
# Codeblock 9
backbone_neck = BackboneNeck()
x = torch.randn(1, 3, 224, 224)
n2, n3, n4, n5 = backbone_neck(x)
Right here we are able to see that our dummy RGB picture of dimension 224×224 efficiently flows by means of the complete community. The C tensors produced by the spine initially have various numbers of channels (#(1–4)). Our FPN then normalizes them to a uniform 256 channels whereas enriching the shallower layers with semantic data (#(5–8)). We additional course of the ensuing P tensors with PANet to inject spatial data to the deeper layers, ensuing within the N tensors whereas sustaining the identical spatial dimensions as their corresponding P tensors (#(9–12)).
# Codeblock 9 Output
authentic : torch.Measurement([1, 3, 224, 224])
after conv1 : torch.Measurement([1, 64, 224, 224])
after pool : torch.Measurement([1, 64, 112, 112])
after conv2 : torch.Measurement([1, 256, 112, 112])
after pool (c2) : torch.Measurement([1, 256, 56, 56])
after conv3 : torch.Measurement([1, 512, 56, 56])
after pool (c3) : torch.Measurement([1, 512, 28, 28])
after conv4 : torch.Measurement([1, 1024, 28, 28])
after pool (c4) : torch.Measurement([1, 1024, 14, 14])
after conv5 : torch.Measurement([1, 2048, 14, 14])
after pool (c5) : torch.Measurement([1, 2048, 7, 7])
c2: torch.Measurement([1, 256, 56, 56]) #(1)
c3: torch.Measurement([1, 512, 28, 28]) #(2)
c4: torch.Measurement([1, 1024, 14, 14]) #(3)
c5: torch.Measurement([1, 2048, 7, 7]) #(4)
p2: torch.Measurement([1, 256, 56, 56]) #(5)
p3: torch.Measurement([1, 256, 28, 28]) #(6)
p4: torch.Measurement([1, 256, 14, 14]) #(7)
p5: torch.Measurement([1, 256, 7, 7]) #(8)
n2: torch.Measurement([1, 256, 56, 56]) #(9)
n3: torch.Measurement([1, 256, 28, 28]) #(10)
n4: torch.Measurement([1, 256, 14, 14]) #(11)
n5: torch.Measurement([1, 256, 7, 7]) #(12)
Ending
And that’s every part about how PANet works, particularly relating to the way in which it enriches spatial data within the deeper layers. There are literally a number of different further parts of PANet I haven’t coated on this article, i.e., the (c), (d), and (e) parts in Determine 2, which I believe I’ll put it aside for my upcoming articles.
Thanks very a lot for studying. You can too discover the code used on this article in my GitHub repo [5]. Please let me know if there are errors within the code or in my rationalization. See ya in my subsequent article!
References
[1] Muhammad Ardi. FPN Paper Walkthrough: Leveraging the Inner Pyramid. https://towardsdatascience.com/fpn-paper-walkthrough-leveraging-the-internal-pyramid/ [Accessed July 3, 2026].
[2] Shu Liu et al. Path Aggregation Community for Occasion Segmentation. Arxiv. https://arxiv.org/abs/1803.01534 [Accessed October 8, 2025].
[3] Tsung-Yi Lin et al. Characteristic Pyramid Networks for Object Detection. Arxiv. https://arxiv.org/abs/1612.03144 [Accessed October 8, 2025].
[4] Muhammad Ardi. Paper Walkthrough: Residual Community (ResNet). Python in Plain English. https://medium.com/python-in-plain-english/paper-walkthrough-residual-network-resnet-62af58d1c521 [Accessed October 8, 2025].
[5] MuhammadArdiPutra. PANet. GitHub. https://github.com/MuhammadArdiPutra/medium_articles/blob/essential/Deeppercent20Learningpercent20Frompercent20Scratch/PANet.ipynb [Accessed October 8, 2025].





