CFPFormer: Feature-pyramid like Transformer Decoder for Segmentation and Detection
Hongyi Cai Mohammad Mahdinur Rahman Jingyu Wu Yulun Deng







A]Hongyi Cai
A]Mohammad Mahdinur Rahman 
B]Jingyu Wu
A]Yulun Deng

[A]University of Malaya
[B]Fuzhou University of International Studies and Trade


Feature pyramids have been widely adopted in convolutional neural networks (CNNs) and transformers for tasks like medical image segmentation and object detection. However, the currently existing models generally focus on the Encoder-side Transformer to extract features, from which decoder improvement can bring further potential with well-designed architecture. We propose CFPFormer, a novel decoder block that integrates feature pyramids and transformers. Specifically, by leveraging patch embedding, cross-layer feature concatenation, and Gaussian attention mechanisms, CFPFormer enhances feature extraction capabilities while promoting generalization across diverse tasks. Benefiting from Transformer structure and U-shaped Connections, our introduced model gains the ability to capture long-range dependencies and effectively up-sample feature maps. Our model achieves superior performance in detecting small objects compared to existing methods. We evaluate CFPFormer on medical image segmentation datasets and object detection benchmarks (VOC 2007, VOC2012, MS-COCO), demonstrating its effectiveness and versatility. On the ACDC Post-2017-MICCAI-Challenge online test set, our model reaches exceptionally impressive accuracy, and performed well compared with the original decoder setting in Synapse multi-organ segmentation dataset.








§ INTRODUCTION

The advent of deep learning techniques, particularly convolutional neural networks (CNNs) like U-Net <cit.>, has significantly advanced the field by improving the accuracy and efficiency of tasks such as image segmentation and analysis, which plays a pivotal role in modern healthcare, aiding clinicians in accurate diagnosis, treatment planning, and disease monitoring. However, as networks deepen and downscale, they may lose crucial dense information from lower layers, impacting their ability to capture fine-grained details and small structures in medical images. So previous works have used Skip Connections<cit.> to recover this, which are in form of simple concatenation such as TransUnet <cit.>.

Simultaneously, transformers have demonstrated remarkable capabilities in capturing long-range dependencies in various domains, including natural language processing<cit.> and image processing<cit.>.  In image processing, 2D image patches with positional encodings are fed as input sequences to the transformer, allowing it to model long-range dependencies across the entire image effectively<cit.>.

However, traditional transpose convolutional layers used in upsampling and decoding stages of U-Net-like architectures often struggle to capture dense, localized features, which are crucial for global context and accurate segmentation of small structures in medical images <cit.>. This limitation arises from the fixed kernel sizes and sparse connectivity patterns of convolutional operations, which may not effectively integrate multi-scale features and long-range dependencies <cit.>. Additionally, the simple concatenation of features in skip connections may not optimally fuse the rich representations learned at different scales <cit.>.


Motivated by these challenges and recognizing the strengths of both U-Net-like architectures and transformers, our work introduces a novel architecture known as the Cross Feature Pyramid (CFP) block within a feature pyramid framework. The CFP block serves as a decoder block designed to recover the loss of dense information during downscaling. By incorporating patch embedding and attention mechanisms, the CFP block aims to retrieve pixel-level information from lower layers, thereby enhancing the model's capacity to capture fine details and small structures essential for medical image analysis tasks.

A key aspect of our architecture is the utilization of Gaussian attention mechanisms within the CFP block. This attention mechanism is designed to decay attention within a curve, efficiently prioritizing information from relevant layers while filtering out noise and irrelevant details. Furthermore, by concatenating feature maps with key-value pairs (KV), the CFP block makes the attention mechanism aware of cross-layer features. This not only enhances the model's performance for small object detection and segmentation but also improves its overall understanding of complex spatial relationships within medical images.

In this work, we propose a novel approach that addresses key challenges in medical image analysis. Our model leverages Gaussian attention over rows and columns to capture long-range dependencies efficiently, without the high computational cost of pixel-level attentions.

The core of our approach is the Cross Feature Pyramid (CFP) architecture, offering flexibility to integrate seamlessly with various network architectures like U-Net and CenterNet. This versatile method is thus well-suited for diverse medical imaging applications, as will be shown in the subsequent sections.

In general, our work proposed that:

    
  * We proposed our main mechanism CFPFormer, by extending potentials from Vision Transformer, effectively decoding long-range details from Encoders.
    
  * We break down the attention calculation into rows and columns with Gaussian Decay. The new methods precisely enhance the decoded feature map and contributes to a better performance for our model.
    
  * By introducing Feature Re-encoding (FRE), it re-assemble each output from those image encoders and adjust to be fit into Decoder layers. CFPFormer unfolds the latent increment of decoder-based models and impressive growth.




§ RELATED WORK




 §.§ CNN-based Methods


Convolutional neural networks (CNNs) like U-Net<cit.> have been the workhorses for medical image analysis tasks. These networks consist of an down-sampling and a up-sampling component that work in tandem. 

The down-sampling component progressively reduces the spatial resolution of the input image through a series of convolution and pooling layers. At each layer, the convolution operation utilizes learnable filters to extract local features such as edges, textures, and patterns. Subsequently, the pooling operation spatially downscale these feature maps, decreasing their spatial dimensions, enhancing the receptive field, and promoting in-variance to local translations.

As we go deeper into the down-sampling, the features become more abstract and capture higher-level semantic concepts relevant to the task. However, this abstraction comes at the cost of losing fine-grained spatial details and resolution, which are crucial for pixel-wise prediction tasks like segmentation.

The up-sampling component aims to recover this lost spatial resolution and generate the final output predictions. It performs a series of up-sampling operations, often using transposed convolutions<cit.> or interpolation, to gradually increase the spatial dimensions of the feature maps. Skip connections<cit.> in the U-Net architecture concatenate the feature maps from corresponding down-sampling layers with the up-sampling features. These skip connections provide the up-sampling layers with fine-grained details from the earlier layers, aiding in precise localization and boundary delineation.

Despite their success, CNNs face limitations in capturing long-range dependencies due to their localized receptive fields, which hinders their ability to model complex spatial relationships and global contexts in medical images effectively. To address this, techniques like dilated convolutions<cit.> have been employed to expand the receptive field without increasing computational cost.



 §.§ Transformer-based Methods

Transformers<cit.>, initially proposed for sequence-to-sequence tasks in natural language processing, have emerged as a powerful alternative for modeling long-range dependencies in various domains, including medical image analysis. Unlike CNNs, which operate on local neighborhoods, transformers employ self-attention mechanisms that enable them to capture global dependencies across the entire input sequence. 

To adapt transformers for image data, researchers have proposed various strategies. One common approach, introduced in the Vision Transformer (ViT) <cit.>, is to divide the input image into non-overlapping patches and treat these patches as tokens in the input sequence. Positional encodings are incorporated to imbue the model with spatial awareness, enabling it to differentiate between patches from different locations within the image. The transformer encoder processes the input sequence and generates encoded representations encapsulating spatial and semantic information.

While pure transformer architectures have demonstrated remarkable performance in capturing long-range dependencies, their application to medical image analysis poses challenges. They typically require vast computational resources, making them less practical for resource-constrained applications. Additionally, their inherent lack of inductive biases for spatial data can hinder their ability to efficiently model local interactions and fine-grained details, which are crucial in medical imaging tasks.

To address these limitations, recent research has explored hybrid architectures that combine the strengths of CNNs and transformers. These models leverage CNN encoders to extract hierarchical features from the input image, which are then flattened and projected into a sequence of embeddings serving as input to the transformer encoder. The transformer encoder and up-sampling decoder capture global dependencies and generate output predictions. Examples of this approach include TransUNet<cit.> and CoTr<cit.>.

Alternatively, some architectures interleave CNN and transformer layers, allowing for iterative refinement of features and predictions. CNN layers extract local features, followed by transformer layers modeling long-range dependencies, and then CNN layers again for spatial reconstruction and output generation. This approach is employed in models like SwinUNet<cit.> and UNetr<cit.>, which incorporate attention mechanisms to modulate the flow of information between CNN and transformer components, enabling the model to adaptively focus on relevant features and dependencies for the task at hand.

By combining the complementary strengths of CNNs and transformers, these hybrid architectures aim to overcome the limitations of individual architectures and provide a more comprehensive solution for medical image analysis tasks that require both fine-grained spatial modeling and global context understanding.








§ METHOD




 §.§ Preliminary




  
Decoder for Downstream Tasks Our model architecture is established upon the structure of Decoder. Hence, CFPFormer behaves to strengthen association between Encoder and Decoder layers to activate the potentials of encoded features. At the core of this architecture lies the Cross Feature Pyramid (CFP) Block, which incorporates three key innovations: Gaussian Attention, Feature Re-encoding and Cross-Layer Feature Integration. These components are designed to enhance the model's ability to capture complex spatial relationships, integrate information across different scales, and mitigate the loss of dense information during downscaling.







 §.§ Network Architecture


The feature embeddings of the backbone serve as input to the Cross Feature Pyramid (CFP) block at the lowest resolution level of the pyramidal hierarchy, as shown in figure <ref>. The output of this block is then upsampled and passed to the next CFP block at a higher resolution level in the pyramid. This process is repeated, progressively moving up the pyramid to higher resolution levels. 

At each level of the pyramid, the CFP block receives the upsampled features from the previous lower-resolution block. These upsampled features are combined with features from the same resolution level of the backbone network, which provides low-level spatial information and enhances long-range dependency to guide the attention mechanisms within the CFP block. 

As the decoding process ascends through the pyramidal hierarchy, the features are passed through our proposed Gaussian Attention, with the calculated decay mask based upon distance, and subsequently output is a set of high-resolution feature maps with strengthened mechanism to the top of the pyramid.

These feature maps can then be further processed by task-specific heads for applications such as image segmentation, object detection, or classification.






 §.§ Cross Feature Pyramid (CFP) Block




  
Gaussian Attention and Axial Decomposed Calculation.

The CFP block employs an attention mechanism, termed Gaussian Attention, which situate the feature computations along rows and columns in pixels. Due to the burdening computational cost triggered by most of Vision Transformers, We proposed Axial Decomposed Calculation upon Gaussian decay, from which our module efficiently dissociates all-pixel wised attention into row-wised attention and column-wised attention.

The  module first linearly projects the input embeddings 𝐗' into query (𝐐), key (𝐊), and value (𝐕) representations:


    Q = X'W^Q 
    
    K = X'W^K ⊕ F_enc
    
    V = X'W^V ⊕ F_enc


where 𝐖^Q, 𝐖^K, and 𝐖^V are learnable projection matrices, and F_enc denotes the features extracted from Image Encoder.

These representations are then reshaped and used for row-wise and column-wise attention computations:


    A_r    = softmax(reshape(Q) reshape(K)^T/√(d_k)) ⊙ M    (row-wise)
    
    A_c    = softmax(reshape(Q) reshape(K)^T/√(d_k)) ⊙ M    (column-wise)


where reshape(𝐐), reshape(𝐊), and reshape(𝐕) are reshaped matrices suitable for row-wise and column-wise attention computations, and d_k is the dimensionality of the key vectors.



A innovative aspect of the CFP block is the use of Gaussian attention mechanisms. Unlike attention mechanisms that assign equal importance to all positions within the receptive field, Gaussian attention decays the attention weights based on a Gaussian curve, efficiently prioritizing information from relevant layers while filtering out noise and irrelevant details.

The Gaussian attention mechanism is implemented by generating a 2D decay mask 𝐌∈ℝ^H' × W' based on the Euclidean distance between spatial positions:


    𝐌[i, j] = exp(-i^2 + j^2/2σ^2)



    D = M^ √((x_2 - x_1)^2 + (y_2 - y_1)^2)

where σ is a learnable parameter that controls the rate of decay.

This decay mask is then applied to the attention scores, effectively modulating the attention weights with a Gaussian decay, as depicted in formula <ref>.





  
Feature Re-encoding

A key component of the CFP block is the Feature Re-encoding mechanism, which aims to enhance the model's ability to capture fine-grained details and small structures by leveraging information from lower-resolution feature maps.

To incorporate cross-layer feature information, the key (K) and value (V) tensors in the Feature Re-encoding (FRE) module are combined with encoder features F_enc from a lower layer of the network, as depicted in follows:


    FRE(K,F_enc) = 
        FRE(V,F_enc) = V ⊕ Patchembed(F_enc)



where F_enc∈ℝ^B × H_enc× W_enc× C_enc, Patchembed are layers decomposing features into image embeddings. Here we accept the features in down-sampling layers, with image size H_enc with W_enc, varying into H_enc/P with W_enc/P. Unlike those cascaded decoder, for example: TransUnet, PVT-CASCADE, which directly concatenate up-sampling convolutional layers with encoder features, our cross-feature combination allows the attention mechanism to interact with low-level spatial information from the encoder in a more effective way. 

A similar work can be traced to Pyramid Vision Transformers<cit.>, from which suggest using Spatial Reduction to fit channel dimensions of K and V tensors by linear projection. 

However, our method appears to be an interactions within the decoder stages, which enables the model to better capture fine-grained details by taking advantages of Gaussian Axial Attention and structures present in the input data, as depicted in Figure <ref>.











§ EXPERIMENTS



 §.§ Datasets


To evaluate the effectiveness of our proposed CFPFormer method, we performed experiments on two different tasks: Object Detection and Medical Image Segmentation.

Object Detection Datasets. we utilized the popular COCO dataset <cit.>. This dataset contains 20 object categories with bounding box annotations, allowing us to assess the model's capability in detecting objects of varying sizes, including small objects. In addition to that, VOC 2007+2012 datasets<cit.> are also involved during training, which exceeds over 20,000 images of real-life scenarios.

Medical Image Segmentation Datasets. we employed two challenging datasets: the MRI Automatic Cardiac Diagnosis Challenge (ACDC) <cit.> and the Synapse Multi-organ Segmentation Challenge <cit.>. The ACDC dataset comprises 100 MRI scans, with ground truth annotations for the left ventricle (LV), right ventricle (RV), and myocardium (MYO). We followed a standard train-validation-test split of 70-10-20. The Synapse dataset, on the other hand, contains CT scans from 30 patients, and our experimental setup and pre-processing closely followed the methodology described in TransUNet <cit.>.



 §.§ Performance Evaluation

For the Object Detection task, we adopted the standard mean Average Precision (mAP) metric, which evaluates the model's accuracy in detecting objects and localizing bounding boxes correctly. Specifically, we report the mAP@[0.5:0.95] score, which is calculated as:


    mAP@[0.5:0.95] = 1/10∑_i=0.5^0.95AP@i


where AP@i represents the Average Precision at an intersection over union (IoU) threshold of i. A prediction is considered correct if the IoU between the predicted and ground truth bounding boxes exceeds the threshold i.

In the case of Medical Image Segmentation, we utilized the widely-used Dice Similarity Coefficient (DSC) and Hausdorff Distance (HD) metrics to assess the model's performance. The DSC measures the overlap between the predicted segmentation masks (P) and the ground truth masks (G), and is defined as:


    DSC(P, G) = 2 × |P ∩ G|/|P| + |G|


where |·| denotes the cardinality of a set. A DSC value of 1 indicates perfect overlap between the predictions and ground truth.

The Hausdorff Distance (HD) quantifies the maximum distance between the predicted and ground truth boundaries, and is calculated as:


    HD(P, G) = max{sup_p ∈ Pinf_g ∈ G d(p, g), sup_g ∈ Ginf_p ∈ P d(g, p) }


where d(p, g) represents the Euclidean distance between points p and g. A lower HD value indicates better alignment between the predicted and ground truth boundaries.




 §.§ Implementation Details


Segmentation Setting. During the data augmentation process for Image Segmentation, we incorporated random rotations of 0, 90, 180, or 270 degrees, as well as horizontal or vertical flips, each with a 50% probability. Additionally, we resized the images using cubic interpolation to attain a specific image size. For the ACDC<cit.> and Synapse dataset<cit.>, our image size was set to 256×256. The training was started with a learning rate of 1e-4 with a decay of 1e-4. The Adam optimizer<cit.> was used to optimize our models during training.

Object Detection Setting. We adopt VOC2007+2012<cit.> and COCO<cit.> dataset as the training data, while VOC 2007 validation set are situated as our benchmark result. We opted CenterNet<cit.> as our foundmental detection methodology and AdamW<cit.> is selected as the optimizer for training object detection, along with learning rate set to be 5e-5 with 200 epochs warmup.Each image is resized and randomly flipped with resolution size 384×384.

Model Settings. During training, the model is optimized using a standard loss function, such as cross-entropy for classification tasks or a combination of dice loss and cross-entropy for segmentation tasks. The loss function is defined as:


    ℒ = ℒ_task(𝐗^L, 𝐘)


where 𝐘 represents the ground truth labels, and ℒ_task is the task-specific loss function (e.g., cross-entropy for classification, dice loss for segmentation). Our proposed model offers flexibility in terms of the ratio and number of each blocks: By default setting, CFPFormer-Tiny, each bottleneck are set with 2,2,6,2, which represents number of blocks in each stage. The drop-path rate is set to be 0.15 to avoid from overfitting.







 §.§ Results





  §.§.§ Medical Image Segmentation


For the Medical Image Segmentation tasks, we report the DSC and HD metrics for the ACDC and Synapse datasets in Tables <ref> and <ref> respectively.For evaluation usage, our decoder is assembled with U-net as the encoder, representative as a baseline model of Medical Segmentation, coupled with backbones VGG-16 and Resnet-50. 



Our CFPFormer architecture outperformed other relevant methods, especially from its baseline U-net, demonstrating its effectiveness in capturing intricate anatomical structures and delineating precise segmentation boundaries. Compared with those models with Resnet-50 Backbone, yet our VGG-16 CFPFormer, with less parameters in backbone, evidently exceed in both RV and MYO categories. Our R50 CFPFormer, making use of strong context extraction and larger parameter size, reaches a better DSC in MYO Category.



To provide a qualitative evaluation, Figure <ref> illustrates a sample MRI slice from the ACDC dataset, along with the corresponding prediction and ground truth segmentation masks generated by our CFPFormer model. The figure demonstrates the model's ability to accurately segment intricate anatomical structures, such as the left and right ventricles, and the myocardium. 











  §.§.§ Object Detection


Table <ref> presents the AP_50 scores achieved by our CFPFormer model on COCO and VOC dataset, along with several related methods for comparison. We select CenterNet, coupled with backbone Resnet-50 as the encoder. Our model demonstrated superior performance, compared with its baseline model CenterNet and those related variants, owing to its ability to capture fine-grained details and long-range dependencies effectively. Here we compare with CornerNet, CenterNet as our baseline models, from which these models adopt anchor-less methods to detect objects in scenarios.



 §.§ Analysis

Increased Accuracy in the Downstream Tasks. Our method proves a better performance on average compared with baseline models. Since our main idea is to improve the model during the decoding part, we integrate our CFPFormer into the upsampling layers of CenterNet and U-Net, which we consider areas that need improvement. The results shown in Table <ref> and Table <ref> demonstrate the superior performance achieved by adopting our methods.

Boosting with Transformer Encoder. We combine Pyramid Vison Transformer<cit.> and examine on ACDC datasets in Table <ref>. PVT primarily acts as image backbone nowadays and performed a higher accruacy than Resnet50. To stack with a larger encoder, especially transformer-based networks, we easily plug CFPFormers and pass encoder features through training functions. In order to reduce the complexity during the model construction, we use a set of parameters to receive tensors from each encoder layer. Our CFPFormer decoder consists of 4 blocks transformer decoders, from which we replace CFP blocks and fix the embedding dimension of layers corresponding to its encoder layers. As a result, PVT CFPFormer-T outperforms PVT-CASCADE<cit.> by 0.57 points in DSC, which proves the better decomposition and rearrangement than CASCADE Decoder<cit.>.

Upsampling Layers. A slight difference can be spotted from choosing different upsampling layers. Here we experiment on Transpose Convolutional Layer and Bilinear Interpolation, as shown in <ref>. The results indicates superior upsampling capability, while taking up for less parameters at the same time. 






 §.§ Ablation Studies


Comparison with related works.
To further analyze the impact of various components in our CFPFormer architecture, we strictly compare with previous related works, by aligning our model into existing models as a decoder.



Gaussian Attention.
The row "CFPFormer w/o GA" refers to the CFPFormer model without the Gaussian Attention component. Instead, we replace Gaussian Attention with default settings of Multi-head Attention (MHA)<cit.>. By comparing its AP_50 score of 63.9 with the CenterNet baseline (63.7), we can observe a slight improvement of 0.2 points in Table <ref>. This suggests that the Gaussian Attention component contributes positively to the overall performance.

Feature Re-encoding with K & V.
The row "CFPFormer w/o FRE" refers to the CFPFormer model without the Feature Re-encoding with Key (K) and Value (V) component. Its AP_50 score of 64.1 shows an improvement of 0.4 points over the CenterNet baseline and 0.2 points over the "CFPFormer w/o GA" variant.

CFPFormer-T.
The "CFPFormer-T" row represents the complete CFPFormer architecture with all components included. It achieves the highest AP_50 score of 66.0, outperforming the CenterNet baseline by 2.3 points and the "CFPFormer w/o GA" and "CFPFormer w/o FRE" variants by 2.1 and 1.9 points, respectively. This suggests that the combination of all components in the CFPFormer architecture leads to the best performance among the compared models.
















§ CONCLUSION

Our work mainly contributes a novel decoder that associates the features across with Encoder layers, with U-shaped Pyramid Re-encoding connections between modules, which is conducive to undermine the deterioration of feature lost caused by those long distant models. Our Gaussian Attention mechanism successfully speeds up the computation while scaling up in the model, and effectively utilized masked decays of Gaussian distribution to elevate the performance taken from Attention. Taking advantage of flexibility of our decoder, it is capable of gaining a higher performance in a number of image downstream tasks, like Medical Image Segmentation and Object Detection. 







