# Person Re-Identification across Data Distributions Based on General Purpose DNN Object Detector
## 4 Proposed Method
### Second Training Phase
#### 4.4.2 Triplet Loss  
Triplet loss is a loss function that is often used in machine learning methods [44; 45]. This function provides the idea of similarity, respectively, dissimilarity of images from a database. If the number of classes is considered to be known before training in the classification methods, using triplet loss is no longer necessary. Thus, the training phase does not have to be resumed every time that a new class is added. In this way, the re-identification is seen as a problem of similarity, not of recognition. The main difference is that, for each input image, two more images are needed, thus forming a triplet. The three images from the triplet represent:  
* **Anchor image. ** This is the image from the batch, which will pass through the network. * **Positive image. ** It is an image similar to the anchor image (practically, from the same class). * **Negative image. ** This must be dissimilar to the anchor image, belonging to another class. Mathematically, triplet loss is computed while using Equation (20), being practically an Euclidean distance [44]. \[L(A,P,N)=[D(A,P)-D(A,N)+\alpha]_{+} \tag{20}\]  
\[[D(A,P)-D(A,N)+\alpha]_{+}=max(0,D(A,P)-D(A,N)+\alpha), \tag{21}\]  
where \(D(x,y)\) represents a distance between two learned feature vectors, \(x\) and \(y\). As distance, one of the distances presented in Table 3 can be used. The second parameter used in triplet loss (i.e., the margin \(\alpha\)) represents the distance between two clusters. The purpose of this loss function is to ensure that for each anchor \(A\), its features representation is closer to the positive image's representation than that of the negative image, by at least \(\alpha\). \begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Type of Layer** & **Input Dimensions** & **Number of Filters** & **Size** & **Stride** & **Output Dimensions** \\ \hline Convolutional & \(52\times 52\times 128\) & \(128\) & \(3\) & \(2\) & \(26\times 26\times 128\) \\ Convolutional & \(26\times 26\times 128\) & \(64\) & \(1\) & \(1\) & \(26\times 26\times 64\) \\ Fully Connected & \(26\times 26\times 64\) & - & - & - & \(1024\) \\ Dropout & \(1024\) & - & - & - & \(1024\) \\ Fully Connected & \(1024\) & - & - & - & \(128\) \\ Dropout & \(128\) & - & - & - & \(128\) \\ Re-Identification & \(128\) & - & - & - & - \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Proposed Network Architecture—layers added in the second phase of training. \begin{table}
\begin{tabular}{c c} \hline \hline
**Distance Metric** & **Equation** \\ \hline Euclidean Distance & \(D(x,y)=\sqrt{(x_{1}-y_{1})^{2}+(x_{2}-y_{2})^{2}+\cdots+(x_{i}-y_{i})^{2}+ \cdots+(x_{N}-y_{N})^{2}}\) \\
1—cosine similarity & \(D(x,y)=1-\frac{\sum_{i=1}^{N}x_{i}y_{i}}{\sqrt{\sum_{i=1}^{N}x_{i}^{2}}\sqrt{ \sum_{i=1}^{N}y_{i}^{2}}}\) \\ \hline \hline \end{tabular}
\end{table} 
Table 3: Distance metrics that are used in triplet loss. Those triplets can be chosen in many different ways, when considering the extent to which the anchor and the positive image are similar, or even to which the negative image differs from the anchor. One of the most common ways to choose triplets is the so-called _hard negative triplets_. In this case, we choose, for every anchor, the most similar negative image, according to the condition presented in Equation (22). In the worst-case scenario, the similarity between the anchor and positive image will be smaller than the similarity between the anchor and negative image. Another type of triplets is the _semihard negatives triplets_. In this case, according to Equation (24), we have to choose for every anchor, the negatives sample that is the most similar with the anchor, but also less similar than the corresponding positive image. Regarding the third type of triplets (i.e., the moderate positives) they are chosen based on both the conditions from Equations (22) and (23).