Note
You are reading the documentation for MMSelfSup 0.x, which will soon be deprecated by the end of 2022. We recommend you upgrade to MMSelfSup 1.0.0rc versions to enjoy fruitful new features and better performance brought by OpenMMLab 2.0. Check out the changelog, code and documentation of MMSelfSup 1.0.0rc for more details.
Source code for mmselfsup.models.algorithms.simmim
# Copyright (c) OpenMMLab. All rights reserved.
from typing import List, Optional
import torch
from ..builder import ALGORITHMS, build_backbone, build_head, build_neck
from .base import BaseModel
[docs]@ALGORITHMS.register_module()
class SimMIM(BaseModel):
"""SimMIM.
Implementation of `SimMIM: A Simple Framework for Masked Image Modeling
<https://arxiv.org/abs/2111.09886>`_.
Args:
backbone (dict): Config dict for encoder. Defaults to None.
neck (dict): Config dict for encoder. Defaults to None.
head (dict): Config dict for loss functions. Defaults to None.
init_cfg (dict, optional): Config dict for weight initialization.
Defaults to None.
"""
def __init__(self,
backbone: dict,
neck: dict,
head: dict,
init_cfg: Optional[dict] = None) -> None:
super(SimMIM, self).__init__(init_cfg)
assert backbone is not None
self.backbone = build_backbone(backbone)
assert neck is not None
self.neck = build_neck(neck)
assert head is not None
self.head = build_head(head)
[docs] def extract_feat(self, img: torch.Tensor) -> tuple:
"""Function to extract features from backbone.
Args:
img (torch.Tensor): Input images of shape (N, C, H, W).
Returns:
tuple[Tensor]: Latent representations of images.
"""
return self.backbone(img)
[docs] def forward_train(self, x: List[torch.Tensor], **kwargs) -> dict:
"""Forward the masked image and get the reconstruction loss.
Args:
x (List[torch.Tensor, torch.Tensor]): Images and masks.
Returns:
dict: Reconstructed loss.
"""
img, mask = x
img_latent = self.backbone(img, mask)
img_rec = self.neck(img_latent[0])
losses = self.head(img, img_rec, mask)
return losses