contrastive-learning-encoder-construction
Summary
Build a shared-weight ResNet18 encoder module that processes pairs of augmented ion images to produce 512-dimensional representation vectors for contrastive learning in mass spectrometry imaging. This skill applies contrastive loss to maximize similarity between augmentations of the same image while avoiding representation collapse.
When to use
You have mass spectrometry imaging (MSI) data with ion images that need low-dimensional representation learning for downstream tasks like co-localized ion searching or isotope discovery. You want to learn invariant representations by contrasting two augmented views of the same image without supervised labels.
When NOT to use
- Your ion images are already embedded in a learned low-dimensional space and need only downstream classification or clustering
- You are working with supervised labels and can use a standard classification loss instead of contrastive learning
- Your input data lacks paired augmentations or you cannot generate meaningful augmentations for your ion imaging modality
Inputs
- Two augmented ion images (tensor pairs from Data Augmentation module)
- Ion image batch with shape [batch_size, channels, height, width]
Outputs
- Two 512-dimensional representation vectors per image pair (shape: batch_size × 512)
- Encoder module instance with learned or pretrained weights
How to apply
Define a ResNet18 architecture accepting ion image inputs and outputting 512-dimensional feature vectors. Instantiate a single ResNet18 encoder with shared parameters that will process both augmented ion images sequentially in the forward pass. Configure the encoder to freeze or selectively train convolutional and pooling layers according to your contrastive learning objective (typically freezing early layers for transfer learning). For each input pair of augmented ion images, propagate each through the shared encoder to produce two distinct 512-dimensional representation vectors. Apply projection and prediction modules after the encoder outputs to prevent representation collapse during optimization. Verify output tensor shapes match (batch_size × 512) and check numerical stability of the representation vectors before feeding them to the contrastive loss function.
Related tools
- ResNet18 (Backbone convolutional encoder architecture that accepts ion image inputs and outputs fixed 512-dimensional feature vectors through shared parameters across both augmented images)
- torchvision (Provides ResNet18 implementation and pretrained weights for transfer learning initialization)
- kornia (Provides augmentation operations (color jitter, filtering, Poisson noise, random missing value) applied before encoder input)
- PyTorch (Deep learning framework for defining the encoder module, managing shared parameters, and implementing the forward pass with contrastive loss)
Examples
python run.py --input_Matrix .../DATASET/Pos_brain_data_matrix.txt --input_PeakList .../DATASET/Pos_brain_data_peak.csv --input_shape 198 422 --mode COL --ion_mode positive --num 5 --output_file Pos_COL_result
Evaluation signals
- Output tensor shapes are exactly [batch_size, 512] for each of the two representation vectors from a single image pair
- Encoder parameters are truly shared: identical forward pass on identical input yields identical output vectors within numerical precision (< 1e-6 difference)
- Representation vectors have bounded norm and no NaN/Inf values after a full training epoch
- Contrastive loss decreases during training, indicating the encoder is learning to maximize similarity between augmentations of the same image
- Projection module prevents representation collapse by maintaining sufficient variance across the batch dimension (checked via singular value decomposition or rank)
Limitations
- ResNet18 is a relatively shallow architecture; deeper architectures may improve representation quality but increase computational cost for large MSI datasets
- The 512-dimensional bottleneck is fixed by design; changing output dimensionality requires retraining the entire encoder
- Encoder performance depends critically on data augmentation quality (T_COL or T_ISO); poor augmentations yield meaningless contrastive signals
- Shared-weight architecture assumes both augmented images contribute equally to the learned representation; mode-specific augmentation differences (COL vs. ISO) may bias learning toward dominant augmentation types
- No explicit guidance provided on whether to freeze early ResNet18 layers vs. train end-to-end; this choice affects convergence speed and final representation quality
Evidence
- [readme] Two augmented images are propagated through a pair of ResNet18-based encoders that shared parameters, then output two 512-dimensional representation vectors: "Two augmented images are propagated through a pair of ResNet18-based encoders that shared parameters, then output two 512-dimensional representation vectors"
- [readme] Projection module and Prediction module are used to avoid collapsing solutions during the optimization process of maximizing the similarity between two augmentations from a same image: "Projection module and Prediction module are used to avoid collapsing solutions during the optimization process of maximizing the similarity between two augmentations from a same image"
- [readme] A contrastive loss is employed to maximize similarity with a stop-gradient operation to prevent collapsing during training: "A contrastive loss is employed to maximize similarity with a stop-gradient operation to prevent collapsing during training"
- [readme] The Data Augmentation module generates two augmented images, where the T_COL including color jitter, filtering, Poisson noise, and random missing value is carried in COL mode, while T_ISO introduces an additional process of intensity-dependent missing value in ISO mode: "The original ion image is first imported into the data augmentation module "T" to generate two augmented images, where the T_COL including color jitter, filtering, Poisson noise, and random missing"
- [other] The encoder module accepts two augmented ion images and propagates them through a pair of ResNet18-based encoders with shared parameters to output two 512-dimensional representation vectors: "The Encoder module accepts two augmented ion images and propagates them through a pair of ResNet18-based encoders with shared parameters to output two 512-dimensional representation vectors"
1---2name: contrastive-learning-encoder-construction3description: Use when you have mass spectrometry imaging (MSI) data with ion images that need low-dimensional representation learning for downstream tasks like co-localized ion searching or isotope discovery.4license: CC-BY-4.05---67# contrastive-learning-encoder-construction89## Summary1011Build a shared-weight ResNet18 encoder module that processes pairs of augmented ion images to produce 512-dimensional representation vectors for contrastive learning in mass spectrometry imaging. This skill applies contrastive loss to maximize similarity between augmentations of the same image while avoiding representation collapse.1213## When to use1415You have mass spectrometry imaging (MSI) data with ion images that need low-dimensional representation learning for downstream tasks like co-localized ion searching or isotope discovery. You want to learn invariant representations by contrasting two augmented views of the same image without supervised labels.1617## When NOT to use1819- Your ion images are already embedded in a learned low-dimensional space and need only downstream classification or clustering20- You are working with supervised labels and can use a standard classification loss instead of contrastive learning21- Your input data lacks paired augmentations or you cannot generate meaningful augmentations for your ion imaging modality2223## Inputs2425- Two augmented ion images (tensor pairs from Data Augmentation module)26- Ion image batch with shape [batch_size, channels, height, width]2728## Outputs2930- Two 512-dimensional representation vectors per image pair (shape: batch_size × 512)31- Encoder module instance with learned or pretrained weights3233## How to apply3435Define a ResNet18 architecture accepting ion image inputs and outputting 512-dimensional feature vectors. Instantiate a single ResNet18 encoder with shared parameters that will process both augmented ion images sequentially in the forward pass. Configure the encoder to freeze or selectively train convolutional and pooling layers according to your contrastive learning objective (typically freezing early layers for transfer learning). For each input pair of augmented ion images, propagate each through the shared encoder to produce two distinct 512-dimensional representation vectors. Apply projection and prediction modules after the encoder outputs to prevent representation collapse during optimization. Verify output tensor shapes match (batch_size × 512) and check numerical stability of the representation vectors before feeding them to the contrastive loss function.3637## Related tools3839- **ResNet18** (Backbone convolutional encoder architecture that accepts ion image inputs and outputs fixed 512-dimensional feature vectors through shared parameters across both augmented images)40- **torchvision** (Provides ResNet18 implementation and pretrained weights for transfer learning initialization)41- **kornia** (Provides augmentation operations (color jitter, filtering, Poisson noise, random missing value) applied before encoder input)42- **PyTorch** (Deep learning framework for defining the encoder module, managing shared parameters, and implementing the forward pass with contrastive loss)4344## Examples4546```47python run.py --input_Matrix .../DATASET/Pos_brain_data_matrix.txt --input_PeakList .../DATASET/Pos_brain_data_peak.csv --input_shape 198 422 --mode COL --ion_mode positive --num 5 --output_file Pos_COL_result48```4950## Evaluation signals5152- Output tensor shapes are exactly [batch_size, 512] for each of the two representation vectors from a single image pair53- Encoder parameters are truly shared: identical forward pass on identical input yields identical output vectors within numerical precision (< 1e-6 difference)54- Representation vectors have bounded norm and no NaN/Inf values after a full training epoch55- Contrastive loss decreases during training, indicating the encoder is learning to maximize similarity between augmentations of the same image56- Projection module prevents representation collapse by maintaining sufficient variance across the batch dimension (checked via singular value decomposition or rank)5758## Limitations5960- ResNet18 is a relatively shallow architecture; deeper architectures may improve representation quality but increase computational cost for large MSI datasets61- The 512-dimensional bottleneck is fixed by design; changing output dimensionality requires retraining the entire encoder62- Encoder performance depends critically on data augmentation quality (T_COL or T_ISO); poor augmentations yield meaningless contrastive signals63- Shared-weight architecture assumes both augmented images contribute equally to the learned representation; mode-specific augmentation differences (COL vs. ISO) may bias learning toward dominant augmentation types64- No explicit guidance provided on whether to freeze early ResNet18 layers vs. train end-to-end; this choice affects convergence speed and final representation quality6566## Evidence6768- [readme] Two augmented images are propagated through a pair of ResNet18-based encoders that shared parameters, then output two 512-dimensional representation vectors: "Two augmented images are propagated through a pair of ResNet18-based encoders that shared parameters, then output two 512-dimensional representation vectors"69- [readme] Projection module and Prediction module are used to avoid collapsing solutions during the optimization process of maximizing the similarity between two augmentations from a same image: "Projection module and Prediction module are used to avoid collapsing solutions during the optimization process of maximizing the similarity between two augmentations from a same image"70- [readme] A contrastive loss is employed to maximize similarity with a stop-gradient operation to prevent collapsing during training: "A contrastive loss is employed to maximize similarity with a stop-gradient operation to prevent collapsing during training"71- [readme] The Data Augmentation module generates two augmented images, where the T_COL including color jitter, filtering, Poisson noise, and random missing value is carried in COL mode, while T_ISO introduces an additional process of intensity-dependent missing value in ISO mode: "The original ion image is first imported into the data augmentation module "T" to generate two augmented images, where the T_COL including color jitter, filtering, Poisson noise, and random missing"72- [other] The encoder module accepts two augmented ion images and propagates them through a pair of ResNet18-based encoders with shared parameters to output two 512-dimensional representation vectors: "The Encoder module accepts two augmented ion images and propagates them through a pair of ResNet18-based encoders with shared parameters to output two 512-dimensional representation vectors"