• 第四周作业:卷积神经网络学习part3


    代码学习

    HybridSN高光谱分类网络

    import numpy as np
    import matplotlib.pyplot as plt
    import scipy.io as sio
    from sklearn.decomposition import PCA
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import confusion_matrix, accuracy_score, classification_report, cohen_kappa_score
    import spectral
    import torch
    import torchvision
    import torch.nn as nn
    import torch.nn.functional as F
    import torch.optim as optim
    
    class_num = 16
    
    class HybridSN(nn.Module):
      def __init__(self):
        super(HybridSN,self).__init__()
        self.conv1 = nn.Conv3d(1,8,kernel_size=(7,3,3),stride=1,padding=0)
        self.bn1 = nn.BatchNorm3d(8)
        self.conv2 = nn.Conv3d(8,16,kernel_size=(5,3,3),stride=1,padding=0)
        self.bn2 = nn.BatchNorm3d(16)
        self.conv3 = nn.Conv3d(16,32,kernel_size=(3,3,3),stride=1,padding=0)
        self.bn3 = nn.BatchNorm3d(32)
        self.conv4 = nn.Conv2d(576,64,kernel_size=(3,3),stride=1,padding=0)
        self.bn4 = nn.BatchNorm2d(64)
    
        self.fc1 = nn.Linear(18496,256)
        self.fc2 = nn.Linear(256,128)
        self.fc3 = nn.Linear(128,16)
        self.drop = nn.Dropout(0.4)
    
      def forward(self,x):
        #三维卷积部分:
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        out = F.relu(self.bn3(self.conv3(out)))
        #把前面的 32*18 reshape 一下,得到 (576, 19, 19)
        out = out.reshape(out.shape[0],-1,19,19)
        #二维卷积
        out = F.relu(self.bn4(self.conv4(out)))
        # flatten 操作,变为 18496 维的向量
        out = out.reshape(out.shape[0],-1)
        #全连接层
        out = F.relu(self.drop(self.fc1(out)))
        out = F.relu(self.drop(self.fc2(out)))
        out = self.fc3(out)
        return out
        
    # 对高光谱数据 X 应用 PCA 变换
    def applyPCA(X, numComponents):
        newX = np.reshape(X, (-1, X.shape[2]))
        pca = PCA(n_components=numComponents, whiten=True)
        newX = pca.fit_transform(newX)
        newX = np.reshape(newX, (X.shape[0], X.shape[1], numComponents))
        return newX
    
    # 对单个像素周围提取 patch 时,边缘像素就无法取了,因此,给这部分像素进行 padding 操作
    def padWithZeros(X, margin=2):
        newX = np.zeros((X.shape[0] + 2 * margin, X.shape[1] + 2* margin, X.shape[2]))
        x_offset = margin
        y_offset = margin
        newX[x_offset:X.shape[0] + x_offset, y_offset:X.shape[1] + y_offset, :] = X
        return newX
    
    # 在每个像素周围提取 patch ,然后创建成符合 keras 处理的格式
    def createImageCubes(X, y, windowSize=5, removeZeroLabels = True):
        # 给 X 做 padding
        margin = int((windowSize - 1) / 2)
        zeroPaddedX = padWithZeros(X, margin=margin)
        # split patches
        patchesData = np.zeros((X.shape[0] * X.shape[1], windowSize, windowSize, X.shape[2]))
        patchesLabels = np.zeros((X.shape[0] * X.shape[1]))
        patchIndex = 0
        for r in range(margin, zeroPaddedX.shape[0] - margin):
            for c in range(margin, zeroPaddedX.shape[1] - margin):
                patch = zeroPaddedX[r - margin:r + margin + 1, c - margin:c + margin + 1]   
                patchesData[patchIndex, :, :, :] = patch
                patchesLabels[patchIndex] = y[r-margin, c-margin]
                patchIndex = patchIndex + 1
        if removeZeroLabels:
            patchesData = patchesData[patchesLabels>0,:,:,:]
            patchesLabels = patchesLabels[patchesLabels>0]
            patchesLabels -= 1
        return patchesData, patchesLabels
    
    def splitTrainTestSet(X, y, testRatio, randomState=345):
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=testRatio, random_state=randomState, stratify=y)
        return X_train, X_test, y_train, y_test
       
    # 地物类别
    class_num = 16
    X = sio.loadmat('Indian_pines_corrected.mat')['indian_pines_corrected']
    y = sio.loadmat('Indian_pines_gt.mat')['indian_pines_gt']
    
    # 用于测试样本的比例
    test_ratio = 0.90
    # 每个像素周围提取 patch 的尺寸
    patch_size = 25
    # 使用 PCA 降维,得到主成分的数量
    pca_components = 30
    
    print('Hyperspectral data shape: ', X.shape)
    print('Label shape: ', y.shape)
    
    print('
    ... ... PCA tranformation ... ...')
    X_pca = applyPCA(X, numComponents=pca_components)
    print('Data shape after PCA: ', X_pca.shape)
    
    print('
    ... ... create data cubes ... ...')
    X_pca, y = createImageCubes(X_pca, y, windowSize=patch_size)
    print('Data cube X shape: ', X_pca.shape)
    print('Data cube y shape: ', y.shape)
    
    print('
    ... ... create train & test data ... ...')
    Xtrain, Xtest, ytrain, ytest = splitTrainTestSet(X_pca, y, test_ratio)
    print('Xtrain shape: ', Xtrain.shape)
    print('Xtest  shape: ', Xtest.shape)
    
    # 改变 Xtrain, Ytrain 的形状,以符合 keras 的要求
    Xtrain = Xtrain.reshape(-1, patch_size, patch_size, pca_components, 1)
    Xtest  = Xtest.reshape(-1, patch_size, patch_size, pca_components, 1)
    print('before transpose: Xtrain shape: ', Xtrain.shape) 
    print('before transpose: Xtest  shape: ', Xtest.shape) 
    
    # 为了适应 pytorch 结构,数据要做 transpose
    Xtrain = Xtrain.transpose(0, 4, 3, 1, 2)
    Xtest  = Xtest.transpose(0, 4, 3, 1, 2)
    print('after transpose: Xtrain shape: ', Xtrain.shape) 
    print('after transpose: Xtest  shape: ', Xtest.shape) 
    
    
    """ Training dataset"""
    class TrainDS(torch.utils.data.Dataset): 
        def __init__(self):
            self.len = Xtrain.shape[0]
            self.x_data = torch.FloatTensor(Xtrain)
            self.y_data = torch.LongTensor(ytrain)        
        def __getitem__(self, index):
            # 根据索引返回数据和对应的标签
            return self.x_data[index], self.y_data[index]
        def __len__(self): 
            # 返回文件数据的数目
            return self.len
    
    """ Testing dataset"""
    class TestDS(torch.utils.data.Dataset): 
        def __init__(self):
            self.len = Xtest.shape[0]
            self.x_data = torch.FloatTensor(Xtest)
            self.y_data = torch.LongTensor(ytest)
        def __getitem__(self, index):
            # 根据索引返回数据和对应的标签
            return self.x_data[index], self.y_data[index]
        def __len__(self): 
            # 返回文件数据的数目
            return self.len
    
    # 创建 trainloader 和 testloader
    trainset = TrainDS()
    testset  = TestDS()
    train_loader = torch.utils.data.DataLoader(dataset=trainset, batch_size=128, shuffle=True, num_workers=2)
    test_loader  = torch.utils.data.DataLoader(dataset=testset,  batch_size=128, shuffle=False, num_workers=2)
    
    # 使用GPU训练,可以在菜单 "代码执行工具" -> "更改运行时类型" 里进行设置
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    
    # 网络放到GPU上
    net = HybridSN().to(device)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(net.parameters(), lr=0.001)
    net.train()
    # 开始训练
    total_loss = 0
    for epoch in range(100):
        for i, (inputs, labels) in enumerate(train_loader):
            inputs = inputs.to(device)
            labels = labels.to(device)
            # 优化器梯度归零
            optimizer.zero_grad()
            # 正向传播 + 反向传播 + 优化 
            outputs = net(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            total_loss += loss.item()
        print('[Epoch: %d]   [loss avg: %.4f]   [current loss: %.4f]' %(epoch + 1, total_loss/(epoch+1), loss.item()))
    
    print('Finished Training')
    
    net.eval()
    count = 0
    # 模型测试
    for inputs, _ in test_loader:
        inputs = inputs.to(device)
        outputs = net(inputs)
        outputs = np.argmax(outputs.detach().cpu().numpy(), axis=1)
        if count == 0:
            y_pred_test =  outputs
            count = 1
        else:
            y_pred_test = np.concatenate( (y_pred_test, outputs) )
    
    # 生成分类报告
    classification = classification_report(ytest, y_pred_test, digits=4)
    print(classification)
    
    

    多次测试后结果:0.9719 0.9730 0.9732

    测试结果不稳定,上网查询,在训练模型时会在前面加上:

    model.train()
    

    在测试模型时在前面使用:

    model.eval()
    

    这两个方法是针对在网络训练和测试时采用不同方式的情况,比如Batch NormalizationDropout

    • 训练时是针对每个min-batch的,但是在测试中往往是针对单张图片,即不存在min-batch的概念。由于网络训练完毕后参数都是固定的,因此每个批次的均值和方差都是不变的,因此直接结算所有batch的均值和方差。所有Batch Normalization的训练和测试时的操作不同
    • 在训练中,每个隐层的神经元先乘概率P,然后在进行激活,在测试中,所有的神经元先进行激活,然后每个隐层神经元的输出乘P。

    添加后结果稳定为:0.9792

    SENet实现

    将SE模块添加到上述HybridSN网络后面两个2D卷积中,添加的SE模块及修改后的HybridSN网络:

    class_num = 16
    
    class SEBlock(nn.Module):
      def __init__(self,channel,r=16):
        super(SEBlock,self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc1 = nn.Linear(channel,round(channel/r))
        self.fc2 = nn.Linear(round(channel/r),channel)
    
      def forward(self,x):
        out = self.avg_pool(x)
        out = out.view(out.shape[0],-1)
        out = F.relu(self.fc1(out))
        out = F.sigmoid(self.fc2(out))
        out = out.view(x.shape[0],x.shape[1],1,1)
        out = x * out
        return out
    
    class HybridSN(nn.Module):
      def __init__(self):
        super(HybridSN,self).__init__()
        self.conv1 = nn.Conv3d(1,8,kernel_size=(7,3,3),stride=1,padding=0)
        self.bn1 = nn.BatchNorm3d(8)
        self.conv2 = nn.Conv3d(8,16,kernel_size=(5,3,3),stride=1,padding=0)
        self.bn2 = nn.BatchNorm3d(16)
        self.conv3 = nn.Conv3d(16,32,kernel_size=(3,3,3),stride=1,padding=0)
        self.bn3 = nn.BatchNorm3d(32)
        self.conv4 = nn.Conv2d(576,64,kernel_size=(3,3),stride=1,padding=0)
    
        self.SElayer = SEBlock(64,16)
    
        self.bn4 = nn.BatchNorm2d(64)
        self.fc1 = nn.Linear(18496,256)
        self.fc2 = nn.Linear(256,128)
        self.fc3 = nn.Linear(128,16)
        self.dropout = nn.Dropout(0.4)
    
      def forward(self,x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        out = F.relu(self.bn3(self.conv3(out)))
    
        out = out.reshape(out.shape[0],-1,19,19)
        out = F.relu(self.bn4(self.conv4(out)))
    
        out = self.SElayer(out)
    
        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
    

    测试结果:0.9870

    SENet的提升分类性能的本质原理:Excitation使用全连接神经网络,对Sequeeze后的结果做非线性变换,,之后使用Excitation得到的结果作为权重,乘到输入特征上,提升有效特征,抑制无效特征。

    视频学习

    1、《语义分割中的自注意力机制和低秩重建》-李夏 链接

    2、《 图像语义分割前沿进展》-程明明 链接

  • 相关阅读:
    值得推荐的C/C++框架和库
    P2P技术基础: 关于TCP打洞技术
    Java FileInputStream与FileReader的区别
    如何理解java采用Unicode编码
    细说:Unicode, UTF-8, UTF-16, UTF-32, UCS-2, UCS-4
    程序员趣味读物:谈谈Unicode编码
    字符编码笔记:ASCII,Unicode 和 UTF-8
    Java并发编程:volatile关键字解析(转)
    java线程安全问题之静态变量、实例变量、局部变量
    Hystrix使用详解
  • 原文地址:https://www.cnblogs.com/lixinhh/p/13502069.html
Copyright © 2020-2023  润新知