-
Notifications
You must be signed in to change notification settings - Fork 9
/
file_for_debug.py
96 lines (80 loc) · 3 KB
/
file_for_debug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
# Torch
import torch.nn as nn
import torch.nn.functional as F
import torch
import torch.optim as optim
from torch.nn import init
# utils
import math
import os
import datetime
import numpy as np
from sklearn.externals import joblib
from tqdm import tqdm
from utils import grouper, sliding_window, count_sliding_window,\
camel_to_snake
class OwnNetTest(nn.Module):
"""
based on 'lee': CONTEXTUAL DEEP CNN BASED HYPERSPECTRAL CLASSIFICATION, IGARSS 2016
"""
@staticmethod
def weight_init(m):
if isinstance(m, nn.Linear) or isinstance(m, nn.Conv3d):
init.kaiming_uniform_(m.weight)
init.zeros_(m.bias)
def __init__(self, in_channels, n_classes):
super(OwnNetTest, self).__init__()
# The first convolutional layer applied to the input hyperspectral
# image uses an inception module that locally convolves the input
# image with two convolutional filters with different sizes
# (1x1xB and 3x3xB where B is the number of spectral bands)
self.conv_3x3 = nn.Conv3d(
1, 128, (in_channels, 3, 3), stride=(1, 1, 1), padding=(0, 1, 1))
self.conv_1x1 = nn.Conv3d(
1, 128, (in_channels, 1, 1), stride=(1, 1, 1), padding=0)
# We use two modules from the residual learning approach
# Residual block 1
self.conv1 = nn.Conv2d(256, 128, (1, 1))
self.conv2 = nn.Conv2d(128, 128, (1, 1))
self.conv3 = nn.Conv2d(128, 128, (1, 1))
# Residual block 2
self.conv4 = nn.Conv2d(128, 128, (1, 1))
self.conv5 = nn.Conv2d(128, 128, (1, 1))
# The layer combination in the last three convolutional layers
# is the same as the fully connected layers of Alexnet
self.conv6 = nn.Conv2d(128, 128, (1, 1))
self.conv7 = nn.Conv2d(128, 128, (1, 1))
self.conv8 = nn.Conv2d(128, n_classes, (1, 1))
self.lrn1 = nn.LocalResponseNorm(256)
self.lrn2 = nn.LocalResponseNorm(128)
# The 7 th and 8 th convolutional layers have dropout in training
self.dropout = nn.Dropout(p=0.5)
self.apply(self.weight_init)
def forward(self, x):
# Inception module
x_3x3 = self.conv_3x3(x)
x_1x1 = self.conv_1x1(x)
x = torch.cat([x_3x3, x_1x1], dim=1)
# Remove the third dimension of the tensor
x = torch.squeeze(x)
# Local Response Normalization
x = F.relu(self.lrn1(x))
# First convolution
x = self.conv1(x)
# Local Response Normalization
x = F.relu(self.lrn2(x))
# First residual block
x_res = F.relu(self.conv2(x))
x_res = self.conv3(x_res)
x = F.relu(x + x_res)
# Second residual block
x_res = F.relu(self.conv4(x))
x_res = self.conv5(x_res)
x = F.relu(x + x_res)
x = F.relu(self.conv6(x))
x = self.dropout(x)
x = F.relu(self.conv7(x))
x = self.dropout(x)
x = self.conv8(x)
return x