# Research on Rapid Recognition of Moving Small Targets by Robotic Arms Based on Attention Mechanisms
## 3 YOLOv8 Model Improvements
### Improving the Regression Loss Function  
In the YOLOv8 model, the Complete IOU (CIOU) is used as the detection box regression loss function [40]. The model takes into account the overlapping area, center point distance, and aspect ratio between the predicted box and the actual box in bounding box regression. That is,  
\[\left\{\left(\mathrm{W}=\mathrm{k}W_{gt},\;\mathrm{H}=\mathrm{k}H_{gt}\right) \left|\mathrm{k}\in\mathrm{R}+\right.\right\} \tag{3}\]  
In the equation, \(\mathrm{W}\) and \(\mathrm{H}\) denote the width and height of the predicted bounding box, respectively, while \(W_{gt}\) and \(H_{gt}\) stand for the width and height of the ground truth box. The aspect ratio within the formula simply indicates the proportional relationship between the width or height of the predicted box and that of the ground truth box. When the aspect ratios between various predicted boxes and actual boxes align, the computed result of the CIOU metric will be identical. Addressing this issue, in this study, the SIOU is adopted as the detection box regression loss function. [PERSON] demonstrated that center-aligned bounding boxes converge more rapidly, forming the SIOU by composing the angle cost, distance cost, and shape cost. The 'angle cost' describes the smallest included angle between the line connecting the centers of the bounding boxes and the \(x\)-\(y\) axis:  
\[\mathcal{I}=\sin\left(2\sin\frac{\min\left(\left|x-x_{gt}\right|,\left|y-y_{ gt}\right|\right)}{\sqrt{\left(x-x_{gt}\right)^{2}+\left(y-y_{gt}\right)^{2}+ \mu}}\right) \tag{4}\]  
The term 'distance cost' describes the normalized distance between the center points of the two bounding boxes on both the \(x\)-axis and \(y\)-axis. Its penalty strength is directly proportional to the 'angle cost'. 'Distance cost' is defined as follows:  
\[\Delta=\frac{1}{2}{\sum_{t=w,h}}\left(1-e^{-\mathrm{Y}gt}\right) \mathrm{Y}=2-\mathcal{I} \tag{5}\]  
Figure 5: Improved c2f module.  
\[\begin{split}\rho_{w}&=\left(\frac{x-x_{gt}}{W_{g}}\right)^{ 2}\\ \rho_{h}&=\left(\frac{y-y_{gt}}{W_{g}}\right)^{2} \end{split} \tag{6}\]  
The term'shape cost' describes the shape differences between the two bounding boxes. It is not equal to zero when the sizes of the two bounding boxes are inconsistent. 'Shape cost' is defined as follows:  
\[\Omega=\frac{1}{2}{\sum_{t=w,h}}\left(1-e^{wt}\right)^{\theta},\theta=4 \tag{7}\]  
\[\begin{split}\omega_{w}&=\left(\frac{|w-w_{gt}|}{max (w,w_{gt})}\right)\\ \omega_{h}&=\left(\frac{|h-h_{gt}|}{max(h,h_{gt})} \right)\end{split} \tag{8}\]  
\(R_{SIOU}\) is similar to \(R_{CIOU}\), as both are composed of a distance cost and a shape cost. Building upon the foundation of CIOU, SIOU introduces an additional consideration for shape similarity, which refines the loss calculation further. This enhancement allows for more effective handling of bounding box matches that exhibit significant shape differences. Thus, the focus is not only on matching position and size, but also on ensuring shape consistency. The formula for \(R_{SIOU}\) can be described as follows:  
\[R_{SIOU}=\Delta+\Omega \tag{9}\]  
For rapidly moving small targets, a grab algorithm is designed based on the improved YOLOv8 target detection method and depth ranging. This article uses the D435i camera to real-time capture both color images and depth images of grabbing scenarios. Through the target detection method applied to color images, the positional information of the target object is acquired, resulting in the output of the two-dimensional pixel coordinates (X, Y) for the grasping location. The depth value (Z) corresponding to this grasping position is derived from the depth image. These coordinates are then transformed into rotation angles relative to the various axes of the robot arm, guided by the coordinate transformation relationship. This process enables the robotic arm to precisely grasp the target.