YOLOV4网络
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网络的更多相关文章
- YOLOv3和YOLOv4长篇核心综述(上)
YOLOv3和YOLOv4长篇核心综述(上) 对目标检测算法会经常使用和关注,比如Yolov3.Yolov4算法. 实际项目进行目标检测任务,比如人脸识别.多目标追踪.REID.客流统计等项目.因此目 ...
- Yolov3&Yolov4网络结构与源码分析
Yolov3&Yolov4网络结构与源码分析 从2018年Yolov3年提出的两年后,在原作者声名放弃更新Yolo算法后,俄罗斯的Alexey大神扛起了Yolov4的大旗. 文章目录 1. 论 ...
- [炼丹术]YOLOv5目标检测学习总结
Yolov5目标检测训练模型学习总结 一.YOLOv5介绍 YOLOv5是一系列在 COCO 数据集上预训练的对象检测架构和模型,代表Ultralytics 对未来视觉 AI 方法的开源研究,结合了在 ...
- 万字长文详解 YOLOv1-v5 系列模型
一,YOLOv1 Abstract 1. Introduction 2. Unified Detectron 2.1. Network Design 2.2 Training 2.4. Inferen ...
- 【论文笔记】YOLOv4: Optimal Speed and Accuracy of Object Detection
论文地址:https://arxiv.org/abs/2004.10934v1 github地址:https://github.com/AlexeyAB/darknet 摘要: 有很多特征可以提高卷积 ...
- YOLOV4源码详解
一. 整体架构 整体架构和YOLO-V3相同(感谢知乎大神@江大白),创新点如下: 输入端 --> Mosaic数据增强.cmBN.SAT自对抗训练: BackBone --> CSPDa ...
- 深度剖析目标检测算法YOLOV4
深度剖析目标检测算法YOLOV4 目录 简述 yolo 的发展历程 介绍 yolov3 算法原理 介绍 yolov4 算法原理(相比于 yolov3,有哪些改进点) YOLOV4 源代码日志解读 yo ...
- 网络可视化工具netron详细安装流程
1.netron 简介 在实际的项目中,经过会遇到各种网络模型,需要我们快速去了解网络结构.如果单纯的去看模型文件,脑海中很难直观的浮现网络的架构. 这时,就可以使用netron可视化工具,可以清晰的 ...
- YOLOv4
@ 目录 YOLO v4源码 CMake安装 CUDA安装 cuDNN安装 OpenCV安装 Cmake编译 VS编译 图像测试 测试结果 YOLOv4是最近开源的一个又快又准确的目标检测器. 首先看 ...
- YOLOv4全文阅读(全文中文翻译)
YOLOv4全文阅读(全文中文翻译) YOLOv4: Optimal Speed and Accuracy of Object Detection 论文链接: https://arxiv.org/pd ...
随机推荐
- P4525 【模板】自适应辛普森法 1
\(\text{Solution}\) 题目 \(\text{Simpson}\) 公式: \[\int_l^r f(x) {\mathrm d}x = \frac{(r-l)(f(l)+f(r)+4 ...
- JZOJ 2022.02.11【提高A组】模拟
\(\text{Solution}\) 首先把 \(T2\) 给切了,\(T1\) 找半天规律找不到 然后打了个表算是暴力了 \(T3\) 也暴... 太暴了... \(T4\) 直接啥也不会 \(\ ...
- JZOJ 1389. 玩诈欺的小杉
思路 考虑一个点要不要翻,如果它左边的点为 \(1\),那么它必须翻 所以我们可以从左往右一列一列地翻 先枚举第 \(0\) 列的状态 然后之后的列就确定了 判断一下最后一列是不是 \(0\) 就行了 ...
- el-dialog中中使用echarts
1.在dialog中使用open方法 <el-dialog :title="diaTitle" :visible.sync="dialogVisible" ...
- Cesium加载三维路线
1. 概述 将路线加载到三维地图中,能直观显示道路的坡度变化,协同DEM和遥感影像,能极大丰富道路的可视化效果 本文此处基于Cesium,加载地形数据,叠加遥感影像,再叠加路网数据,形成三维地图,效果 ...
- C#/VB.NET 如何在 Word 文档中添加页眉和页脚
页眉位于文档中每个页面的顶部区域,常用于显示文档的附加信息,可以插入时间.图形.公司微标.文档标题.文件名或作者姓名等:页脚位于文档中每个页面的底部的区域,常用于显示文档的附加信息,可以在页脚中插入文 ...
- 如何使用visual studio code的插件remote ssh远程操作virtual box虚拟机
0 Remote-SSH是什么?为什么要用它? The Remote-SSH extension lets you use any remote machine with a SSH server a ...
- 1163:阿克曼(Ackmann)函数
我的博客: https://www.cnblogs.com/haoningdeboke-2022/ 1163:阿克曼(Ackmann)函数 时间限制: 1000 ms 内存限制: 65 ...
- lombok.config
# 声明该配置文件是一个根配置文件,从该配置文件所在的目录开始扫描 config.stopBubbling=true # 全局配置 equalsAndHashCode 的 callSuper 属性为t ...
- vue封装组件
父组件 <template> <view> <assembly @submitToParent="submitToParent"> <te ...