保留正文与补采附件。源页面当前未重新核验;具体限制见交付清单。
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 Artificial Intelligence
A detailed walkthrough of the YOLOv1 architecture and its PyTorch implementation from scratch
Muhammad Ardi Putra
December 5, 202517 min read
Photo by Hassan on Unsplash
If we talk about object detection, one model that likely comes to our mind first is YOLO — well, at least for me, thanks to its popularity in the field of computer vision. The very first version of this model, referred to as YOLOv1, was released back in 2015 in the research paper titled “ You Only Look Once: Unified, Real-Time Object Detection” [1]. Before YOLOv1 was invented, one of the state-of-the-art algorithms for performing object detection was R-CNN (Region-based Convolutional Neural Network), in which it uses multi-stage mechanism to do the task. It initially employs selective search algorithm to create region proposal, then uses CNN-based model to extract the features within all these regions, and finally classifies the detected objects using SVM [2]. Here you can clearly imagine how long the process is just to perform object detection on a single image. The motivation of YOLO in the first place was to improve speed. In fact, not only achieving low computational complexity, but the authors proved that their proposed deep learning model was also able to achieve high accuracy. As this article is written, YOLOv13 has just published several days ago [3]. But let’s just talk about its very first ancestor for now so that you can see the beauty of this model starting from the time it first came out. This article is going to discuss how YOLOv1 works and how to build this neural network architecture from scratch with PyTorch. ··· Learn this step by step with the interactive AI Engineer roadmap.
Before we get into the architecture, it would be better if we understand the idea behind YOLOv1 in advance. Let’s start with an example. Suppose we have a picture of a cat, and we are about to use it as a training sample of a YOLOv1 model. And so, we need to create a ground truth for that. It is mentioned in the original paper that we need to define the parameter S , which denotes the number of grid cells we are going to divide our image into along each spatial dimension. By default, this parameter is set to 7, so we will have 7×7=49 cells in total. Take a look at Figure 1 below to better understand this idea.
Figure 1. What the image looks like after being divided into uniform-sized grid cells. The cell (3, 3) is responsible to store the information about the cat [4].
Next, we need to determine which cell corresponds to the midpoint of the object. In the above case, the cat is located almost exactly at the center of the image, hence the midpoint must lie at cell (3, 3). Later in the inference phase, we can think of this cell as the one responsible to predict the cat. Now taking a closer look at the cell, we need to determine the exact position of the midpoint. Here you can see that along the vertical axis it is located exactly in the middle, but in the horizontal axis it is slightly shifted to the left from the middle. So, if I were to approximate, the coordinate would be (0.4, 0.5). This coordinate value is relative to the cell and is normalized to the range of 0 to 1. It might be worth noting that the (x , y) coordinate of the midpoint should neither be less than 0 nor greater than 1, since a value outside this range would mean the midpoint lies in another cell. Meanwhile, the width w and the height h of the bounding box are approximately 2.4 and 3.2, respectively. These numbers are relative to the cell size, meaning that if the object is bigger than the cell, then the value will be greater than 1. Later on, if we were to create a ground truth for an image, we need to store all these x , y , w and h information in the so-called target vector.
The length of the target vector itself is 25 for each cell, in which the first 20 elements (index 0 to 19) store the class of the object in form of one-hot encoding. This is essentially because YOLOv1 was originally trained on PASCAL VOC dataset which has that number of classes. Next, index 20 is used to store the confidence of the bounding box prediction, which in the training phase this is set to 1 whenever there is an object midpoint within the cell. Lastly, the (x , y) coordinate of the midpoint are placed at indices 21 and 22, whereas w and h are stored at indices 23 and 24. The illustration in Figure 2 below displays what the target vector for cell (3, 3) looks like.
Figure 2. The target vector for cell (3, 3) of the image in Figure 1 [5].
Again, remember that the above target vector only corresponds to a single cell. To create the ground truth for the entire image, we need to have a bunch of similar vectors concatenated, forming the so-called target tensor as shown in Figure 3. Note that the class probabilities as well as the bounding box confidences, locations, and sizes from all other cells are set to zero because there is no other object appearing within the image.
Figure 3. How all target vectors from each cell are concatenated. This entire tensor will act as the ground truth of a single image [5].
The prediction vector is quite a bit different. If the target vector consists of 25 elements, the prediction vector consists of 30. This is because by default YOLOv1 predicts two bounding boxes for the same object during inference. Thus, we need 5 additional elements to store the information about the second bounding box generated by the model. Despite predicting two bounding boxes, later we will only take the one that has greater confidence.
Figure 4. The prediction vector has 5 additional elements dedicated for the second bounding box (highlighted in orange) [5].
This unique target and prediction vector dimensions required the authors to rethink the loss function. For regression problems, we typically use MAE, MSE or RMSE, whereas for classification tasks we usually use cross entropy loss. But YOLOv1 is more than just a regression and classification problem, considering that we have both continuous (bounding box) and discrete (class) values in the vector representation. Thanks to this reason, the authors created a new loss function specialized for this model as shown in Figure 5. This loss function is quite complex (you see, right?), so I decided to write it in a separate article because there are lots of things to explain about it — stay tuned, I’ll publish it very soon.
Figure 5. The loss function of YOLOv1 [1].
···
Just like typical earlier computer vision models, YOLOv1 uses CNN-based architecture as the backbone of the model. It comprises 24 convolution layers stacked according to the structure in Figure 6. If you take a closer look at the figure, you will notice that the output layer produces a tensor of shape 30×7×7. This dimension indicates that every single cell has its corresponding prediction vector of length 30 containing the class and the bounding box information of the detected object, in which this matches exactly with our previous discussion.
Figure 6. The architecture of YOLOv1 [1].
Well, I think I’ve covered all the fundamentals of YOLOv1, so now let’s start implementing the architecture from scratch with PyTorch. Before doing anything, what we need to do first is to import the required modules and initialize the parameters S , B , and C. See Codeblock 1 below.
python
# Codeblock 1
import torch
import torch.nn as nn
S = 7
B = 2
C = 20
The three parameters I initialized above are the default values given in the paper, in which S represents the number of grid cells along the horizontal and vertical axes, B denotes the number of bounding boxes generated by each cell, and C is the number of classes available in the dataset. Since we use S=7 and B=2, our YOLOv1 will produce7×7×2=98 bounding boxes in total for each image.
Next, we are going to create the ConvBlock class, in which it contains a single convolution layer (line #(1)), a leaky ReLU activation function (#(2)), and an optional maxpooling layer (#(3)) as shown in Codeblock 2.
python
# Codeblock 2
class ConvBlock(nn.Module):
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride,
padding,
maxpool_flag=False):
super().__init__()
self.maxpool_flag = maxpool_flag
self.conv = nn.Conv2d(in_channels=in_channels, #(1)
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding)
self.leaky_relu = nn.LeakyReLU(negative_slope=0.1) #(2)
if self.maxpool_flag:
self.maxpool = nn.MaxPool2d(kernel_size=2, #(3)
stride=2)
def forward(self, x):
print(f'original\t: {x.size()}')
x = self.conv(x)
print(f'after conv\t: {x.size()}')
x = self.leaky_relu(x)
print(f'after leaky relu: {x.size()}')
if self.maxpool_flag:
x = self.maxpool(x)
print(f'after maxpool\t: {x.size()}')
return x
In modern architectures, we normally use the Conv-BN-ReLU structure, but at the time YOLOv1 was created, it seems like batch normalization layer was not quite popular just yet, as it came out only several months before YOLOv1. So, I guess this is probably the reason that the authors did not utilize this normalization layer. Instead, it only uses a stack of convolutions and leaky ReLUs throughout the entire network.
Just a quick refresher, leaky ReLU is an activation function similar to the standard ReLU, except that the negative values are multiplied with a small number instead of being zeroed out. In the case of YOLOv1, we set the multiplier to 0.1 (#(2)) so that it can still preserve a little bit amount of information contained in the negative input numbers.
Figure 7. ReLU vs Leaky ReLU activation functions [6].
As the ConvBlock class has been defined, now I am going to test it just to check if it works properly. In Codeblock 3 below I try to implement the very first layer in the network and pass a dummy tensor through it. You can see in the codeblock that in_channels is set to 3 (#(1)) and out_channels is set to 64 (#(2)) because we want this initial layer to accept an RGB image as the input and return a 64-channel image. The size of the kernel is 7×7 (#(3)), hence we need to set the padding to 3 (#(5)). Normally, this configuration allows us to preserve the spatial dimension of the image, but since we use stride=2 (#(4)), this padding size ensures that the image is exactly halved. Next, if you go back to Figure 6, you will notice that some conv layers are followed by a maxpooling layer and some others are not. Since the first convolution utilizes a maxpooling layer, we need to set the maxpool_flag parameter to True (#(6)).
python
# Codeblock 3
convblock = ConvBlock(in_channels=3, #(1)
out_channels=64, #(2)
kernel_size=7, #(3)
stride=2, #(4)
padding=3, #(5)
maxpool_flag=True) #(6)
x = torch.randn(1, 3, 448, 448) #(7)
out = convblock(x)
Afterwards, we can simply generate a tensor of random values with the dimension of 1×3×448×448 (#(7)) which simulates a batch of a single RGB image of size 448×448 and then pass it through the network. You can see in the resulting output below that our convolution layer successfully increased the number of channels to 64 and halved the spatial dimension to 224×224. The halving was done once again all the way to 112×112 thanks to the maxpooling layer.
markup
# Codeblock 3 Outputoriginal : torch.Size([1, 3, 448, 448])after conv : torch.Size([1, 64, 224, 224])after leaky relu : torch.Size([1, 64, 224, 224])after maxpool : torch.Size([1, 64, 112, 112])
The next thing we are going to do is to create a sequence of ConvBlocks to build the entire backbone of the network. In case you’re still not familiar with the term backbone , in this case it is essentially everything before the two fully-connected layers (refer to Figure 6). Now look at the Codeblock 4a and 4b below to see how I define the Backbone class.
python
# Codeblock 4a
class Backbone(nn.Module):
def __init__(self):
super().__init__()
# in_channels, out_channels, kernel_size, stride, padding, maxpool_flag
self.stage0 = ConvBlock(3, 64, 7, 2, 3, maxpool_flag=True) #(1)
self.stage1 = ConvBlock(64, 192, 3, 1, 1, maxpool_flag=True) #(2)
self.stage2 = nn.ModuleList([
ConvBlock(192, 128, 1, 1, 0),
ConvBlock(128, 256, 3, 1, 1),
ConvBlock(256, 256, 1, 1, 0),
ConvBlock(256, 512, 3, 1, 1, maxpool_flag=True) #(3)
])
self.stage3 = nn.ModuleList([])
for _ in range(4):
self.stage3.append(ConvBlock(512, 256, 1, 1, 0))
self.stage3.append(ConvBlock(256, 512, 3, 1, 1))
self.stage3.append(ConvBlock(512, 512, 1, 1, 0))
self.stage3.append(ConvBlock(512, 1024, 3, 1, 1, maxpool_flag=True)) #(4)
self.stage4 = nn.ModuleList([])
for _ in range(2):
self.stage4.append(ConvBlock(1024, 512, 1, 1, 0))
self.stage4.append(ConvBlock(512, 1024, 3, 1, 1))
self.stage4.append(ConvBlock(1024, 1024, 3, 1, 1))
self.stage4.append(ConvBlock(1024, 1024, 3, 2, 1)) #(5)
self.stage5 = nn.ModuleList([])
self.stage5.append(ConvBlock(1024, 1024, 3, 1, 1))
self.stage5.append(ConvBlock(1024, 1024, 3, 1, 1))
What we do in the above codeblock is to instantiate ConvBlock instances according to the architecture given in the paper. There are several things I want to emphasize here. First, the term stage I use in the code is not explicitly mentioned in the paper. However, I decided to use that word to describe the six groups of convolutional layers in Figure 6. Second, notice that we need to set the maxpool_flag to True for the last ConvBlock in the first four groups to perform spatial downsampling (#(1–4)). For the fifth group, the downsampling is done by setting the stride of the last convolution layer to 2 (#(5)). Third, Figure 6 does not mention the padding size of the convolution layers, so we need to calculate them manually. There is indeed a specific formula to find padding size based on the given kernel size. However, I feel like it is much easier to memorize it. Just keep in mind that if we use kernel of size 7×7, then we need to set the padding to 3 to preserve the spatial dimension. Meanwhile, for 5×5, 3×3 and 1×1 kernels, the padding should be set to 2, 1, and 0, respectively.
As all layers in the backbone have been instantiated, we can now connect them all using the forward() method below. I don’t think I need to explain anything here since it basically only works by passing the input tensor x through the layers sequentially.
python
# Codeblock 4b
def forward(self, x):
print(f'original\t: {x.size()}\n')
x = self.stage0(x)
print(f'after stage0\t: {x.size()}\n')
x = self.stage1(x)
print(f'after stage1\t: {x.size()}\n')
for i in range(len(self.stage2)):
x = self.stage2[i](x)
print(f'after stage2 #{i}\t: {x.size()}')
print()
for i in range(len(self.stage3)):
x = self.stage3[i](x)
print(f'after stage3 #{i}\t: {x.size()}')
print()
for i in range(len(self.stage4)):
x = self.stage4[i](x)
print(f'after stage4 #{i}\t: {x.size()}')
print()
for i in range(len(self.stage5)):
x = self.stage5[i](x)
print(f'after stage5 #{i}\t: {x.size()}')
return x
Now let’s verify if our implementation is correct by running the following testing code. python
# Codeblock 5
backbone = Backbone()
x = torch.randn(1, 3, 448, 448)
out = backbone(x)
If you try to run the above codeblock, the following output should appear on your screen. Here you can see that the spatial dimension of the image correctly got reduced after the last ConvBlock of each stage. This process continued all the way to the last stage until eventually we obtained a tensor of size 1024×7×7, in which this matches exactly with the illustration in Figure 6.
markup
# Codeblock 5 Outputoriginal : torch.Size([1, 3, 448, 448])after stage0 : torch.Size([1, 64, 112, 112])after stage1 : torch.Size([1, 192, 56, 56])after stage2 #0 : torch.Size([1, 128, 56, 56])after stage2 #1 : torch.Size([1, 256, 56, 56])after stage2 #2 : torch.Size([1, 256, 56, 56])after stage2 #3 : torch.Size([1, 512, 28, 28])after stage3 #0 : torch.Size([1, 256, 28, 28])after stage3 #1 : torch.Size([1, 512, 28, 28])after stage3 #2 : torch.Size([1, 256, 28, 28])after stage3 #3 : torch.Size([1, 512, 28, 28])after stage3 #4 : torch.Size([1, 256, 28, 28])after stage3 #5 : torch.Size([1, 512, 28, 28])after stage3 #6 : torch.Size([1, 256, 28, 28])after stage3 #7 : torch.Size([1, 512, 28, 28])after stage3 #8 : torch.Size([1, 512, 28, 28])after stage3 #9 : torch.Size([1, 1024, 14, 14])after stage4 #0 : torch.Size([1, 512, 14, 14])after stage4 #1 : torch.Size([1, 1024, 14, 14])after stage4 #2 : torch.Size([1, 512, 14, 14])after stage4 #3 : torch.Size([1, 1024, 14, 14])after stage4 #4 : torch.Size([1, 1024, 14, 14])after stage4 #5 : torch.Size([1, 1024, 7, 7])after stage5 #0 : torch.Size([1, 1024, 7, 7])after stage5 #1 : torch.Size([1, 1024, 7, 7])
After the backbone is done, we can now move on to the fully-connected part, which I write in Codeblock 6 below. This part of the network is very simple as it mainly only consists of two linear layers. Speaking of the details, it is mentioned in the paper that the authors apply a dropout layer with the rate of 0.5 (#(3)) between the first (#(1)) and the second (#(4)) linear layers. It is important to note that the leaky ReLU activation function is still used (#(2)) but only after the first linear layer. This is because the second one acts as the output layer, hence it does not require any activation applied to it.
python
# Codeblock 6
class FullyConnected(nn.Module):
def __init__(self):
super().__init__()
self.linear0 = nn.Linear(in_features=1024*7*7, out_features=4096) #(1)
self.leaky_relu = nn.LeakyReLU(negative_slope=0.1) #(2)
self.dropout = nn.Dropout(p=0.5) #(3)
self.linear1 = nn.Linear(in_features=4096, out_features=(C+B*5)*S*S)#(4)
def forward(self, x):
print(f'original\t: {x.size()}')
x = self.linear0(x)
print(f'after linear0\t: {x.size()}')
x = self.leaky_relu(x)
x = self.dropout(x)
x = self.linear1(x)
print(f'after linear1\t: {x.size()}')
return x
Run the Codeblock 7 below to see how the tensor transforms as it is processed by the stack of linear layers. python
# Codeblock 7
fc = FullyConnected()
x = torch.randn(1, 1024*7*7)
out = fc(x)
markup
# Codeblock 7 Outputoriginal : torch.Size([1, 50176])after linear0 : torch.Size([1, 4096])after linear1 : torch.Size([1, 1470])
We can see in the above output that the fc block takes an input of shape 50176, which is essentially the flattened 1024×7×7 tensor. The linear0 layer works by mapping this input into 4096-dimensional vector, and then the linear1 layer eventually maps it further to 1470. Later in the post-processing stage we need to reshape it to 30×7×7 so that we can take the bounding box and the object classification results easily. Technically speaking, this reshaping process can be done either internally by the model or outside the model. For the sake of simplicity, I decided to leave the output flattened, meaning the reshaping will be handled externally.
At this point we already have our backbone and the fully-connected layers done. Thus, they are now ready to be assembled to construct the entire YOLOv1 architecture. There is not much thing I can explain regarding the following code, as what we do here is only instantiating both parts and connect them in the forward() method. Just don’t forget to flatten (#(1)) the output of backbone to make it compatible with the input of the fc block.
python
# Codeblock 8
class YOLOv1(nn.Module):
def __init__(self):
super().__init__()
self.backbone = Backbone()
self.fc = FullyConnected()
def forward(self, x):
x = self.backbone(x)
x = torch.flatten(x, start_dim=1) #(1)
x = self.fc(x)
return x
In order to test our model, we can simply instantiate the YOLOv1 model and pass a dummy tensor that simulates an RGB image of size 448×448 (#(1)). After feeding the tensor into the network (#(2)), I also try to simulate the post-processing step by reshaping the output tensor to 30×7×7 as shown at line #(3).
python
# Codeblock 9
yolov1 = YOLOv1()
x = torch.randn(1, 3, 448, 448) #(1)
out = yolov1(x) #(2)
out = out.reshape(-1, C+B*5, S, S) #(3)
And below is what the output looks like after the code above is run. Here you can see that our input tensor successfully flows through all layers within the entire network, indicating that our YOLOv1 model works properly and thus is ready to train. markup
# Codeblock 9 Outputoriginal : torch.Size([1, 3, 448, 448])after stage0 : torch.Size([1, 64, 112, 112])after stage1 : torch.Size([1, 192, 56, 56])after stage2 #0 : torch.Size([1, 128, 56, 56])after stage2 #1 : torch.Size([1, 256, 56, 56])after stage2 #2 : torch.Size([1, 256, 56, 56])after stage2 #3 : torch.Size([1, 512, 28, 28])after stage3 #0 : torch.Size([1, 256, 28, 28])after stage3 #1 : torch.Size([1, 512, 28, 28])after stage3 #2 : torch.Size([1, 256, 28, 28])after stage3 #3 : torch.Size([1, 512, 28, 28])after stage3 #4 : torch.Size([1, 256, 28, 28])after stage3 #5 : torch.Size([1, 512, 28, 28])after stage3 #6 : torch.Size([1, 256, 28, 28])after stage3 #7 : torch.Size([1, 512, 28, 28])after stage3 #8 : torch.Size([1, 512, 28, 28])after stage3 #9 : torch.Size([1, 1024, 14, 14])after stage4 #0 : torch.Size([1, 512, 14, 14])after stage4 #1 : torch.Size([1, 1024, 14, 14])after stage4 #2 : torch.Size([1, 512, 14, 14])after stage4 #3 : torch.Size([1, 1024, 14, 14])after stage4 #4 : torch.Size([1, 1024, 14, 14])after stage4 #5 : torch.Size([1, 1024, 7, 7])after stage5 #0 : torch.Size([1, 1024, 7, 7])after stage5 #1 : torch.Size([1, 1024, 7, 7])original : torch.Size([1, 50176])after linear0 : torch.Size([1, 4096])after linear1 : torch.Size([1, 1470])torch.Size([1, 30, 7, 7])
···
It might be worth noting that all the codes I show you throughout this entire article is for the base YOLOv1 architecture. It is mentioned in the paper that the authors also proposed the lite version of this model which they refer to as Fast YOLO. This smaller YOLOv1 version offers faster computation time as it only consists of 9 convolution layers instead of 24. Unfortunately, the paper does not provide the implementation details, so I cannot demonstrate you how to implement that one. Here I encourage you to play around with the above code. In theory, it is possible to replace the CNN-based backbone with other deep learning models, such as ResNet, ResNeXt, ViT, etc. All you need to do is just to match the output shape of the backbone with the input shape of the fully-connected part. Not only that, I also want you to try training this model from scratch. But if you decided to do so, you might probably want to make this model smaller by reducing the depth (no of convolution layers) or the width (no of kernels) of the model. This is essentially because the authors mentioned that they required around a week just to do the pretraining on ImageNet dataset, not to mention the time for fine tuning on the object detection task. And well, I think that’s pretty much everything I can explain you about how YOLOv1 works and its architecture. Please let me know if you spot any mistake in this article. Thank you! By the way, the code used in this article is also available on my GitHub repo [7]. ···
[1] Joseph Redmon et al. You Only Look Once: Unified, Real-Time Object Detection. Arxiv. https://arxiv.org/pdf/1506.02640 [Accessed July 5, 2025]. [2] Ross Girshick et al. Rich feature hierarchies for accurate object detection and semantic segmentation. Arxiv. https://arxiv.org/pdf/1311.2524 [Accessed July 5, 2025]. [3] Mengqi Lei et al. YOLOv13: Real-Time Object Detection with Hypergraph-Enhanced Adaptive Visual Perception. Arxiv. https://arxiv.org/abs/2506.17733 [Accessed July 5, 2025]. [4] Image generated by author with Gemini, edited by author. [5] Image originally created by author. [6] Bing Xu et al. Empirical Evaluation of Rectified Activations in Convolutional Network. Arxiv. https://arxiv.org/pdf/1505.00853 [Accessed July 5, 2025]. [7] MuhammadArdiPutra. The Day YOLO First Saw the World — YOLOv1. GitHub. https://github.com/MuhammadArdiPutra/medium_articles/blob/main/The Day YOLO First Saw the World - YOLOv1.ipynb [Accessed July 7, 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 DivesDeep LearningObject DetectionPythonYolo 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
Data Science Intro Efe Buyuk November 24, 20204 min read
Artificial Intelligence So, I was revising machine learning algorithms yesterday; and I observed that SVM is probably a widely used but complicated machine... Satyam Mishra October 29, 20205 min read
Data Science CAUSAL DATA SCIENCE How to use regression trees to estimate heterogeneous treatment effects In causal... Matteo Courthoud February 3, 202315 min read
Artificial Intelligence A quick chat with IBM researchers Pin-Yu and Sijia about their recent papers with the two concepts of 'Pruning' and 'Block Changing' Alex Moltzau August 15, 20190 min read
Artificial Intelligence Pandas offer a great deal of flexibility. Soner Yıldırım September 11, 20204 min read
Artificial Intelligence Which advertising spendings are really driving your sales? Robert Kübler September 22, 20218 min read
Deep Learning A comprehensive and detailed formalization of multi-head attention. Jean Meunier-Pion June 11, 20249 min read
Step by step guide on how to integrate your private PyPI repositories on Databricks clusters. David Suarez October 4, 202110 min read
Data Science How python can help you find the perfect surfboard Businesses pay astronomical amounts on data... Nikki Smit May 28, 20215 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