class_docstr: Support SAM for semantic segmentation.
  Refer to https://huggingface.co/docs/transformers/main/model_doc/sam
docstr: Load a pretrained Segment Anything Model (SAM).
  
  Parameters
  ----------
  prefix
      The prefix of the SAMForSemanticSegmentation model.
  checkpoint_name
      Name of the SAM checkpoint.
  num_classes
      The number of classes
  pretrained
      Whether using the pretrained SAM models. If pretrained=True, download the pretrained model.
  frozen_layers
      A list of substrings of frozen layers' names.
  num_mask_tokens
      The number of mask proposals.
docstr_tok: ["Load","a","pretrained","Segment","Anything","Model","(","SAM",")",".","Parameters","--","--","--","--","--","prefix","The","prefix","of","the","SAMForSemanticSegmentation","model",".","checkpoint_name","Name","of","the","SAM","checkpoint",".","num_classes","The","number","of","classes","pretrained","Whether","using","the","pretrained","SAM","models",".","If","pretrained=True",",","download","the","pretrained","model",".","frozen_layers","A","list","of","substrings","of","frozen","layers","'","names",".","num_mask_tokens","The","number","of","mask","proposals","."]
code: def __init__(
      self,
      prefix: str,
      checkpoint_name: str,
      num_classes: int = 1,
      pretrained: Optional[bool] = True,
      frozen_layers: Optional[list] = None,
      num_mask_tokens: int = 1,
  ):
      """
      Load a pretrained Segment Anything Model (SAM).
  
      Parameters
      ----------
      prefix
          The prefix of the SAMForSemanticSegmentation model.
      checkpoint_name
          Name of the SAM checkpoint.
      num_classes
          The number of classes
      pretrained
          Whether using the pretrained SAM models. If pretrained=True, download the pretrained model.
      frozen_layers
          A list of substrings of frozen layers' names.
      num_mask_tokens
          The number of mask proposals.
      """
  
      super().__init__()
      self.prefix = prefix
      self.pretrained = pretrained
      self.checkpoint_name = checkpoint_name
      self.num_classes = num_classes
      self.frozen_layers = frozen_layers
  
      self.device = None
      self.name_to_id = {}
  
      self._load_checkpoint(checkpoint_name)
  
      freeze_model_layers(self.model, self.frozen_layers)
  
      self.image_size = self.model.vision_encoder.image_size
      self.config = self.model.config
  
      self.model.mask_decoder.num_mask_tokens = num_mask_tokens
      mask_token_data = self.model.mask_decoder.mask_tokens.weight.data[
          0
      ]
      self.model.mask_decoder.mask_tokens = nn.Embedding(
          num_mask_tokens, self.model.mask_decoder.hidden_size
      )
      for i in range(num_mask_tokens):
          self.model.mask_decoder.mask_tokens.weight.data[
              i
          ] = mask_token_data
      hyper_mlps = self.model.mask_decoder.output_hypernetworks_mlps[0]
      self.model.mask_decoder.output_hypernetworks_mlps = nn.ModuleList(
          [hyper_mlps]
      )
      if num_classes > 1:
          self.model.mask_decoder.num_classes = num_classes
          self.model.mask_decoder.output_classifier_mlps = nn.Linear(
              self.model.mask_decoder.hidden_size, num_classes + 1
          )
  
          mask_decoder_forward = (
              multi_class_mask_decoder_forward.__get__(
                  self.model.mask_decoder,
                  self.model.mask_decoder.__class__,
              )
          )
          setattr(
              self.model.mask_decoder, "forward", mask_decoder_forward
          )
  
          sam_model_forward = multi_class_sam_model_forward.__get__(
              self.model, self.model.__class__
          )
          setattr(self.model, "forward", sam_model_forward)
  
imports: import logging
  import os
  import time
  import warnings
  from typing import Dict, List, Optional, Tuple
  import torch
  import torch.nn.functional
  from omegaconf import DictConfig
  from torch import nn
  from transformers import SamConfig, SamModel
  from transformers.models.sam.modeling_sam import SamImageSegmentationOutput
  from ..constants import CLASS_LABEL, CLASS_LOGITS, COLUMN, IMAGE, IMAGE_VALID_NUM, LABEL, LOGITS, MASK_LABEL
  from .utils import assign_layer_ids, freeze_model_layers
label_desc: Use image node_id 1 to create a new SAMForSemanticSegmentation object from inherited base classes: nn with example: obj = SAMForSemanticSegmentation(prefix, checkpoint_name, num_classes, pretrained, frozen_layers, num_mask_tokens)