## 3 Methodology
### Loss Function Design  
In rail defect detection, the primary loss function in YOLOv8 comprises classification and bounding-box regression objectives, optimizing both category discrimination and positional accuracy for defect targets. Given the stringent localization requirements for small defects (such as pit corrosion and micro-cracks) on rail surfaces, this study augments the original YOLOv8 loss with a perceptual loss component to heighten the model's attention to fine-grained texture details in defect regions. To enhance the precision of class predictions, YOLOv8 leverages Binary Cross Entropy (BCE) to measure the divergence between the predicted class distribution and ground-truth labels. In the simplest binary detection scenario \"defect or non-defect\", a probability \(p_{i}\) is assigned to each target and compared to its corresponding label \(y_{i}\in\{0,1\}\). Let N represent the number of samples, and \(\sigma(\cdot)\) be the Sigmoid function; the classification loss is calculated by Equation (4):  
\[L_{cls}=-\frac{1}{N}\sum\limits_{i=1}^{N}[y_{i}log(\sigma(p_{i}))+(1-y_{i}) log(1-\sigma(p_{i}))] \tag{4}\]  
In real-world rail inspections, diverse defect types may co-occur within the same region, forming a multi-class or multi-label setting. Typically, YOLOv8 computes the BCE for each output channel representing a specific defect class, summing or averaging these to yield a final classification loss. If K classes of defects exist and each target is associated with a K-length probability vector \(\{p_{i,1},p_{i2}\ldots,p_{i,K}\}\), then the classification loss is expressed in Equation (5):  
\[L_{cls}^{multi-class}=-\frac{1}{N\times K}\sum\limits_{i=1}^{N}\sum\limits_{k= 1}^{K}[y_{i,k}log(\sigma(p_{i,k}))+(1-y_{i,k})log(1-\sigma(p_{i,k}))] \tag{5}\]  
where \(y_{i,k}=1\) indicates that the ii-th target belongs to the k-th defect category, and \(y_{i,k}=0\) otherwise. For bounding-box regression, YOLOv8 employs the Complete IoU (CIoU) loss and Distribution Focal Loss (DFL) to optimize overlap and shape alignment between predicted boxes and ground truth. CIoU takes into account the Intersection over Union (IoU), center distance, and aspect ratio discrepancies. Let \(\rho^{2}\left(b,b_{gt}\right)\) be the squared Euclidean distance between predicted and ground-truth centers, and c the diagonal of the smallest enclosing region covering both boxes. Denote v as the measure of aspect ratio consistency; \(\alpha\) the balancing coefficient; and (w, h), (\(w^{gt}\)t, \(w^{gt}\)) the predicted and ground-truth box dimensions, respectively. CIoU loss is formulated in Equations (6) and (7):  
\[L_{\text{CIoU}}=1-IoU+\frac{\rho^{2}\left(b,b_{gt}\right)}{c^{2}}+\alpha v \tag{6}\]  
\[\alpha=\frac{v}{\left(1-IoU\right)+v},v=\frac{4}{\pi^{2}}\left(arctan\frac{w ^{gt}}{h^{gt}}-arctan\frac{w}{h}\right)^{2} \tag{7}\]  
Meanwhile, DFL introduces a probabilistic model between predicted and actual coordinate values, improving regression precision. Let y lie between two adjacent discrete points \(y_{i}\) and \(y_{i+1}\), with corresponding predicted probabilities \(S_{i}\) and \(S_{i+1}\). The DFL loss function is given by Equation (8):  
\[L_{\text{DFL}}=-\sum\limits_{i=0}^{n-1}[\left(y_{i+1}-y\right)log\left(S_{i} \right)+\left(y-y_{i}\right)log\left(S_{i+1}\right)] \tag{8}\]  
where n is the number of discrete points, \(y\in[y_{i},y_{i+1}]\) denotes the ground-truth coordinate interval, and \([S_{0},S_{1},\ \ldots\,S_{n}]\) is the model-predicted coordinate distribution. Compared with regressing a single coordinate value, DFL enables a smoother approximation of the true value. This approach particularly benefits small or subtly distinct defects, as it incrementally aligns the predicted coordinate with the actual one by weighting the two nearest discrete points.