# SqueezeNet: An Improved Lightweight Neural Network for Sheep Facial Recognition
## 2 Materials and Methods
### SSD Model Improvement
#### 2.3.3 BalancedL1 Loss Function  
The loss function of SSD includes the position loss function smoothL1 for the bounding boxes and the confidence loss function Softmax. Refer to Equation (1).  
\[L(x,c,l,g)=\frac{1}{N}(L_{conf}(x,c)+\alpha L_{loc}(x,l,g)) \tag{1}\]  
In the equation, \(N\) represents the number of positive samples for prior boxes, \(c\) is the predicted value for category confidence, \(l\) is the predicted position value for the bounding box corresponding to the prior box, and \(g\) is the ground truth position parameter.  
Figure 5: ECA mechanism module.  
\begin{table}
\begin{tabular}{c c c c} \hline \hline
**Algorithm Model** & \
\begin{tabular}{c} **Mean Average** \\ **Precision mAP/\%** \\ \end{tabular} & **Model Volume/MB** &
\begin{tabular}{c} **Detection** \\ **Speed/(Frame-s-1)** \\ \end{tabular} \\ \hline SSD + MobileNetv2 & 76.60 & 56.4 & 63.24 \\ SSD + ShuffleNetv1 & 78.53 & 88.1 & 61.58 \\ SSD + SqueezeNet & 77.21 & 35.6 & 64.12 \\ \hline \hline \end{tabular}
\end{table}
Table 3: Comparison table of three lightweight backbone networks.  
To improve the detection accuracy of the model and further reduce the errors between the model predictions and actual values, this paper replaces the position loss function smoothL1 in SSD with the BalancedL1 loss function.  
BalancedL1 is inspired by the smoothL1 loss function, where the smoothL1 loss classifies inliers and outliers by setting a turning point and truncates the gradient for outliers using max (p, 1.0). Compared to smoothL1, BalancedL1 significantly enhances the gradient for inliers, allowing these accurate points to play a more crucial role during training [24]. By setting a turning point to distinguish outliers from inliers, for those identified as outliers, the gradient is fixed at 1.  
The core idea of BalancedL1 is to boost the crucial regression gradients and balance the included samples, thus achieving more balanced training in terms of classification, overall localization, and precise positioning. The detection box regression loss for BalancedL1 is shown in Equation (2).  
\[L_{loc}=\sum_{i\in x,y,w,h}^{K}L_{b}(t_{i}^{u}-v_{i}) \tag{2}\]  
The corresponding gradient for BalancedL1 is given in Equation (3).  
\[\frac{\partial L_{loc}}{\partial w}\propto\frac{\partial L_{b}}{\partial t_{i }^{u}}\propto\frac{\partial L_{b}}{\partial x} \tag{3}\]  
Based on the above equations, a generalized gradient is designed as shown in Equation (4).  
\[\frac{\partial L_{b}}{\partial x}=\left\{\begin{array}{l}\alpha\ln(b|x|+1), if|x|<1\\ \gamma,otherwise\end{array}\right. \tag{4}\]  
Here, the elevation of the inliers' gradient is controlled; a smaller gradient enhances inliers without affecting the value of outliers. The parameter adjusts the upper bound of the regression error, aiming to achieve a better balance between different tasks, controlling the balance at both the sample and task levels. By adjusting these two parameters, more balanced training is achieved. The BalancedL1 loss function is given in Equation (5).  
\[L_{b}(x)=\left\{\begin{array}{l}\frac{a}{b}(b|x|+1)\ln(b|x|+1)-\alpha|x|, if|x|<1\\ \gamma|x|+C,otherwise\end{array}\right. \tag{5}\]  
The parameters satisfy the following conditions as shown in Equation (6).  
\[\alpha\ln(b|x|+1)=\gamma \tag{6}\]  
Default parameter settings: \(\alpha=0.5\), \(\gamma=1.5\).