Exercise (Designing a Darknet-19 Architecture)

Learn how to design the architecture of Darknet-19, a building block of YOLO architectures.

Let’s dive into the practical applications of various architectural concepts, like the backbone and neck.

Supplemental reading materials

  • YOLO v1 by Joseph Redmon, Santosh Divvala, Ali Farhadi, and Ross Girshick is the first research paper of the YOLO series, which is an easy-to-read paper and highly recommended to get an in-depth knowledge of YOLO fundamentals.1

  • Darknet-19 is a backbone that was used in YOLOv2. The YOLOv2 paper contains the architecture of the backbone, which helps in implementing the backbone from scratch. This is an interesting paper that explains in detail the approaches tried before finalizing the YOLOv2 model architecture and training strategies.2


This task will help you gain in-depth knowledge of one of the most important parts of any object detection network, which is the backbone.

Design specifications

  • Convolutional layers: The architecture should have a total of 17 convolutional layers. Each convolutional layer should be followed by a batch normalization layer and a LeakyReLU activation.

  • Pooling layers: Introduce max-pooling layers after specific convolutional layers to reduce spatial dimensions. Pooling layers need to be added for the following layers: 1, 2, 5, 8, and 11.

  • Final layers: After the convolutional blocks, incorporate a global average pooling layer to reduce spatial dimensions to 1 × 1. This should be followed by a fully connected layer for classification.


A class named Darknet19 that inherits from the nn.Module has been created to define the architecture.

  1. Setting up: Start by importing the necessary PyTorch libraries: torch and torch.nn.

  2. Creating a convolutional block helper function:

    1. Inside the Darknet19 class, define a helper function named conv_block. This function will help create convolutional blocks for the architecture.

    2. The function should take the following parameters: in_channels, out_channels, kernel_size, stride=1, and max_pool=False.

    3. The function should return a sequence of layers: a convolutional layer, batch normalization, LeakyReLU activation, and, optionally, a max-pooling layer.

  3. Implementing the darknet-19 architecture:

    1. Using the conv_block helper function, design the Darknet-19 architecture with 19 layers. Follow the sequence and configurations as mentioned in the design specification.

    2. Ensure that you understand the role of each layer and its parameters.

  4. Final layers:

    1. After the layers, add a fully connected layer and a global average pooling layer to complete the architecture.

  5. Forward pass:

    1. Define the forward method to pass the input tensor through the Darknet-19 architecture.

    2. Ensure the tensor flows through all layers in the correct sequence.

Get hands-on with 1200+ tech skills courses.