-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathencoders3d.py
307 lines (263 loc) · 10.8 KB
/
encoders3d.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""Encoder modules for the satellite/NWP data based on 3D concolutions.
"""
import torch
from torch import nn
from torchvision.transforms import CenterCrop
from pvnet.models.multimodal.encoders.basic_blocks import (
AbstractNWPSatelliteEncoder,
ResidualConv3dBlock,
)
class DefaultPVNet(AbstractNWPSatelliteEncoder):
"""This is the original encoding module used in PVNet, with a few minor tweaks."""
def __init__(
self,
sequence_length: int,
image_size_pixels: int,
in_channels: int,
out_features: int,
number_of_conv3d_layers: int = 4,
conv3d_channels: int = 32,
fc_features: int = 128,
):
"""This is the original encoding module used in PVNet, with a few minor tweaks.
Args:
sequence_length: The time sequence length of the data.
image_size_pixels: The spatial size of the image. Assumed square.
in_channels: Number of input channels.
out_features: Number of output features.
number_of_conv3d_layers: Number of convolution 3d layers that are used.
conv3d_channels: Number of channels used in each conv3d layer.
fc_features: number of output nodes out of the hidden fully connected layer.
"""
super().__init__(sequence_length, image_size_pixels, in_channels, out_features)
# Check that the output shape of the convolutional layers will be at least 1x1
cnn_spatial_output_size = image_size_pixels - 2 * number_of_conv3d_layers
cnn_sequence_length = sequence_length #- 6 * number_of_conv3d_layers
if not (cnn_spatial_output_size >= 1):
raise ValueError(
f"cannot use this many conv3d layers ({number_of_conv3d_layers}) with this input "
f"spatial size ({image_size_pixels})"
)
conv_layers = []
conv_layers += [
nn.Conv3d(
in_channels=in_channels,
out_channels=conv3d_channels,
kernel_size=(3, 3, 3),
padding=(1, 0, 0),
),
nn.ELU(),
]
for i in range(0, number_of_conv3d_layers - 1):
conv_layers += [
nn.Conv3d(
in_channels=conv3d_channels,
out_channels=conv3d_channels,
kernel_size=(3, 3, 3),
padding=(1, 0, 0),
),
nn.ELU(),
]
self.conv_layers = nn.Sequential(*conv_layers)
# Calculate the size of the output of the 3D convolutional layers
cnn_output_size = conv3d_channels * cnn_spatial_output_size**2 * cnn_sequence_length
self.final_block = nn.Sequential(
nn.Linear(in_features=cnn_output_size, out_features=fc_features),
nn.ELU(),
nn.Linear(in_features=fc_features, out_features=out_features),
nn.ELU(),
)
def forward(self, x):
"""Run model forward"""
out = self.conv_layers(x)
out = out.reshape(x.shape[0], -1)
# Fully connected layers
out = self.final_block(out)
return out
class DefaultPVNet2(AbstractNWPSatelliteEncoder):
"""The original encoding module used in PVNet, with a few minor tweaks, and batchnorm."""
def __init__(
self,
sequence_length: int,
image_size_pixels: int,
in_channels: int,
out_features: int,
number_of_conv3d_layers: int = 4,
conv3d_channels: int = 32,
fc_features: int = 128,
batch_norm=True,
fc_dropout=0.2,
):
"""The original encoding module used in PVNet, with a few minor tweaks, and batchnorm.
Args:
sequence_length: The time sequence length of the data.
image_size_pixels: The spatial size of the image. Assumed square.
in_channels: Number of input channels.
out_features: Number of output features.
number_of_conv3d_layers: Number of convolution 3d layers that are used.
conv3d_channels: Number of channels used in each conv3d layer.
fc_features: number of output nodes out of the hidden fully connected layer.
batch_norm: Whether to include 3D batch normalisation.
fc_dropout: Probability of an element to be zeroed before the last two fully connected
layers.
"""
super().__init__(sequence_length, image_size_pixels, in_channels, out_features)
# Check that the output shape of the convolutional layers will be at least 1x1
cnn_spatial_output_size = image_size_pixels - 2 * number_of_conv3d_layers
if not (cnn_spatial_output_size > 0):
raise ValueError(
f"cannot use this many conv3d layers ({number_of_conv3d_layers}) with this input "
f"spatial size ({image_size_pixels})"
)
conv_layers = [
nn.Conv3d(
in_channels=in_channels,
out_channels=conv3d_channels,
kernel_size=(3, 3, 3),
padding=(1, 0, 0),
),
nn.LeakyReLU(),
]
if batch_norm:
# Inserted before activation using position -1
conv_layers.insert(-1, nn.BatchNorm3d(conv3d_channels))
for i in range(0, number_of_conv3d_layers - 1):
conv_layers += [
nn.Conv3d(
in_channels=conv3d_channels,
out_channels=conv3d_channels,
kernel_size=(3, 3, 3),
padding=(1, 0, 0),
),
nn.LeakyReLU(),
]
if batch_norm:
# Inserted before activation using position -1
conv_layers.insert(-1, nn.BatchNorm3d(conv3d_channels))
self.conv_layers = nn.Sequential(*conv_layers)
# Calculate the size of the output of the 3D convolutional layers
cnn_output_size = conv3d_channels * cnn_spatial_output_size**2 * sequence_length
final_block = [
nn.Linear(in_features=cnn_output_size, out_features=fc_features),
nn.LeakyReLU(),
nn.Linear(in_features=fc_features, out_features=out_features),
nn.LeakyReLU(),
]
if fc_dropout > 0:
# Insert after the linear layers
final_block.insert(1, nn.Dropout(fc_dropout))
final_block.insert(-1, nn.Dropout(fc_dropout))
self.final_block = nn.Sequential(*final_block)
def forward(self, x):
"""Run model forward"""
out = self.conv_layers(x)
out = out.reshape(x.shape[0], -1)
# Fully connected layers
out = self.final_block(out)
return out
class EncoderUNET(AbstractNWPSatelliteEncoder):
"""An encoder based on emodifed UNet architecture.
An encoder for satellite and/or NWP data taking inspiration from the kinds of skip
connections in UNet. This differs from an actual UNet in that it does not have upsampling
layers, instead it concats features from different spatial scales, and applies a few extra
conv3d layers.
"""
def __init__(
self,
sequence_length: int,
image_size_pixels: int,
in_channels: int,
out_features: int,
n_downscale: int = 3,
res_block_layers: int = 2,
conv3d_channels: int = 32,
dropout_frac: float = 0.1,
):
"""An encoder based on emodifed UNet architecture.
Args:
sequence_length: The time sequence length of the data.
image_size_pixels: The spatial size of the image. Assumed square.
in_channels: Number of input channels.
out_features: Number of output features.
n_downscale: Number of conv3d and spatially downscaling layers that are used.
res_block_layers: Number of residual blocks used after each downscale layer.
conv3d_channels: Number of channels used in each conv3d layer.
dropout_frac: Probability of an element to be zeroed in the residual pathways.
"""
cnn_spatial_output = image_size_pixels // (2**n_downscale)
if not (cnn_spatial_output > 0):
raise ValueError(
f"cannot use this many downscaling layers ({n_downscale}) with this input "
f"spatial size ({image_size_pixels})"
)
super().__init__(sequence_length, image_size_pixels, in_channels, out_features)
self.first_layer = nn.Sequential(
nn.Conv3d(
in_channels=in_channels,
out_channels=conv3d_channels,
kernel_size=(1, 1, 1),
padding=(0, 0, 0),
),
ResidualConv3dBlock(
in_channels=conv3d_channels,
n_layers=res_block_layers,
dropout_frac=dropout_frac,
),
)
downscale_layers = []
for _ in range(n_downscale):
downscale_layers += [
nn.Sequential(
ResidualConv3dBlock(
in_channels=conv3d_channels,
n_layers=res_block_layers,
dropout_frac=dropout_frac,
),
nn.ELU(),
nn.Conv3d(
in_channels=conv3d_channels,
out_channels=conv3d_channels,
kernel_size=(1, 2, 2),
padding=(0, 0, 0),
stride=(1, 2, 2),
),
)
]
self.downscale_layers = nn.ModuleList(downscale_layers)
self.crop_fn = CenterCrop(cnn_spatial_output)
cat_channels = conv3d_channels * (1 + n_downscale)
self.post_cat_conv = nn.Sequential(
ResidualConv3dBlock(
in_channels=cat_channels,
n_layers=res_block_layers,
),
nn.ELU(),
nn.Conv3d(
in_channels=cat_channels,
out_channels=conv3d_channels,
kernel_size=(1, 1, 1),
),
)
final_channels = (
(image_size_pixels // (2**n_downscale)) ** 2 * conv3d_channels * sequence_length
)
self.final_layer = nn.Sequential(
nn.ELU(),
nn.Linear(
in_features=final_channels,
out_features=out_features,
),
nn.ELU(),
)
def forward(self, x):
"""Run model forward"""
out = self.first_layer(x)
outputs = [self.crop_fn(out)]
for layer in self.downscale_layers:
out = layer(out)
outputs += [self.crop_fn(out)]
out = torch.cat(outputs, dim=1)
out = self.post_cat_conv(out)
out = torch.flatten(out, start_dim=1)
out = self.final_layer(out)
return out