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 ...
随机推荐
- 题解 [ZJOI2007]棋盘制作
把悬线法这个坑填了,还是很简单的 qwq. 悬线法一般解决有一定约束条件的最大矩形问题.悬线的定义是从一个点一直往上走直到边界或者不符合条件结束. 炒个例子,在这题里面比如说有这样一个矩形 0 1 0 ...
- js获取时间戳的方法
js获取时间戳的方法 转载脚本之家: https://www.jb51.net/article/77066.htm 转载博客园八英里: https://www.cnblogs.com/deepalle ...
- 3D数字孪生场景编辑器介绍
1.背景 数字孪生的建设流程涉及建模.美术.程序.仿真等多种人才的协同作业,人力要求高,实施成本高,建设周期长.如何让小型团队甚至一个人就可以完成数字孪生的开发,是数字孪生工具链要解决的重要问题.目前 ...
- 三天吃透Java并发八股文!
本文已经收录到Github仓库,该仓库包含计算机基础.Java基础.多线程.JVM.数据库.Redis.Spring.Mybatis.SpringMVC.SpringBoot.分布式.微服务.设计模式 ...
- CF1768F 题解
题意 传送门 给定长度为 \(n\) 的序列 \(a\),求序列 \(f\),满足: \[f_i= \begin{equation} \begin{cases} 0&(i=1)\\ \min\ ...
- Android获取获取悬浮窗一下的view办法
getwindows可以获取当前手机屏幕所有有交互功能的view getactitywindows只能获取最顶层有交互的view
- UE打LOG整理
Kismet库 蓝图方法cpp使用 例:打LOG:Print String 蓝图节点的鼠标tips:Target is Kismet System Library #include "Run ...
- webpack5的基本用法
webpack的基本使用 webpack 本身功能有限: 开发模式: 仅能编译JS中的ES Module 语法 生产模式: 能编译ES Module 语法, 还能压缩JS代码 添加实例文件 npm i ...
- 7. mixin的实现原理
mixin的实现原理 在Vue.mixin()中的内容会被维护到Vue.options静态属性里面 然后通过mergeOptions以特定的合并策略将全局的属性和用户属性合并起来 在获取用户选项的时候 ...
- Linux系统实时监控
命令 top Top命令用于实时显示process的动态.参数如下: d:设置显示的更新速度 q:无延迟的显示速度 c:切换显示模式,一共有两种显示模式,一是只显示执行档,另一种是显示 ...