Source code for monai.networks.nets.vit

# Copyright 2020 - 2021 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import Tuple

import torch.nn as nn

from monai.networks.blocks.patchembedding import PatchEmbeddingBlock
from monai.networks.blocks.transformerblock import TransformerBlock


[docs]class ViT(nn.Module): """ Vision Transformer (ViT), based on: "Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale <https://arxiv.org/abs/2010.11929>" """ def __init__( self, in_channels: int, img_size: Tuple[int, int, int], patch_size: Tuple[int, int, int], hidden_size: int, mlp_dim: int, num_layers: int, num_heads: int, pos_embed: str, classification: bool, num_classes: int = 2, dropout_rate: float = 0.0, ) -> None: """ Args: in_channels: dimension of input channels. img_size: dimension of input image. patch_size: dimension of patch size. hidden_size: dimension of hidden layer. mlp_dim: dimension of feedforward layer. num_layers: number of transformer blocks. num_heads: number of attention heads. pos_embed: position embedding layer type. classification: bool argument to determine if classification is used. num_classes: number of classes if classification is used. dropout_rate: faction of the input units to drop. """ super().__init__() if not (0 <= dropout_rate <= 1): raise AssertionError("dropout_rate should be between 0 and 1.") if hidden_size % num_heads != 0: raise AssertionError("hidden size should be divisible by num_heads.") if pos_embed not in ["conv", "perceptron"]: raise KeyError(f"Position embedding layer of type {pos_embed} is not supported.") self.classification = classification self.patch_embedding = PatchEmbeddingBlock( in_channels, img_size, patch_size, hidden_size, num_heads, pos_embed, dropout_rate ) self.blocks = nn.ModuleList( [TransformerBlock(hidden_size, mlp_dim, num_heads, dropout_rate) for i in range(num_layers)] ) self.norm = nn.LayerNorm(hidden_size) if self.classification: self.classification_head = nn.Linear(hidden_size, num_classes)
[docs] def forward(self, x): x = self.patch_embedding(x) hidden_states_out = [] for blk in self.blocks: x = blk(x) hidden_states_out.append(x) x = self.norm(x) if self.classification: x = self.classification_head(x[:, 0]) return x, hidden_states_out