credit.models.unet_attention_modules
====================================

.. py:module:: credit.models.unet_attention_modules


Classes
-------

.. autoapisummary::

   credit.models.unet_attention_modules.ECABlock
   credit.models.unet_attention_modules.CoordinateAttention
   credit.models.unet_attention_modules.LightSpatialAttention
   credit.models.unet_attention_modules.SCSEAttention
   credit.models.unet_attention_modules.EfficientMixedAttention


Functions
---------

.. autoapisummary::

   credit.models.unet_attention_modules.load_unet_attention


Module Contents
---------------

.. py:function:: load_unet_attention(attention_type, out_chans, reduction=32, spatial_kernel=7)

   Factory function to load different attention mechanisms for UNet.

   :param attention_type: 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
   :type attention_type: str
   :param out_chans: Number of output channels
   :type out_chans: int
   :param reduction: Reduction ratio for channel attention mechanisms (default: 32)
   :type reduction: int
   :param spatial_kernel: Kernel size for spatial attention (default: 7)
   :type spatial_kernel: int

   :returns: Attention module instance or None
   :rtype: nn.Module or None

   :raises ValueError: If attention_type is not recognized


.. py:class:: ECABlock(channels, gamma=2, b=1)

   Bases: :py:obj:`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 :meth:`to`, etc.

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

   :ivar training: Boolean represents whether this module is in training or
                   evaluation mode.
   :vartype training: bool


   .. py:attribute:: avg_pool


   .. py:attribute:: conv


   .. py:attribute:: sigmoid


   .. py:method:: forward(x)


.. py:class:: CoordinateAttention(channels, reduction=32)

   Bases: :py:obj:`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 :meth:`to`, etc.

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

   :ivar training: Boolean represents whether this module is in training or
                   evaluation mode.
   :vartype training: bool


   .. py:attribute:: pool_h


   .. py:attribute:: pool_w


   .. py:attribute:: conv1


   .. py:attribute:: bn1


   .. py:attribute:: act


   .. py:attribute:: conv_h


   .. py:attribute:: conv_w


   .. py:attribute:: sigmoid


   .. py:method:: forward(x)


.. py:class:: LightSpatialAttention(kernel_size=7)

   Bases: :py:obj:`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 :meth:`to`, etc.

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

   :ivar training: Boolean represents whether this module is in training or
                   evaluation mode.
   :vartype training: bool


   .. py:attribute:: conv


   .. py:attribute:: sigmoid


   .. py:method:: forward(x)


.. py:class:: SCSEAttention(channels, reduction=16, use_depthwise=True)

   Bases: :py:obj:`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 :meth:`to`, etc.

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

   :ivar training: Boolean represents whether this module is in training or
                   evaluation mode.
   :vartype training: bool


   .. py:attribute:: cse


   .. py:method:: forward(x)


.. py:class:: EfficientMixedAttention(channels, reduction=16, spatial_kernel=7)

   Bases: :py:obj:`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 :meth:`to`, etc.

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

   :ivar training: Boolean represents whether this module is in training or
                   evaluation mode.
   :vartype training: bool


   .. py:attribute:: channel_attention


   .. py:attribute:: spatial_attention


   .. py:method:: forward(x)


