# Research on a Surface Defect Detection Algorithm Based on MobileNet-SSD
## 2 Surface Defect Detection Method
### Defect Detection Model Based on MobileNet-SSD Network
#### 2.2.2 SSD Meta Structure  
SSD network is a regression model, which uses features of different convolution layers to make classify regression and boundary box regression. The model solves the conflict between translation invariance and variability, and achieves good detection precision and speed. Figure 3: The basic convolutional structure of MobileNet. Dw = depth-wise layers. Pw = point-wise layers. BN = batch normalization. Conv = convolution. ReLU6 = rectified liner unit 6. In each selected feature map, there are K frames that differ in size and width-to-height ratio. These frames are called the default boxes. Figure 4 expresses default boxes on feature maps of different convolutional layers. Each default box predicts the B class score and the four position parameters. Hence, \(\mathrm{B}*\mathrm{k}*\mathrm{w}*\mathrm{h}\) class score and \(4*\mathrm{k}*\mathrm{w}*\mathrm{h}\) position parameters must be predicted for a \(\mathrm{w}*\mathrm{h}\) feature image. This requires \((\mathrm{B}+4)*\mathrm{k}*\mathrm{w}*\mathrm{h}\) convolution kernels of the size 3 \(\times\) 3 to process the feature map. Then, the convolution results should be taken as the final feature for classification regression and bounding box regression. Here, B is set to four because there are four typical defects on the sealing surface of a container in the filling line. The scale of the default boxes for each feature map is computed as:  
\[\mathrm{S}_{\mathrm{k}}\mathrm{=S}_{\mathrm{min}}+\frac{\mathrm{S}_{\mathrm{ max}}\mathrm{-S}_{\mathrm{min}}}{\mathrm{m}-1}(\mathrm{k}-1)\,,\;(\mathrm{k} \in[1,\mathrm{m}]) \tag{9}\]  
where m is the number of feature maps; and \(\mathrm{S}_{\mathrm{max}}\), \(\mathrm{S}_{\mathrm{min}}\) are parameters that can be set. In order to control the fairness of feature vectors in the training and test experiments, the same five kinds of width-to-height ratios \(a_{r}=\{1,2,3,0.5,0.33\}\) were used to generate default boxes. Then, each default box can be described as:  
\[\mathrm{w}_{\mathrm{k}}^{\mathrm{a}}\mathrm{=S}_{\mathrm{k}}\sqrt{\mathrm{a}_ {r}} \tag{10}\]  
where \(\mathrm{w}_{\mathrm{k}}^{\mathrm{a}}\) is the width of default boxes; and \(\mathrm{h}_{\mathrm{k}}^{\mathrm{a}}\) is the height of default boxes. Next, a default box \(\mathrm{S}_{\mathrm{k}}^{\prime}=\sqrt{\mathrm{S}_{\mathrm{k}}\mathrm{S}_{ \mathrm{k}+1}}\) should be added when the width-to-height ratio is one. The center of each default box is \((\frac{i+0.5}{|\mathrm{k}_{i}|},\frac{i+0.5}{|\mathrm{k}_{i}|})\), and \(|\mathrm{f}_{\mathrm{k}}|\) is the size of the K-th feature unit, \(\mathrm{i},\mathrm{j}\in[0,|\mathrm{k}_{\mathrm{i}}|]\). The intersection over union (IoU) between area A and area B can be calculated as:  
\[\mathrm{IoU}=\frac{\mathrm{area}(\mathrm{A})\cap\mathrm{area}(\mathrm{B})}{ \mathrm{area}(\mathrm{A})\cup\mathrm{area}(\mathrm{B})} \tag{11}\]  
If the IoU of default box and calibration box (Ground-truth Box) is greater than 0.5, it means the default box matches the calibration box of that category. The SSD is an end-to-end training model. The overall loss function of the training contains the confidence loss \(\mathrm{L}_{\mathrm{conf}}(\mathrm{s},\mathrm{c})\) of the classification regression and the position loss of the bounding box regression \(\mathrm{L}_{\mathrm{loc}}(\mathrm{r},\mathrm{l},\mathrm{g})\).