minerva.models.nets.image.unetplusplus_resnet50

Classes

ConvBlock

Base class for all neural network modules.

DeepLabV3ResNet50Backbone

Base class for all neural network modules.

LitUNetPlusPlusDeepLabV3

Base class for all neural network modules.

UNetPlusPlusDeepLabV3

UNet++ with DeepLabV3 ResNet50 backbone for semantic segmentation.

Module Contents

class minerva.models.nets.image.unetplusplus_resnet50.ConvBlock(in_channels, out_channels)[source]

Bases: torch.nn.Module

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

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


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:
  • in_channels (int)

  • out_channels (int)

Double convolution block for UNet++ decoder.

Applies two consecutive 3x3 convolutions, each followed by batch normalization and ReLU activation, commonly used in U-Net architectures for feature refinement.

Parameters

in_channelsint

Number of input channels.

out_channelsint

Number of output channels.

conv
forward(x)[source]

Forward pass through the double convolution block.

Parameters

xtorch.Tensor

Input feature tensor of shape (batch_size, in_channels, H, W).

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

class minerva.models.nets.image.unetplusplus_resnet50.DeepLabV3ResNet50Backbone(pretrained=True)[source]

Bases: torch.nn.Module

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

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


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:

pretrained (bool)

Notes

The dilation rates are set as follows: - layer3: dilation=2, stride=1 - layer4: dilation=4, stride=1 This preserves the spatial resolution at H/8, W/8 for c3, c4, and c5.

References

recognition. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 770-778).

bn1
conv1
forward(x)[source]

Forward pass through the ResNet50 backbone.

Parameters

xtorch.Tensor

Input tensor of shape (batch_size, 3, H, W).

Parameters:

x (torch.Tensor)

Return type:

Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]

layer1
layer2
layer3
layer4
maxpool
relu
class minerva.models.nets.image.unetplusplus_resnet50.LitUNetPlusPlusDeepLabV3(in_channels=3, num_classes=6, deep_supervision=True, lr=0.0003, pretrained=True)[source]

Bases: lightning.LightningModule

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

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


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:
  • in_channels (int)

  • num_classes (int)

  • deep_supervision (bool)

  • lr (float)

  • pretrained (bool)

PyTorch Lightning module for UNet++ with DeepLabV3 backbone.

Wraps the UNet++ DeepLabV3 model with training, validation, and testing loops, optimizer configuration, and metrics for multi-class segmentation.

Parameters

in_channelsint, optional

Number of input image channels (default is 3).

num_classesint, optional

Number of segmentation classes (default is 6).

deep_supervisionbool, optional

If True, enables deep supervision training (default is True).

lrfloat, optional

Learning rate for the optimizer (default is 3e-4).

pretrainedbool, optional

If True, uses ImageNet pre-trained backbone (default is True).

Notes

Metrics include accuracy, F1-score, mean IoU, precision, and recall, all computed using torchmetrics.

configure_optimizers()[source]

Configure optimizer for training.

Return type:

torch.optim.Optimizer

forward(x)[source]

Forward pass through the model.

Parameters

xtorch.Tensor

Input batch of images of shape (batch_size, in_channels, H, W).

Parameters:

x (torch.Tensor)

Return type:

Union[torch.Tensor, List[torch.Tensor]]

loss_fn
lr = 0.0003
model
num_classes = 6
test_accuracy
test_f1
test_miou
test_precision
test_recall
test_step(batch, batch_idx)[source]

Test step for one batch with metrics evaluation.

Parameters

batchtuple of torch.Tensor

Batch containing (images, masks).

batch_idxint

Index of the current batch.

Parameters:
  • batch (Tuple[torch.Tensor, torch.Tensor])

  • batch_idx (int)

Return type:

torch.Tensor

training_step(batch, batch_idx)[source]

Training step for one batch.

Parameters

batchtuple of torch.Tensor

Batch containing (images, masks).

batch_idxint

Index of the current batch.

Parameters:
  • batch (Tuple[torch.Tensor, torch.Tensor])

  • batch_idx (int)

Return type:

torch.Tensor

validation_step(batch, batch_idx)[source]

Validation step for one batch.

Parameters

batchtuple of torch.Tensor

Batch containing (images, masks).

batch_idxint

Index of the current batch.

Parameters:
  • batch (Tuple[torch.Tensor, torch.Tensor])

  • batch_idx (int)

Return type:

torch.Tensor

class minerva.models.nets.image.unetplusplus_resnet50.UNetPlusPlusDeepLabV3(in_channels=3, num_classes=6, deep_supervision=True, pretrained=True)[source]

Bases: torch.nn.Module

UNet++ with DeepLabV3 ResNet50 backbone for semantic segmentation.

Combines DeepLabV3’s multi-scale feature extraction with UNet++’s nested skip connections for robust semantic segmentation, particularly suited for seismic image segmentation tasks.

Parameters

in_channelsint, optional

Number of input channels (default is 3 for RGB images).

num_classesint, optional

Number of segmentation classes (default is 6).

deep_supervisionbool, optional

If True, enables deep supervision with auxiliary losses (default is True).

pretrainedbool, optional

If True, uses ImageNet pre-trained ResNet50 backbone (default is True).

Notes

The architecture includes: - DeepLabV3 ResNet50 backbone with dilated convolutions. - UNet++ decoder with nested skip connections for feature refinement. - Bilinear upsampling to restore original input resolution. - Optional deep supervision for improved training stability.

References

Unet++: A nested u-net architecture for medical image segmentation. In Deep learning in medical image analysis and multimodal learning for clinical decision support (pp. 3-11). Springer. .. [2] He, K., Zhang, X., Ren, S., & Sun, J. (2016). Deep residual learning for image recognition. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 770-778).

_safe_upsample(x, target)[source]

Upsample tensor to match target spatial dimensions.

Handles cases where spatial dimensions may not align due to rounding errors in pooling or upsampling operations.

Parameters

xtorch.Tensor

Tensor to be upsampled.

targettorch.Tensor

Reference tensor with target spatial dimensions.

Parameters:
  • x (torch.Tensor)

  • target (torch.Tensor)

Return type:

torch.Tensor

backbone
conv0_1
conv0_2
conv0_3
conv1_1
conv1_2
conv2_1
deep_supervision = True
forward(x)[source]

Forward pass through the UNet++ DeepLabV3 network.

Parameters

xtorch.Tensor

Input tensor of shape (batch_size, in_channels, H, W).

Parameters:

x (torch.Tensor)

Return type:

Union[torch.Tensor, List[torch.Tensor]]

proj0
proj1
proj2
proj3
up
up_conv
Parameters:
  • in_channels (int)

  • num_classes (int)

  • deep_supervision (bool)

  • pretrained (bool)