credit.models.unet_attention_modules#

Classes#

ECABlock

Base class for all neural network modules.

CoordinateAttention

Base class for all neural network modules.

LightSpatialAttention

Base class for all neural network modules.

SCSEAttention

Base class for all neural network modules.

EfficientMixedAttention

Base class for all neural network modules.

Functions#

load_unet_attention(attention_type, out_chans[, ...])

Factory function to load different attention mechanisms for UNet.

Module Contents#

credit.models.unet_attention_modules.load_unet_attention(attention_type, out_chans, reduction=32, spatial_kernel=7)#

Factory function to load different attention mechanisms for UNet.

Parameters:
  • attention_type (str) – Type of attention mechanism - ‘coordinate’: CoordinateAttention - Best for rectangular images like 128x256 - ‘eca’: ECABlock - Most efficient for high channel counts - ‘spatial’: LightSpatialAttention - Minimal overhead, spatial focus - ‘scse_optimized’: OptimizedSCSEBlock with depthwise separable convs - ‘scse_standard’: OptimizedSCSEBlock with standard convs - ‘mixed’: EfficientMixedAttention - Balance of channel + spatial - None or ‘none’: No attention

  • out_chans (int) – Number of output channels

  • reduction (int) – Reduction ratio for channel attention mechanisms (default: 32)

  • spatial_kernel (int) – Kernel size for spatial attention (default: 7)

Returns:

Attention module instance or None

Return type:

nn.Module or None

Raises:

ValueError – If attention_type is not recognized

class credit.models.unet_attention_modules.ECABlock(channels, gamma=2, b=1)#

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.

avg_pool#
conv#
sigmoid#
forward(x)#
class credit.models.unet_attention_modules.CoordinateAttention(channels, reduction=32)#

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.

pool_h#
pool_w#
conv1#
bn1#
act#
conv_h#
conv_w#
sigmoid#
forward(x)#
class credit.models.unet_attention_modules.LightSpatialAttention(kernel_size=7)#

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.

conv#
sigmoid#
forward(x)#
class credit.models.unet_attention_modules.SCSEAttention(channels, reduction=16, use_depthwise=True)#

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.

cse#
forward(x)#
class credit.models.unet_attention_modules.EfficientMixedAttention(channels, reduction=16, spatial_kernel=7)#

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.

channel_attention#
spatial_attention#
forward(x)#