|
| 1 | +import torch.nn as nn |
| 2 | +from torch import Tensor |
| 3 | +import torch |
| 4 | + |
| 5 | +from ice_station_zebra.models.common.convblock import ConvBlock |
| 6 | +from ice_station_zebra.models.common.bottleneckblock import BottleneckBlock |
| 7 | +from ice_station_zebra.models.common.upconvblock import UpconvBlock |
| 8 | + |
| 9 | + |
| 10 | +class UNetProcessor(nn.Module): |
| 11 | + """UNet model that processes input through a UNet architecture |
| 12 | +
|
| 13 | + Structure based on Andersson et al. (2021) Nature Communications |
| 14 | + https://doi.org/10.1038/s41467-021-25257-4""" |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + n_latent_channels: int, |
| 19 | + filter_size: int, |
| 20 | + start_out_channels: int, |
| 21 | + ) -> None: |
| 22 | + super().__init__() |
| 23 | + |
| 24 | + channels = [start_out_channels * 2**pow for pow in range(4)] |
| 25 | + |
| 26 | + # Encoder |
| 27 | + self.conv1 = ConvBlock(n_latent_channels, channels[0], filter_size=filter_size) |
| 28 | + self.maxpool1 = nn.MaxPool2d(kernel_size=2) |
| 29 | + self.conv2 = ConvBlock(channels[0], channels[1], filter_size=filter_size) |
| 30 | + self.maxpool2 = nn.MaxPool2d(kernel_size=2) |
| 31 | + self.conv3 = ConvBlock(channels[1], channels[2], filter_size=filter_size) |
| 32 | + self.maxpool3 = nn.MaxPool2d(kernel_size=2) |
| 33 | + self.conv4 = ConvBlock(channels[2], channels[2], filter_size=filter_size) |
| 34 | + self.maxpool4 = nn.MaxPool2d(kernel_size=2) |
| 35 | + |
| 36 | + # Bottleneck |
| 37 | + self.conv5 = BottleneckBlock(channels[2], channels[3], filter_size=filter_size) |
| 38 | + |
| 39 | + # Decoder |
| 40 | + self.up6 = UpconvBlock(channels[3], channels[2]) |
| 41 | + self.up7 = UpconvBlock(channels[2], channels[2]) |
| 42 | + self.up8 = UpconvBlock(channels[2], channels[1]) |
| 43 | + self.up9 = UpconvBlock(channels[1], channels[0]) |
| 44 | + |
| 45 | + self.up6b = ConvBlock(channels[3], channels[2], filter_size=filter_size) |
| 46 | + self.up7b = ConvBlock(channels[3], channels[2], filter_size=filter_size) |
| 47 | + self.up8b = ConvBlock(channels[2], channels[1], filter_size=filter_size) |
| 48 | + self.up9b = ConvBlock( |
| 49 | + channels[1], channels[0], filter_size=filter_size, final=True |
| 50 | + ) |
| 51 | + |
| 52 | + # Final layer |
| 53 | + self.final_layer = nn.Conv2d( |
| 54 | + channels[0], n_latent_channels, kernel_size=1, padding="same" |
| 55 | + ) |
| 56 | + |
| 57 | + def forward(self, x: Tensor) -> Tensor: |
| 58 | + # Process in latent space: tensor with (batch_size, all_variables, latent_height, latent_width) |
| 59 | + |
| 60 | + # Encoder |
| 61 | + bn1 = self.conv1(x) |
| 62 | + conv1 = self.maxpool1(bn1) |
| 63 | + bn2 = self.conv2(conv1) |
| 64 | + conv2 = self.maxpool1(bn2) |
| 65 | + bn3 = self.conv3(conv2) |
| 66 | + conv3 = self.maxpool3(bn3) |
| 67 | + bn4 = self.conv4(conv3) |
| 68 | + conv4 = self.maxpool4(bn4) |
| 69 | + |
| 70 | + # Bottleneck |
| 71 | + bn5 = self.conv5(conv4) |
| 72 | + |
| 73 | + # Decoder |
| 74 | + up6 = self.up6b(torch.cat([bn4, self.up6(bn5)], dim=1)) |
| 75 | + up7 = self.up7b(torch.cat([bn3, self.up7(up6)], dim=1)) |
| 76 | + up8 = self.up8b(torch.cat([bn2, self.up8(up7)], dim=1)) |
| 77 | + up9 = self.up9b(torch.cat([bn1, self.up9(up8)], dim=1)) |
| 78 | + |
| 79 | + # Final layer |
| 80 | + output = self.final_layer(up9) |
| 81 | + |
| 82 | + return output |
0 commit comments