conv.py 17.8 KB
Newer Older
mibaumgartner's avatar
models  
mibaumgartner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""
Copyright 2020 Division of Medical Image Computing, German Cancer Research Center (DKFZ), Heidelberg, Germany

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import torch
import torch.nn as nn
from typing import Union, Callable, Any, Optional, Tuple, Sequence, Type

mibaumgartner's avatar
mibaumgartner committed
21
22
from nndet.arch.initializer import InitWeights_He
from nndet.arch.layers.norm import GroupNorm
mibaumgartner's avatar
models  
mibaumgartner committed
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417


NdParam = Union[int, Tuple[int, int], Tuple[int, int, int]]


class Generator:
    def __init__(self, conv_cls, dim: int):
        """
        Factory helper which saves the conv class and dimension to generate objects

        Args:
            conv_cls (callable): class of convolution
            dim (int): number of spatial dimensions (in general 2 or 3)
        """
        self.dim = dim
        self.conv_cls = conv_cls

    def __call__(self, *args, **kwargs) -> Any:
        """
        Create object

        Args:
            *args: passed to object
            **kwargs: passed to object

        Returns:
            Any
        """
        return self.conv_cls(self.dim, *args, **kwargs)


class BaseConvNormAct(torch.nn.Sequential):
    def __init__(self,
                 dim: int,
                 in_channels: int,
                 out_channels: int,
                 norm: Optional[Union[Callable[..., Type[nn.Module]], str]],
                 act: Optional[Union[Callable[..., Type[nn.Module]], str]],
                 kernel_size: Union[int, tuple],
                 stride: Union[int, tuple] = 1,
                 padding: Union[int, tuple] = 0,
                 dilation: Union[int, tuple] = 1,
                 groups: int = 1,
                 bias: bool = None,
                 transposed: bool = False,
                 norm_kwargs: Optional[dict] = None,
                 act_inplace: Optional[bool] = None,
                 act_kwargs: Optional[dict] = None,
                 initializer: Callable[[nn.Module], None] = None,
                 ):
        """
        Baseclass for default ordering:
        conv -> norm -> activation

        Args
            dim: number of dimensions the convolution should be chosen for
            in_channels: input channels
            out_channels: output_channels
            norm: type of normalization. If None, no normalization will be applied
            kernel_size: size of convolution kernel
            act: class of non linearity; if None no actication is used.
            stride: convolution stride
            padding: padding value
                (if input or output padding depends on whether the convolution
                is transposed or not)
            dilation: convolution dilation
            groups: number of convolution groups
            bias: whether to include bias or not
                If None, the bias will be determined dynamicaly: False
                if a normalization follows otherwise True
            transposed: whether the convolution should be transposed or not
            norm_kwargs: keyword arguments for normalization layer
            act_inplace: whether to perform activation inplce or not
                If None, inplace will be determined dynamicaly: True
                if a normalization follows otherwise False
            act_kwargs: keyword arguments for non linearity layer.
            initializer: initilize weights
        """
        super().__init__()
        # process optional arguments
        norm_kwargs = {} if norm_kwargs is None else norm_kwargs
        act_kwargs = {} if act_kwargs is None else act_kwargs

        if "inplace" in act_kwargs:
            raise ValueError("Use keyword argument to en-/disable inplace activations")
        if act_inplace is None:
            act_inplace = bool(norm is not None)
        act_kwargs["inplace"] = act_inplace

        # process dynamic values
        bias = bool(norm is None) if bias is None else bias

        conv = nd_conv(dim=dim,
                       in_channels=in_channels,
                       out_channels=out_channels,
                       kernel_size=kernel_size,
                       stride=stride,
                       padding=padding,
                       dilation=dilation,
                       groups=groups,
                       bias=bias,
                       transposed=transposed
                       )
        self.add_module("conv", conv)

        if norm is not None:
            if isinstance(norm, str):
                _norm = nd_norm(norm, dim, out_channels, **norm_kwargs)
            else:
                _norm = norm(dim, out_channels, **norm_kwargs)
            self.add_module("norm", _norm)

        if act is not None:
            if isinstance(act, str):
                _act = nd_act(act, dim, **act_kwargs)
            else:
                _act = act(**act_kwargs)
            self.add_module("act", _act)

        if initializer is not None:
            self.apply(initializer)


class ConvInstanceRelu(BaseConvNormAct):
    def __init__(self,
                 dim: int,
                 in_channels: int,
                 out_channels: int,
                 kernel_size: Union[int, tuple],
                 stride: Union[int, tuple] = 1,
                 padding: Union[int, tuple] = 0,
                 dilation: Union[int, tuple] = 1,
                 groups: int = 1,
                 bias: bool = None,
                 transposed: bool = False,
                 add_norm: bool = True,
                 add_act: bool = True,
                 act_inplace: Optional[bool] = None,
                 norm_eps: float = 1e-5,
                 norm_affine: bool = True,
                 initializer: Callable[[nn.Module], None] = None,
                 ):
        """
        Baseclass for default ordering:
        conv -> norm -> activation

        Args
            dim: number of dimensions the convolution should be chosen for
            in_channels: input channels
            out_channels: output_channels
            norm: type of normalization. If None, no normalization will be applied
            kernel_size: size of convolution kernel
            act: class of non linearity; if None no actication is used.
            stride: convolution stride
            padding: padding value
                (if input or output padding depends on whether the convolution
                is transposed or not)
            dilation: convolution dilation
            groups: number of convolution groups
            bias: whether to include bias or not
                If None the bias will be determined dynamicaly: False
                if a normalization follows otherwise True
            transposed: whether the convolution should be transposed or not
            add_norm: add normalisation layer to conv block
            add_act: add activation layer to conv block
            act_inplace: whether to perform activation inplce or not
                If None, inplace will be determined dynamicaly: True
                if a normalization follows otherwise False
            norm_eps: instance norm eps (see pytorch for more info)
            norm_affine: instance affine parameter (see pytorch for more info)
            initializer: initilize weights
        """
        norm = "Instance" if add_norm else None
        act = "ReLU" if add_act else None
        
        super().__init__(
            dim=dim,
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            stride=stride,
            padding=padding,
            dilation=dilation,
            groups=groups,
            bias=bias,
            transposed=transposed,
            norm=norm,
            act=act,
            norm_kwargs={
                "eps": norm_eps,
                "affine": norm_affine,
            },
            act_inplace=act_inplace,
            initializer=initializer,
        )


class ConvGroupRelu(BaseConvNormAct):
    def __init__(self,
                 dim: int,
                 in_channels: int,
                 out_channels: int,
                 kernel_size: Union[int, tuple],
                 stride: Union[int, tuple] = 1,
                 padding: Union[int, tuple] = 0,
                 dilation: Union[int, tuple] = 1,
                 groups: int = 1,
                 bias: bool = None,
                 transposed: bool = False,
                 add_norm: bool = True,
                 add_act: bool = True,
                 act_inplace: Optional[bool] = None,
                 norm_eps: float = 1e-5,
                 norm_affine: bool = True,
                 norm_channels_per_group: int = 16,
                 initializer: Callable[[nn.Module], None] = None,
                 ):
        """
        Baseclass for default ordering:
        conv -> norm -> activation

        Args
            dim: number of dimensions the convolution should be chosen for
            in_channels: input channels
            out_channels: output_channels
            norm: type of normalization. If None, no normalization will be applied
            kernel_size: size of convolution kernel
            act: class of non linearity; if None no actication is used.
            stride: convolution stride
            padding: padding value
                (if input or output padding depends on whether the convolution
                is transposed or not)
            dilation: convolution dilation
            groups: number of convolution groups
            bias: whether to include bias or not
                If None the bias will be determined dynamicaly: False
                if a normalization follows otherwise True
            transposed: whether the convolution should be transposed or not
            add_norm: add normalisation layer to conv block
            add_act: add activation layer to conv block
            act_inplace: whether to perform activation inplce or not
                If None, inplace will be determined dynamicaly: True
                if a normalization follows otherwise False
            norm_eps: instance norm eps (see pytorch for more info)
            norm_affine: instance affine parameter (see pytorch for more info)
            norm_channels_per_group: channels per group for group norm
            initializer: initilize weights
        """
        norm = "Group" if add_norm else None
        act = "ReLU" if add_act else None
        
        super().__init__(
            dim=dim,
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
            stride=stride,
            padding=padding,
            dilation=dilation,
            groups=groups,
            bias=bias,
            transposed=transposed,
            norm=norm,
            act=act,
            norm_kwargs={
                "eps": norm_eps,
                "affine": norm_affine,
                "channels_per_group": norm_channels_per_group,
            },
            act_inplace=act_inplace,
            initializer=initializer,
        )


def nd_conv(dim: int,
            in_channels: int,
            out_channels: int,
            kernel_size: Union[int, tuple],
            stride: Union[int, tuple] = 1,
            padding: Union[int, tuple] = 0,
            dilation: Union[int, tuple] = 1,
            groups: int = 1,
            bias: bool = True,
            transposed: bool = False,
            **kwargs,
            ) -> torch.nn.Module:
    """
    Convolution Wrapper to Switch accross dimensions and transposed by a
    single argument

    Args
        n_dim (int): number of dimensions the convolution should be chosen for
        in_channels (int): input channels
        out_channels (int): output_channels
        kernel_size (int or Iterable): size of convolution kernel
        stride (int or Iterable): convolution stride
        padding (int or Iterable): padding value
            (if input or output padding depends on whether the convolution
            is transposed or not)
        dilation (int or Iterable): convolution dilation
        groups (int): number of convolution groups
        bias (bool): whether to include bias or not
        transposed (bool): whether the convolution should be transposed or not

    Returns:
        torch.nn.Module: generated module

    See Also
        Torch Convolutions:
            * :class:`torch.nn.Conv1d`
            * :class:`torch.nn.Conv2d`
            * :class:`torch.nn.Conv3d`
            * :class:`torch.nn.ConvTranspose1d`
            * :class:`torch.nn.ConvTranspose2d`
            * :class:`torch.nn.ConvTranspose3d`
    """
    if transposed:
        transposed_str = "Transpose"
    else:
        transposed_str = ""

    conv_cls = getattr(torch.nn, f"Conv{transposed_str}{dim}d")

    return conv_cls(in_channels=in_channels, out_channels=out_channels,
                    kernel_size=kernel_size, stride=stride, padding=padding,
                    dilation=dilation, groups=groups, bias=bias, **kwargs)


def nd_pool(pooling_type: str, dim: int, *args, **kwargs) -> torch.nn.Module:
    """
    Wrapper to switch between different pooling types and convolutions by a single argument

    Args
        pooling_type (str): Type of Pooling, case sensitive.
                Supported values are
                * ``Max``
                * ``Avg``
                * ``AdaptiveAvg``
                * ``AdaptiveMax``
        n_dim (int): number of dimensions
        *args : positional arguments of the chosen pooling class
        **kwargs : keyword arguments of the chosen pooling class

    Returns:
        torch.nn.Module: generated module

    See Also
        Torch Pooling Classes:
            * :class:`torch.nn.MaxPool1d`
            * :class:`torch.nn.MaxPool2d`
            * :class:`torch.nn.MaxPool3d`
            * :class:`torch.nn.AvgPool1d`
            * :class:`torch.nn.AvgPool2d`
            * :class:`torch.nn.AvgPool3d`
            * :class:`torch.nn.AdaptiveMaxPool1d`
            * :class:`torch.nn.AdaptiveMaxPool2d`
            * :class:`torch.nn.AdaptiveMaxPool3d`
            * :class:`torch.nn.AdaptiveAvgPool1d`
            * :class:`torch.nn.AdaptiveAvgPool2d`
            * :class:`torch.nn.AdaptiveAvgPool3d`
    """
    pool_cls = getattr(torch.nn, f"{pooling_type}Pool{dim}d")
    return pool_cls(*args, **kwargs)


def nd_norm(norm_type: str, dim: int, *args, **kwargs) -> torch.nn.Module:
    """
    Wrapper to switch between different types of normalization and
    dimensions by a single argument

    Args
        norm_type (str): type of normalization, case sensitive.
            Supported types are:
                * ``Batch``
                * ``Instance``
                * ``LocalResponse``
                * ``Group``
                * ``Layer``
        n_dim (int, None): dimension of normalization input; can be None if normalization
            is dimension-agnostic (e.g. LayerNorm)
        *args : positional arguments of chosen normalization class
        **kwargs : keyword arguments of chosen normalization class

    Returns
        torch.nn.Module: generated module

    See Also
        Torch Normalizations:
                * :class:`torch.nn.BatchNorm1d`
                * :class:`torch.nn.BatchNorm2d`
                * :class:`torch.nn.BatchNorm3d`
                * :class:`torch.nn.InstanceNorm1d`
                * :class:`torch.nn.InstanceNorm2d`
                * :class:`torch.nn.InstanceNorm3d`
                * :class:`torch.nn.LocalResponseNorm`
mibaumgartner's avatar
mibaumgartner committed
418
                * :class:`nndet.arch.layers.norm.GroupNorm`
mibaumgartner's avatar
models  
mibaumgartner committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
    """
    if dim is None:
        dim_str = ""
    else:
        dim_str = str(dim)

    if norm_type.lower() == "group":
        norm_cls = GroupNorm
    else:
        norm_cls = getattr(torch.nn, f"{norm_type}Norm{dim_str}d")
    return norm_cls(*args, **kwargs)


def nd_act(act_type: str, dim: int, *args, **kwargs) -> torch.nn.Module:
    """
    Helper to search for activations by string
    The dim parameter is ignored.
    Searches in torch.nn for activatio.

    Args:
        act_type: name of activation layer to look up.
        dim: ignored

    Returns:
        torch.nn.Module: activation module
    """
    act_cls = getattr(torch.nn, f"{act_type}")
    return act_cls(*args, **kwargs)


def nd_dropout(dim: int, p: float = 0.5, inplace: bool = False, **kwargs) -> torch.nn.Module:
    """
    Generate 1,2,3 dimensional dropout

    Args:
        dim (int): number of dimensions
        p (float): doupout probability
        inplace (bool): apply operation inplace
        **kwargs: passed to dropout

    Returns:
        torch.nn.Module: generated module
    """
    dropout_cls = getattr(torch.nn, "Dropout%dd" % dim)
    return dropout_cls(p=p, inplace=inplace, **kwargs)


def compute_padding_for_kernel(kernel_size: Union[int, Sequence[int]]) -> \
        Union[int, Tuple[int, int], Tuple[int, int, int]]:
    """
    Compute padding such that feature maps keep their size with stride 1

    Args:
        kernel_size: kernel size to compute padding for

    Returns:
        Union[int, Tuple[int, int], Tuple[int, int, int]]: computed padding
    """
    if isinstance(kernel_size, Sequence):
        padding = tuple([(i - 1) // 2 for i in kernel_size])
    else:
        padding = (kernel_size - 1) // 2
    return padding


def conv_kwargs_helper(norm: bool, activation: bool):
    """
    Helper to force disable normalization and activation in layers
    which have those by default

    Args:
        norm: en-/disable normalization layer
        activation: en-/disable activation layer

    Returns:
        dict: keyword arguments to pass to conv generator
    """
    kwargs = {
        "add_norm": norm,
        "add_act": activation,
    }
    return kwargs