# Automated Deep Learning Model for Sperm Head Segmentation, Pose Correction, and Classification
## 2 Materials and Methods
### Model Construction
#### 2.2.2 Sperm Head Pose Correction Network  
The network includes four main modules: Detection Network (DNet), Direction Estimation Module (DEM), Polarity Network (PNet), and Rotated Region of Interest Align (RROI align) module. The first three modules predict the position, size, and angle of the sperm head. These predictions are then input into the RROI align module for feature sampling at specified locations, thereby enabling the standardization of sperm head pose.  
The input of DNet is the confidence map from the mask decoder, which has been downsampled. DNet includes two 3 \(\times\) 3 convolutional layers, followed by two pooling layers and two fully connected layers. The last fully connected layer is used to regress the center point (x_c, y_c) and length s of the square bounding box. The benefit of the square bounding box is that it eliminates concerns about aspect ratio changes affecting features when standardizing image scale, and it helps the network learn the spatial differences between pyriform, tapered, and normal sperm.  
In image angle prediction, the periodicity of angles (e.g., the square bounding boxes corresponding to 0 and 360 degrees are exactly the same) causes the loss function to be unable to effectively calculate the true gradients, which makes the network training difficult to converge or unstable. In our work, we decompose the sperm's direction into angle and polarity. The angle between the sperm's long axis and the x-axis is calculated in DEM  
Figure 4: Illustration of Sperm Head Pose Correction and Morphology Classification Model.  
using the image moments and the arctangent function, with a value range of [\(-\)90, 90]. The equation is shown as follows:  
\[\mu_{20}=\sum_{x=0}^{W-1}\sum_{y=0}^{H-1}{(x-\overline{x})I(x,y)}\]  
\[\mu_{20}=\sum_{x=0}^{W-1}\sum_{y=0}^{H-1}{(y-\overline{y})I(x,y)}\]  
\[\mu_{11}=\sum_{x=0}^{W-1}\sum_{y=0}^{H-1}{(x-\overline{x})(y-\overline{y})I(x,y)}\]  
\[\theta=\frac{1}{2}tan^{-1}\bigg{(}\frac{2\mu_{11}}{\mu_{20}-\mu_{02}}\bigg{)}\]  
where \(\overline{x}\) and \(\overline{y}\) represent the centroid of the image, \(\mu_{20}\), \(\mu_{20}\), and \(\mu_{11}\) represent the second central moments and the second mixed moments.  
Polarity refers to whether the sperm's acrosome is positioned on the left or right of the image's vertical centerline when the angle is zero degrees. Here, we downsample the input image and use RROI align to sample the sperm head based on the predicted bounding box and angle, then feed it into PNet for polarity prediction. The hyperparameters in PNet are the same as in DNet. Once all pose parameters are obtained, RROI Align extracts the region containing the sperm head features from the feature map produced by EdgeSAM's RepViT encoder. The extracted features have a shape of 256 \(\times\) 64 \(\times\) 64. If the output from PNet is labeled as L (with a value of 0), it indicates that the sperm head is oriented to the left. In this case, the extracted features are flipped both vertically and horizontally to correct the orientation. If the output from PNet is not L, the extracted features are retained without any flipping.  
Notably, EdgeSAM's features are not used for training DNet and PNet, but they are used only for sampling. Using confidence maps and downsampled images significantly reduces computational overhead; for example, PNet's complexity is only 0.082 GFLOPS, whereas utilizing feature maps would push the complexity of conv1 alone to 0.6 GFLOPS. Additionally, PNet's input is downsampled from the original image, and DNet can be trained using ground truth to simulate segmentation results. Since these networks are independent of EdgeSAM's features, they allow for separate training.