Yolov4网络代码

from collections import OrderedDict
import torch
import torch.nn as nn
from Darknet_53 import darknet53 def conv(in_channels, out_channels, kernel_size, stride=1):
pad = (kernel_size-1)//2 if kernel_size else 0
return nn.Sequential(OrderedDict(
[
("conv", nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=pad)),
("bn", nn.BatchNorm2d(out_channels)),
("relu", nn.LeakyReLU(0.1))
]
))
class SPP(nn.Module):
def __init__(self, pool_sizes=[5, 9, 13]):
super(SPP, self).__init__()
self.maxpools = nn.ModuleList([nn.MaxPool2d(pool_size, 1, pool_size//2) for pool_size in pool_sizes])
def forward(self, x):
features = [maxpool(x) for maxpool in self.maxpools[::-1]]
features = torch.cat(features + [x], dim=1)
return features
class Upsample(nn.Module):
def __init__(self, in_channels, out_channels):
super(Upsample, self).__init__()
self.upsample = nn.Sequential(
conv(in_channels=in_channels, out_channels=out_channels,kernel_size=1),
nn.Upsample(scale_factor=2, mode="nearest")
)
def forward(self, x):
x = self.upsample(x)
return x
def conv_three(channels_list, in_channels):
m = nn.Sequential(
conv(in_channels=in_channels, out_channels=channels_list[0], kernel_size=1),
conv(in_channels=channels_list[0], out_channels=channels_list[1], kernel_size=3),
conv(in_channels=channels_list[1], out_channels=channels_list[0], kernel_size=1)
)
return m
def conv_five(channels_list, in_channels):
m = nn.Sequential(
conv(in_channels=in_channels, out_channels=channels_list[0], kernel_size=1),
conv(in_channels=channels_list[0], out_channels=channels_list[1], kernel_size=3),
conv(in_channels=channels_list[1], out_channels=channels_list[0], kernel_size=1),
conv(in_channels=channels_list[0], out_channels=channels_list[1], kernel_size=3),
conv(in_channels=channels_list[1], out_channels=channels_list[0], kernel_size=1)
)
return m
def Yolov4_head(channels_list, in_channels):
m = nn.Sequential(
conv(in_channels=in_channels, out_channels=channels_list[0], kernel_size=3),
conv(in_channels=channels_list[0], out_channels=channels_list[1], kernel_size=1)
)
return m
class YoloBody(nn.Module):
def __init__(self, anchors_mask, num_classes, pretrained = False):
super(YoloBody, self).__init__()
self.backbone = darknet53(pretrained) self.conv1=conv_three(channels_list=[512, 1024], in_channels=1024)
self.spp = SPP()
self.conv2=conv_three(channels_list=[512, 1024], in_channels=2048) self.upsample1 = Upsample(512, 256)
self.conv_for_p4 = conv(in_channels=512, out_channels=256, kernel_size=1)
self.make_five_conv1=conv_five(channels_list=[256, 512], in_channels=512) self.upsample2 = Upsample(in_channels=256, out_channels=128)
self.conv_for_p3=conv(in_channels=256, out_channels=128, kernel_size=1)
self.make_five_conv2=conv_five(channels_list=[128, 256], in_channels=256) # 3*(5+num_classes) = 3*(5+20) = 3*(4+1+20)=75
self.yolo_head3=Yolov4_head(channels_list= [256, len(anchors_mask[0]) * (5 + num_classes)], in_channels=128) self.down_sample1 = conv(in_channels=128, out_channels=256, kernel_size=3, stride=2)
self.make_five_conv3 = conv_five(channels_list=[256, 512], in_channels=512) # 3*(5+num_classes) = 3*(5+20) = 3*(4+1+20)=75
self.yolo_head2 = Yolov4_head(channels_list=[512, len(anchors_mask[1]) * (5 + num_classes)], in_channels=256) self.down_sample2 = conv(in_channels=256, out_channels=512, kernel_size=3, stride=2)
self.make_five_conv4 = conv_five(channels_list=[512, 1024], in_channels=1024) # 3*(5+num_classes)=3*(5+20)=3*(4+1+20)=75
self.yolo_head1 = Yolov4_head(channels_list=[1024, len(anchors_mask[2]) * (5 + num_classes)], in_channels=512) def forward(self, x):
x2, x1, x0 = self.backbone(x) # 13,13,1024 -> 13,13,512 -> 13,13,1024 -> 13,13,512 -> 13,13,2048
p5 = self.conv1(x0)
p5 = self.spp(p5)
# 13,13,2048 -> 13,13,512 -> 13,13,1024 -> 13,13,512
p5 = self.conv2(p5) # 13,13,512 -> 13,13,256 -> 26,26,256
p5_upsample = self.upsample1(p5)
# 26,26,512 -> 26,26,256
p4 = self.conv_for_p4(x1)
# 26,26,256 + 26,26,256 -> 26,26,512
p4 = torch.cat([p4, p5_upsample], axis=1)
# 26,26,512 -> 26,26,256 -> 26,26,512 -> 26,26,256 -> 26,26,512 -> 26,26,256
p4 = self.make_five_conv1(p4) # 26,26,256 -> 26,26,128 -> 52,52,128
p4_upsample = self.upsample2(p4)
# 52,52,256 -> 52,52,128
p3 = self.conv_for_p3(x2)
p3=torch.cat([p3, p4_upsample], axis=1)
p3=self.make_five_conv2(p3) p3_downsample=self.down_sample1(p3)
p4=torch.cat([p3_downsample, p4], axis=1)
p4=self.make_five_conv3(p4) p4_downsample=self.down_sample2(p4)
p5=torch.cat([p4_downsample, p5], axis=1)
p5=self.make_five_conv4(p5) out2=self.yolo_head3(p3)
out1=self.yolo_head2(p4)
out0=self.yolo_head1(p5) return out0, out1, out2 # from torchsummary import summary
# yoloyolo=YoloBody(anchors_mask=["0","0","0"], num_classes=5, pretrained = False)
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# summary(yoloyolo, input_size=(3, 416, 416))
# print(yoloyolo)

代码没有注释,欢迎留言共同讨论,顺便给个关注,感谢。

YOLOV4网络的更多相关文章

  1. YOLOv3和YOLOv4长篇核心综述(上)

    YOLOv3和YOLOv4长篇核心综述(上) 对目标检测算法会经常使用和关注,比如Yolov3.Yolov4算法. 实际项目进行目标检测任务,比如人脸识别.多目标追踪.REID.客流统计等项目.因此目 ...

  2. Yolov3&Yolov4网络结构与源码分析

    Yolov3&Yolov4网络结构与源码分析 从2018年Yolov3年提出的两年后,在原作者声名放弃更新Yolo算法后,俄罗斯的Alexey大神扛起了Yolov4的大旗. 文章目录 1. 论 ...

  3. [炼丹术]YOLOv5目标检测学习总结

    Yolov5目标检测训练模型学习总结 一.YOLOv5介绍 YOLOv5是一系列在 COCO 数据集上预训练的对象检测架构和模型,代表Ultralytics 对未来视觉 AI 方法的开源研究,结合了在 ...

  4. 万字长文详解 YOLOv1-v5 系列模型

    一,YOLOv1 Abstract 1. Introduction 2. Unified Detectron 2.1. Network Design 2.2 Training 2.4. Inferen ...

  5. 【论文笔记】YOLOv4: Optimal Speed and Accuracy of Object Detection

    论文地址:https://arxiv.org/abs/2004.10934v1 github地址:https://github.com/AlexeyAB/darknet 摘要: 有很多特征可以提高卷积 ...

  6. YOLOV4源码详解

    一. 整体架构 整体架构和YOLO-V3相同(感谢知乎大神@江大白),创新点如下: 输入端 --> Mosaic数据增强.cmBN.SAT自对抗训练: BackBone --> CSPDa ...

  7. 深度剖析目标检测算法YOLOV4

    深度剖析目标检测算法YOLOV4 目录 简述 yolo 的发展历程 介绍 yolov3 算法原理 介绍 yolov4 算法原理(相比于 yolov3,有哪些改进点) YOLOV4 源代码日志解读 yo ...

  8. 网络可视化工具netron详细安装流程

    1.netron 简介 在实际的项目中,经过会遇到各种网络模型,需要我们快速去了解网络结构.如果单纯的去看模型文件,脑海中很难直观的浮现网络的架构. 这时,就可以使用netron可视化工具,可以清晰的 ...

  9. YOLOv4

    @ 目录 YOLO v4源码 CMake安装 CUDA安装 cuDNN安装 OpenCV安装 Cmake编译 VS编译 图像测试 测试结果 YOLOv4是最近开源的一个又快又准确的目标检测器. 首先看 ...

  10. YOLOv4全文阅读(全文中文翻译)

    YOLOv4全文阅读(全文中文翻译) YOLOv4: Optimal Speed and Accuracy of Object Detection 论文链接: https://arxiv.org/pd ...

随机推荐

  1. JZOJ 2020.07.28【NOIP提高组】模拟

    2020.07.28[NOIP提高组]模拟 考试时状态不好,暴力不想打 结束前勉勉强强骗点分 已经不想说什么了······ \(T1\) 复制&粘贴2 逆推答案,枚举 \(k\),分类讨论 \ ...

  2. MySQL创建表的三种方式

    创建表的三种方式 通过create语句直接创建 语法: create [TEMPORARY] table [IF NOT EXISTS] table_name ( col_name column_de ...

  3. 英国学者在真实世界早期RA队列研究中发现较高比例的临床缓解患者仍存在能量多普勒超声活性

    标签: 类风湿关节炎; 目标治疗策略; 能量多普勒活性; 预测因子 英国学者在真实世界早期RA队列研究中发现较高比例的临床缓解患者仍存在能量多普勒超声活性 电邮发布日期:2016年4月6日 本研究的重 ...

  4. 中后端做Excel导出功能返回数据流前端如何做处理

    exportFile(params).then(res => { // 直接返回来就是blob数据 if (res) { const xlsx = 'application/vnd.ms-exc ...

  5. SpringMVC基础源码分析(一)

    实现Controller的三种方式分析 每种实现的方式对应的HanderAdapter都不同. 实现Controller接口 该接口对应的HanderAdapter为SimpleControllerH ...

  6. CSS3,线性渐变(适用标题背景)

    .test{ margin:200px auto; height:30px; border:1px #D4D4D4 solid; box-shadow:0 -1px 10px rgba(0,0,0,0 ...

  7. swiper滑动异常

    遇到这样的情况,一共5张slide,但是从第三个起再往右划就不动了.后来发现是已经初始化swiper后又追加了slide.(大概意思是这样.太久的程序了,也不想仔细捋了) 暂且用到的办法: 先定义好: ...

  8. CF468E Permanent 题解

    考虑暴力状压 DP. 按行 DP,记录列哪些被选过,可以做到 \(O(2^kk^2)\). 注意到某一列扫完了之后这一列选没选过不重要,可以减少这里的状态. 简单优化一下,每次选择最少的一列,使这一列 ...

  9. vue-cli-serve启动报错

    报错信息: > vue-cli-service serve INFO Starting development server... 10% building 2/2 modules 0 acti ...

  10. RabbitMQ Linux安装与启动服务

    本文转载自 https://blog.csdn.net/chengmin123456789/article/details/124710277 1.先下载 erlang-23.2.3-1.el7.x8 ...