# An Improved Method for Ship Target Detection Based on YOLOv4
## 3 Methods
### YOLOv4 Model
#### 3.1.2 Loss Function  
There are three components to the loss of YOLOv4:  
\[Loss=Loss_{reg}+Loss_{conf}+Loss_{cls}. \tag{1}\]  
Regression loss of the object bounding box: the regression loss of the predicted bounding box is calculated using CIOU (Complete Intersection Over Union) [23]. As shown in Figure 2, CIOU considers three geometric measures: overlap area, central point distance and aspect ratio, which makes the bounding box regression more stable.  
The formula for calculating CIOU is shown as follows:  
\[CIOU=IOU-\frac{\rho^{2}(b,b^{st})}{c^{2}}-\alpha v, \tag{2}\]  
where  
\[IOU=\frac{area(b\cap b^{st})}{area(b\cup b^{st})}, \tag{3}\]\[\alpha=\frac{v}{(1-IOU)+v}, \tag{4}\]  
\[v=\frac{4}{\pi^{2}}(\arctan\frac{w^{st}}{h^{st}}-\arctan\frac{w}{h})^{2}, \tag{5}\]  
where \(b\) represents the predicted bounding box; \(w^{st}\) and \(h^{st}\) represent the width and height of the ground truth, respectively; \(\rho^{2}(b,\,b^{st})\) represents the distance in Euclidean space between the predicted bounding box's center point and the ground truth's center point; \(c\) represents the diagonal distance of the smallest closed area that contains both the predicted bounding box and the ground truth; \(\alpha\) represents the weighting factor; \(v\) is used to consider the similarity of aspect ratio; \(IOU\) represents the intersection ratio between the ground truth and the predicted bounding box; \(1\)-\(CIOU\) represents the regression loss of the predicted bounding box.  
Figure 1: YOLOv4 network structure diagram. * represents multiply.  
Figure 2: Target frame distance diagram. c represents the diagonal distance of the smallest closed area which contains both the predicted bounding box and the ground truth. d represents the distance in Euclidean space between the predicted bounding box’s center point and the ground truth’s center point.  
\[loss_{CIOU} =1-CIOU=1-IOU+\frac{\rho^{2}(b,b^{gt})}{c^{2}}+\alpha v, \tag{6}\] \[Loss_{reg} =\lambda_{coord}\sum\limits_{i=0}^{K\times K}\sum\limits_{j=0}^{M}I_ {ij}^{obj}(2-w_{i}\times h_{i})loss_{CIOU}, \tag{7}\]  
where \(\lambda_{coord}\) represents the weighting factor of the positive sample. The input image is segmented into \(K\times K\) cells, and each cell generates \(M\) predicted bounding boxes. \(I_{ij}^{obj}=1\) when one object is in the \(j\)th prediction box in cell \(i\), and on the contrary, \(I_{ij}^{obj}=0\) when there is no target. \(w_{i}\) and \(h_{i}\) denote the width and height of the predicted bounding box, with \((2-w_{i}\times h_{i})\) representing the penalty term. The smaller the bounding box is, the greater its weight becomes.  
Confidence loss: confidence loss consists of two parts: the loss of confidence for positive and negative samples, and the loss value is calculated using cross-entropy.  
\[\begin{array}{ll}Loss_{conf}=&-\sum\limits_{i=0}^{K\times K}\sum\limits_{j= 0}^{M}I_{ij}^{obj}[\hat{C}_{i}\log(C_{i})+(1-\hat{C}_{i})\log(1-C_{i})]\\ &-\lambda_{noobj}\sum\limits_{i=0}^{K\times K}\sum\limits_{j=0}^{M}I_{ij}^{ noobj}[\hat{C}_{i}\log(C_{i})+(1-\hat{C}_{i})\log(1-C_{i})]\end{array}, \tag{8}\]  
where \(\hat{C}_{i}\) represents the sample value of the confidence, \(C_{i}\) represents the value of the predicted confidence and \(\lambda_{noobj}\) represents the weighting factor of the negative sample \(I_{ij}^{noobj}=1\) when the \(j\)th prediction box in cell \(i\) has no object, and on the contrary, \(I_{ij}^{noobj}=0\) when there is an object.  
Loss of predicted classes: this consists of classification loss of positive samples, and the value is calculated using cross-entropy.  
\[Loss_{cls}=-\sum\limits_{i=0}^{K\times K}\sum\limits_{j=0}^{M}I_{ij}^{obj} \sum\limits_{c\in classes}[\hat{p}_{i}(c)\log(p_{i}(c))+(1-\hat{p}_{i}(c)) \log(1-p_{i}(c))], \tag{9}\]  
where \(\hat{p}_{i}(c)\) represents the sample value of the class and \(p_{i}(c)\) denotes the probability of an object belonging to the \(c\)th category.