代码下载:
1.研究的背景
2.研究的意义
总之,基于全局注意力的改进YOLOv7-AC的水下场景目标检测系统具有重要的研究背景和意义。通过引入全局注意力机制和优化网络结构,我们可以提高水下目标检测的准确性和鲁棒性,为水下任务的执行提供有力支持。这对于提高水下任务的执行效果和水下资源的利用效率具有重要意义。
3.图片演示
5.核心代码讲解
5.1 fit.py
class CBS(nn.Module):
""" Convolution -> BatchNorm -> SiLU """
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(CBS, self).__init__()
self.layer = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=kernel_size // 2),
nn.BatchNorm2d(out_channels),
nn.SiLU()
)
def forward(self, x):
return self.layer(x)
class MP1(nn.Module):
""" MP1 block """
def __init__(self, in_channels):
super(MP1, self).__init__()
# Upper branch
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.cbs_upper = CBS(in_channels, in_channels // 2, 1, 1)
# Lower branch
self.cbs_lower1 = CBS(in_channels, in_channels // 2, 1, 1)
self.cbs_lower2 = CBS(in_channels // 2, in_channels // 2, 3, 2)
def forward(self, x):
# Upper branch
x_upper = self.maxpool(x)
x_upper = self.cbs_upper(x_upper)
# Lower branch
x_lower = self.cbs_lower1(x)
x_lower = self.cbs_lower2(x_lower)
# Concatenate
return torch.cat([x_upper, x_lower], dim=1)
class MP2(nn.Module):
""" MP2 block, similar to MP1 but with different output channels """
def __init__(self, in_channels):
super(MP2, self).__init__()
# Upper branch
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.cbs_upper = CBS(in_channels, in_channels, 1, 1)
# Lower branch
self.cbs_lower1 = CBS(in_channels, in_channels, 1, 1)
self.cbs_lower2 = CBS(in_channels, in_channels, 3, 2)
def forward(self, x):
# Upper branch
x_upper = self.maxpool(x)
x_upper = self.cbs_upper(x_upper)
# Lower branch
x_lower = self.cbs_lower1(x)
x_lower = self.cbs_lower2(x_lower)
# Concatenate
return torch.cat([x_upper, x_lower], dim=1)
......
CBS
类包含一个序列模块,其中包含一个卷积层、一个批归一化层和一个SiLU激活函数。它的forward
方法将输入x
传递给这个序列模块,并返回输出。
MP1
类表示MP1块,它包含一个上分支和一个下分支。上分支包括一个最大池化层和一个CBS
模块,下分支包括两个CBS
模块。它的forward
方法将输入x
分别传递给上分支和下分支,并将它们的输出在通道维度上进行拼接后返回。
MP2
类表示MP2块,它与MP1块类似,但输出通道数不同。它的forward
方法与MP1块的forward
方法相同。
这些类封装了卷积、批归一化和激活函数的组合以及MP1和MP2块的功能,可以在模型中使用它们来构建更复杂的网络结构。
5.2 GAM.py
class ChannelAttention(nn.Module):
""" Channel Attention Submodule for GAM """
def __init__(self, in_channels):
super(ChannelAttention, self).__init__()
self.mlp = nn.Sequential(
nn.Linear(in_channels, in_channels // 2),
nn.SiLU(),
nn.Linear(in_channels // 2, in_channels),
nn.Sigmoid()
)
def forward(self, x):
avg_pool = torch.mean(x, dim=[2, 3])
return self.mlp(avg_pool).unsqueeze(2).unsqueeze(3) * x
class SpatialAttention(nn.Module):
""" Spatial Attention Submodule for GAM """
def __init__(self, in_channels, out_channels):
super(SpatialAttention, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.SiLU(),
nn.Conv2d(out_channels, 1, kernel_size=3, stride=1, padding=1),
nn.Sigmoid()
)
def forward(self, x):
return self.conv(x) * x
class GAM(nn.Module):
""" Global Attention Module (GAM) """
def __init__(self, in_channels):
super(GAM, self).__init__()
self.channel_attention = ChannelAttention(in_channels)
self.spatial_attention = SpatialAttention(in_channels, in_channels // 2)
def forward(self, x):
x = self.channel_attention(x)
x = self.spatial_attention(x)
return x
5.3 models\common.py
class CBS(nn.Module):
""" Convolution -> BatchNorm -> SiLU """
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(CBS, self).__init__()
self.layer = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=kernel_size // 2),
nn.BatchNorm2d(out_channels),
nn.SiLU()
)
def forward(self, x):
return self.layer(x)
class MP1(nn.Module):
""" MP1 block """
def __init__(self, in_channels):
super(MP1, self).__init__()
# Upper branch
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.cbs_upper = CBS(in_channels, in_channels // 2, 1, 1)
# Lower branch
self.cbs_lower1 = CBS(in_channels, in_channels // 2, 1, 1)
self.cbs_lower2 = CBS(in_channels // 2, in_channels // 2, 3, 2)
def forward(self, x):
# Upper branch
x_upper = self.maxpool(x)
x_upper = self.cbs_upper(x_upper)
# Lower branch
x_lower = self.cbs_lower1(x)
x_lower = self.cbs_lower2(x_lower)
# Concatenate
return torch.cat([x_upper, x_lower], dim=1)
class MP2(nn.Module):
""" MP2 block, similar to MP1 but with different output channels """
def __init__(self, in_channels):
super(MP2, self).__init__()
# Upper branch
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.cbs_upper = CBS(in_channels, in_channels, 1, 1)
# Lower branch
self.cbs_lower1 = CBS(in_channels, in_channels, 1, 1)
self.cbs_lower2 = CBS(in_channels, in_channels, 3, 2)
def forward(self, x):
# Upper branch
x_upper = self.maxpool(x)
x_upper = self.cbs_upper(x_upper)
# Lower branch
x_lower = self.cbs_lower1(x)
x_lower = self.cbs_lower2(x_lower)
# Concatenate
return torch.cat([x_upper, x_lower], dim=1)
class ChannelAttention(nn.Module):
""" Channel Attention Submodule for GAM """
def __init__(self, in_channels):
super(ChannelAttention, self).__init__()
self.mlp = nn.Sequential(
nn.Linear(in_channels, in_channels // 2),
nn.SiLU(),
nn.Linear(in_channels // 2, in_channels),
nn.Sigmoid()
)
def forward(self, x):
avg_pool = torch.mean(x, dim=[2, 3])
return self.mlp(avg_pool).unsqueeze(2).unsqueeze(3) * x
class SpatialAttention(nn.Module):
""" Spatial Attention Submodule for GAM """
def __init__(self, in_channels, out_channels):
super(SpatialAttention, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.SiLU(),
nn.Conv2d(out_channels, 1, kernel_size=3, stride=1, padding=1),
nn.Sigmoid()
)
def forward(self, x):
return self.conv(x) * x
class GAM(nn.Module):
""" Global Attention Module (GAM) """
def __init__(self, in_channels):
super(GAM, self).__init__()
self.channel_attention = ChannelAttention(in_channels)
self.spatial_attention = SpatialAttention(in_channels, in_channels // 2)
def forward(self, x):
x = self.channel_attention(x)
x = self.spatial_attention(x)
return x
......
6.系统整体结构
下面是每个文件的功能整理:
文件路径 | 功能概述 |
---|---|
fit.py | 定义了卷积、批归一化和激活函数的组合模块类 |
GAM.py | 实现了全局注意力模块的神经网络模型 |
torch_utils.py | 包含了一些常用的函数和类,如设备选择、时间同步、模型性能测量等 |
train.py | 用于训练模型的脚本,包括训练函数和一些辅助函数和类 |
ui.py | 基于PyQt5的GUI界面,用于选择文件和显示检测结果 |
models\common.py | 定义了一些常用的模块和函数,用于构建YOLOv7模型 |
models\experimental.py | 包含了一些实验性的模块和函数 |
models\tf.py | 包含了一些与TensorFlow相关的模块和函数 |
models\yolo.py | 定义了YOLO模型的相关类 |
models_init_.py | 空文件 |
tools\activations.py | 定义了一些激活函数 |
tools\augmentations.py | 定义了一些数据增强函数 |
tools\autoanchor.py | 定义了自动锚框生成的函数 |
tools\autobatch.py | 定义了自动批处理大小的函数 |
tools\callbacks.py | 定义了一些回调函数 |
tools\datasets.py | 定义了数据加载器的创建函数 |
tools\downloads.py | 定义了一些用于下载数据集和权重的函数 |
tools\general.py | 定义了一些通用的辅助函数 |
tools\loss.py | 定义了一些损失函数 |
tools\metrics.py | 定义了一些评估指标的计算函数 |
tools\plots.py | 定义了一些绘图函数 |
tools\torch_utils.py | 定义了一些与PyTorch相关的辅助函数 |
tools_init_.py | 空文件 |
tools\aws\resume.py | 定义了一些用于AWS训练恢复的函数 |
tools\aws_init_.py | 空文件 |
tools\flask_rest_api\example_request.py | 定义了一些用于Flask REST API的示例请求函数 |
tools\flask_rest_api\restapi.py | 定义了Flask REST API的相关类和函数 |
tools\loggers_init_.py | 空文件 |
tools\loggers\wandb\log_dataset.py | 定义了用于记录数据集的类 |
tools\loggers\wandb\sweep.py | 定义了用于记录超参数搜索的类 |
tools\loggers\wandb\wandb_utils.py | 定义了一些与WandB日志记录相关的辅助函数 |
tools\loggers\wandb_init_.py | 空文件 |
utils\activations.py | 定义了一些激活函数 |
utils\augmentations.py | 定义了一些数据增强函数 |
utils\autoanchor.py | 定义了自动锚框生成的函数 |
utils\autobatch.py | 定义了自动批处理大小的函数 |
utils\callbacks.py | 定义了一些回调函数 |
utils\datasets.py | 定义了数据加载器的创建函数 |
utils\downloads.py | 定义了一些用于下载数据集和权重的函数 |
utils\general.py | 定义了一些通用的辅助函数 |
utils\loss.py | 定义了一些损失函数 |
utils\metrics.py | 定义了一些评估指标的计算函数 |
utils\plots.py | 定义了一些绘图函数 |
utils\torch_utils.py | 定义了一些与PyTorch相关的辅助函数 |
utils_init_.py | 空文件 |
utils\aws\resume.py | 定义了一些用于AWS训练恢复的函数 |
utils\aws_init_.py | 空文件 |
utils\flask_rest_api\example_request.py | 定义了一些用于Flask REST API的示例请求函数 |
utils\flask_rest_api\restapi.py | 定义了Flask REST API的相关类和函数 |
utils\loggers_init_.py | 空文件 |
utils\loggers\wandb\log_dataset.py | 定义了用于记录数据集的类 |
utils\loggers\wandb\sweep.py | 定义了用于记录超参数搜索的类 |
utils\loggers\wandb\wandb_utils.py | 定义了一些与WandB日志记录相关的辅助函数 |
7.YOLOv7简介
输入模块:YOLOv7模型的预处理阶段采用了镶嵌和混合数据增强技术,并利用YOLOv5建立的自适应锚框计算方法,保证输入的彩色图像统一缩放到640×640尺寸,从而满足骨干网络输入大小的要求。 骨干网络:YOLOv7网络由三个主要组件组成:CBS、E-ELAN和MP1。CBS模块由卷积、批量归一化和SiLU激活函数组成。E-ELAN模块保持了原有的ELAN设计架构,通过引导不同的特征组计算块学习更多样的特征来增强网络的学习能力,保留原有的梯度路径。MP1由CBS和MaxPool组成,分为上下分支。上分支使用 MaxPool 将图像的长度和宽度减半,并使用具有 128 个输出通道的 CBS 将图像通道减半。下分支通过具有 1 × 1 核和步长的 CBS 将图像通道减半,通过 3 × 3 核和 2 × 2 步长的 CBS 将图像长和宽减半,最后通过串联(Cat)操作。MaxPool提取小局部区域的最大值信息,而CBS提取小局部区域的所有值信息,从而提高网络的特征提取能力。 Head网络:YOLOv7的Head网络采用特征金字塔网络(FPN)架构,采用PANet设计。该网络包含多个卷积、批量归一化和 SiLU 激活 (CBS) 块,并引入了空间金字塔池化和卷积空间金字塔池化 (Sppcspc) 结构、扩展高效层聚合网络 (E-ELAN) 和 MaxPool -2(MP2)。Sppcspc 结构通过在空间金字塔池化 (SPP) 结构中结合卷积空间金字塔 (CSP) 结构,以及帮助优化和特征提取的大剩余边缘,改善了网络的感知场。ELAN-H层是基于E-ELAN的多个特征层的融合,进一步增强了特征提取。MP2 块具有与 MP1 块类似的结构,只是输出通道的数量略有修改。 预测网络:YOLOv7的预测网络采用Rep结构来调整头部网络输出的特征的图像通道数,然后应用1×1卷积来预测置信度、类别和锚框。Rep 结构受 RepVGG [ 44 ] 的启发,引入了特殊的残差设计来帮助训练过程。这种独特的残差结构在实际预测中可以简化为简单的卷积,从而在不牺牲其预测性能的情况下降低网络复杂性。
8.改进模块
GAM
ResNet-ACmix 模块
AC-E-ELAN模块
10.参考文献
- Zhou, X.; Ding, W.; Jin, W. Microwave-assisted extraction of lipids, carotenoids, and other compounds from marine resources. In Innovative and Emerging Technologies in the Bio-Marine Food Sector; Academic Press: Cambridge, MA, USA, 2022; pp. 375–394. [Google Scholar]
- Liu, Y.; Anderlini, E.; Wang, S.; Ma, S.; Ding, Z. Ocean explorations using autonomy: Technologies, strategies and applications. In Offshore Robotics; Springer: Singapore, 2022; Volume I, pp. 35–58. [Google Scholar]
- Ghafoor, H.; Noh, Y. An overview of next-generation underwater target detection and tracking: An integrated underwater architecture. IEEE Access 2019, 7, 98841–98853. [Google Scholar] [CrossRef]
- Liu, K.; Liang, Y. Enhancement of underwater optical images based on background light estimation and improved adaptive transmission fusion. Opt. Express 2021, 29, 28307. [Google Scholar] [CrossRef]
- Shi, J.; Zhuo, X.; Zhang, C.; Bian, Y.X.; Shen, H. Research on key technologies of underwater target detection. In Seventh Symposium on Novel Photoelectronic Detection Technology and Applications; SPIE: Kunming, China, 2021; Volume 11763, pp. 1128–1137. [Google Scholar]
- Zhang, W.; Sun, W. Research on small moving target detection algorithm based on complex scene. J. Phys. Conf. Ser. 2021, 1738, 012093. [Google Scholar] [CrossRef]
- Fu, H.; Song, G.; Wang, Y. Improved YOLOv4 marine target detection combined with CBAM. Symmetry 2021, 13, 623. [Google Scholar] [CrossRef]
- Samantaray, S.; Deotale, R.; Chowdhary, C.L. Lane detection using sliding window for intelligent ground vehicle challenge. In Innovative Data Communication Technologies and Application: Proceedings of ICIDCA 2020; Springer: Singapore, 2021; pp. 871–881. [Google Scholar]
- Bakheet, S.; Al-Hamadi, A. A framework for instantaneous driver drowsiness detection based on improved HOG features and naïve Bayesian classification. Brain Sci. 2021, 11, 240. [Google Scholar] [CrossRef]
- Bellavia, F. SIFT matching by context exposed. IEEE Trans. Pattern Anal. Mach. Intell. 2022, 45, 2445–2457. [Google Scholar] [CrossRef] [PubMed]