Skip to content

config.py

High level definition of CNN architectures.

@author: Nicola VIGANĂ’, CEA-MEM, Grenoble, France

NetworkParams

Bases: ABC

Abstract base class for storing network parameters.

Source code in src/autoden/models/config.py
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
class NetworkParams(ABC):
    """Abstract base class for storing network parameters."""

    n_channels_in: int
    n_channels_out: int
    n_features: int

    def __init__(self, n_features: int, n_channels_in: int = 1, n_channels_out: int = 1) -> None:
        self.n_channels_in = n_channels_in
        self.n_channels_out = n_channels_out
        self.n_features = n_features

    def __repr__(self) -> str:
        """Produce the string representation of the object.

        Returns
        -------
        str
            The string representation.
        """
        return self.__class__.__name__ + " {\n" + ",\n".join([f"  {k} = {v}" for k, v in self.__dict__.items()]) + "\n}"

    @abstractmethod
    def get_model(self, device: str = "cuda" if is_cuda_available() else "cpu") -> Module:
        """Get the associated model with the selected parameters.

        Parameters
        ----------
        device : str, optional
            The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

        Returns
        -------
        Module
            The model.
        """

__repr__()

Produce the string representation of the object.

Returns:

Type Description
str

The string representation.

Source code in src/autoden/models/config.py
33
34
35
36
37
38
39
40
41
def __repr__(self) -> str:
    """Produce the string representation of the object.

    Returns
    -------
    str
        The string representation.
    """
    return self.__class__.__name__ + " {\n" + ",\n".join([f"  {k} = {v}" for k, v in self.__dict__.items()]) + "\n}"

get_model(device='cuda' if is_cuda_available() else 'cpu') abstractmethod

Get the associated model with the selected parameters.

Parameters:

Name Type Description Default
device str

The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

'cuda' if is_available() else 'cpu'

Returns:

Type Description
Module

The model.

Source code in src/autoden/models/config.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@abstractmethod
def get_model(self, device: str = "cuda" if is_cuda_available() else "cpu") -> Module:
    """Get the associated model with the selected parameters.

    Parameters
    ----------
    device : str, optional
        The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

    Returns
    -------
    Module
        The model.
    """

NetworkParamsDnCNN

Bases: NetworkParams

Store DnCNN parameters.

Source code in src/autoden/models/config.py
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
class NetworkParamsDnCNN(NetworkParams):
    """Store DnCNN parameters."""

    n_layers: int

    def __init__(self, n_channels_in: int = 1, n_channels_out: int = 1, n_layers: int = 20, n_features: int = 64) -> None:
        """Initialize the DnCNN network parameters definition.

        Parameters
        ----------
        n_channels_in : int, optional
            Number of input channels. Default is 1.
        n_channels_out : int, optional
            Number of output channels. Default is 1.
        n_layers : int, optional
            Number of layers. Default is 20.
        n_features : int, optional
            Number of features. Default is 64.
        """
        super().__init__(n_features=n_features, n_channels_in=n_channels_in, n_channels_out=n_channels_out)
        self.n_layers = n_layers

    def get_model(self, device: str = "cuda" if is_cuda_available() else "cpu") -> Module:
        """Get a DnCNN model with the selected parameters.

        Parameters
        ----------
        device : str, optional
            The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

        Returns
        -------
        Module
            The DnCNN model.
        """
        return DnCNN(
            n_channels_in=self.n_channels_in,
            n_channels_out=self.n_channels_out,
            n_layers=self.n_layers,
            n_features=self.n_features,
            device=device,
        )

__init__(n_channels_in=1, n_channels_out=1, n_layers=20, n_features=64)

Initialize the DnCNN network parameters definition.

Parameters:

Name Type Description Default
n_channels_in int

Number of input channels. Default is 1.

1
n_channels_out int

Number of output channels. Default is 1.

1
n_layers int

Number of layers. Default is 20.

20
n_features int

Number of features. Default is 64.

64
Source code in src/autoden/models/config.py
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def __init__(self, n_channels_in: int = 1, n_channels_out: int = 1, n_layers: int = 20, n_features: int = 64) -> None:
    """Initialize the DnCNN network parameters definition.

    Parameters
    ----------
    n_channels_in : int, optional
        Number of input channels. Default is 1.
    n_channels_out : int, optional
        Number of output channels. Default is 1.
    n_layers : int, optional
        Number of layers. Default is 20.
    n_features : int, optional
        Number of features. Default is 64.
    """
    super().__init__(n_features=n_features, n_channels_in=n_channels_in, n_channels_out=n_channels_out)
    self.n_layers = n_layers

get_model(device='cuda' if is_cuda_available() else 'cpu')

Get a DnCNN model with the selected parameters.

Parameters:

Name Type Description Default
device str

The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

'cuda' if is_available() else 'cpu'

Returns:

Type Description
Module

The DnCNN model.

Source code in src/autoden/models/config.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
def get_model(self, device: str = "cuda" if is_cuda_available() else "cpu") -> Module:
    """Get a DnCNN model with the selected parameters.

    Parameters
    ----------
    device : str, optional
        The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

    Returns
    -------
    Module
        The DnCNN model.
    """
    return DnCNN(
        n_channels_in=self.n_channels_in,
        n_channels_out=self.n_channels_out,
        n_layers=self.n_layers,
        n_features=self.n_features,
        device=device,
    )

NetworkParamsMSD

Bases: NetworkParams

Store MS-D net parameters.

Source code in src/autoden/models/config.py
 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
class NetworkParamsMSD(NetworkParams):
    """Store MS-D net parameters."""

    dilations: Sequence[int] | NDArray[np.integer]
    n_layers: int

    def __init__(
        self,
        n_channels_in: int = 1,
        n_channels_out: int = 1,
        n_layers: int = 80,
        n_features: int = 1,
        dilations: Sequence[int] | NDArray[np.integer] = np.arange(1, 10),
    ) -> None:
        """Initialize the MS-D network parameters definition.

        Parameters
        ----------
        n_channels_in : int, optional
            Number of input channels, by default 1.
        n_channels_out : int, optional
            Number of output channels, by default 1.
        n_layers : int, optional
            Number of layers in the network, by default 80.
        n_features : int, optional
            Number of features, by default 1.
        dilations : Sequence[int] | NDArray[np.integer], optional
            Dilation values for the network, by default np.arange(1, 10).
        """
        super().__init__(n_features=n_features, n_channels_in=n_channels_in, n_channels_out=n_channels_out)
        self.n_layers = n_layers
        self.dilations = dilations

    def get_model(self, device: str = "cuda" if is_cuda_available() else "cpu") -> Module:
        """Get a MS-D net model with the selected parameters.

        Parameters
        ----------
        device : str, optional
            The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

        Returns
        -------
        Module
            The model.
        """
        return MSDnet(
            self.n_channels_in,
            self.n_channels_out,
            n_layers=self.n_layers,
            n_features=self.n_features,
            dilations=list(self.dilations),
            device=device,
        )

__init__(n_channels_in=1, n_channels_out=1, n_layers=80, n_features=1, dilations=np.arange(1, 10))

Initialize the MS-D network parameters definition.

Parameters:

Name Type Description Default
n_channels_in int

Number of input channels, by default 1.

1
n_channels_out int

Number of output channels, by default 1.

1
n_layers int

Number of layers in the network, by default 80.

80
n_features int

Number of features, by default 1.

1
dilations Sequence[int] | NDArray[integer]

Dilation values for the network, by default np.arange(1, 10).

arange(1, 10)
Source code in src/autoden/models/config.py
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
def __init__(
    self,
    n_channels_in: int = 1,
    n_channels_out: int = 1,
    n_layers: int = 80,
    n_features: int = 1,
    dilations: Sequence[int] | NDArray[np.integer] = np.arange(1, 10),
) -> None:
    """Initialize the MS-D network parameters definition.

    Parameters
    ----------
    n_channels_in : int, optional
        Number of input channels, by default 1.
    n_channels_out : int, optional
        Number of output channels, by default 1.
    n_layers : int, optional
        Number of layers in the network, by default 80.
    n_features : int, optional
        Number of features, by default 1.
    dilations : Sequence[int] | NDArray[np.integer], optional
        Dilation values for the network, by default np.arange(1, 10).
    """
    super().__init__(n_features=n_features, n_channels_in=n_channels_in, n_channels_out=n_channels_out)
    self.n_layers = n_layers
    self.dilations = dilations

get_model(device='cuda' if is_cuda_available() else 'cpu')

Get a MS-D net model with the selected parameters.

Parameters:

Name Type Description Default
device str

The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

'cuda' if is_available() else 'cpu'

Returns:

Type Description
Module

The model.

Source code in src/autoden/models/config.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def get_model(self, device: str = "cuda" if is_cuda_available() else "cpu") -> Module:
    """Get a MS-D net model with the selected parameters.

    Parameters
    ----------
    device : str, optional
        The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

    Returns
    -------
    Module
        The model.
    """
    return MSDnet(
        self.n_channels_in,
        self.n_channels_out,
        n_layers=self.n_layers,
        n_features=self.n_features,
        dilations=list(self.dilations),
        device=device,
    )

NetworkParamsUNet

Bases: NetworkParams

Store UNet parameters.

Source code in src/autoden/models/config.py
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
class NetworkParamsUNet(NetworkParams):
    """Store UNet parameters."""

    n_levels: int

    DEFAULT_LEVELS: int = 3
    DEFAULT_FEATURES: int = 32

    def __init__(
        self,
        n_channels_in: int = 1,
        n_channels_out: int = 1,
        n_levels: int = DEFAULT_LEVELS,
        n_features: int = DEFAULT_FEATURES,
        n_channels_skip: int | None = None,
        bilinear: bool = True,
        pad_mode: str = "replicate",
    ) -> None:
        """Initialize the UNet network parameters definition.

        Parameters
        ----------
        n_channels_in : int, optional
            Number of input channels. Default is 1.
        n_channels_out : int, optional
            Number of output channels. Default is 1.
        n_levels : int, optional
            Number of levels in the UNet. Default is 3.
        n_features : int, optional
            Number of features in the UNet. Default is 32.
        n_channels_skip : int, optional
            Number of skip connections channels. Default is None.
        bilinear : bool, optional
            Whether to use bilinear interpolation. Default is True.
        pad_mode : str, optional
            Padding mode for convolutional layers. Default is "replicate".
        """
        super().__init__(n_features=n_features, n_channels_in=n_channels_in, n_channels_out=n_channels_out)
        self.n_levels = n_levels
        self.n_channels_skip = n_channels_skip
        self.bilinear = bilinear
        self.pad_mode = pad_mode

    def get_model(self, device: str = "cuda" if is_cuda_available() else "cpu") -> Module:
        """Get a U-net model with the selected parameters.

        Parameters
        ----------
        device : str, optional
            The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

        Returns
        -------
        Module
            The U-net model.
        """
        return UNet(
            n_channels_in=self.n_channels_in,
            n_channels_out=self.n_channels_out,
            n_features=self.n_features,
            n_levels=self.n_levels,
            n_channels_skip=self.n_channels_skip,
            bilinear=self.bilinear,
            pad_mode=self.pad_mode,
            device=device,
        )

__init__(n_channels_in=1, n_channels_out=1, n_levels=DEFAULT_LEVELS, n_features=DEFAULT_FEATURES, n_channels_skip=None, bilinear=True, pad_mode='replicate')

Initialize the UNet network parameters definition.

Parameters:

Name Type Description Default
n_channels_in int

Number of input channels. Default is 1.

1
n_channels_out int

Number of output channels. Default is 1.

1
n_levels int

Number of levels in the UNet. Default is 3.

DEFAULT_LEVELS
n_features int

Number of features in the UNet. Default is 32.

DEFAULT_FEATURES
n_channels_skip int

Number of skip connections channels. Default is None.

None
bilinear bool

Whether to use bilinear interpolation. Default is True.

True
pad_mode str

Padding mode for convolutional layers. Default is "replicate".

'replicate'
Source code in src/autoden/models/config.py
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
def __init__(
    self,
    n_channels_in: int = 1,
    n_channels_out: int = 1,
    n_levels: int = DEFAULT_LEVELS,
    n_features: int = DEFAULT_FEATURES,
    n_channels_skip: int | None = None,
    bilinear: bool = True,
    pad_mode: str = "replicate",
) -> None:
    """Initialize the UNet network parameters definition.

    Parameters
    ----------
    n_channels_in : int, optional
        Number of input channels. Default is 1.
    n_channels_out : int, optional
        Number of output channels. Default is 1.
    n_levels : int, optional
        Number of levels in the UNet. Default is 3.
    n_features : int, optional
        Number of features in the UNet. Default is 32.
    n_channels_skip : int, optional
        Number of skip connections channels. Default is None.
    bilinear : bool, optional
        Whether to use bilinear interpolation. Default is True.
    pad_mode : str, optional
        Padding mode for convolutional layers. Default is "replicate".
    """
    super().__init__(n_features=n_features, n_channels_in=n_channels_in, n_channels_out=n_channels_out)
    self.n_levels = n_levels
    self.n_channels_skip = n_channels_skip
    self.bilinear = bilinear
    self.pad_mode = pad_mode

get_model(device='cuda' if is_cuda_available() else 'cpu')

Get a U-net model with the selected parameters.

Parameters:

Name Type Description Default
device str

The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

'cuda' if is_available() else 'cpu'

Returns:

Type Description
Module

The U-net model.

Source code in src/autoden/models/config.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def get_model(self, device: str = "cuda" if is_cuda_available() else "cpu") -> Module:
    """Get a U-net model with the selected parameters.

    Parameters
    ----------
    device : str, optional
        The device that the the model should run on, by default "cuda" if cuda is available, otherwise "cpu".

    Returns
    -------
    Module
        The U-net model.
    """
    return UNet(
        n_channels_in=self.n_channels_in,
        n_channels_out=self.n_channels_out,
        n_features=self.n_features,
        n_levels=self.n_levels,
        n_channels_skip=self.n_channels_skip,
        bilinear=self.bilinear,
        pad_mode=self.pad_mode,
        device=device,
    )