# Automated Pneumothorax Segmentation with a Spatial Prior Contrast Adapter
## 3 Materials and Methods
### Loss Function  
In this study, SPCA-enhanced nnUNet was trained using a combined loss function, which is the sum of Dice loss and cross-entropy loss, which was previously used by standard nnUNet [7]. The loss function can be represented as follows:  
\[L_{total}=L_{Dice}+L_{Cross-entropy}. \tag{2}\]  
Figure 6: Probabilistic atlas generated from ground truth masks demonstrates segmentation labels in dataset CANDID-PTX (**left**) and probabilistic atlas (**right**).  
The total loss \(L_{total}\) of the model is formulated as the sum of two components, as follows: the Dice loss and the cross-entropy loss, both of which serve complementary purposes in segmentation tasks. Specifically, the Dice loss is computed as the negative of the Dice coefficient, a measure of overlap between predicted and ground truth masks. Since the Dice coefficient ranges from 0 to 1, the Dice loss consequently lies within the interval \([-1,0]\), inherently contributing a negative value to the total loss. In contrast, the cross-entropy loss evaluates pixel-wise classification performance and is strictly non-negative. When the model yields relatively high overlap and low cross-entropy loss, the magnitude of the negative Dice loss can exceed that of the cross-entropy term, leading to a negative total loss.  
The Dice loss [29] formulation here is like a finely tuned compass, based on the multi-class version proposed by [PERSON] et al., which uses skip connections to preserve intricate details in complex biomedical images. Its loss function can be represented as follows:  
\[L_{Dice}=-\frac{2}{|K|}\sum_{k\in K}\frac{\sum_{i\in I}u^{k}_{i}v^{k}_{i}}{ \sum_{i\in I}u_{i}^{k}+\sum_{i\in I}v_{i}^{k}}, \tag{3}\]  
where \(u\) is the softmax output of the network and \(v\) is the one-hot encoding of the ground truth segmentation map. Both \(u\) and \(v\) are of shape \(I\times K\), where \(I\) denotes the number of pixels in the training patch or batch, and \(K\) is the number of classes, with \(i\in I\) indexing the pixels and \(k\in K\) indexing the class labels.  
As a seasoned guide helps you navigate through dense forests by ensuring you stay on track, [PERSON] et al. found that this formula excels when dealing with imbalanced data, ensuring the model focuses on the right areas without getting lost in the noise [30]. It is like giving the model a clear map of where the important features lie. Furthermore, [PERSON] et al.'s generalized Dice overlap loss function acts like a powerful engine, boosting the model's ability to break through tougher barriers, allowing it to handle the most challenging and imbalanced segments with precision [31]. Together, these advancements work harmoniously, transforming the Dice loss into a high-performance tool for medical image segmentation, empowering us to cut through the complexity of difficult segmentation tasks like a sharp scalpel through tissue, ensuring both accuracy and reliability in the results. The cross-entropy loss function can be represented as follows:  
\[L_{Cross-entropy}=-\frac{1}{N}\sum_{i=1}^{N}\sum_{c=1}^{C}y_{i,c}\log(\hat{y}_ {i,c}). \tag{4}\]  
Cross-entropy loss is a widely used loss function in deep learning. It measures the difference between two probability distributions, that is, the predicted probability distribution output by a model and the true distribution represented by the labels. Cross-entropy calculates the negative logarithm of the predicted probability corresponding to the true class label, penalizing incorrect predictions more heavily as the confidence in the wrong class increases. This loss function encourages the model to output probabilities closer to 1 for the correct class and closer to 0 for incorrect ones, thus improving prediction accuracy over time.