# A Multi-Scale Rebar Detection Network with an Embedded Attention Mechanism
## 3 Methods
### SD_IoU Bounding Box Loss Function  
The loss function of YOLOv5 consists of classification loss, localization loss, and confidence loss, where the CloU loss function is used for the bounding box loss of localization loss. The principle of CloU loss [29] is shown in Figure 7 and Equations (10)-(13), where A is the prediction box, B is the true box, and C is the smallest outer rectangle of the two boxes A and B.  
\[L_{CIoU}=1-IoU+\frac{\rho^{2}(b,b^{st})}{c^{2}}+\alpha v \tag{10}\]  
\[\alpha=\frac{v}{(1-IoU)+v} \tag{11}\]  
\[IoU=\frac{A\cap B}{A\cup B} \tag{12}\]  
\[v=\frac{4}{\pi^{2}}(\arctan\frac{w^{st}}{h^{st}}-\arctan\frac{w}{h})^{2} \tag{13}\]  
In the above equation, \(IoU\) is the intersection ratio of the prediction box and the true box. \(\rho\) is the Euclidean distance between the center points of the prediction box and the true box; \(b\) and \(b^{st}\) represent the center points of the prediction box and the true box, respectively; \(c\) represents the diagonal length of the smallest external rectangle of the prediction box and the true box; \(\alpha\) is the weighting factor; \(w\) and \(w^{st}\) represent the width of the prediction box and the true box, respectively; and \(h\) and \(h^{st}\) represent the width of the prediction box and the true box width, respectively. The boundary box loss of YOLOv5 uses CIoU loss because it considers the overlapping area, centroid distance, and aspect ratio of the boundary box regression, but the rebar cross-section targets are dense and it is difficult to optimize the error between the prediction \begin{table}
\begin{tabular}{c c c c} \hline \hline
**Module Name** & **Module Number** & **Parameters** & **Output** \\ \hline Conv & 1 & 3520 & 320 \(\times\) 320 \\ Conv & 1 & 18,560 & 160 \(\times\) 160 \\ C3 & 3 & 18,816 & 160 \(\times\) 160 \\ Conv & 1 & 73,984 & 80 \(\times\) 80 \\ C3 & 6 & 115,712 & 80 \(\times\) 80 \\ Conv & 1 & 295,424 & 40 \(\times\) 40 \\ C3 & 9 & 625,152 & 40 \(\times\) 40 \\ Conv & 1 & 1,180,672 & 20 \(\times\) 20 \\ C3 & 3 & 1,182,720 & 20 \(\times\) 20 \\
**CA** & **1** & **25,648** & **20 \(\times\) 20** \\ SPPF & 1 & 656,896 & 20 \(\times\) 20 \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Backbone network structure after joining CA. Figure 7: CIoU diagram. box and the true box during the network training. Inspired by SloU Loss [30], this paper considers the angle deviation and distance relationship between the prediction box and the true box, and we design the SD_IoU Loss bounding box loss function, which reduces the degrees of freedom of the prediction box by adding angle and distance penalty costs to prevent it from \"wandering around\". In addition, the shapes of the rebar cross-sections studied in this paper are all circular and of the same shape, and the sizes of the anchor boxes are fixed using the K-means++ clustering algorithm before the network training. Therefore, the shape difference between the prediction box and the true box is not considered a factor affecting the regression of the bounding box, which can increase the training speed of the network. The SD_IoU Loss is calculated as shown in Equation (14). \[L_{SD\_IoU}=1-IoU+\frac{\Gamma}{2}+\frac{\Delta}{2} \tag{14}\]  
In the above equation, \(\Gamma\) is the angle loss and \(\Delta\) is the distance loss. The angular loss is calculated as shown in Equations (15)-(18).