Simulai models unet

U-Net#

Bases: NetworkTemplate

Source code in simulai/models/_pytorch_models/_unet.py
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
class UNet(NetworkTemplate):
    def __init__(
        self,
        layers_config: Dict = None,
        intermediary_outputs_indices: List[int] = None,
        intermediary_inputs_indices: List[int] = None,
        encoder_extra_args: Dict = dict(),
        decoder_extra_args: Dict = dict(),
    ) -> None:
        """U-Net.

        Args:
            layers_config (Dict, optional): A dictionary containing the complete configuration for the
            U-Net encoder and decoder. (Default value = None)
            intermediary_outputs_indices (List[int], optional): A list of indices for indicating the encoder outputs. (Default value = None)
            intermediary_inputs_indices (List[int], optional): A list of indices for indicating the decoder inputs. (Default value = None)
            encoder_extra_args (Dict, optional): A dictionary containing extra arguments for the encoder. (Default value = dict())
            decoder_extra_args (Dict, optional): A dictionary containing extra arguments for the decoder. (Default value = dict())

        """

        super(UNet, self).__init__()

        self.layers_config = layers_config
        self.intermediary_outputs_indices = intermediary_outputs_indices
        self.intermediary_inputs_indices = intermediary_inputs_indices

        self.layers_config_encoder = self.layers_config["encoder"]
        self.layers_config_decoder = self.layers_config["decoder"]

        self.encoder_activations = self.layers_config["encoder_activations"]
        self.decoder_activations = self.layers_config["decoder_activations"]

        self.encoder_horizontal_outputs = dict()

        # Configuring the encoder
        encoder_type = self.layers_config_encoder.get("type")
        layers_config_encoder = self.layers_config_encoder.get("architecture")

        if encoder_type == "cnn":
            self.encoder = CNNUnetEncoder(
                layers=self.layers_config_encoder["architecture"],
                activations=self.encoder_activations,
                intermediary_outputs_indices=self.intermediary_outputs_indices,
                case="2d",
                name="encoder",
                **encoder_extra_args,
            )
        else:
            raise Exception(f"Option {encoder_type} is not available.")

        # Configuring the decoder
        decoder_type = self.layers_config_decoder.get("type")
        layers_config_encoder = self.layers_config_encoder.get("architecture")

        if encoder_type == "cnn":
            self.decoder = CNNUnetDecoder(
                layers=self.layers_config_decoder["architecture"],
                activations=self.decoder_activations,
                intermediary_inputs_indices=self.intermediary_inputs_indices,
                case="2d",
                name="decoder",
                **decoder_extra_args,
            )
        else:
            raise Exception(f"Option {encoder_type} is not available.")

        self.add_module("encoder", self.encoder)
        self.add_module("decoder", self.decoder)

    @as_tensor
    def forward(
        self, input_data: Union[torch.Tensor, np.ndarray] = None
    ) -> torch.Tensor:
        """The U-Net forward method.

        Args:
            input_data (Union[torch.Tensor, np.ndarray], optional): A dataset to be inputted in the CNN U-Net encoder. (Default value = None)

        Returns:
            torch.Tensor: The U-Net output.

        """

        encoder_main_output, encoder_intermediary_outputs = self.encoder(
            input_data=input_data
        )
        output = self.decoder(
            input_data=encoder_main_output,
            intermediary_encoder_outputs=encoder_intermediary_outputs,
        )

        return output

    def summary(self):
        """It shows a general view of the architecture."""

        print(self)

__init__(layers_config=None, intermediary_outputs_indices=None, intermediary_inputs_indices=None, encoder_extra_args=dict(), decoder_extra_args=dict()) #

U-Net.

Parameters:

Name Type Description Default
layers_config Dict

A dictionary containing the complete configuration for the

None
intermediary_outputs_indices List[int]

A list of indices for indicating the encoder outputs. (Default value = None)

None
intermediary_inputs_indices List[int]

A list of indices for indicating the decoder inputs. (Default value = None)

None
encoder_extra_args Dict

A dictionary containing extra arguments for the encoder. (Default value = dict())

dict()
decoder_extra_args Dict

A dictionary containing extra arguments for the decoder. (Default value = dict())

dict()
Source code in simulai/models/_pytorch_models/_unet.py
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
def __init__(
    self,
    layers_config: Dict = None,
    intermediary_outputs_indices: List[int] = None,
    intermediary_inputs_indices: List[int] = None,
    encoder_extra_args: Dict = dict(),
    decoder_extra_args: Dict = dict(),
) -> None:
    """U-Net.

    Args:
        layers_config (Dict, optional): A dictionary containing the complete configuration for the
        U-Net encoder and decoder. (Default value = None)
        intermediary_outputs_indices (List[int], optional): A list of indices for indicating the encoder outputs. (Default value = None)
        intermediary_inputs_indices (List[int], optional): A list of indices for indicating the decoder inputs. (Default value = None)
        encoder_extra_args (Dict, optional): A dictionary containing extra arguments for the encoder. (Default value = dict())
        decoder_extra_args (Dict, optional): A dictionary containing extra arguments for the decoder. (Default value = dict())

    """

    super(UNet, self).__init__()

    self.layers_config = layers_config
    self.intermediary_outputs_indices = intermediary_outputs_indices
    self.intermediary_inputs_indices = intermediary_inputs_indices

    self.layers_config_encoder = self.layers_config["encoder"]
    self.layers_config_decoder = self.layers_config["decoder"]

    self.encoder_activations = self.layers_config["encoder_activations"]
    self.decoder_activations = self.layers_config["decoder_activations"]

    self.encoder_horizontal_outputs = dict()

    # Configuring the encoder
    encoder_type = self.layers_config_encoder.get("type")
    layers_config_encoder = self.layers_config_encoder.get("architecture")

    if encoder_type == "cnn":
        self.encoder = CNNUnetEncoder(
            layers=self.layers_config_encoder["architecture"],
            activations=self.encoder_activations,
            intermediary_outputs_indices=self.intermediary_outputs_indices,
            case="2d",
            name="encoder",
            **encoder_extra_args,
        )
    else:
        raise Exception(f"Option {encoder_type} is not available.")

    # Configuring the decoder
    decoder_type = self.layers_config_decoder.get("type")
    layers_config_encoder = self.layers_config_encoder.get("architecture")

    if encoder_type == "cnn":
        self.decoder = CNNUnetDecoder(
            layers=self.layers_config_decoder["architecture"],
            activations=self.decoder_activations,
            intermediary_inputs_indices=self.intermediary_inputs_indices,
            case="2d",
            name="decoder",
            **decoder_extra_args,
        )
    else:
        raise Exception(f"Option {encoder_type} is not available.")

    self.add_module("encoder", self.encoder)
    self.add_module("decoder", self.decoder)

forward(input_data=None) #

The U-Net forward method.

Parameters:

Name Type Description Default
input_data Union[Tensor, ndarray]

A dataset to be inputted in the CNN U-Net encoder. (Default value = None)

None

Returns:

Type Description
Tensor

torch.Tensor: The U-Net output.

Source code in simulai/models/_pytorch_models/_unet.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
@as_tensor
def forward(
    self, input_data: Union[torch.Tensor, np.ndarray] = None
) -> torch.Tensor:
    """The U-Net forward method.

    Args:
        input_data (Union[torch.Tensor, np.ndarray], optional): A dataset to be inputted in the CNN U-Net encoder. (Default value = None)

    Returns:
        torch.Tensor: The U-Net output.

    """

    encoder_main_output, encoder_intermediary_outputs = self.encoder(
        input_data=input_data
    )
    output = self.decoder(
        input_data=encoder_main_output,
        intermediary_encoder_outputs=encoder_intermediary_outputs,
    )

    return output

summary() #

It shows a general view of the architecture.

Source code in simulai/models/_pytorch_models/_unet.py
280
281
282
283
def summary(self):
    """It shows a general view of the architecture."""

    print(self)