-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
190 lines (159 loc) · 6.11 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import numpy as np
import torch
import torch.nn as nn
from torchvision.models.resnet import resnet18
from torchvision.models import mobilenet_v2
import torch.nn.functional as F
# Large CNN
class CNNlarge(nn.Module):
def __init__(self, class_n):
super().__init__()
self.color_map = nn.Conv2d(3, 3, (1, 1), stride=(1, 1), padding=0)
self.module1 = nn.Sequential(
nn.Conv2d(3, 32, (5, 5), stride=(1, 1), padding=2),
nn.ReLU(),
nn.Conv2d(32, 32, (5, 5), stride=(1, 1), padding=2),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
nn.ReLU(),
nn.Dropout(p=0.5),
)
self.module2 = nn.Sequential(
nn.Conv2d(32, 64, (5, 5), stride=(1, 1), padding=2),
nn.ReLU(),
nn.Conv2d(64, 64, (5, 5), stride=(1, 1), padding=2),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
nn.ReLU(),
nn.Dropout(p=0.5),
)
self.module3 = nn.Sequential(
nn.Conv2d(64, 128, (5, 5), stride=(1, 1), padding=2),
nn.ReLU(),
nn.Conv2d(128, 128, (5, 5), stride=(1, 1), padding=2),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
nn.ReLU(),
nn.Dropout(p=0.5),
)
self.fc1 = nn.Sequential(
nn.Linear(14336, 1024, bias=True),
nn.ReLU(),
nn.Dropout(p=0.5)
)
self.fc2 = nn.Sequential(
nn.Linear(1024, 1024, bias=True),
nn.ReLU(),
nn.Dropout(p=0.5),
)
self.fc3 = nn.Linear(1024, class_n, bias=True)
def forward(self, x):
x = self.color_map(x)
branch1 = self.module1(x)
branch2 = self.module2(branch1)
branch3 = self.module3(branch2)
branch1 = branch1.reshape(branch1.shape[0], -1)
branch2 = branch2.reshape(branch2.shape[0], -1)
branch3 = branch3.reshape(branch3.shape[0], -1)
concat = torch.cat([branch1, branch2, branch3], 1)
out = self.fc1(concat)
out = self.fc2(out)
out = self.fc3(out)
return out
# Spatial transformer model
class Transformer(nn.Module):
def __init__(self, class_n):
super(Transformer, self).__init__()
# CNN layers
self.conv1 = nn.Conv2d(3, 100, kernel_size=5)
self.bn1 = nn.BatchNorm2d(100)
self.conv2 = nn.Conv2d(100, 150, kernel_size=3)
self.bn2 = nn.BatchNorm2d(150)
self.conv3 = nn.Conv2d(150, 250, kernel_size=3)
self.bn3 = nn.BatchNorm2d(250)
self.conv_drop = nn.Dropout2d()
self.fc1 = nn.Linear(250*2*2, 350)
self.fc2 = nn.Linear(350, class_n)
self.localization = nn.Sequential(
nn.Conv2d(3, 8, kernel_size=7),
nn.MaxPool2d(2, stride=2),
nn.ReLU(True),
nn.Conv2d(8, 10, kernel_size=5),
nn.MaxPool2d(2, stride=2),
nn.ReLU(True)
)
# Regressor for the 3 * 2 affine matrix
self.fc_loc = nn.Sequential(
nn.Linear(10 * 4 * 4, 32),
nn.ReLU(True),
nn.Linear(32, 3 * 2)
)
# Initialize the weights/bias with identity transformation
self.fc_loc[2].weight.data.zero_()
self.fc_loc[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float))
# Spatial transformer network forward function
def stn(self, x):
xs = self.localization(x)
xs = xs.view(-1, 10 * 4 * 4)
theta = self.fc_loc(xs)
theta = theta.view(-1, 2, 3)
grid = F.affine_grid(theta, x.size())
x = F.grid_sample(x, grid)
return x
def forward(self, x):
# transform the input
x = self.stn(x)
# Perform forward pass
x = self.bn1(F.max_pool2d(F.leaky_relu(self.conv1(x)),2))
x = self.conv_drop(x)
x = self.bn2(F.max_pool2d(F.leaky_relu(self.conv2(x)),2))
x = self.conv_drop(x)
x = self.bn3(F.max_pool2d(F.leaky_relu(self.conv3(x)),2))
x = self.conv_drop(x)
x = x.view(-1, 250*2*2)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x, dim=1)
# Small model
class CNNsmall(nn.Module):
def __init__(self, class_n):
super().__init__()
self.conv1 = nn.Conv2d(3, 64, (8, 8), stride=(2, 2), padding=3)
self.conv2 = nn.Conv2d(64, 128, (6, 6), stride=(2, 2), padding=0)
self.conv3 = nn.Conv2d(128, 128, (5, 5), stride=(1, 1), padding=0)
self.fc = nn.Linear(512, class_n)
def forward(self, x):
x = nn.ReLU()(self.conv1(x))
x = nn.ReLU()(self.conv2(x))
x = nn.ReLU()(self.conv3(x))
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
class ResNet18(nn.Module):
def __init__(self, class_n):
super().__init__()
# Load ResNet18 architecture
resnet = resnet18()
# Adjust first conv layer and removing maxpool layer
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = resnet.bn1
self.relu = resnet.relu
# Adding remaining ResNet18 layers
self.layers = nn.ModuleList()
self.layers.append(resnet.layer1)
self.layers.append(resnet.layer2)
self.layers.append(resnet.layer3)
self.layers.append(resnet.layer4)
# Adjust number of output neurons in fc layer
self.avgpool = resnet.avgpool
self.fc = nn.Linear(in_features=512, out_features=class_n, bias=True)
def forward(self, x: torch.Tensor) -> torch.Tensor:
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.layers[0](out)
out = self.layers[1](out)
out = self.layers[2](out)
out = self.layers[3](out)
out = self.avgpool(out)
out = torch.flatten(out, 1)
out = self.fc(out)
return out