codingutf8 2023 the huggingface inc team 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 orglicenseslicense2 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 sam model configuration from configurationutils import pretrainedconfig from utils import logging logger logging getloggername sampretrainedconfigarchivemap facebooksamvithuge https huggingface cofacebooksamvithugeresolvemainconfig json facebooksamvitlarge https huggingface cofacebooksamvitlargeresolvemainconfig json facebooksamvitbase https huggingface cofacebooksamvitbaseresolvemainconfig json class sampromptencoderconfigpretrainedconfig r this is the configuration class to store the configuration of a sampromptencoder the sampromptencoder module is used to encode the input 2d points and bounding boxes instantiating a configuration defaults will yield a similar configuration to that of the samvith facebooksamvithugehttps huggingface cofacebooksamvithuge architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args hiddensize int optional defaults to 256 dimensionality of the hidden states imagesize int optional defaults to 1024 the expected output resolution of the image patchsize int optional defaults to 16 the size resolution of each patch maskinputchannels int optional defaults to 16 the number of channels to be fed to the maskdecoder module numpointembeddings int optional defaults to 4 the number of point embeddings to be used hiddenact str optional defaults to gelu the nonlinear activation function in the encoder and pooler def init self hiddensize256 hiddenactrelu mlpdim2048 numhiddenlayers2 numattentionheads8 attentiondownsamplerate2 nummultimaskoutputs3 iouheaddepth3 iouheadhiddendim256 layernormeps1e6 kwargs super initkwargs self hiddensize hiddensize self hiddenact hiddenact self mlpdim mlpdim self numhiddenlayers numhiddenlayers self numattentionheads numattentionheads self attentiondownsamplerate attentiondownsamplerate self nummultimaskoutputs nummultimaskoutputs self iouheaddepth iouheaddepth self iouheadhiddendim iouheadhiddendim self layernormeps layernormeps class samvisionconfigpretrainedconfig r this is the configuration class to store the configuration of a samvisionmodel it is used to instantiate a sam vision encoder according to the specified arguments defining the model architecture instantiating a configuration defaults will yield a similar configuration to that of the sam vith facebooksamvithugehttps huggingface cofacebooksamvithuge architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args hiddensize int optional defaults to 768 dimensionality of the encoder layers and the pooler layer outputchannels int optional defaults to 256 dimensionality of the output channels in the patch encoder numhiddenlayers int optional defaults to 12 number of hidden layers in the transformer encoder numattentionheads int optional defaults to 12 number of attention heads for each attention layer in the transformer encoder numchannels int optional defaults to 3 number of channels in the input image imagesize int optional defaults to 1024 expected resolution target size of the resized input image patchsize int optional defaults to 16 size of the patches to be extracted from the input image hiddenact str optional defaults to gelu the nonlinear activation function function or string layernormeps float optional defaults to 1e06 the epsilon used by the layer normalization layers attentiondropout float optional defaults to 0 0 the dropout ratio for the attention probabilities initializerrange float optional defaults to 1e10 the standard deviation of the truncatednormalinitializer for initializing all weight matrices qkvbias bool optional defaults to true whether to add a bias to query key value projections mlpratio float optional defaults to 4 0 ratio of mlp hidden dim to embedding dim useabspos bool optional defaults to true whether to use absolute position embedding userelpos bool optional defaults to true whether to use relative position embedding windowsize int optional defaults to 14 window size for relative position globalattnindexes listint optional defaults to 2 5 8 11 the indexes of the global attention layers numposfeats int optional defaults to 128 the dimensionality of the position embedding mlpdim int optional the dimensionality of the mlp layer in the transformer encoder if none defaults to mlpratio hiddensize coding utf 8 2023 the huggingface inc team 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 sam model configuration this is the configuration class to store the configuration of a sampromptencoder the sampromptencoder module is used to encode the input 2d points and bounding boxes instantiating a configuration defaults will yield a similar configuration to that of the sam vit h facebook sam vit huge https huggingface co facebook sam vit huge architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args hidden_size int optional defaults to 256 dimensionality of the hidden states image_size int optional defaults to 1024 the expected output resolution of the image patch_size int optional defaults to 16 the size resolution of each patch mask_input_channels int optional defaults to 16 the number of channels to be fed to the maskdecoder module num_point_embeddings int optional defaults to 4 the number of point embeddings to be used hidden_act str optional defaults to gelu the non linear activation function in the encoder and pooler this is the configuration class to store the configuration of a sammaskdecoder it is used to instantiate a sam mask decoder to the specified arguments defining the model architecture instantiating a configuration defaults will yield a similar configuration to that of the sam vit h facebook sam vit huge https huggingface co facebook sam vit huge architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args hidden_size int optional defaults to 256 dimensionality of the hidden states hidden_act str optional defaults to relu the non linear activation function used inside the sammaskdecoder module mlp_dim int optional defaults to 2048 dimensionality of the intermediate i e feed forward layer in the transformer encoder num_hidden_layers int optional defaults to 2 number of hidden layers in the transformer encoder num_attention_heads int optional defaults to 8 number of attention heads for each attention layer in the transformer encoder attention_downsample_rate int optional defaults to 2 the downsampling rate of the attention layer num_multimask_outputs int optional defaults to 3 the number of outputs from the sammaskdecoder module in the segment anything paper this is set to 3 iou_head_depth int optional defaults to 3 the number of layers in the iou head module iou_head_hidden_dim int optional defaults to 256 the dimensionality of the hidden states in the iou head module layer_norm_eps float optional defaults to 1e 06 the epsilon used by the layer normalization layers this is the configuration class to store the configuration of a samvisionmodel it is used to instantiate a sam vision encoder according to the specified arguments defining the model architecture instantiating a configuration defaults will yield a similar configuration to that of the sam vit h facebook sam vit huge https huggingface co facebook sam vit huge architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args hidden_size int optional defaults to 768 dimensionality of the encoder layers and the pooler layer output_channels int optional defaults to 256 dimensionality of the output channels in the patch encoder num_hidden_layers int optional defaults to 12 number of hidden layers in the transformer encoder num_attention_heads int optional defaults to 12 number of attention heads for each attention layer in the transformer encoder num_channels int optional defaults to 3 number of channels in the input image image_size int optional defaults to 1024 expected resolution target size of the resized input image patch_size int optional defaults to 16 size of the patches to be extracted from the input image hidden_act str optional defaults to gelu the non linear activation function function or string layer_norm_eps float optional defaults to 1e 06 the epsilon used by the layer normalization layers attention_dropout float optional defaults to 0 0 the dropout ratio for the attention probabilities initializer_range float optional defaults to 1e 10 the standard deviation of the truncated_normal_initializer for initializing all weight matrices qkv_bias bool optional defaults to true whether to add a bias to query key value projections mlp_ratio float optional defaults to 4 0 ratio of mlp hidden dim to embedding dim use_abs_pos bool optional defaults to true whether to use absolute position embedding use_rel_pos bool optional defaults to true whether to use relative position embedding window_size int optional defaults to 14 window size for relative position global_attn_indexes list int optional defaults to 2 5 8 11 the indexes of the global attention layers num_pos_feats int optional defaults to 128 the dimensionality of the position embedding mlp_dim int optional the dimensionality of the mlp layer in the transformer encoder if none defaults to mlp_ratio hidden_size samconfig is the configuration class to store the configuration of a sammodel it is used to instantiate a sam model according to the specified arguments defining the vision model prompt encoder model and mask decoder configs instantiating a configuration with the defaults will yield a similar configuration to that of the sam vit h facebook sam vit huge https huggingface co facebook sam vit huge architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args vision_config union dict samvisionconfig optional dictionary of configuration options used to initialize samvisionconfig prompt_encoder_config union dict sampromptencoderconfig optional dictionary of configuration options used to initialize sampromptencoderconfig mask_decoder_config union dict sammaskdecoderconfig optional dictionary of configuration options used to initialize sammaskdecoderconfig kwargs optional dictionary of keyword arguments example python from transformers import samvisionconfig sampromptencoderconfig sammaskdecoderconfig sammodel initializing a samconfig with facebook sam vit huge style configuration configuration samconfig initializing a sammodel with random weights from the facebook sam vit huge style configuration model sammodel configuration accessing the model configuration configuration model config we can also initialize a samconfig from a samvisionconfig sampromptencoderconfig and sammaskdecoderconfig initializing sam vision sam q former and language model configurations vision_config samvisionconfig prompt_encoder_config sampromptencoderconfig mask_decoder_config sammaskdecoderconfig config samconfig vision_config prompt_encoder_config mask_decoder_config
from ...configuration_utils import PretrainedConfig
from ...utils import logging


logger = logging.get_logger(__name__)

SAM_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "facebook/sam-vit-huge": "https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json",
    "facebook/sam-vit-large": "https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json",
    "facebook/sam-vit-base": "https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json",
}


class SamPromptEncoderConfig(PretrainedConfig):
    r

    def __init__(
        self,
        hidden_size=256,
        image_size=1024,
        patch_size=16,
        mask_input_channels=16,
        num_point_embeddings=4,
        hidden_act="gelu",
        layer_norm_eps=1e-6,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.hidden_size = hidden_size
        self.image_size = image_size
        self.patch_size = patch_size
        self.image_embedding_size = image_size // patch_size
        self.mask_input_channels = mask_input_channels
        self.num_point_embeddings = num_point_embeddings
        self.hidden_act = hidden_act
        self.layer_norm_eps = layer_norm_eps


class SamMaskDecoderConfig(PretrainedConfig):
    r

    def __init__(
        self,
        hidden_size=256,
        hidden_act="relu",
        mlp_dim=2048,
        num_hidden_layers=2,
        num_attention_heads=8,
        attention_downsample_rate=2,
        num_multimask_outputs=3,
        iou_head_depth=3,
        iou_head_hidden_dim=256,
        layer_norm_eps=1e-6,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.hidden_size = hidden_size
        self.hidden_act = hidden_act
        self.mlp_dim = mlp_dim
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.attention_downsample_rate = attention_downsample_rate
        self.num_multimask_outputs = num_multimask_outputs
        self.iou_head_depth = iou_head_depth
        self.iou_head_hidden_dim = iou_head_hidden_dim
        self.layer_norm_eps = layer_norm_eps


class SamVisionConfig(PretrainedConfig):
    r

    def __init__(
        self,
        hidden_size=768,
        output_channels=256,
        num_hidden_layers=12,
        num_attention_heads=12,
        num_channels=3,
        image_size=1024,
        patch_size=16,
        hidden_act="gelu",
        layer_norm_eps=1e-06,
        attention_dropout=0.0,
        initializer_range=1e-10,
        qkv_bias=True,
        mlp_ratio=4.0,
        use_abs_pos=True,
        use_rel_pos=True,
        window_size=14,
        global_attn_indexes=[2, 5, 8, 11],
        num_pos_feats=128,
        mlp_dim=None,
        **kwargs,
    ):
        super().__init__(**kwargs)

        self.hidden_size = hidden_size
        self.output_channels = output_channels
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.num_channels = num_channels
        self.image_size = image_size
        self.patch_size = patch_size
        self.hidden_act = hidden_act
        self.layer_norm_eps = layer_norm_eps
        self.attention_dropout = attention_dropout
        self.initializer_range = initializer_range
        self.qkv_bias = qkv_bias
        self.mlp_ratio = mlp_ratio
        self.use_abs_pos = use_abs_pos
        self.use_rel_pos = use_rel_pos
        self.window_size = window_size
        self.global_attn_indexes = global_attn_indexes
        self.num_pos_feats = num_pos_feats
        self.mlp_dim = int(hidden_size * mlp_ratio) if mlp_dim is None else mlp_dim


class SamConfig(PretrainedConfig):
    r

    model_type = "sam"

    def __init__(
        self,
        vision_config=None,
        prompt_encoder_config=None,
        mask_decoder_config=None,
        initializer_range=0.02,
        **kwargs,
    ):
        super().__init__(**kwargs)
        vision_config = vision_config if vision_config is not None else {}
        prompt_encoder_config = prompt_encoder_config if prompt_encoder_config is not None else {}
        mask_decoder_config = mask_decoder_config if mask_decoder_config is not None else {}

        if isinstance(vision_config, SamVisionConfig):
            vision_config = vision_config.to_dict()
        if isinstance(prompt_encoder_config, SamPromptEncoderConfig):
            prompt_encoder_config = prompt_encoder_config.to_dict()
        if isinstance(mask_decoder_config, SamMaskDecoderConfig):
            mask_decoder_config = mask_decoder_config.to_dict()

        self.vision_config = SamVisionConfig(**vision_config)
        self.prompt_encoder_config = SamPromptEncoderConfig(**prompt_encoder_config)
        self.mask_decoder_config = SamMaskDecoderConfig(**mask_decoder_config)
        self.initializer_range = initializer_range