# A Real-Time Fabric Defect Detection Method Based on Improved YOLOv8
## 3 Methods
### Inner-WloU Loss Function  
In the object detection task, YOLOv8 adopts the CIoU loss function as the main optimization objective for bounding box regression. CIoU not only takes into account the overlapping regions of the predicted and real boxes in the traditional IoU but also makes the model fit the geometrical information of the object more accurately by additionally introducing two key factors, namely, the distance of the centroid point and the aspect ratio. Specifically, the CIoU loss function is shown in Equation (5). \[\mathcal{L}_{\text{CIoU}}=1-\text{IoU}+\frac{\rho^{2}\left(\text{b},\text{b}^ {gt}\right)}{c^{2}}+\alpha\
u \tag{5}\]  
where IoU is the overlap weight of the prediction box and the real box, \(\rho^{2}\left(\text{b},\text{b}^{gt}\right)\) denotes the Euclidean distance between their centroids, c is the diagonal distance of the smallest enclosing box, \(\
u\) is the aspect ratio of the two boxes, and \(\alpha\) is the coefficient regulating the aspect ratio weight. The advantage of CIoU is that it considers several geometric factors, making it better than traditional IoU, DIoU [22], GIoU [23], etc. in terms of accuracy. In particular, during the training process, CIoU uses the same weight-updating strategy for all samples, ignoring  
Figure 4: Flowchart for generating a sample set using dynamic range factors. Figure 3: Structure of DySample. the difficulty differences between samples. For complex or low-quality samples, CIoU may not be able to provide enough gradient information, which leads to the model preferring to optimize simple samples and ignoring those difficult to locate. Meanwhile, although the CIoU loss function takes into account the overlap region, center distance, and aspect ratio of the frames, it tends to lead to poor model convergence and performance when dealing with small targets (e.g., fabric defects), especially those with small and extreme aspect ratios such as holes. To overcome these challenges, the WIoU [24] loss function is introduced in this paper, which ensures that the model can handle complex samples more efficiently during training by dynamically adjusting the weights of the samples. The core idea of WIoU is to regulate the weight of each sample according to the sample's outlier degree \(\beta\) so as to enhance the model's generalization ability. The loss function of the WIoU is shown in Equation (6). \[\mathcal{L}_{\text{WIoUv3}}=\text{r}\cdot\mathcal{L}_{\text{WIoUv1}} \tag{6}\]  
\[\mathcal{L}_{\text{WIoUv1}}=\mathcal{R}_{\text{WIoU}}\mathcal{L}_{\text{IoU}} \tag{7}\]  
\[\mathcal{R}_{\text{WIoU}}=\exp\left(\frac{\left(\text{x}-\text{x}_{\text{gt} }\right)^{2}+\left(\text{y}-\text{y}_{\text{gt}}\right)^{2}}{\left(\text{W}_{ \text{g}}^{2}+\text{H}_{\text{g}}^{2}\right)^{*}}\right) \tag{8}\]  
where \(\mathcal{L}_{\text{WIoUv3}}\) denotes the 3 rd version of the WIoU loss function, r is the gradient weight factor, and \(\mathcal{L}_{\text{IoU}}\) denotes the IoU loss function value. The weight factor r is determined by the outlier \(\beta\) of the sample, which is calculated as  
\[\beta=\frac{\mathcal{L}_{\text{IoU}}^{*}}{\mathcal{L}_{\text{IoU}}} \tag{9}\]  
where \(\mathcal{L}_{\text{IoU}}^{*}\) denotes the IoU loss value of the current sample, and \(\mathcal{L}_{\text{IoU}}\) is the average of the IoU loss values of all the samples in the training process. The specific formula for weight r is  
\[\text{r}=\frac{\beta}{\delta\alpha^{\beta-\delta}} \tag{10}\]  
where \(\alpha\) and \(\delta\) are two hyperparameters used to control the weight adjustment, which are used to adjust the weight gain of the samples, enabling the WIoU to dynamically adjust the gradient according to the sample outliers. Also, this paper introduces the inner-IoU [25] loss function, which accelerates the process of edge regression by utilizing auxiliary boxes of different scales for loss calculation. With this approach, the model can converge faster, especially in fabric defect detection, and this fast convergence helps to improve the detection of defects of multiple sizes.