# ReBiDet: An Enhanced Ship Detection Model Utilizing ReDet and Bi-Directional Feature Fusion
## 2 The Proposed Method
### Anchor Improvement  
ReBiDet is a two-stage object detection model that follows an anchor-based approach, where anchors are predefined boxes placed at various positions in an image [13]. The selection of anchor sizes and aspect ratios should be based on the distribution of object annotation boxes in the dataset, which can be considered as prior knowledge [30]. Improper anchor settings can hinder the network from effectively learning features of certain objects or result in the misidentification of adjacent objects. Therefore, it is crucial to carefully consider the size and shape distribution of objects in the training dataset and experiment with different numbers and ratios of anchors to find the most suitable settings for improving the network's detection performance. To accomplish this, we employ the K-means algorithm to group the aspect ratios of ship bounding boxes in the HRSC2016 dataset into multiple clusters and determine anchor sizes based on the average aspect ratio of each cluster.  
The K-means algorithm, presented in Algorithm 1, is an unsupervised learning technique. It starts by taking a set of aspect ratio data from the HRSC2016 dataset, randomly selecting \(K\) (set to 5 in our case) initial cluster centers. Each data point is then assigned to the nearest cluster center, and the cluster centers are iteratively recalculated and adjusted until they converge or reach a preset number of iterations. The objective is to maximize similarity within each cluster while minimizing similarity between clusters.  
```
1:Set of bounding box aspect ratios \(R=r_{1},r_{2},\ldots,r_{n}\), number of clusters \(K\)
2:Set of K cluster centers \(m_{1},m_{2},\ldots,m_{K}\) and cluster labels for each bounding box aspect
3:Randomly initialize \(K\) centroids \(m_{1},m_{2},\ldots,m_{K}\) from \(R\)
4:repeat
5:for\(i=1\) to \(n\)do
6: Assign \(r_{i}\) to the nearest centroid \(m_{j}\), \(j\) in \([1,K]\)
7:for\(j=1\) to \(K\)do
8: Recompute \(m_{j}\) as the mean of all \(r_{i}\) assigned to centroid \(m_{j}\)
9:until convergence or maximum number of iterations is reached
10:return Set of \(K\) cluster centers \(m_{1},m_{2},\ldots,m_{K}\) and cluster labels for each bounding box aspect
```  
**Algorithm 1** K-means used to cluster the aspect ratios of ground truth boxes.  
Figure 5 illustrates the aspect ratio data of bounding boxes in the HRSC2016 dataset, grouped into five categories. The deep blue dots represent the cluster centroids of each aspect ratio group, which are {3.9, 5.8, 6.2, 6.3, 7.3}. Although these centroids span from 3.9 to 7.3, they are concentrated around 6. This suggests that the aspect ratios of objects in the HRSC2016 dataset tend to cluster around the value of 6. After conducting multiple experiments, aspect ratios of 7.3 and above did not yield satisfactory results in practical detection. Analysis indicates that such aspect ratios are too large. Although they appear in the clustering results, many ground truth boxes in the dataset are not strictly horizontal or vertical, but rather arbitrary. Large aspect ratios result in low intersection over union (IoU) values between anchor boxes and ground truth boxes, thus failing to enhance the model's performance and introducing numerous negative samples that adversely impact performance. Consequently, we set the ratios of the RPN anchor generator as {1/6, 1/4, 1/2, 1, 2, 4, 6}.