# Transmission Tower Re-Identification Algorithm Based on Machine Vision
## 2 The Proposed Method
### Transmission Tower matching Network
#### 2.3.3 Triplets Generation Strategy Based on Dynamic Negative Allocation  
Triplet loss [35] is a loss function utilized for similarity metric learning. It aims to map images of the same identity in Euclidean space to proximal positions. Simultaneously, it maps images of the different identity to more distant positions in the space. When the network embeds image \(x\) into a d-dimensional Euclidean space, the embedding is represented as \(f(x)\in R^{d}\). The objective of the triplet loss function is to reduce the distance between images of the same identity (anchor \(x_{i}^{d}\) and positive \(x_{i}^{p}\)), while enlarging the distance from images of a different identity (negative \(x_{i}^{n}\)). Consequently, the model can adeptly cluster features of transmission towers with identical identities. The formula for calculating the triplet loss is presented as Equation (9):  
\[L_{\text{triplet}} =\sum_{i=1}^{N}\left[\parallel f(x_{i}^{a})-f\Big{(}x_{i}^{p} \Big{)}\parallel_{2}^{2}-\parallel f(x_{i}^{a})-f(x_{i}^{n})\parallel_{2}^{2} +a\right]_{+} \tag{9}\]  
where \(L_{\text{triplet}}\) represents triplet loss. N represents the number of triplets. \(x_{i}^{a}\), \(x_{i}^{p}\), \(x_{i}^{n}\) respectively represent the anchor, positive, and negative, in the _ith_ triplet. \(f\big{(}x_{i}^{a}\big{)}\), \(f\Big{(}x_{i}^{p}\Big{)}\) and \(f\big{(}x_{i}^{n}\big{)}\), respectively, represent the feature vectors after \(x_{i}^{a}\), \(x_{i}^{p}\) and \(x_{i}^{n}\) have been processed through the network. \(\alpha\) represents the enforced margin between positive and negative sample pairs. \(\left[\ \right]_{+}\) indicates that when the value \(\left[\ \right]\) is greater than 0, it is taken as the loss; when it is less than or equal to 0, the loss is considered as 0. However, simple triplets can lead to a triplet loss of 0, which is not conducive for model training [44]. For transmission tower matching, we think that examples of the same type of transmission tower are more challenging to distinguish than those of different types. Therefore, we believe it is necessary to dynamically adjust triplet samples. Based on the literature [45], this paper introduces an online triplet generation method that fixes the anchor and positive within a batch, optimizing the maximum triplet loss, and employs the Hungarian algorithm to allocate the negatives in the batch. The generation process of  
Figure 6: Results after applying the image augmentation. triplets is shown in Figure 7. During the training process, the batchsize is set as a multiple of 3. The batchsize is divided into multiple items (s_1, s_2,..., s_m), with the total number of items being one-third of the batchsize. Each item (s_i) consists of an anchor (\(x_{i}^{n}\)), a positive (\(x_{i}^{p}\)), and a negative (\(x_{i}^{n}\)). Following the application of the Hungarian algorithm, the positions of the negative samples in each batch were dynamically optimized, leading to the creation of new items (i_1, i_2,..., i_m). During the training process, the Hungarian algorithm is employed to allocate the negative samples from all items in the batch. To facilitate optimization, we ensured that the identities of the transmission tower in different items were distinct. Therefore, the maximum batch size can be set to half the total number of transmission tower instances. This strategy seeks to minimize the scoring metric \(S\) between the anchor and its corresponding positive relative to the unassigned negative for each batch. The optimization target \(S\) is computed as Equation (14). Utilizing the Hungarian algorithm minimizes the value of \(S\), yielding the current allocation matrix \(x_{i,j}\) and completing the distribution of negative samples among the items. Each row and column of \(x_{i,j}\) contains only one '1', ensuring a unique matching scheme. \[value_{i}^{j}=\left\|f(x_{i}^{n})-f(x_{i}^{p})\right\|_{2}^{2}-\left\|f(x_{i}^{ n})-f\Big{(}x_{j}^{n}\Big{)}\right\|_{2}^{2}+a \tag{10}\]  
\[score_{i}^{j}= \begin{cases}0&value_{i}^{j}\leq 0\\ value_{i}^{j}&0<value_{i}^{j}\end{cases}