1. 程式人生 > 實用技巧 >HybridSN 高光譜分類網路的優化

HybridSN 高光譜分類網路的優化

1.HybridSN

class HybridSN(nn.Module):
    def __init__(self):
        super(HybridSN, self).__init__()
        self.conv1 = nn.Conv3d(1, 8, (7, 3, 3), stride=1, padding=0)
        self.conv2 = nn.Conv3d(8, 16, (5, 3, 3), stride=1, padding=0)
        self.conv3 = nn.Conv3d(16, 32, (3, 3, 3), stride=1, padding=0
) self.conv4 = nn.Conv2d(576, 64, kernel_size=3, stride=1, padding=0) self.bn1 = nn.BatchNorm2d(64) self.fc1 = nn.Linear(18496, 256) self.dropout1 = nn.Dropout(p=0.4) self.fc2 = nn.Linear(256, 128) self.dropout2 = nn.Dropout(p=0.4) self.fc3 = nn.Linear(128
, class_num) def forward(self, x): out = self.conv1(x) out = self.conv2(out) out = self.conv3(out) #print(batch) out = out.reshape(batch, 576, 19, 19) out = self.conv4(out) out = self.bn1(out) out = F.relu(out) out = out.view(-1
, 64 * 17 * 17) out = self.fc1(out) out = F.relu(out) out = self.dropout1(out) out = self.fc2(out) out = F.relu(out) out = self.dropout2(out) out = self.fc3(out) return out

2.新增SENet

class SELayer(nn.Module):
    def __init__(self, channel, r=16):
        super(SELayer, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d((1,1))
        self.fc = nn.Sequential(
            nn.Linear(channel, channel // r),
            nn.ReLU(),
            nn.Linear(channel // r, channel),
            nn.Sigmoid()
        )

    def forward(self, x):
        a, b, _, _ = x.size()
        y = self.avg_pool(x).view(a, b)
        y = self.fc(y).view(a, b, 1, 1)
        return x * y.expand_as(x)
class HybridSN(nn.Module):
      def __init__(self, num_classes=16):
    '''
        self.senet = SELayer(64)
    '''
      def forward(self, x):
    '''
        out = self.conv3_2d(out)
        out = self.senet(out)
    '''

3.為什麼每次測試結果會不同

在pytorch中,網路有train和eval兩種模式

在訓練時應當指定當前是訓練模式:model.train(),dropout和batch normalization會生效

BN層在測試的時候採用的是固定的mean和var,dropout不生效,bn固定引數。