0
点赞
收藏
分享

微信扫一扫

在RTX3050上安装python3.9、anaconda、pycharm、cuda11.6、cudnn、jupyter等工具的详细步骤和方法

代码下载:

1.研究的背景

image.png

2.研究的意义

总之,基于全局注意力的改进YOLOv7-AC的水下场景目标检测系统具有重要的研究背景和意义。通过引入全局注意力机制和优化网络结构,我们可以提高水下目标检测的准确性和鲁棒性,为水下任务的执行提供有力支持。这对于提高水下任务的执行效果和水下资源的利用效率具有重要意义。

3.图片演示

2.png

3.png

4.png

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模块

image.png

10.参考文献


  1. 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]
  2. 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]
  3. 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]
  4. 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]
  5. 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]
  6. 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]
  7. Fu, H.; Song, G.; Wang, Y. Improved YOLOv4 marine target detection combined with CBAM. Symmetry 2021, 13, 623. [Google Scholar] [CrossRef]
  8. 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]
  9. 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]
  10. Bellavia, F. SIFT matching by context exposed. IEEE Trans. Pattern Anal. Mach. Intell. 2022, 45, 2445–2457. [Google Scholar] [CrossRef] [PubMed]
举报

相关推荐

0 条评论