1. 程式人生 > 實用技巧 >SENet 實現

SENet 實現

HybridSN中新增SENet

#! wget http://www.ehu.eus/ccwintco/uploads/6/67/Indian_pines_corrected.mat
#! wget http://www.ehu.eus/ccwintco/uploads/c/c4/Indian_pines_gt.mat
#! pip install spectral
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 SEBlock(nn.Module): def __init__
(self,in_channels,r=16): super(SEBlock,self).__init__() self.globalAvgPool = nn.AdaptiveAvgPool2d((1,1)) self.fc1 = nn.Linear(in_channels,round(in_channels/r)) self.fc2 = nn.Linear(round(in_channels/r),in_channels) def forward(self,x): out = self.globalAvgPool(x) out = out.view(x.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.conv3d1 = nn.Conv3d(1,8,kernel_size=(7,3,3),stride=1,padding=0) self.bn1 = nn.BatchNorm3d(8) self.conv3d2 = nn.Conv3d(8,16,kernel_size=(5,3,3),stride=1,padding=0) self.bn2 = nn.BatchNorm3d(16) self.conv3d3 = nn.Conv3d(16,32,kernel_size=(3,3,3),stride=1,padding=0) self.bn3 = nn.BatchNorm3d(32) self.conv2d4 = 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.conv3d1(x))) out = F.relu(self.bn2(self.conv3d2(out))) out = F.relu(self.bn3(self.conv3d3(out))) out = F.relu(self.bn4(self.conv2d4(out.reshape(out.shape[0],-1,19,19)))) 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 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('\n... ... PCA tranformation ... ...') X_pca = applyPCA(X, numComponents=pca_components) print('Data shape after PCA: ', X_pca.shape) print('\n... ... 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('\n... ... 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) scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min',verbose=True,factor=0.9,min_lr=1e-6) # 開始訓練 total_loss = 0 net.train() 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() scheduler.step(loss) total_loss += loss.item() nn.ReLU() 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,labels in test_loader: inputs = inputs.to(device) labels = labels.to(device) outputs = net(inputs) _,preds = torch.max(outputs,1) count += (preds == labels).sum().item() print("Test ACC:{}".format(count/len(testset)))