-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodels.py
More file actions
239 lines (170 loc) · 5.87 KB
/
models.py
File metadata and controls
239 lines (170 loc) · 5.87 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
Implementation of the different models.
These models are :
- U-Net for semantic segmentation
- Siamese Network for template matching
- Spatial Transformer Network for homography refinement
"""
###########
# Imports #
###########
import torch
import torch.nn as nn
from kornia.geometry.warp import HomographyWarper
###########
# Classes #
###########
# Generic classes
class Conv(nn.Sequential):
"""Generic convolution layer"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1):
super().__init__(
nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
class LConv(nn.Sequential):
"""Generic convolution layer"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1):
super().__init__(
nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding),
nn.BatchNorm2d(out_channels),
nn.LeakyReLU(inplace=True)
)
class DoubleConv(nn.Sequential):
"""Generic double convolution layer"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1):
super().__init__(
Conv(in_channels, out_channels, kernel_size, stride, padding),
Conv(out_channels, out_channels, kernel_size, stride, padding)
)
class Deconv(nn.Sequential):
"""Generic deconvolution layer"""
def __init__(self, in_channels, out_channels, kernel_size=2, stride=2, padding=0):
super().__init__(
nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
class Dense(nn.Sequential):
"""Generic dense layer"""
def __init__(self, in_channels, out_channels):
super().__init__(
nn.Linear(in_channels, out_channels),
nn.ReLU(inplace=True)
)
# Models
class UNet(nn.Module):
"""
Implementation of a U-Net network for semantic
segmentation of field images.
"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv1 = DoubleConv(in_channels, 32)
self.conv2 = DoubleConv(32, 64)
self.conv3 = DoubleConv(64, 128)
self.conv4 = DoubleConv(128, 128)
self.conv5 = DoubleConv(128 + 128, 128)
self.conv6 = DoubleConv(64 + 64, 64)
self.conv7 = DoubleConv(32 + 32, 32)
self.upsample1 = Deconv(128, 128)
self.upsample2 = Deconv(128, 64)
self.upsample3 = Deconv(64, 32)
self.maxpool = nn.MaxPool2d(2, ceil_mode=True)
self.last = nn.Sequential(
nn.Conv2d(32, out_channels, 1),
nn.LogSoftmax(dim=1)
)
def forward(self, x):
# Downhill
d1 = self.conv1(x)
x = self.maxpool(d1)
d2 = self.conv2(x)
x = self.maxpool(d2)
d3 = self.conv3(x)
x = self.maxpool(d3)
x = self.conv4(x)
# Uphill
x = self.upsample1(x)
x = torch.cat([x[:, :, :d3.shape[-2:][0], :d3.shape[-2:][1]], d3], dim=1)
x = self.conv5(x)
x = self.upsample2(x)
x = torch.cat([x[:, :, :d2.shape[-2:][0], :d2.shape[-2:][1]], d2], dim=1)
x = self.conv6(x)
x = self.upsample3(x)
x = torch.cat([x[:, :, :d1.shape[-2:][0], :d1.shape[-2:][1]], d1], dim=1)
x = self.conv7(x)
return self.last(x)
class Siamese(nn.Module):
"""
Implementation of a Siamese Network to find the template
that best matches a semantic image of a field.
"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv = nn.Sequential(
DoubleConv(in_channels, 8),
nn.MaxPool2d(4),
DoubleConv(8, 16),
nn.MaxPool2d(2),
DoubleConv(16, 32),
nn.MaxPool2d(2)
)
self.dense = nn.Sequential(
Dense(4608, 2048),
Dense(2048, 512)
)
self.last = nn.Linear(512, out_channels)
def encode(self, x):
x = self.conv(x)
x = torch.flatten(x, start_dim=1)
x = self.dense(x)
return self.last(x)
def forward(self, x0, x1):
x0 = self.encode(x0)
x1 = self.encode(x1)
return x0, x1
class STN(nn.Module):
"""
Implementation of a Spatial Transformer Network to refine
homographies.
"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv1 = LConv(in_channels, 16)
self.conv2 = LConv(16, 16)
self.conv3 = LConv(16, 32)
self.conv4 = LConv(32, 32)
self.conv5 = LConv(32, 64)
self.conv6 = LConv(64, 64)
self.pool1 = nn.MaxPool2d(4)
self.pool2 = nn.MaxPool2d(2)
self.dense = nn.Sequential(
Dense(9216, 4096),
Dense(4096, 1024)
)
self.last = nn.Linear(1024, out_channels)
# Initialize such that all elements in the kernel are zero
self.last.weight.data.zero_()
# Initialize such that the bias is to the first 8 values of a flattened identity matrix
self.last.bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0, 0, 0], dtype=torch.float))
self.warper = HomographyWarper(144, 256, mode='nearest')
def forward(self, x):
# Get the relative transformation theta
d1 = self.conv1(x)
x = self.conv2(d1)
x = self.conv2(x + d1)
x = self.pool1(x)
d2 = self.conv3(x)
x = self.conv4(d2)
x = self.conv4(x + d2)
x = self.pool2(x)
d3 = self.conv5(x)
x = self.conv6(d3)
x = self.conv6(x + d3)
x = self.pool2(x)
x = torch.flatten(x, start_dim=1)
theta = self.dense(x)
theta = self.last(theta)
return theta