## 2 Method
### Improvements and Training of the SegFormer Network
#### 2.1.2 Constructing Networks  
The network construction framework for this study is illustrated in Figure 2. Semantic segmentation, a highly popular and crucial aspect of deep learning in computer vision, involves labeling each pixel in an image with its corresponding semantic class, achieving fine-grained segmentation and comprehension of the image. Standard datasets like COCO (common objects in context) and Cityscapes are extensively employed for semantic segmentation tasks [27, 28]. These datasets furnish numerous images with pixel-level labels that are utilized for training and evaluating the performance of semantic segmentation models. Many deep learning algorithms are designed to address semantic segmentation tasks akin to the aforementioned standard datasets. However, everyday photos exhibit notable differences from CT images:  
Figure 1: The example of labeling in LabelMe. (**a**) Original image, (**b**) image after labeling. 1. CT images encapsulate high-resolution internal structural information, enabling applications in rock classification and mineral composition analysis. They excel in capturing subtle structures and details at the microscale, contributing to their larger dimensions. 2. Owing to specific domain constraints, CT images typically possess a smaller overall data volume compared to extensive existing datasets. Moreover, data diversity may be relatively low, and the distinctions between labels might not be as apparent. Traditional algorithms may struggle to predict effectively in this context. 3. In the research process, it is essential to output the porosity of specified regions based on CT images. This necessitates achieving more comprehensive segmentation on the boundaries of these regions and the capability to model long distances. Considering points 2 and 3, along with additional considerations related to image texture, the Transformer architecture was selected. It is crucial to account for the impact of features at different scales and determine how to amalgamate these diverse features. The decoder of SegFormer incorporates this functionality. The fundamental concept of this algorithm involves integrating the self-attention mechanism of Transformer into the semantic segmentation task, empowering the model to establish relationships between different pixels and comprehend the global and local structure of the image [29; 30]. In contrast to traditional convolutional neural network (CNN)-based segmentation methods, SegFormer harnesses Transformer's ability to capture long-range dependencies between pixels, proving highly effective in addressing intricate segmentation challenges. The network architecture of SegFormer, comprising the hierarchical Transformer encoder and lightweight ALL-MLP decoder modules, is shown in Figure 3.  
Figure 2: An overview of image segmentation process from data collection to prediction and statistics. * The hierarchical Transformer encoder functions as the core network in the SegFormer model. It is tasked with extracting features from the input image and transmitting these features to the segmentation decoder. Its purpose is to capture features at various scales within the image and integrate global contextual information, thereby improving segmentation performance. * The objective of this module is to generate CNN-like multi-level features when given an input image. These features offer high-resolution coarse features and low-resolution fine-grained features, typically enhancing the performance of semantic segmentation. More precisely, for an input image with a resolution of \(H\times W\times 3\), patch merging is performed to yield a hierarchical feature map \(F_{i}\), with a resolution calculated as follows: \[\frac{H}{2^{i+1}}\times\frac{W}{2^{i+1}}\times C_{i},\ i=\{1,2,3,4\},\ C_{i+1}>C_{i}\] (1)
* Overlapped patch merging is employed to reduce the size of the feature maps while simultaneously increasing the number of channels. * The self-attention layer in the encoder represents the most computationally demanding component.