class_docstr: Multiple Instance Learning (MIL) model, with a backbone classification model.
  Currently, it only works for 2D images, a typical use case is for classification of the
  digital pathology whole slide images. The expected shape of input data is `[B, N, C, H, W]`,
  where `B` is the batch_size of PyTorch Dataloader and `N` is the number of instances
  extracted from every original image in the batch. A tutorial example is available at:
  https://github.com/Project-MONAI/tutorials/tree/master/pathology/multiple_instance_learning.
  
  Args:
      num_classes: number of output classes.
      mil_mode: MIL algorithm, available values (Defaults to ``"att"``):
  
          - ``"mean"`` - average features from all instances, equivalent to pure CNN (non MIL).
          - ``"max"`` - retain only the instance with the max probability for loss calculation.
          - ``"att"`` - attention based MIL https://arxiv.org/abs/1802.04712.
          - ``"att_trans"`` - transformer MIL https://arxiv.org/abs/2111.01556.
          - ``"att_trans_pyramid"`` - transformer pyramid MIL https://arxiv.org/abs/2111.01556.
  
      pretrained: init backbone with pretrained weights, defaults to ``True``.
      backbone: Backbone classifier CNN (either ``None``, a ``nn.Module`` that returns features,
          or a string name of a torchvision model).
          Defaults to ``None``, in which case ResNet50 is used.
      backbone_num_features: Number of output features of the backbone CNN
          Defaults to ``None`` (necessary only when using a custom backbone)
      trans_blocks: number of the blocks in `TransformEncoder` layer.
      trans_dropout: dropout rate in `TransformEncoder` layer.
code: def calc_head(self, x: torch.Tensor) -> torch.Tensor:
      sh = x.shape
  
      if self.mil_mode == "mean":
          x = self.myfc(x)
          x = torch.mean(x, dim=1)
  
      elif self.mil_mode == "max":
          x = self.myfc(x)
          x, _ = torch.max(x, dim=1)
  
      elif self.mil_mode == "att":
          a = self.attention(x)
          a = torch.softmax(a, dim=1)
          x = torch.sum(x * a, dim=1)
  
          x = self.myfc(x)
  
      elif (
          self.mil_mode == "att_trans" and self.transformer is not None
      ):
          x = x.permute(1, 0, 2)
          x = self.transformer(x)
          x = x.permute(1, 0, 2)
  
          a = self.attention(x)
          a = torch.softmax(a, dim=1)
          x = torch.sum(x * a, dim=1)
  
          x = self.myfc(x)
  
      elif (
          self.mil_mode == "att_trans_pyramid"
          and self.transformer is not None
      ):
          l1 = (
              torch.mean(self.extra_outputs["layer1"], dim=(2, 3))
              .reshape(sh[0], sh[1], -1)
              .permute(1, 0, 2)
          )
          l2 = (
              torch.mean(self.extra_outputs["layer2"], dim=(2, 3))
              .reshape(sh[0], sh[1], -1)
              .permute(1, 0, 2)
          )
          l3 = (
              torch.mean(self.extra_outputs["layer3"], dim=(2, 3))
              .reshape(sh[0], sh[1], -1)
              .permute(1, 0, 2)
          )
          l4 = (
              torch.mean(self.extra_outputs["layer4"], dim=(2, 3))
              .reshape(sh[0], sh[1], -1)
              .permute(1, 0, 2)
          )
  
          transformer_list = cast(nn.ModuleList, self.transformer)
  
          x = transformer_list[0](l1)
          x = transformer_list[1](torch.cat((x, l2), dim=2))
          x = transformer_list[2](torch.cat((x, l3), dim=2))
          x = transformer_list[3](torch.cat((x, l4), dim=2))
  
          x = x.permute(1, 0, 2)
  
          a = self.attention(x)
          a = torch.softmax(a, dim=1)
          x = torch.sum(x * a, dim=1)
  
          x = self.myfc(x)
  
      else:
          raise ValueError("Wrong model mode" + str(self.mil_mode))
  
      return x
  
file_path: MONAI/monai/networks/nets/milmodel.py
imports: from __future__ import annotations
  from typing import cast
  import torch
  import torch.nn
  from monai.utils.module import optional_import
label_desc: Use image node_id 2 for calling the MILModel obj's underlying member method code with example usage: obj.calc_head(x) and returns: x