# SS-YOLOv8: A Lightweight Algorithm for Surface Litter Detection
## 3 SS(Sobel and Share)-YOLOv8 Algorithm
### Optimizing the Loss Function  
CIoU [27] is used as the loss function in YOLOv8. Three factors are considered: the overlap area between the predicted frame and the real frame and the distance between the center point and the aspect ratio. The definition range is as follows:  
\[L_{CIOU}=1-IOU(A,B)+\frac{\rho^{2}(A_{ctr},B_{ctr})}{c^{2}}+\alpha\upsilon \tag{6}\]  
\[\upsilon=\frac{4}{\pi^{2}}\bigg{(}arctan\frac{\omega s^{st}}{h^{st}}-arctan \frac{\omega}{h}\bigg{)}^{2},\ \alpha=\frac{\upsilon}{(1-IOU)+\upsilon} \tag{7}\]  
In the formula, \(A,B\) represent two frames, and \(A_{ctr},B_{ctr}\_{\text{ctr}}\) represent the center point of \(A\) and \(B\). From the formula, it can be seen that if the aspect ratio of the predicted frame and the \(g^{t}\) frame are the same, then the aspect ratio penalty term is constant 0, which is not reasonable. Observing the gradient of \(\omega,h\) relative to \(\upsilon\) in CIoU, it is found that these two gradients are a pair of opposite numbers, i.e., \(\omega\) and h cannot be increased or decreased at the same time, which is obviously not reasonable either. The loss function based on CIoU is affected by an unreasonable penalty factor, which causes the anchor frame to expand during the regression process, significantly slowing down the convergence.  
To solve this problem, an enhanced IOU loss function PIoU is proposed, which combines a penalty factor of adaptive target size with a gradient adjustment function based on anchor frame mass. PIoU losses can guide the anchor frame regression along an effective path, thus converging faster than traditional IoU-based loss functions. As shown in Figure 7.  
Although PIoU can effectively accelerate the convergence speed, there is a limitation that the accuracy decreases in the face of the surface litter dataset; so, this paper introduces a non-monotonic attention function and uses it in combination with a single hyperparameter controlled PIoU. By incorporating an attention mechanism into PIoU loss to yield the PIoU2 loss, we enhance the focus on mid-to-high-quality anchor frames, leading to improved target detector performance and precision. The attention function and the PIoU2 loss formulation are outlined below:  
\[q=e^{-P},q\in(0,1],\ u(x)=3x\cdot e^{-x^{2}},\ L_{PIoU2}=u(\lambda q)\cdot L_{ PIoU}=3\cdot(\lambda q)\cdot e^{-(\lambda q)^{2}}\cdot L_{PIoU} \tag{8}\]  
Introduce a new parameter \(q\), ranging from 0 to 1 inclusively, to represent the quality assessment of the anchor frame, substituting the penalty factor \(P\). Setting \(q\) to 1 corresponds to \(P\) at 0, signifying perfect alignment of the anchor frame with the target frame. As \(q\) grows, the value of \(P\) diminishes, reflecting a decline in anchor frame quality. The hyperparameter \(\lambda\) regulates the dynamics of the attention function, enhancing the focus on medium-quality anchor frames through a non-monotonic attention function, \(u(\lambda q)\). This innovation ensures that the PIoUv2 loss incorporates not just the attention mechanism  
Figure 7: PIoU loss function diagram.  
but also maximizes its benefits, particularly by prioritizing the medium-quality phase of the anchor frame regression. By concentrating on these medium-quality frames, PIoUv2 boosts the model's precision in identifying the target amidst surface clutter, streamlining hyperparameter optimization efforts.