# PMG--Pyramidal Multi-Granular Matching for Text-Based Person Re-Identification
## 3 Proposed Method
### Pyramidal Multi-Granular Feature Extraction
#### 3.1.2 Textual Feature Extraction  
To extract global and local textual representations, we begin by utilizing a pretrained BERT language model to embed each word within the textual description. Specifically, given a description of length \(n\), we build an embedding matrix \(W_{e}\in\mathbb{R}^{m\times n}\) and then obtain the word embedding for each word by \(x_{i}=W_{e}\times w_{i}\), where \(w_{i}\in\mathbb{R}^{n}\) is the \(i\)-th word in the description, and \(x_{i}\in\mathbb{R}^{m}\) denotes \(w_{i}\) is embedded into a vector with dimensionality of \(m\). After that, we feed the embedded words through a bi-directional gated recurrent unit (Bi-GRU) to capture the relationship among them, which follows  
\[\overrightarrow{h_{i}}=\overrightarrow{GRU}(x_{i},\overrightarrow{h_{i-1}} ),1\leq i\leq n \tag{5}\]  
\[\overleftarrow{h_{i}}=\overleftarrow{GRU}(x_{i},\overleftarrow{h_{i-1}}),1 \leq i\leq n \tag{6}\]  
where \(\overrightarrow{h_{i}},\overleftarrow{h_{i}}\in\mathbb{R}^{c}\) and represent the forward and backward hidden states of the \(i\)-th word, respectively.  
Then, we define the representation of the \(i\)-th word as the average of the \(i\)-th last hidden states from both forward and backward GRUs, namely \(e_{i}=(\overrightarrow{h_{i}}+\overleftarrow{h_{i}})/2\), where \(e_{i}\in\mathbb{R}^{c}(1\leq i\leq n)\). To generate the representation \(E\) for the entire textual description, we concatenate all \(n\) word representations together as \(E=\{e_{1},e_{2},...,e_{n}\}\).  
After obtaining the textual feature map \(E\), we carry out a row-wise max pooling (RMP) operation on \(E\) and then obtain the global textual representation \(T_{g}\) through a \(1\times 1\) convolutional layer as follows  
\[T_{g}=W_{T_{g}}\times softmax(\max_{c}E), \tag{7}\]  
where \(W_{T_{g}}\in\mathbb{R}^{P\times n}\) and \(T_{g}\in\mathbb{R}^{P}\). Next, we utilize a textual pyramidal module to obtain local textual representations according to the word-part correspondences, which expends the part-level textual feature extraction method in SSAN [13] to various pyramidal scales corresponding to the \(K\)-partitioned feature map \(\psi_{K}(I)(K=2,4,6)\). We first predict the probability that the \(i\)-th word belongs to the \(k\)-th strip by \(s_{K}^{i,k}=\sigma(\omega_{K}^{k}e_{i})\), where \(\sigma\) denotes the Sigmoid function and \(\omega_{K}^{k}\in\mathbb{R}^{1\times c}\) stands for a linear transformation operation. Then, we modify the textual feature map \(E=\{e_{i}\}_{i=1}^{n}\) as \(E_{K}^{k}=\{s_{K}^{i,k}e_{i}\}_{i=1}^{n}\) to represent the textual description for the \(k\)-th local strip. Similar to the process of obtaining \(T_{g}\), each modifiedtextual description \(E_{K}^{k}\) is passed through an RMP layer followed by a \(1\times 1\) convolutional layer to obtain \(t_{IK}^{k}\) as  
\[t_{IK}^{k}=W_{T_{K}}^{k}\times softmax(\max_{c}E_{K}^{k}), \tag{8}\]  
where \(W_{T_{K}}^{k}\in\mathbb{R}^{P\times n}\), and \(t_{IK}^{k}\in\mathbb{R}^{P}\) represents the textual features for the \(k\)-th local strip. Finally, we stack \(t_{IK}^{k}(1\leq k\leq K)\) as \(T_{IK}=\{t_{IK}^{1},t_{IK}^{2},...,t_{IK}^{K}\}\) to obtain the final local textual representation with a given pyramidal scale \(K\).