【深度学习Week4】MobileNet_ShuffleNet

报错:unsafe legacy renegotiation disabled

【深度学习Week4】MobileNet_ShuffleNet_第1张图片
解决方案:

尝试了更换cryptography==36.0.2版本,以及更换下载链接的方法,都不行,最后采用了手动下载mat文件并上传到colab的方法

高光谱图像分类数据集简介Indian Pines&mat

【深度学习Week4】MobileNet_ShuffleNet_第2张图片

定义网络:
【深度学习Week4】MobileNet_ShuffleNet_第3张图片

class HybridSN(nn.Module):
  def __init__(self):
    super(HybridSN, self).__init__()
    self.conv3d_1 = nn.Sequential(
        nn.Conv3d(1, 8, kernel_size=(7, 3, 3), stride=1, padding=0),
        nn.BatchNorm3d(8),
        nn.ReLU(inplace = True),
    )
    self.conv3d_2 = nn.Sequential(
        nn.Conv3d(8, 16, kernel_size=(5, 3, 3), stride=1, padding=0),
        nn.BatchNorm3d(16),
        nn.ReLU(inplace = True),
    ) 
    self.conv3d_3 = nn.Sequential(
        nn.Conv3d(16, 32, kernel_size=(3, 3, 3), stride=1, padding=0),
        nn.BatchNorm3d(32),
        nn.ReLU(inplace = True)
    )

    self.conv2d_4 = nn.Sequential(
        nn.Conv2d(576, 64, kernel_size=(3, 3), stride=1, padding=0),
        nn.BatchNorm2d(64),
        nn.ReLU(inplace = True),
    )
    self.fc1 = nn.Linear(18496,256)
    self.fc2 = nn.Linear(256,128)
    self.fc3 = nn.Linear(128,16)
    self.dropout = nn.Dropout(p = 0.4)

  def forward(self,x):
    out = self.conv3d_1(x)
    out = self.conv3d_2(out)
    out = self.conv3d_3(out)
    out = self.conv2d_4(out.reshape(out.shape[0],-1,19,19))
    out = out.reshape(out.shape[0],-1)
    out = F.relu(self.dropout(self.fc1(out)))
    out = F.relu(self.dropout(self.fc2(out)))
    out = self.fc3(out)
    return out

实验结果:
【深度学习Week4】MobileNet_ShuffleNet_第4张图片
本次准确率为97.89%

思考题

● 训练HybridSN,然后多测试几次,会发现每次分类的结果都不一样,请思考为什么?
每次训练的时候,神经网络的参数和权重都是随机的,所以每次的结果都不一样。

● 如果想要进一步提升高光谱图像的分类性能,可以如何改进?
增加注意力机制,把Attention加在第三个三维卷积后,以保留更多的光谱信息,从而进一步提升高光谱图像的分类性能。

● depth-wise conv 和 分组卷积有什么区别与联系?
Depth-wise conv(深度可分离卷积)和分组卷积是两种用于减少卷积计算量的优化技术。区别在于:

  • Depth-wise conv是在每个输入通道上独立地进行卷积操作,然后再将结果在通道维度上进行组合。这样可以减少参数数量和计算量,但每个通道之间没有交互信息。
  • 分组卷积是将输入通道分为若干组,然后在每组内进行卷积操作。这样可以在一定程度上减少计算量,并且每组内的通道可以相互交互信息。但相比普通卷积,分组卷积可能引入一定的信息损失。

● SENet 的注意力是不是可以加在空间位置上?
SENet的注意力机制主要是通过学习通道之间的关系来提升特征的重要性,但也可以通过适当的调整将注意力扩展到空间位置上,从而使网络能够关注不同空间位置上的特征,进一步提升性能。

● 在 ShuffleNet 中,通道的 shuffle 如何用代码实现?

import torch

def channel_shuffle(x, groups):
    batch_size, height, width, channels = x.size()
    channels_per_group = channels // groups
    
    # Reshape the tensor to (batch_size, height, width, groups, channels_per_group)
    x = x.view(batch_size, height, width, groups, channels_per_group)
    
    # Transpose the tensor along the last two dimensions (swap channels_per_group and groups)
    x = x.permute(0, 1, 2, 4, 3)
    
    # Reshape the tensor back to its original shape
    x = x.view(batch_size, height, width, channels)
    
    return x

你可能感兴趣的:(2023新征程,深度学习,人工智能)