# A Novel Geo-Localization Method for UAV and Satellite Images Using Cross-View Consistent Attention
## 3 Proposed Method
### Piecewise Soft-Margin Triplet Loss Function  
Soft-margin triplet loss [26], a loss function to maximize the similarity between positive and negative samples, has been used as the most common loss function in cross-view image retrieval for geo-localization. Compared with the triplet loss function [15; 50], the soft-margin triplet loss function is proved to be smoother and can accelerate the convergence of the algorithm. Some weighted soft-margin triplet loss functions are proposed [51; 52] for a one-to-one correspondence dataset CVUSA, which simultaneously restricts the ground view retrieval and the synthesized aerial view retrieval. However, the weighted soft-margin triplet loss is unsuitable for the UAV dataset due to the imbalanced amount of UAV and satellite images. Let us suppose the Euclidean distance of the embedded feature vector space is adopted to measure the similarity between samples. The distance between the queried embedded feature vector and the positive reference feature vector is defined as \(d_{pos}\), and the distance between the queried embedded feature vector and the negative reference feature vector is defined as \(d_{neg}\), where the distance calculation formula is presented in Equation (4). The formula of the soft-margin triplet loss function is as follows:  
\[L=\log(1+e^{\alpha(d_{pos}-d_{neg})}) \tag{5}\]  
where \(\alpha\) is a hyper-parameter. Adopting soft-margin triplet loss can cause many problems in UAV and satellite geo-localization. The model will converge to a local optimum where the distance between positive and negative samples \(d_{pos}-d_{neg}\) is large enough. However, the distance to the feature vector of the positive reference images \(d_{pos}\) can not be guaranteed. The reason is that only the relative gap between \(d_{pos}\) and \(d_{neg}\) is optimized in Equation (5), which ignores the absolute value of \(d_{pos}\). The unconstrained distance values cause the failure to converge to the optimal solution. To overcome the shortage of original soft-margin triplet loss function, an improvement to the original loss function is proposed and then a soft constraint for the positive distance is designed. The advanced triplet loss function is defined as follows:  
\[L=\log(1+e^{\alpha(d_{pos}-d_{neg})}+e^{\beta d_{pos}}) \tag{6}\]  
where \(\alpha\) and \(\beta\) are hyper-parameters. As a result, the convergence direction of the model not only ensures the distance between the positive and the negative samples, but also decreases the distance to positive samples. Compared with the original triplet loss function, the effectiveness of the proposed loss function is apparent and the comparison is presented in the experiment section. However, the advanced triplet loss function, defined as Equation (6), cannot efficiently find the initial convergence direction. To better converge the model and avoid local optimum, a piecewise triplet loss function is further designed. In the first step, the original triplet loss function is used to distinguish the positive samples and negative samples. While the distance between the positive and negative sample reaches a threshold M, the advanced triplet loss function is adopted. The formula of the piecewise triplet loss function is as follows:  
\[L=\left\{ \begin{array}{ll}\log(1+e^{\alpha(d_{pos}-d_{neg})})&(d_{pos}-d_{ neg})<M\\ \log(1+e^{\alpha(d_{pos}-d_{neg})}+e^{\beta d_{pos}})&\text{otherwise}\end{array} \right. \tag{7}\]  
where \(\alpha\), \(\beta\) and threshold \(M\) are hyper-parameters. The novel loss function proposed in this paper ensures that the model has enough distinction between positive and negative samples and can jump out of the local optimum by reducing the distance to the positive samples. The piecewise loss function can better converge while ensuring the accuracy of the model, which is an important factor in building the one-stage Transformer architecture.