# Domain-Invariant Few-Shot Contrastive Learning for Hyperspectral Image Classification
## 3 Proposed Method
### Overall Architecture  
Figure 1 shows the model structure of the proposed method. During the training phase, a patch dataset is pre-constructed by extracting n \(\times\) n patches around the center of each pixel from given images. Subsequently, under an episode-based training framework, this dataset is used to generate multiple tasks, each comprising a support set and a query set, through the sampling of patches. Specifically, in each iteration, \(C\) classes from both the source and target domains are randomly selected to form an episode, then \(K\) labeled samples per class are sampled to create the support set \(S=(x_{i},y_{i})^{C\times K}i=1\), where \(x_{i}\) represents the sample of the i-th support set, and \(y_{i}\) represents the corresponding label, defining a C-way K-shot task for FSL. The remaining samples of the same C classes, \(N\) unlabeled samples per class, are used to form the query set \(Q=(x_{j},y_{j})^{C\times T}j=1\), where \((x_{j},y_{j})\) represents the sample and its label from the query set. It is important to emphasize that in an episode, data from two domains are input into the model for training simultaneously. Unlike the DCFSL [14] method, where training alternates between the source and target domains, our method processes data from two domains in each episode and back-propagates the losses simultaneously. This training strategy aims to directly reinforce the learning of cross-domain features with each training step, thereby improving the model's generalization ability to the target domain (referred to as simultaneous training in the following text). In the described approach, the datasets for the source and target domains are represented as \(D_{S}\) and \(D_{T}\), respectively. Samples from the support and query sets in both the source and target domains are first input into a mapping layer, aiming to equalize the spectral dimensions of the source and target domains, and then fed into the MSAAN to extract embedded features. Regarding a query sample \(x_{j}\) within the query set \(Q_{s}\), the class distribution is  
\[P(y_{j}=k\mid x_{j}\in Q_{s})=\frac{\exp(-d(f_{\phi}(x_{j}),c_{k}))}{\sum_{k=1} ^{C}\exp(-d(f_{\phi}(x_{j}),c_{k}))}. \tag{1}\]  
In this context, \(d(.)\) denotes the Euclidean distance function, \(c_{k}\) corresponds to the embedded feature of the \(k\)-th class within the support set, and \(C\) indicates the total number of classes present in each episode. The mapping layer and MSAAN are denoted by \(f_{\phi}\) with parameters \(\phi\). After obtaining the embedded features, three types of losses are calculated:  
\[L=L_{con}+L_{fsl}+L_{d} \tag{2}\]  
In each episode, the few-shot learning loss is calculated as \[L_{fsl}=L_{fsl}^{t}+L_{fsl}^{s} \tag{3}\]  
where \(L_{fsl}^{t}\) is the few-shot learning loss calculated from the target domain data, \(L_{fsl}^{s}\) is calculated the same as \(L_{fsl}^{t}\), and the specific formula is  
\[L_{fsl}^{t}=E_{S_{t},Q_{t}}\left[-\sum_{(x,y)\in Q_{t}}\log p_{\phi}(y=k|x)\right] \tag{4}\]  
During the testing phase, the target mapping layer is first used to reduce the spectral dimensions. Subsequently, the trained network extracts embedded features. Ultimately, a k-Nearest Neighbor (NN) classifier [6] is used to categorize the unlabeled samples. All labeled samples from the target domain are used to fit the NN classifier.