Shortcuts

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.simsiam

# Copyright (c) OpenMMLab. All rights reserved.
import torch.nn as nn

from ..builder import ALGORITHMS, build_backbone, build_head, build_neck
from .base import BaseModel


[docs]@ALGORITHMS.register_module() class SimSiam(BaseModel): """SimSiam. Implementation of `Exploring Simple Siamese Representation Learning <https://arxiv.org/abs/2011.10566>`_. The operation of fixing learning rate of predictor is in `core/hooks/simsiam_hook.py`. Args: backbone (dict): Config dict for module of backbone. neck (dict): Config dict for module of deep features to compact feature vectors. Defaults to None. head (dict): Config dict for module of loss functions. Defaults to None. """ def __init__(self, backbone, neck=None, head=None, init_cfg=None, **kwargs): super(SimSiam, self).__init__(init_cfg) assert neck is not None self.encoder = nn.Sequential( build_backbone(backbone), build_neck(neck)) self.backbone = self.encoder[0] self.neck = self.encoder[1] assert head is not None self.head = build_head(head)
[docs] def extract_feat(self, img): """Function to extract features from backbone. Args: img (Tensor): Input images of shape (N, C, H, W). Typically these should be mean centered and std scaled. Returns: tuple[Tensor]: backbone outputs. """ x = self.backbone(img) return x
[docs] def forward_train(self, img): """Forward computation during training. Args: img (list[Tensor]): A list of input images with shape (N, C, H, W). Typically these should be mean centered and std scaled. Returns: loss[str, Tensor]: A dictionary of loss components """ assert isinstance(img, list) img_v1 = img[0] img_v2 = img[1] z1 = self.encoder(img_v1)[0] # NxC z2 = self.encoder(img_v2)[0] # NxC losses = 0.5 * (self.head(z1, z2)['loss'] + self.head(z2, z1)['loss']) return dict(loss=losses)
Read the Docs v: 0.x
Versions
latest
stable
1.x
dev-1.x
0.x
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.