TFAttentionBlocks Module¶
Provides helper functions and classes for building and utilizing attention blocks in TensorFlow models.
- class HMB.TFAttentionBlocks.CoordAttention(*args, **kwargs)[source]¶
Bases:
LayerCoordinate Attention block implementation as a Keras Layer.
This block factorizes channel attention into two directional attention maps that encode positional information along height and width respectively.
- Parameters:
reduction (int) – Reduction ratio for the internal bottleneck. Default is 32.
Initialize CoordAttention.
- Parameters:
reduction (int) – Reduction ratio for the bottleneck. Default is 32.
- __init__(reduction=32, **kwargs)[source]¶
Initialize CoordAttention.
- Parameters:
reduction (int) – Reduction ratio for the bottleneck. Default is 32.
- class HMB.TFAttentionBlocks.SCSEBlock(*args, **kwargs)[source]¶
Bases:
LayerSpatial-Channel Squeeze-and-Excitation (scSE) block implementation as a Keras Layer.
This block combines channel Squeeze-and-Excitation (cSE) and a spatial squeeze-and-excitation (sSE) to recalibrate features both channel-wise and spatially.
- Parameters:
ratio (int) – Reduction ratio for the channel bottleneck. Default is 16.
Initialize the scSEBlock.
- Parameters:
ratio (int) – Reduction ratio for the channel bottleneck. Default is 16.
- __init__(ratio=16, **kwargs)[source]¶
Initialize the scSEBlock.
- Parameters:
ratio (int) – Reduction ratio for the channel bottleneck. Default is 16.
- class HMB.TFAttentionBlocks.CBAMBlock(*args, **kwargs)[source]¶
Bases:
LayerConvolutional Block Attention Module (CBAM) implementation as a Keras Layer. This layer applies channel and spatial attention mechanisms to refine feature maps.
- Parameters:
Examples
from HMB.TFAttentionBlocks import CBAMBlock from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, Input model = Sequential([ Input(shape=(64, 64, 128)), Conv2D(128, (3, 3), padding="same", activation="relu"), CBAMBlock(ratio=8, kernelSize=7), Conv2D(256, (3, 3), padding="same", activation="relu") ]) model.summary()
Initialize CBAM block with given parameters.
- Parameters:
- class HMB.TFAttentionBlocks.SEBlock(*args, **kwargs)[source]¶
Bases:
LayerSqueeze-and-Excitation (SE) block implementation as a Keras Layer. This layer adaptively recalibrates channel-wise feature responses by explicitly modeling interdependencies between channels.
- Parameters:
ratio (int) – Reduction ratio for the bottleneck in the SE block. Default is 16.
Examples
from HMB.TFAttentionBlocks import SEBlock from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, Input model = Sequential([ Input(shape=(64, 64, 128)), Conv2D(128, (3, 3), padding="same", activation="relu"), SEBlock(ratio=16), Conv2D(256, (3, 3), padding="same", activation="relu") ]) model.summary()
Initialize SE block with given parameters.
- Parameters:
ratio (int) – Reduction ratio for the bottleneck in the SE block. Default is 16.
- __init__(ratio=16, **kwargs)[source]¶
Initialize SE block with given parameters.
- Parameters:
ratio (int) – Reduction ratio for the bottleneck in the SE block. Default is 16.
- build(inputShape)[source]¶
Build the SE block components based on input shape.
- Parameters:
inputShape (tuple) – Shape of the input tensor.
- call(inputs)[source]¶
Forward pass: apply squeeze (global pooling) and excitation (MLP) to compute channel-wise weights and scale the input features.
- Parameters:
inputs (tensorflow.Tensor) – Input feature map of shape (b, H, W, C).
- Returns:
Recalibrated feature map after applying SE attention.
- Return type:
tensorflow.Tensor
- class HMB.TFAttentionBlocks.ECABlock(*args, **kwargs)[source]¶
Bases:
LayerEfficient Channel Attention (ECA) block implementation as a Keras Layer. This layer captures local cross-channel interactions without dimensionality reduction.
- Parameters:
Initialize ECA block with given parameters.
- Parameters:
- build(inputShape)[source]¶
Build the ECA block components based on input shape. This method initializes the convolutional layer for channel attention.
- Parameters:
inputShape (tuple) – Shape of the input tensor.
- call(inputs)[source]¶
Forward pass: apply global pooling, 1D convolution for channel attention, and scale the input features.
- Parameters:
inputs (tensorflow.Tensor) – Input feature map of shape (b, H, W, C).
- Returns:
Recalibrated feature map after applying ECA attention.
- Return type:
tensorflow.Tensor
- class HMB.TFAttentionBlocks.MultiHeadSelfAttention(*args, **kwargs)[source]¶
Bases:
LayerMulti-head self-attention adapted for 2D feature maps.
This layer flattens spatial dimensions (H*W) and applies a Transformer-style multi-head self-attention on the flattened sequence, then projects the attended features back to the original spatial shape.
- Parameters:
Initialize MultiHeadSelfAttention.
- build(inputShape)[source]¶
Build an internal tf.keras.layers.MultiHeadAttention instance. Input is expected to be (batch, H, W, C).
- Parameters:
inputShape (tuple) – Shape of the input tensor.
- call(inputs)[source]¶
Forward pass: reshape (b,H,W,C) -> (b,seq_len,C), call the underlying MultiHeadAttention with query=value=key and reshape the output back.
- Parameters:
inputs (tensorflow.Tensor) – Input feature map of shape (b, H, W, C).
- Returns:
Output feature map of shape (b, H, W, C) after self-attention.
- Return type:
tensorflow.Tensor
- class HMB.TFAttentionBlocks.NonLocalBlock(*args, **kwargs)[source]¶
Bases:
LayerNon-local block (self-attention over feature map positions).
Computes pairwise affinities across spatial positions and aggregates global context. Useful to capture long-range dependencies in images.
- Parameters:
interChannels (int|None) – Number of channels for the internal projections; if None it defaults to C//2 where C is input channels.
Initialize NonLocalBlock.
- Parameters:
interChannels (int|None) – Internal projection channels.
- __init__(interChannels=None, **kwargs)[source]¶
Initialize NonLocalBlock.
- Parameters:
interChannels (int|None) – Internal projection channels.
- build(inputShape)[source]¶
Build 1x1 conv projections for theta, phi and g, and an output 1x1 conv. inputShape is expected as (batch, H, W, C).
- Parameters:
inputShape (tuple) – Shape of the input tensor.
- call(inputs)[source]¶
Forward pass: compute affinity matrix between positions and aggregate global context which is then projected and added to the input (residual).
- Parameters:
inputs (tensorflow.Tensor) – Input feature map of shape (b, H, W, C).
- Returns:
Output feature map of shape (b, H, W, C) after non-local attention.
- Return type:
tensorflow.Tensor
- class HMB.TFAttentionBlocks.BAMBlock(*args, **kwargs)[source]¶
Bases:
LayerBottleneck Attention Module (BAM) - lightweight channel + spatial attention.
This implementation builds a compact channel attention branch (MLP on pooled features) and a spatial branch (dilated conv stack) and multiplies them with the input feature map.
- Parameters:
Initialize BAM block.
- Parameters:
- build(inputShape)[source]¶
Build channel MLP and spatial dilated conv stack based on input channels.
- Parameters:
inputShape (tuple) – Shape of the input tensor.
- call(inputs)[source]¶
Forward pass: compute channel attention via pooled MLP and spatial attention via dilated convs, then apply both to input.
- Parameters:
inputs (tensorflow.Tensor) – Input feature map of shape (b, H, W, C).
- Returns:
Output feature map of shape (b, H, W, C) after BAM attention.
- Return type:
tensorflow.Tensor
- class HMB.TFAttentionBlocks.GCBlock(*args, **kwargs)[source]¶
Bases:
LayerGlobal Context (GC) block - simplified global attention for feature maps.
Computes a soft-attention mask over spatial positions, aggregates a global context vector and applies a small transform to produce a context tensor that is added back to the input (residual).
- Parameters:
reduction (int) – Channel reduction for the transform MLP. Default 16.
Initialize GCBlock.
- Parameters:
reduction (int) – Reduction factor for the internal transform.
- __init__(reduction=16, **kwargs)[source]¶
Initialize GCBlock.
- Parameters:
reduction (int) – Reduction factor for the internal transform.
- build(inputShape)[source]¶
Build conv_mask and transform dense layers based on input channels.
- Parameters:
inputShape (tuple) – Shape of the input tensor.
- call(inputs)[source]¶
Forward pass: compute attention mask, aggregate weighted context and apply small transform then broadcast-add to the input.
- Parameters:
inputs (tensorflow.Tensor) – Input feature map of shape (b, H, W, C).
- Returns:
Output feature map of shape (b, H, W, C) after GC attention.
- Return type:
tensorflow.Tensor
- class HMB.TFAttentionBlocks.AxialAttention(*args, **kwargs)[source]¶
Bases:
LayerAxial attention: factorized attention applied along height then width axes. Applies a lightweight multi-head self-attention along one axis at a time to reduce computational cost compared to full 2D self-attention.
- Parameters:
Initialize AxialAttention.
- class HMB.TFAttentionBlocks.AttentionAugmentedConv(*args, **kwargs)[source]¶
Bases:
LayerAttention-augmented convolutional layer.
Combines a standard convolutional branch with a self-attention branch and concatenates their outputs along the channel axis.
- Parameters:
Initialize AttentionAugmentedConv.
- Parameters:
- __init__(filters, kernelSize=3, numHeads=4, keyDim=32, **kwargs)[source]¶
Initialize AttentionAugmentedConv.
- build(inputShape)[source]¶
Build conv branch and attention branch. Note: user should ensure filters > numHeads * keyDim so the conv branch has a positive number of channels.
- Parameters:
inputShape (tuple) – Shape of the input tensor.
- call(inputs)[source]¶
Forward pass: run conv branch and attention branch then concatenate.
- Parameters:
inputs (tensorflow.Tensor) – Input feature map of shape (b, H, W, C).
- Returns:
Output feature map of shape (b, H, W, filters) after concatenation of conv and attention branches.
- Return type:
tensorflow.Tensor
- class HMB.TFAttentionBlocks.SKBlock(*args, **kwargs)[source]¶
Bases:
LayerSelective Kernel (SK) block: adaptively fuse multiple convolutional branches.
- Parameters:
Initialize SKBlock.
- Parameters:
- class HMB.TFAttentionBlocks.TripletAttention(*args, **kwargs)[source]¶
Bases:
LayerTriplet attention: cross-dimension spatial attention using rotated inputs. Applies spatial attention on three views (original, HW-transposed, and original again — the current implementation applies the original view twice) and averages the three attended outputs.
- Parameters:
kernelSize (int) – Kernel size for the spatial conv used to compute attention maps.
Initialize TripletAttention.
- Parameters:
kernelSize (int) – Kernel size for attention conv.
- __init__(kernelSize=7, **kwargs)[source]¶
Initialize TripletAttention.
- Parameters:
kernelSize (int) – Kernel size for attention conv.
- build(inputShape)[source]¶
Build the small conv used to compute spatial attention maps.
- Parameters:
inputShape (tuple) – Shape of the input tensor.
- call(inputs)[source]¶
Forward pass: apply spatial attention to original and transposed views, then average the results to obtain final output.
- Parameters:
inputs (tensorflow.Tensor) – Input feature map of shape (b, H, W, C).
- Returns:
Output feature map of shape (b, H, W, C) after triplet attention.
- Return type:
tensorflow.Tensor
- class HMB.TFAttentionBlocks.PositionAttentionModule(*args, **kwargs)[source]¶
Bases:
LayerPosition Attention Module (PAM) used in DANet as a spatial attention block.
This module computes a spatial (position) affinity across all positions and aggregates context accordingly. It is lightweight and works on NHWC tensors.
- Parameters:
interChannels (int|None) – Number of channels for the internal projections. If None it defaults to max(1, C//8) where C is input channels.
Initialize PositionAttentionModule.
- Parameters:
interChannels (int|None) – Number of channels for internal projections.
- __init__(interChannels=None, **kwargs)[source]¶
Initialize PositionAttentionModule.
- Parameters:
interChannels (int|None) – Number of channels for internal projections.
- class HMB.TFAttentionBlocks.ChannelAttentionModule(*args, **kwargs)[source]¶
Bases:
LayerChannel Attention Module (CAM) used in DANet as a channel-wise attention block.
This module computes inter-channel affinities by treating spatial locations as the feature dimension and produces channel-wise context.
Initialize ChannelAttentionModule.
- class HMB.TFAttentionBlocks.DANetBlock(*args, **kwargs)[source]¶
Bases:
LayerDual Attention Network (DANet) block combining position and channel attention.
This block applies two parallel branches: a Position Attention Module (PAM) and a Channel Attention Module (CAM). Their outputs are fused and returned.
- Parameters:
interChannels (int|None) – Optional internal channel size for PAM.
Initialize DANetBlock.
- Parameters:
interChannels (int|None) – Internal channel size for PAM.
- __init__(interChannels=None, **kwargs)[source]¶
Initialize DANetBlock.
- Parameters:
interChannels (int|None) – Internal channel size for PAM.
- class HMB.TFAttentionBlocks.CrissCrossAttention(*args, **kwargs)[source]¶
Bases:
LayerCriss-Cross Attention (CCNet) layer implementation as a Keras Layer.
This module computes sparse dense contextual attention by attending along each position’s row and column separately, which reduces memory compared to full N x N attention while still capturing long-range dependencies.
- Parameters:
interChannels (int|None) – Internal projection channels. If None it defaults to max(1, C//8) where C is the input channels.
Initialize CrissCrossAttention.
- Parameters:
interChannels (int|None) – Number of channels for internal projections.
- __init__(interChannels=None, **kwargs)[source]¶
Initialize CrissCrossAttention.
- Parameters:
interChannels (int|None) – Number of channels for internal projections.
- class HMB.TFAttentionBlocks.CrissCrossWrapperBlock(*args, **kwargs)[source]¶
Bases:
LayerWrapper block that applies CrissCrossAttention one or more times.
- Parameters:
repeats (int) – Number of criss-cross attention passes to apply. Default is 1.
Initialize CrissCrossWrapperBlock.
- Parameters:
- build(inputShape)[source]¶
Instantiate the repeated CrissCrossAttention layers.
- Parameters:
inputShape (tuple) – Shape of the input tensor.
- call(inputs)[source]¶
Forward pass: sequentially apply the CrissCrossAttention blocks.
- Parameters:
inputs (tensorflow.Tensor) – Input feature map of shape (b, H, W, C).
- Returns:
Output feature map of shape (b, H, W, C) after applying the repeated criss-cross attention blocks.
- Return type:
tensorflow.Tensor