# On-Board Ship Detection in Micro-Nano Satellite Based on Deep Learning and COTS Component
## 2 On-Board Ship Detection Based on Deep Learning
### Region Proposal Network with Square Anchor  
RPN is used to extract the target proposals, which applies the anchor mechanism to ensure that targets of different positions and orientations can be covered by the proposals. The key parameters in RPN are the size and aspect ratio of anchors according to the training set. The length range of the ship target is very large, so both small anchor and large anchor should be taken into consideration. The length-to-width ratio of a ship target is generally around 3.0 to 7.2. Since ship targets have random orientations, the aspect ratio of bounding box for ship may range from 1:8 to 8:1. However, anchors with multiple aspect ratios will increase the amount of calculation, and some types of anchors with limited positive samples will generate a large number of false alarms. After determining the size and aspect ratio of anchors, there are two principles for choosing positive samples from anchors. (1) The anchor is assigned as a positive sample if its Intersection-over-Union (IoU) overlap with any ground-truth boxes is greater than 0.5, since the correct detection can only be calculated when the IoU overlap of the final detection result and ground-truth boxes is greater than 0.5. Reference [40] showed that the selection rules of training samples are closely related to the final detection results. (2) The anchor with highest IoU overlap with a ground-truth box is also determined to be a positive sample, in order to ensure that each target is covered by a positive sample at least. According to these two principles and the characteristics of large aspect ratio and wide size distribution of ship target, we design square anchors. Figure 3 shows some positive samples of different types of anchors, where B and S represent original anchor and square anchor respectively, followed by  
Figure 2: The process of on-board ship detection based on deep learning. the number of anchors. For the ships in training set, we use K-means to select four scales and choose three aspect ratios to adapt the rectangular target at the same time. As shown in Figure 3, for the target orientation approximately 45\({}^{\circ}\), the square anchors (aspect ratio is 1) have better positive samples. However, for the target orientation 90\({}^{\circ}\) (or 0\({}^{\circ}\)), B-4 cannot choose appropriate samples, and B-12 can select the positive samples close to the target. Compared with B-4 and B-12, positive samples of good quality can be selected by S-4 under various circumstances, which will provide good support for subsequent networks. We form a multi-task loss function for training RPN, which is defined as follows  
\[L\left(\{p_{i}\},\{t_{i}\}\right)=\frac{\lambda_{1}}{N_{cls}}\sum_{i}L_{cls}(p _{i},{p_{i}}^{*})+\frac{\lambda_{2}}{N_{det}}\sum_{i}{p_{i}}^{*}L_{det}(t_{i},{ t_{i}}^{*}) \tag{1}\]  
where \(i\) is the index number of anchors, \(p_{i}\) is the probability that the image patch of the anchor \(i\) is ship target, \(p_{i}\)* is the ground truth label (1 represents that the anchor is positive sample, and 0 means that the anchor is non-ship target), \(t_{i}\) is the predicted position vector of candidate regions, and \(t_{i}\)* is the ground truth for the position vector of the candidate region. The cross entropy loss function \(L_{cls}\) is adopted to represent the classification loss of candidate regions. \(L_{det}\) is smooth L1 loss, which is counted for bounding box regression. The balance factors \(\lambda_{1}\) and \(\lambda_{2}\) are set as 1 and 10 respectively. In order to normalize each loss, \(N_{cls}\) is the number of candidate regions to be classified, and \(N_{det}\) equals the number of positive samples selected from anchors. The regression task of RPN is to regress four offset values, \(t=(t_{x},t_{y},t_{w},t_{h})\), from an anchor to the corresponding ground truth. We adopt the parameterization for \(t\) given in Reference [16], where \(t\) is defined as  
\[