# Improved U2 Net-Based Surface Defect Detection Method for Blister Tablets
## 4 Proposed Methods
### Loss Function Consisting of Gaussian Laplace Operator and Cross-Entropy Function  
Since this paper targets the defect detection of blister tablets, the key to detection is to be able to detect the edges of the defects, that is, the model should be able to accurately segment the edges of the defects when utilizing U2 Net for segmentation. Starting from the design of the loss function, the intention is to guide the model to strengthen the ability of segmentation detection of defect edges.  
The Laplace operator [23] is a second-order differential operator specialized in image processing and suitable for edge detection. The Laplace operator captures rapidly changing regions in an image, which usually correspond to edges, by calculating the second-order derivatives of the grey values of the image. However, just applying only the Laplacian operator does not solve the problem effectively, mainly because the process of extracting the features of the model is easy to be mixed with noise; in order to eliminate the noise, the noise of the defective image is eliminated by using a Gaussian filter [24], so, in this paper, the Laplacian of Gaussian (LoG) operator is used.  
Equation (3) is a Gaussian filter, where \(x\) and \(y\) are the distances of the pixel positions relative to the center of the convolution kernel, and \(\sigma\) is the standard deviation of the Gaussian kernel, which controls the width of the Gaussian function.  
\[G(x,y)=\frac{1}{2\pi\sigma^{2}}e^{-\frac{x^{2}+y^{2}}{2\pi\sigma^{2}}}, \tag{3}\]  
Equation (4) is a second-order differential operator of the Laplace operator used to calculate the second-order rate of change of the grey values of the image.  
\[\
abla^{2}f=\frac{\partial^{2}f}{\partial x^{2}}+\frac{\partial^{2}f}{ \partial y^{2}} \tag{4}\]  
Combining the Gaussian filter with the Laplace operator yields the Gaussian Laplace operator based on Gaussian filter smoothing, as shown in Equation (5). \(G(x,y)\) is the Gaussian kernel, \(f(x,y)\) is the grey value of the image at coordinates \(x,y\), and \(*\) denotes the convolution operation.  
\[\text{LoG}(x,y)=\
abla^{2}[G(x,y)*f(x,y)]=-\frac{1}{\pi\sigma^{4}}(1-\frac{x^{ 2}+y^{2}}{2\sigma^{2}})e^{-\frac{x^{2}+y^{2}}{2\sigma^{2}}} \tag{5}\]  
On the basis of these theoretical derivations, this paper gives the loss function based on the combination of Gaussian Laplacian operators, as shown in Equation (6), where \(\text{P}_{\text{G}(x,y)}\) and \(\text{P}_{\text{s}(x,y)}\) are the pixel values of the true values, respectively, the predicted values of the model are located at the coordinates \(x,y\), respectively, and \(h,w\) are the height and width of the image, respectively.  
\[\text{LoG}=-\sum_{(x,y)}^{(h,w)}\left[\text{P}_{\text{G}(x,y)}\text{P}_{\text{ S}(x,y)}\right] \tag{6}\]  
Cross-entropy loss function is one of the methods commonly used by U2-Net to measure the true and predicted values. This approach directs the model to learn faster, but the disadvantage is that the pixels of the image are focused all consistently without a focus point, which results in insufficient attention to the edges. The cross-entropy loss function is shown in Equation (7).  
\[\text{CrossEntropyLoss}=-\sum_{(x,y)}^{(h,w)}\left[\text{P}_{\text{G}(x,y)} \log\text{P}_{\text{S}(x,y)}+(1-\text{P}_{\text{G}(x,y)})\log(1-\text{P}_{ \text{S}(x,y)})\right] \tag{7}\]  
In summary of the above study, this paper adopts a loss function that combines the Gaussian Laplace operator and the cross-entropy function as the loss function of the model. Equation (8) is a new loss function called GLoss taken in this paper; \(\gamma\) denotes the weightof the edge loss. This design makes the model learn faster and at the same time have more attention to defective segmentation of the edges.  
\[\text{GLoss}=\text{CrossEntropyLoss}+\gamma\text{LoG} \tag{8}\]