# RANDnet: Vehicle Re-Identification with Relation Attention and Nuance-Disparity Masks
## 3 Vehicle Re-Identification With RANDnet
### Relation Attention Module  
Introducing the D&N masks, we adeptly extracted feature representations from both the most and sub-discriminative regions of vehicles, thereby amalgamating these elements to forge a holistic representation of the vehicle's entirety. However, an intrinsic challenge arises due to the immutable rigid geometric structure of vehicles, which manifests as pronounced disparities in images of identical vehicle IDs when viewed from divergent perspectives. This phenomenon necessitates the identification of consistent elements that persist across varied photographic angles, focusing particularly on the inter-relationships among the vehicle's components. Consequently, our goal is to unearth invariant features across different photographic angles of the same vehicle, specifically focusing on the inter-relations among vehicle components. To address this, we introduce the ra-module, a framework devised to capture these consistent relationships.  
The transformer architecture, renowned for its exceptional capability in extracting relational information within sequences, has found extensive applications in the field of Computer Vision. Classification networks such as [42] incorporate a class token to serve as the feature vector input for classifiers. Detection and segmentation networks utilize the decoder module to restore the resolution of feature maps altered by the encoder, facilitating detailed pixel-level outcomes. Diverging from these approaches, the re-identification task necessitates the derivation of a feature vector that is not only succinct but also exhibits heightened generalizability to unseen classes.  
First of all, an LA layer is applied to the feature map of the network's final layer to extract more condensed regional information. Contrary to the traditional transformer approach, which partitions an image into fixed-size patches, our method divides the feature map into \(1\times 1\times c\) units. Subsequently, an LA composed of parameter-sharing \(1\times 1\) convolution and batch normalization (BN) is employed to extract information from these segments:  
\[\begin{split} Units=\{F_{ij}\mid F_{ij}\in\mathbb{R}^{1\times 1 \times c},\\ \forall i\in\{1,\dots,h\},\ \forall j\in\{1,\dots,w\}\}\end{split} \tag{4}\]  
where \(F_{ij}\) represents the unit at position \((i,j)\) in the original feature map \(F\). Each \(F_{ij}\) is a vector of length \(c\), encapsulating the channel values at that specific spatial location within \(F\).  
Then, the \(Q\), \(K\), and \(V\) vectors are generated by  
\[Q=Proj(Concat^{0}(BN(conv(Unit))))W^{Q} \tag{5}\]  
\[K=Proj(Concat^{0}(BN(conv(Unit))))W^{K} \tag{6}\]  
\[V=Proj(Concat^{0}(BN(conv(Unit))))W^{V} \tag{7}\]  
where \(W^{Q}\in R^{d_{RA}\times d_{k}}\), \(W^{K}\in R^{d_{RA}\times d_{k}}\), and \(W^{V}\in R^{d_{RA}\times d_{V}}\). The notation \(Concat^{0}\) denotes the concatenation of matrices along the \(0 th\) dimension. Then, self-attention and multi-head attention are calculated as  
\[Attention(Q,K,V)=softmax(\frac{QK^{T}}{\sqrt{d_{k}}})V \tag{8}\]  
\[MultiHead(Q,K,V)=Concat(head_{1},\dots,head_{h})W^{O} \tag{9}\]  
where \(W^{O}\in R^{hd_{v}\times d_{LA}}\). The length of the spliced vector is \(hd_{v}\), and the length of each vector in MultiHead(Q,K,V) is \(d_{LA}\). Therefore, the shape of features remains unchanged after the self-attention process. The construction details of RA module are shown in Figure 4. In the framework, we add the relation module after the backbone.  
As shown in Figure 5, the relation attention module consists of \(L\) self-attention blocks. Each block has multi-head and layer normalization operations. The calculation process can be represented by  
\[F_{0}=F \tag{10}\]  
\[F_{l}^{{}^{\prime}}=MultiHead(LayerNorm(F_{l-1}))+F_{l-1} \tag{11}\]  
\[F_{l}=MLP(LayerNorm(F_{l}^{{}^{\prime}}))+F_{l}^{{}^{\prime}} \tag{12}\]  
Figure 4: Feature shape transformation process in relation attention. The 3D feature map is shortened and stretched into a sequence form for self-attention calculation.