MAETask#
- class torchgeo.trainers.MAETask(model='vit_base_patch32_224', weights=None, in_channels=3, transform=None, decoder_dim=512, lr=0.00015, decoder_num_heads=8, decoder_depth=1, weight_decay=0.05, mask_ratio=0.75, size=224, norm_pix_loss=True, warmup_epochs=40)[source]#
Bases:
BaseTaskMAE: Masked Autoencoder for self-supervised learning.
Reference implementations:
If you use this code for your research, please cite the original paper:
- ignore = ('transform', 'weights')#
Parameters to ignore when saving hyperparameters.
- __init__(model='vit_base_patch32_224', weights=None, in_channels=3, transform=None, decoder_dim=512, lr=0.00015, decoder_num_heads=8, decoder_depth=1, weight_decay=0.05, mask_ratio=0.75, size=224, norm_pix_loss=True, warmup_epochs=40)[source]#
Initialize the MAE task.
- Parameters:
model (str) – The ViT architecture to use for the encoder. Must be compatible with timm’s create_model function.
weights (WeightsEnum | str | bool | None) – Pretrained weights to initialize the encoder with. Can be a timm WeightsEnum or a string identifier for a timm weight, True to use default pretrained weights, or None for random initialization.
in_channels (int) – Number of input channels in the images. Must match the in_chans argument of the ViT model.
transform (Module | None) – Optional transform to apply to the input images. If None, a default MAE augmentation will be used.
decoder_dim (int) – The embedding dimension of the MAE decoder. Typically 512 is a good choice for ViT-Base encoders.
lr (float) – Should typically be set to 1.5e-4 * batch_size / 256.
decoder_num_heads (int) – Number of attention heads in the MAE decoder.
decoder_depth (int) – Number of layers in the MAE decoder. Typically 1-4 layers is sufficient for good performance.
weight_decay (float) – Weight decay for the AdamW optimizer.
mask_ratio (float) – The ratio of tokens to mask during training. Typically 0.75 is a good choice.
size (int) – The input image size (height and width) after augmentation. Must match the input size expected by the ViT model.
norm_pix_loss (bool) – If True, normalize each target patch to zero mean and unit variance before computing MSE. Recommended by the original MAE paper.
warmup_epochs (int) – Number of linear warmup epochs before cosine annealing.
- configure_optimizers()[source]#
Initialize the optimizer and learning rate scheduler.
- Returns:
Optimizer and learning rate scheduler.
- Return type:
Optimizer | Sequence[Optimizer] | tuple[Sequence[Optimizer], Sequence[LRScheduler | ReduceLROnPlateau | LRSchedulerConfig]] | OptimizerConfig | OptimizerLRSchedulerConfig | Sequence[OptimizerConfig] | Sequence[OptimizerLRSchedulerConfig] | None
- forward(images, idx_keep, idx_mask)[source]#
Forward pass through MAE encoder and decoder.
- Parameters:
- Returns:
- The predicted pixel values for the masked tokens, with shape (B, N_mask,
patch_size*patch_size*in_channels).
- Return type: