## 2 Materials and Methods
### Training of the YOLO v3 Object Detection Model
#### 2.3.2 Model Training  
Figure 9 shows the model training of YOLO v3 in the following seven steps. First, input the image and training parameters such as the frequency of training and the size of the anchor frame. There is a total of 1000 images including standard products and defects for training, testing, and validation. The training set, the validation set, and testing set account for 72%, 18%, and 10%, respectively. Thus, there are 720 images for training, 180 images for validation, and 100 images for testing. Second, extract the features of an image via the residual network. Third, integrate the feature maps via feature pyramid networks, and output the feature maps in three grids sized 13 \(\times\) 13, 26 \(\times\) 26, and 52 \(\times\) 52, respectively, to examine the defects in large to small areas. Fourth, if the center of a defect feature is located in a grid, the model will detect defects within that grid. The detection  
Figure 8: YOLO v3 network structure map.  
process is as follows: put the coordinates of the center point of the grid as the center to form three bounding boxes and let the model decide the five parameters of each bounding box (center point coordinates (x, y), width (w), height (h), and confidence). The model then predicts the probability of each classification within each bounding box and chooses the bounding boxes based on the object score for the confidence as well as the non-maximum suppression. Fifth, use Equation (4) to calculate the loss value and continue to update it with the progressing of the training. Finally, trigger the termination condition (when the largest number of iterations have been reached) to complete the model training.  
\[\begin{array}{ll}loss&=\lambda_{coord}\sum\limits_{i=0}^{S^{2}}\sum\limits_{j=0 }^{B}I_{ij}^{obj}\Big{[}\big{(}\big{(}t_{x}-t_{x}\big{)}^{2}+\big{(}\big{(}t_{y }-t_{y}\big{)}^{2}+\big{(}\big{(}t_{w}-t_{w}\big{)}^{2}+\big{(}\big{(}t_{h}-t_{h }\big{)}^{2}\big{)}\big{]}\\ &+\sum\limits_{i=0}^{S^{2}}\sum\limits_{j=0}^{B}I_{ij}^{obj}[c_{i}\log(\hat{c }_{i})+(1-c_{i})\log(1-\hat{c}_{i})]\\ &+\lambda_{noobj}\sum\limits_{i=0}^{S^{2}}\sum\limits_{j=0}^{B}I_{ij}^{noobj} [c_{i}\log(\hat{c}_{i})+(1-c_{i})\log(1-\hat{c}_{i})]\\ &+\sum\limits_{i=0}^{S^{2}}I_{i}^{obj}\sum\limits_{c\in classes}[p_{i}(c) \log(\hat{p}_{i}(c))+(1-p_{i}(c))\log(1-\hat{p}_{i}(c))]\end{array} \tag{4}\]  
where \(\lambda_{coord}\) is the weighted loss when the bounding box contains defects (\(\lambda_{coord}\) = 5); and \(\lambda_{noobj}\) is the weighted loss when the bounding box does not contain defects (\(\lambda_{noobj}\) = 0.5). _liobj_ means whether the center of the defect falls into the grid. If yes, it is 1, and if no, it is 0. _lijob_ means whether the No. \(j\) bounding box in grid \(i\) is accountable for this default. If yes, it is 1, and if no, it is 0. _lijob_ means whether the No. \(j\) bounding box in grid \(i\) is not accountable for this defect. If no, it is 1, and if yes, it is 0. S2 means the image is cut into number S \(\times\) S of grids; B is the predicted number of bounding boxes (YOLO v3 sets three by default); _tx_ and _ty_ are the deviation value from the coordinates of the real feature box to the left corner of the grid; \(\hat{t}_{x}\) and \(\hat{t}_{y}\) are the deviation value from the coordinates of the predicted feature box to the left corner of the grid; _tw_ and _th_ reflect the size scaling from the real feature box to the anchor frame; \(\hat{t}_{w}\) and \(\hat{t}_{h}\) reflect the size scaling from the predicted bounding box to the anchor frame; ci is the confidence degree of the real feature box in grid \(i\); \(\hat{c}_{i}\) is the confidence degree of the bounding box in grid \(i\); \(pi(c)\) is the probability of the real grid i belonging to class c; and \(\hat{p}_{i}(c)\) is the probability of the predicted grid \(i\) belonging to class c.