-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
114 lines (96 loc) · 3.54 KB
/
models.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import torch
import torch.nn as nn
import torch.nn.functional as F
from utils.utils import to_spatial, to_spectral
class VanillaCNN(nn.Module):
def __init__(self, as_gray=True,):
super(VanillaCNN, self).__init__()
# Name of the model
self.name = 'VanillaCNN'
# Handle dimensions
if as_gray:
self.input_channels = 1
else:
self.input_channels = 3
# 1 input image channel, 6 output channels, 3x3 square convolution
self.layer1 = nn.Sequential(
nn.Conv2d(self.input_channels, 32, kernel_size=5, stride=1, padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.BatchNorm2d(32))
self.layer2 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3, stride=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.BatchNorm2d(64))
self.layer3 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.BatchNorm2d(64))
self.drop_out_lin1 = nn.Dropout(0.4)
self.lin1 = nn.Linear(1152, 512)
self.drop_out_lin2 = nn.Dropout(0.2)
self.lin2 = nn.Linear(512, 256)
self.drop_out_lin3 = nn.Dropout(0.1)
self.lin3 = nn.Linear(256, 2)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = out.reshape(out.size(0), -1)
out = self.drop_out_lin1(out)
out = F.relu(self.lin1(out))
out = self.drop_out_lin2(out)
out = F.relu(self.lin2(out))
out = self.drop_out_lin3(out)
out = torch.tanh(self.lin3(out))
return out
class SpectralDropoutEasyCNN(nn.Module):
def __init__(self, as_gray=True, dev='cpu'):
super(SpectralDropoutEasyCNN, self).__init__()
# Name of the model
self.name = 'SpectralDropoutCNN'
self.dev = dev
# Handle dimensions
if as_gray:
self.input_channels = 1
else:
self.input_channels = 3
self.layer1 = nn.Sequential(
nn.Conv2d(self.input_channels, 32, kernel_size=5, stride=1, padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.BatchNorm2d(32))
self.layer2 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3, stride=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.BatchNorm2d(64))
self.layer3 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.BatchNorm2d(64))
self.drop_out_lin1 = nn.Dropout(0.4)
self.lin1 = nn.Linear(1152, 512)
self.drop_out_lin2 = nn.Dropout(0.2)
self.lin2 = nn.Linear(512, 256)
self.drop_out_lin3 = nn.Dropout(0.1)
self.lin3 = nn.Linear(256, 2)
self.dropout_layer = nn.Dropout2d(p=0.2)
def forward(self, x):
out = to_spectral(x.cpu())
out = self.dropout_layer(out)
out = to_spatial(out.cpu())
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = out.reshape(out.size(0), -1)
out = self.drop_out_lin1(out)
out = F.relu(self.lin1(out))
out = self.drop_out_lin2(out)
out = F.relu(self.lin2(out))
out = self.drop_out_lin3(out)
out = torch.tanh(self.lin3(out))
return out