# Research on a Multi-Type Barcode Defect Detection Model Based on Machine Vision
## 3 Model Construction
### Lightweight Two-Stage Fusion Model-Y8-LiBAR Net  
The end-to-end barcode defect detection system developed in this study consists of four sequential stages: object detection-cropping, dual-branch feature extraction, hierarchical attention fusion, and three-class decision making. The original image is input into the lightweight detector YOLOv8n, which locates the barcode and generates bounding boxes with millisecond-level speed. The area within the bounding box is then cropped and normalized, forming a new dataset containing only the barcode, which significantly reduces the interference from complex backgrounds for subsequent analysis.  
To adaptively balance the importance of the two types of information, a hierarchical attention fusion module is introduced at the feature level. First, the two vectors are concatenated and input into a lightweight perceptron to obtain normalized weights \(\alpha,\beta\in(0,1)\), then a fused vector is generated through a weighted sum:  
\[f\ =\ \alpha Z_{res}\ +\ \beta Z_{vit},\ \alpha\ +\ \beta\ =\ 1 \tag{12}\]  
Figure 6: ViT-B/16 model Architecture.  
Hierarchical Attention Fusion (HAF).To obtain interpretable and adaptive weights for the two feature streams, HAF adopts a two-level gating strategy.  
(i) Channel-level gating. The ResNet feature vector \(Z_{res}\in\mathbb{R}^{1024}\) and the ViT feature vector \(Z_{vit}\in\mathbb{R}^{1024}\) are first concatenated to form \(h=[Z_{res};Z_{vit}]\in\mathbb{R}^{2048}\) This joint descriptor passes through a bottleneck MLP (r = 4) to capture cross-modal interactions:  
\[h^{\prime}\ =\ ReLU\left(w_{1}h\ +\ b_{1}\right),\ \begin{bmatrix}\alpha\\ \beta\end{bmatrix}\ =\ Softmax\ \left(w_{2}h^{\prime}+\ b_{2}\right)\] (12a) where \[\alpha,\beta\in(0,1)\] and \[\alpha+\beta=1\].  
(ii) Task-level fusion. The normalized weights modulate the two branches to generate the fused representation:  
\[f\ =\ \alpha Z_{res}\ +\ \beta Z_{vit} \tag{12b}\]  
This hierarchy--MLP gating followed by weighted summation--enables the network to emphasize texture-rich cues when surface damage is subtle (higher \(\alpha\)) and to favor global layout cues under severe distortions (higher \(\beta\)). The fused vector \(f\) is finally projected to the three-class logits via the existing classification head (13).  
This strategy not only avoids the dimensional expansion caused by simple concatenation but also allows the model to dynamically adjust the balance between local and global features for different scenarios. Finally, the fused vector is mapped to a three-dimensional probability space via a Softmax classification head, where the output \(\hat{y}\) corresponds to the three classes: \"non-barcode\", \"normal barcode\", and \"defective barcode\":  
\[\hat{y}\ =\ Softmax\ (wf\ +\ b),\hat{y}\in\mathbb{R}^{3} \tag{13}\]  
The network is trained with a composite loss \(L_{total}=L_{CE}+\lambda L_{reg}\), where \(L_{CE}=-\sum_{i=1}^{N}\sum_{c=0}^{2}logp(y_{i}=c|x_{i})\) and \(L_{reg}=\left\|F_{Res}F_{Vit}^{T}\right\|_{F}^{2}\); a grid search \((\lambda=0.01,0.05,0.1,0.2)\) indicates \(\lambda=0.1\) achieves the best trade-off, and this value is therefore adopted in all experiments. This hybrid architecture fully leverages the advantages of each submodule: YOLOv8n provides millisecond-level localization and background removal; ResNet50 retains the sensitivity of convolutional networks to local textures and gaps; and ViT-B/16 compensates for convolution's limitations in modeling long-range dependencies. Hierarchical attention, through an interpretable weight allocation mechanism, enables the model to adaptively balance local and global information for different scenarios, preventing representation redundancy and overfitting caused by simple concatenation. The specific model structure is shown in Figure 7.