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 ...
随机推荐
- windows定时任务执行python爬虫
有一些定时爬取的操作,适合用定时任务去执行.个人单独用的项目不适合放在工作所用的服务器上,也没必要单独买个服务器,我们windows电脑本身就有这项功能.接下来是一个windows定时任务执行pyth ...
- Mac 创建Python3虚拟环境
Mac 创建Python3虚拟环境 1.安装virtualenv pip3 install virtualenv 安装virtualenvwrapper pip3 install virtualenv ...
- 基于Docker使用CTB生成地形切片并加载
1. 引言 CTB(Cesium Terrain Builder)是一个用于地形切片的C++编写的命令行工具 GitHub地址为:GitHub - geo-data/cesium-terrain-bu ...
- ThreadLocal最终版本
ThreadLocal工作原理 目录 ThreadLocal工作原理 一.官方文档描述 二.为什么使用ThreadLocal 2.1.案例 三.ThreadLocal和syncronized关键字区别 ...
- 【HTML】HTML特殊字符大全
使用方法:这些字符属于unicode字符集,所以,你的文档需要声明为UTF-8:下面符号列表的后面有两列编号,它们并不太一样,第一列是用于html的,你需要在前面加上&#符号:第二列可以用于C ...
- idea代码格式xml
<code_scheme name="Default copy" version="173"> <option name="LINE ...
- 关于winform 调用本地html页面路径不正确问题
//为了使网页能够与winform交互 将com的可访问性设置为真 [System.Security.Permissions.PermissionSet(System.Security.Permiss ...
- uni-app (uView) select下拉框添加模糊搜索
先看效果: 因为uniapp内置的下拉查询是没有输入模糊搜索的,有的列表选项过多时还是需要这个搜索功能,所以只能自己筛选 (前台.后台两种方法). 下面是代码: <template> &l ...
- 前端JavaScript深拷贝的三种方法,看了不后悔!!!
深拷⻉ 深拷⻉开辟⼀个新的栈,两个对象属完成相同,但是对应两个不同的地址,修改⼀个对象的属性,不会 改变另⼀个对象的属性 常⻅的深拷⻉⽅式有: _.cloneDeep() jQuery.extend( ...
- Java实现简单个人所得税计算器相关操作代码
/** * 个税计算器 * 1.通过键盘输入用户的月薪 * 2.百度搜素个税计算方法,计算出应缴纳的税款 * 3.直到键盘输入88,则退出程序(使用break语句退出循环) * 应纳税所得额=工资收入 ...