【YOLO v1】模型搭建 | model | 代码

YOLO V1 模型

在这里插入图片描述

import torch
import torch.nn as nn
from torchsummary import summary


def build_block(in_channel, out_channel, kernel_size, stride=1, maxpool=False):
    padding = kernel_size//2
    block = nn.Sequential(nn.Conv2d(in_channel, out_channel, kernel_size=kernel_size, stride=stride, padding=padding, bias=False),
                          nn.BatchNorm2d(out_channel),
                          nn.LeakyReLU(0.1, inplace=True))
    if maxpool:
        block.add_module("maxpool2d", nn.MaxPool2d(kernel_size=2, stride=2))
    return block


class YOLOv1(nn.Module):
    def __init__(self, num_classes=20):
        super(YOLOv1, self).__init__()
        self.num_classes = num_classes
        self.layer1 = nn.Sequential(build_block(3, 64, 7, stride=2, maxpool=True))
        self.layer2 = nn.Sequential(build_block(64, 192, 3, maxpool=True))

        self.layer3 = nn.Sequential(build_block(192, 128, 1),
                                    build_block(128, 256, 3),
                                    build_block(256, 256, 1),
                                    build_block(256, 512, 3, maxpool=True))

        self.layer4 = nn.Sequential(build_block(512, 256, 1),
                                    build_block(256, 512, 3),
                                    build_block(512, 256, 1),
                                    build_block(256, 512, 3),
                                    build_block(512, 256, 1),
                                    build_block(256, 512, 3),
                                    build_block(512, 256, 1),
                                    build_block(256, 512, 3),
                                    build_block(512, 512, 1),
                                    build_block(512, 1024, 3, maxpool=True))

        self.layer5 = nn.Sequential(build_block(1024, 512, 1),
                                    build_block(512, 1024, 3),
                                    build_block(1024, 512, 1),
                                    build_block(512, 1024, 3),
                                    build_block(1024, 1024, 3),
                                    build_block(1024, 1024, 3, stride=2))

        self.layer6 = nn.Sequential(build_block(1024, 1024, 3),
                                    build_block(1024, 1024, 3))

        self.layer7 = nn.Sequential(nn.Flatten(),
                                    nn.Linear(7 * 7 * 1024, 4096, bias=True),
                                    nn.LeakyReLU(0.1),
                                    nn.Dropout(p=0.5),
                                    nn.Linear(4096, 7 * 7 * (self.num_classes + 5 * 2), bias=True),
                                    nn.Sigmoid(),
                                    )

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.layer5(x)
        x = self.layer6(x)
        x = self.layer7(x)
        x = x.view(-1, 7, 7, self.num_classes + 5 * 2)
        return x


if __name__ == '__main__':
    net = YOLOv1()
    print(summary(net, (3, 448, 448)))

    x = torch.randn((1, 3, 448, 448))
    print(net(x).shape)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Enzo 想砸电脑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值