## 3 Defect-Adapted Hierarchical Deep Learning Model
### Adjustment of Balanced Weights for Loss Function  
In multi-output classification, particularly in hierarchical structures with multiple classification layers, it is necessary to carefully calculate the loss function:  
\[L(y,\hat{y})=-\frac{1}{C}{\sum_{i=1}^{C}}y_{i}\cdot\log(\sigma(\hat{y}_{i}))+(1- y_{i})\cdot\log(1-\sigma(\hat{y}_{i})) \tag{1}\]  
In Equation (1), \(C\) is the number of classes, and \(y\) is a binary indicator (0 or 1) that specifies whether class label \(i\) is the correct classification for the observation. \(\hat{y}\) is the predicted raw output score for the class, and \(\sigma\) represents the sigmoid function, which transforms the output scores into probabilities between 0 and 1. This loss function represents a standard multi-output loss function. However, an unweighted loss function that applies equally to all outputs may not suffice when learning opportunities are unevenly distributed across classes. This problem is especially significant in hierarchical models; if the loss function treats all classes equally, it will focus on classes that appear more frequently and may fail to learn finer-grained distinctions for less frequent classes [35].  
In the DHS-CNN, each abnormal class has different learning opportunities, necessitating adjustments in the loss function calculation to ensure accurate learning and prediction at each stage. In other words, assigning appropriate weights to each stage is essential so that the model correctly identifies and predicts critical abnormal types. For multi-output classification problems, using a loss function that assigns different weights to each class is crucial. This approach enables the model to recognize the varying importance of the abnormal types and achieve more accurate classification. For instance, higher weights can be assigned to abnormal types such as **burr**, **broken**, **b_edge**, and **b_bubble**, whereas relatively lower weights can be assigned to types such as no _lens and **etc_abnormal**. This strategy accounts for the possibility that certain abnormal types (e.g., **b_edge** and **b_bubble**) may be more critical or occur more frequently.  
Additionally, in the hierarchical structure, a lower weight is assigned to the **b_bubble** class because it affects more layers than **b_edge**. This implies that, within the current structure, there are relatively more opportunities to learn and identify **b_bubble** defects than **b_edge** defects. Owing to this structural characteristic, the strategy was to assign a lower weight to **b_bubble**, encouraging the model to focus more on other abnormal types, particularly those with fewer learning opportunities.  
\[L_{i}=y_{i}\cdot\log(\sigma(\hat{y}_{i}))+(1-y_{i})\cdot\log(1-\sigma(\hat{y}_ {i})) \tag{2}\]  
\[L=\sum_{i-1}^{N}w_{i}\cdot L_{i} \tag{3}\]  
where \(L\) is the total loss function, and \(N\) is the total number of abnormal types. Thus, \(L_{i}\) represents the loss function for a specific type of abnormality, and \(w_{i}\) denotes the weight of a particular type of target to be adjusted. In this study, the weights for **no_lens**, **etc_abnormal**, **burr**, **broken**, **b_edge**, and **b_bubble** were determined based on the previously described loss function strategy, resulting in values of 1, 1, 2, 2, 3, and 1, respectively.