File size: 12,128 Bytes
1b34a12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
U-shaped DISCO Neural Operator
"""

from typing import List, Tuple

import torch
import torch.nn as nn
from torch.nn import functional as F

from torch_harmonics_local.convolution import (
    EquidistantDiscreteContinuousConv2d as DISCO2d,
)


class UDNO(nn.Module):
    """
    U-shaped DISCO Neural Operator in PyTorch
    """

    def __init__(
        self,
        in_chans: int,
        out_chans: int,
        radius_cutoff: float,
        chans: int = 32,
        num_pool_layers: int = 4,
        drop_prob: float = 0.0,
        in_shape: Tuple[int, int] = (320, 320),
        kernel_shape: Tuple[int, int] = (3, 4),
    ):
        """
        Parameters
        ----------
        in_chans : int
            Number of channels in the input to the U-Net model.
        out_chans : int
            Number of channels in the output to the U-Net model.
        radius_cutoff : float
            Control the effective radius of the DISCO kernel. Values are
            between 0.0 and 1.0. The radius_cutoff is represented as a proportion
            of the normalized input space, to ensure that kernels are resolution
            invaraint.
        chans : int, optional
            Number of output channels of the first DISCO layer. Default is 32.
        num_pool_layers : int, optional
            Number of down-sampling and up-sampling layers. Default is 4.
        drop_prob : float, optional
            Dropout probability. Default is 0.0.
        in_shape : Tuple[int, int]
            Shape of the input to the UDNO. This is required to dynamically
            compile DISCO kernels for resolution invariance.
        kernel_shape : Tuple[int, int], optional
            Shape of the DISCO kernel. Default is (3, 4). This corresponds to 3
            rings and 4 anisotropic basis functions. Under the hood, each DISCO
            kernel has (3 - 1) * 4 + 1 = 9 parameters, equivalent to a standard
            3x3 convolution kernel.

            Note: This is NOT kernel_size, as under the DISCO framework,
            kernels are dynamically compiled to support resolution invariance.
        """
        super().__init__()
        assert len(in_shape) == 2, "Input shape must be 2D"

        self.in_chans = in_chans
        self.out_chans = out_chans
        self.chans = chans
        self.num_pool_layers = num_pool_layers
        self.drop_prob = drop_prob
        self.in_shape = in_shape
        self.kernel_shape = kernel_shape

        self.down_sample_layers = nn.ModuleList(
            [
                DISCOBlock(
                    in_chans,
                    chans,
                    radius_cutoff,
                    drop_prob,
                    in_shape,
                    kernel_shape,
                )
            ]
        )
        ch = chans
        shape = (in_shape[0] // 2, in_shape[1] // 2)
        radius_cutoff = radius_cutoff * 2
        for _ in range(num_pool_layers - 1):
            self.down_sample_layers.append(
                DISCOBlock(
                    ch,
                    ch * 2,
                    radius_cutoff,
                    drop_prob,
                    in_shape=shape,
                    kernel_shape=kernel_shape,
                )
            )
            ch *= 2
            shape = (shape[0] // 2, shape[1] // 2)
            radius_cutoff *= 2

            # test commit

        self.bottleneck = DISCOBlock(
            ch,
            ch * 2,
            radius_cutoff,
            drop_prob,
            in_shape=shape,
            kernel_shape=kernel_shape,
        )

        self.up = nn.ModuleList()
        self.up_transpose = nn.ModuleList()
        for _ in range(num_pool_layers - 1):
            self.up_transpose.append(
                TransposeDISCOBlock(
                    ch * 2,
                    ch,
                    radius_cutoff,
                    in_shape=shape,
                    kernel_shape=kernel_shape,
                )
            )
            shape = (shape[0] * 2, shape[1] * 2)
            radius_cutoff /= 2
            self.up.append(
                DISCOBlock(
                    ch * 2,
                    ch,
                    radius_cutoff,
                    drop_prob,
                    in_shape=shape,
                    kernel_shape=kernel_shape,
                )
            )
            ch //= 2

        self.up_transpose.append(
            TransposeDISCOBlock(
                ch * 2,
                ch,
                radius_cutoff,
                in_shape=shape,
                kernel_shape=kernel_shape,
            )
        )
        shape = (shape[0] * 2, shape[1] * 2)
        radius_cutoff /= 2
        self.up.append(
            nn.Sequential(
                DISCOBlock(
                    ch * 2,
                    ch,
                    radius_cutoff,
                    drop_prob,
                    in_shape=shape,
                    kernel_shape=kernel_shape,
                ),
                nn.Conv2d(
                    ch, self.out_chans, kernel_size=1, stride=1
                ),  # 1x1 conv is always res-invariant (pixel wise channel transformation)
            )
        )

    def forward(self, image: torch.Tensor) -> torch.Tensor:
        """
        Parameters
        ----------
        image : torch.Tensor
            Input 4D tensor of shape `(N, in_chans, H, W)`.

        Returns
        -------
        torch.Tensor
            Output tensor of shape `(N, out_chans, H, W)`.
        """
        stack = []
        output = image

        # apply down-sampling layers
        for layer in self.down_sample_layers:
            output = layer(output)
            stack.append(output)
            output = F.avg_pool2d(output, kernel_size=2, stride=2, padding=0)

        output = self.bottleneck(output)

        # apply up-sampling layers
        for transpose, disco in zip(self.up_transpose, self.up):
            downsample_layer = stack.pop()
            output = transpose(output)

            # reflect pad on the right/botton if needed to handle odd input dimensions
            padding = [0, 0, 0, 0]
            if output.shape[-1] != downsample_layer.shape[-1]:
                padding[1] = 1  # padding right
            if output.shape[-2] != downsample_layer.shape[-2]:
                padding[3] = 1  # padding bottom
            if torch.sum(torch.tensor(padding)) != 0:
                output = F.pad(output, padding, "reflect")

            output = torch.cat([output, downsample_layer], dim=1)
            output = disco(output)

        return output


class DISCOBlock(nn.Module):
    """
    A DISCO Block that consists of two DISCO layers each followed by
    instance normalization, LeakyReLU activation and dropout.
    """

    def __init__(
        self,
        in_chans: int,
        out_chans: int,
        radius_cutoff: float,
        drop_prob: float,
        in_shape: Tuple[int, int],
        kernel_shape: Tuple[int, int] = (3, 4),
    ):
        """
        Parameters
        ----------
        in_chans : int
            Number of channels in the input.
        out_chans : int
            Number of channels in the output.
        radius_cutoff : float
            Control the effective radius of the DISCO kernel. Values are
            between 0.0 and 1.0. The radius_cutoff is represented as a proportion
            of the normalized input space, to ensure that kernels are resolution
            invaraint.
        in_shape : Tuple[int]
            Unbatched spatial 2D shape of the input to this block.
            Rrequired to dynamically compile DISCO kernels for resolution invariance.
        kernel_shape : Tuple[int, int], optional
            Shape of the DISCO kernel. Default is (3, 4). This corresponds to 3
            rings and 4 anisotropic basis functions. Under the hood, each DISCO
            kernel has (3 - 1) * 4 + 1 = 9 parameters, equivalent to a standard
            3x3 convolution kernel.

            Note: This is NOT kernel_size, as under the DISCO framework,
            kernels are dynamically compiled to support resolution invariance.
        drop_prob : float
            Dropout probability.
        """
        super().__init__()

        self.in_chans = in_chans
        self.out_chans = out_chans
        self.drop_prob = drop_prob

        self.layers = nn.Sequential(
            DISCO2d(
                in_chans,
                out_chans,
                kernel_shape=kernel_shape,
                in_shape=in_shape,
                bias=False,
                radius_cutoff=radius_cutoff,
                padding_mode="constant",
            ),
            nn.InstanceNorm2d(out_chans),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
            nn.Dropout2d(drop_prob),
            DISCO2d(
                out_chans,
                out_chans,
                kernel_shape=kernel_shape,
                in_shape=in_shape,
                bias=False,
                radius_cutoff=radius_cutoff,
                padding_mode="constant",
            ),
            nn.InstanceNorm2d(out_chans),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
            nn.Dropout2d(drop_prob),
        )

    def forward(self, image: torch.Tensor) -> torch.Tensor:
        """
        Parameters
        ----------
        image : ndarray
            Input 4D tensor of shape `(N, in_chans, H, W)`.

        Returns
        -------
        ndarray
            Output tensor of shape `(N, out_chans, H, W)`.
        """
        return self.layers(image)


class TransposeDISCOBlock(nn.Module):
    """
    A transpose DISCO Block that consists of an up-sampling layer followed by a
    DISCO layer, instance normalization, and LeakyReLU activation.
    """

    def __init__(
        self,
        in_chans: int,
        out_chans: int,
        radius_cutoff: float,
        in_shape: Tuple[int, int],
        kernel_shape: Tuple[int, int] = (3, 4),
    ):
        """
        Parameters
        ----------
        in_chans : int
            Number of channels in the input.
        out_chans : int
            Number of channels in the output.
        radius_cutoff : float
            Control the effective radius of the DISCO kernel. Values are
            between 0.0 and 1.0. The radius_cutoff is represented as a proportion
            of the normalized input space, to ensure that kernels are resolution
            invaraint.
        in_shape : Tuple[int]
            Unbatched spatial 2D shape of the input to this block.
            Rrequired to dynamically compile DISCO kernels for resolution invariance.
        kernel_shape : Tuple[int, int], optional
            Shape of the DISCO kernel. Default is (3, 4). This corresponds to 3
            rings and 4 anisotropic basis functions. Under the hood, each DISCO
            kernel has (3 - 1) * 4 + 1 = 9 parameters, equivalent to a standard
            3x3 convolution kernel.

            Note: This is NOT kernel_size, as under the DISCO framework,
            kernels are dynamically compiled to support resolution invariance
        """
        super().__init__()

        self.in_chans = in_chans
        self.out_chans = out_chans

        self.layers = nn.Sequential(
            nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True),
            DISCO2d(
                in_chans,
                out_chans,
                kernel_shape=kernel_shape,
                in_shape=(2 * in_shape[0], 2 * in_shape[1]),
                bias=False,
                radius_cutoff=(radius_cutoff / 2),
                padding_mode="constant",
            ),
            nn.InstanceNorm2d(out_chans),
            nn.LeakyReLU(negative_slope=0.2, inplace=True),
        )

    def forward(self, image: torch.Tensor) -> torch.Tensor:
        """
        Parameters
        ----------
        image : torch.Tensor
            Input 4D tensor of shape `(N, in_chans, H, W)`.

        Returns
        -------
        torch.Tensor
            Output tensor of shape `(N, out_chans, H*2, W*2)`.
        """
        return self.layers(image)