## 3 Materials and Methods
### Methods
#### 3.4.5 Combination Loss  
In the training phase, most previous counting methods based on density estimation usually use mean squared error (MSE) as the loss function to measure the error between the estimated density map and the GT density map at the pixel level, which is defined as follows:  
\[L_{MSE}=\frac{1}{N}\sum_{i=1}^{N}\left\|X_{i}^{EST}(\theta)-X_{i}^{GT}\right\| _{2}^{2} \tag{3}\]  
where \(N\) is the batch size, \(X_{i}EST(\theta)\) is the \(i\)-th estimated density map generated by the model based on parameter \(\theta\), \(X_{i}\) is the \(i\)-th input image, and \(X_{i}^{GT}\) is its corresponding GT density map.  
However, MSE loss is based on the assumption of pixel independence and ignores the spatial correlation and consistency between pixels in the density map. Relying solely on MSE loss to train the network may lead to some negative effects, such as blurring effects and the loss of local structural information [43]. Existing studies into FIDTM [44], DSSINet [45], and ASANet [46] have shown that combining SSIM loss can improve the quality of the estimated density map.  
SSIM measures the similarity of images from three aspects: brightness, contrast, and structure. It calculates the similarity between two images based on local statistics (mean, variance, and covariance). It is defined as follows:  
\[SSIM(pred,gt)=\frac{\left(2\mu_{pred}\mu_{gt}+c_{1}\right)\left(2\sigma_{pred, gt}+c_{2}\right)}{\left(\mu_{pred}^{2}+\mu_{gt}^{2}+c_{1}\right)\left(\sigma_{ pred}^{2}+\sigma_{gt}^{2}+c_{2}\right)} \tag{4}\]  
where \(pred\) and \(gt\) represent the estimated density map and GT density map, respectively, \(\mu_{pred}\) and \(\mu_{gt}\) are the pixel means of the two density maps, respectively, \(\sigma^{2}_{pred}\) and \(\sigma^{2}_{gt}\) represent the pixel variance of the two density maps, \(\sigma_{pred,gt}\) represents the pixel covariance between the two density maps, and \(c_{1}\) and \(c_{2}\) are constants used to avoid the denominator being zero. The value range of SSIM is [\(-\)1, 1], where the larger the value, the higher the similarity. Based on this, the calculation formula for SSIM loss can be defined as:  
\[L_{SSIM}=1-SSIM(pred,gt) \tag{5}\]  
This framework includes two aspects of loss design: Globally, MSE loss is used to supervise \(D_{1}\), which is consistent with the input size, to ensure the accuracy of the final density map at the pixel level while ensuring the consistency between the estimated number of objects and the actual number of objects. Locally, SSIM loss is used to perform distributed supervision on \(D_{k}\) (\(k\) = 1,2,3,4) to help the network better capture local details and structural information. It should be noted that GT density maps of other sizes are generated by downsampling. As the resolution decreases, the global information loss of the GT map becomes more serious. In this case, the low-resolution predicted density map is supervised by MSE loss, which optimizes the pixel distribution more rather than the global density structure. Therefore, to avoid learning conflicts between different paths, we did not introduce additional MSE loss supervision for \(D_{k}\) (\(k\) = 2,3,4). By weighting the global loss and local loss, the final objective function \(L_{total}\) is defined as follows:  
\[L_{total}=\lambda\cdot L_{MSE}(D_{1})+\frac{1}{4}\sum_{k=1}^{4}L_{SSIM}(D_{k}) \tag{6}\]where \(\lambda\) is the weight of the MSE loss about \(D_{1}\), which is used to balance the ratio of global loss to local loss.