Ë
    T^(h3  ã                   ó¾  — d Z ddlmZ ddlmZmZ ddlZddlmZ ddlm	Z	 ddl
mZmZmZmZ dd	lmZ d
dlmZ dZe G d„ de«      «       Z G d„ de	«      Z G d„ dej,                  «      Z G d„ dej,                  «      Z G d„ dej,                  «      Z G d„ dej,                  «      Z G d„ dej,                  «      ZdZdZ ede«       G d„ de«      «       ZddgZy) zPyTorch ViTMatte model.é    )Ú	dataclass)ÚOptionalÚTupleN)Únné   )ÚPreTrainedModel)ÚModelOutputÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚreplace_return_docstrings)Úload_backboneé   )ÚVitMatteConfigr   c                   ó¾   — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eeej                        ed<   dZeeej                        ed<   y)ÚImageMattingOutputa»  
    Class for outputs of image matting models.

    Args:
        loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
            Loss.
        alphas (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
           Estimated alpha values.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
            one for the output of each stage) of shape `(batch_size, sequence_length, hidden_size)`. Hidden-states
            (also called feature maps) of the model at the output of each stage.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, patch_size,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
    NÚlossÚalphasÚhidden_statesÚ
attentions)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚtorchÚFloatTensorÚ__annotations__r   r   r   r   © ó    úl/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/vitmatte/modeling_vitmatte.pyr   r   &   sg   … ñð( )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø*.€FˆH�U×&Ñ&Ñ'Ó.Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ó<Ø59€J�˜˜u×0Ñ0Ñ1Ñ2Ô9r   r   c                   ó&   — e Zd ZdZeZdZdZg Zd„ Z	y)ÚVitMattePreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    Úpixel_valuesTc                 ó  — t        |t        j                  «      rm|j                  j                  j                  d| j                  j                  ¬«       |j                  �%|j                  j                  j                  «        y y y )Ng        )ÚmeanÚstd)
Ú
isinstancer   ÚConv2dÚweightÚdataÚnormal_ÚconfigÚinitializer_rangeÚbiasÚzero_)ÚselfÚmodules     r   Ú_init_weightsz%VitMattePreTrainedModel._init_weightsM   sa   € Ü�fœbŸi™iÔ(Ø�M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ð )r   N)
r   r   r   r   r   Úconfig_classÚmain_input_nameÚsupports_gradient_checkpointingÚ_no_split_modulesr1   r   r   r   r!   r!   B   s$   „ ñð
 "€LØ$€OØ&*Ð#ØÐó)r   r!   c                   ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ xZS )ÚVitMatteBasicConv3x3zP
    Basic convolution layers including: Conv3x3, BatchNorm2d, ReLU layers.
    c                 óà   •— t         ‰| �  «        t        j                  ||d||d¬«      | _        t        j
                  ||j                  ¬«      | _        t        j                  «       | _	        y )Nr   F)Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚpaddingr-   )Úeps)
ÚsuperÚ__init__r   r'   ÚconvÚBatchNorm2dÚbatch_norm_epsÚ
batch_normÚReLUÚrelu)r/   r+   r9   r:   r<   r=   Ú	__class__s         €r   r@   zVitMatteBasicConv3x3.__init__Y   sW   ø€ Ü‰ÑÔÜ—I‘IØ#Ø%ØØØØô
ˆŒ	ô Ÿ.™.¨¸6×;PÑ;PÔQˆŒÜ—G‘G“Iˆ�	r   c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S ©N)rA   rD   rF   ©r/   Úhidden_states     r   ÚforwardzVitMatteBasicConv3x3.forwardf   s2   € Ø—y‘y Ó.ˆØ—‘ |Ó4ˆØ—y‘y Ó.ˆàÐr   )é   r   ©r   r   r   r   r@   rL   Ú__classcell__©rG   s   @r   r7   r7   T   s   ø„ ñõör   r7   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚVitMatteConvStreamzc
    Simple ConvStream containing a series of basic conv3x3 layers to extract detail features.
    c                 ó¤  •— t         ‰| �  «        d}|j                  �|j                  j                  }|j                  }t        j                  «       | _        |g|z   | _        t        t        | j                  «      dz
  «      D ]I  }| j                  |   }| j                  |dz      }| j                  j                  t        |||«      «       ŒK y )Né   r   )r?   r@   Úbackbone_configÚnum_channelsÚconvstream_hidden_sizesr   Ú
ModuleListÚconvsÚ
conv_chansÚrangeÚlenÚappendr7   )r/   r+   r9   r:   ÚiÚin_chan_Ú	out_chan_rG   s          €r   r@   zVitMatteConvStream.__init__s   s¸   ø€ Ü‰ÑÔð ˆØ×!Ñ!Ð-Ø ×0Ñ0×=Ñ=ˆKà×5Ñ5ˆä—]‘]“_ˆŒ
Ø&˜-¨,Ñ6ˆŒä”s˜4Ÿ?™?Ó+¨aÑ/Ó0ò 	QˆAØ—‘ qÑ)ˆHØŸ™¨¨A©Ñ.ˆIØ�J‰J×ÑÔ2°6¸8ÀYÓOÕPñ	Qr   c                 ó®   — d|i}|}t        t        | j                  «      «      D ]-  } | j                  |   |«      }dt        |dz   «      z   }|||<   Œ/ |S )NÚdetailed_feature_map_0Údetailed_feature_map_r   )r[   r\   rY   Ústr)r/   r"   Úout_dictÚ
embeddingsr^   Úname_s         r   rL   zVitMatteConvStream.forward†   sc   € Ø,¨lÐ;ˆØ!ˆ
Ü”s˜4Ÿ:™:“Ó'ò 	)ˆAØ&˜Ÿ™ A™ zÓ2ˆJØ+¬c°!°a±%«jÑ8ˆEØ(ˆH�UŠOð	)ð
 ˆr   rN   rP   s   @r   rR   rR   n   s   ø„ ñôQö&r   rR   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚVitMatteFusionBlockz\
    Simple fusion block to fuse features from ConvStream and Plain Vision Transformer.
    c                 óL   •— t         ‰| �  «        t        |||dd¬«      | _        y )Nr   )r<   r=   )r?   r@   r7   rA   )r/   r+   r9   r:   rG   s       €r   r@   zVitMatteFusionBlock.__init__–   s$   ø€ Ü‰ÑÔÜ(¨°¸lÐSTÐ^_Ô`ˆ�	r   c                 ó    — t         j                  j                  |ddd¬«      }t        j                  ||gd¬«      }| j                  |«      }|S )NrM   ÚbilinearF)Úscale_factorÚmodeÚalign_cornersr   )Údim)r   Ú
functionalÚinterpolater   ÚcatrA   )r/   ÚfeaturesÚdetailed_feature_mapÚupscaled_featuresÚouts        r   rL   zVitMatteFusionBlock.forwardš   sK   € ÜŸM™M×5Ñ5°hÈQÐU_ÐotÐ5ÓuÐÜ�i‰iÐ-Ð/@ÐAÀqÔIˆØ�i‰i˜‹nˆàˆ
r   rN   rP   s   @r   ri   ri   ‘   s   ø„ ñôaör   ri   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚVitMatteHeadzJ
    Simple Matting Head, containing only conv3x3 and conv1x1 layers.
    c                 ó*  •— t         ‰| �  «        |j                  d   }d}t        j                  t        j
                  ||ddd¬«      t        j                  |«      t        j                  d«      t        j
                  |dddd¬«      «      | _        y )Néÿÿÿÿé   r   r   )r;   r<   r=   Tr   )	r?   r@   Úfusion_hidden_sizesr   Ú
Sequentialr'   rB   rE   Úmatting_convs)r/   r+   r9   Úmid_channelsrG   s       €r   r@   zVitMatteHead.__init__§   st   ø€ Ü‰ÑÔà×0Ñ0°Ñ4ˆØˆäŸ]™]Ü�I‰I�k <¸QÀqÐRSÔTÜ�N‰N˜<Ó(Ü�G‰G�D‹MÜ�I‰I�l A°1¸QÈÔJó	
ˆÕr   c                 ó(   — | j                  |«      }|S rI   )r   rJ   s     r   rL   zVitMatteHead.forward´   s   € Ø×)Ñ)¨,Ó7ˆàÐr   rN   rP   s   @r   ry   ry   ¢   s   ø„ ñô
ör   ry   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚVitMatteDetailCaptureModulezG
    Simple and lightweight Detail Capture Module for ViT Matting.
    c           
      ó„  •— t         ‰| �  «        t        |j                  «      t        |j                  «      dz   k7  rt        d«      ‚|| _        t        |«      | _        | j                  j                  | _	        t        j                  «       | _        |j                  g|j                  z   | _        t        t        | j                  «      dz
  «      D ]Z  }| j                  j!                  t#        || j                  |   | j                  |dz       z   | j                  |dz      ¬«      «       Œ\ t%        |«      | _        y )Nr   z_The length of fusion_hidden_sizes should be equal to the length of convstream_hidden_sizes + 1.)r+   r9   r:   )r?   r@   r\   r}   rW   Ú
ValueErrorr+   rR   Ú
convstreamrZ   r   rX   Úfusion_blocksÚhidden_sizeÚfusion_channelsr[   r]   ri   ry   Úmatting_head)r/   r+   r^   rG   s      €r   r@   z$VitMatteDetailCaptureModule.__init__¿   s  ø€ Ü‰ÑÔÜˆv×)Ñ)Ó*¬c°&×2PÑ2PÓ.QÐTUÑ.UÒUÜØqóð ð ˆŒÜ,¨VÓ4ˆŒØŸ/™/×4Ñ4ˆŒäŸ]™]›_ˆÔØ &× 2Ñ 2Ð3°f×6PÑ6PÑPˆÔä”s˜4×/Ñ/Ó0°1Ñ4Ó5ò 	ˆAØ×Ñ×%Ñ%Ü#Ø!Ø $× 4Ñ 4°QÑ 7¸$¿/¹/ÈAÐPQÉEÈ(Ñ:SÑ SØ!%×!5Ñ!5°a¸!±eÑ!<ôõð	ô )¨Ó0ˆÕr   c                 ó6  — | j                  |«      }t        t        | j                  «      «      D ]B  }dt	        t        | j                  «      |z
  dz
  «      z   } | j                  |   |||   «      }ŒD t        j                  | j                  |«      «      }|S )Nrc   r   )r†   r[   r\   r‡   rd   r   ÚsigmoidrŠ   )r/   rt   r"   Údetail_featuresr^   Údetailed_feature_map_namer   s          r   rL   z#VitMatteDetailCaptureModule.forwardØ   s—   € ØŸ/™/¨,Ó7ˆÜ”s˜4×-Ñ-Ó.Ó/ò 	cˆAØ(?Ä#ÄcÈ$×J\ÑJ\ÓF]Ð`aÑFaÐdeÑFeÓBfÑ(fÐ%Ø,�t×)Ñ)¨!Ñ,¨X°ÐG`Ñ7aÓb‰Hð	cô —‘˜t×0Ñ0°Ó:Ó;ˆàˆr   rN   rP   s   @r   rƒ   rƒ   º   s   ø„ ñô1ö2r   rƒ   aI  
    Parameters:
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.
        config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
aw  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using
            [`AutoImageProcessor`]. See [`VitMatteImageProcessor.__call__`] for details.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See
            `attentions` under returned tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under
            returned tensors for more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
zNViTMatte framework leveraging any vision backbone e.g. for ADE20k, CityScapes.c                   óÚ   ‡ — e Zd Zˆ fd„Z eej                  d«      «       eee	¬«      	 	 	 	 	 d
de
ej                     de
e   de
e   de
ej                     de
e   f
d	„«       «       Zˆ xZS )ÚVitMatteForImageMattingc                 ó’   •— t         ‰| �  |«       || _        t        |«      | _        t        |«      | _        | j                  «        y rI   )r?   r@   r+   r   Úbackbonerƒ   ÚdecoderÚ	post_init)r/   r+   rG   s     €r   r@   z VitMatteForImageMatting.__init__  s;   ø€ Ü‰Ñ˜Ô ØˆŒä% fÓ-ˆŒÜ2°6Ó:ˆŒð 	�‰Õr   zbatch_size, sequence_length)Úoutput_typer2   r"   Úoutput_attentionsÚoutput_hidden_statesÚlabelsÚreturn_dictc                 óª  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }d}|�t	        d«      ‚| j
                  j                  |||¬«      }|j                  d   }| j                  ||«      }	|s|	f|dd z   }
|�|f|
z   S |
S t        ||	|j                  |j                  ¬«      S )aJ  
        labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*):
            Ground truth image matting for computing the loss.

        Returns:

        Examples:

        ```python
        >>> from transformers import VitMatteImageProcessor, VitMatteForImageMatting
        >>> import torch
        >>> from PIL import Image
        >>> from huggingface_hub import hf_hub_download

        >>> processor = VitMatteImageProcessor.from_pretrained("hustvl/vitmatte-small-composition-1k")
        >>> model = VitMatteForImageMatting.from_pretrained("hustvl/vitmatte-small-composition-1k")

        >>> filepath = hf_hub_download(
        ...     repo_id="hf-internal-testing/image-matting-fixtures", filename="image.png", repo_type="dataset"
        ... )
        >>> image = Image.open(filepath).convert("RGB")
        >>> filepath = hf_hub_download(
        ...     repo_id="hf-internal-testing/image-matting-fixtures", filename="trimap.png", repo_type="dataset"
        ... )
        >>> trimap = Image.open(filepath).convert("L")

        >>> # prepare image + trimap for the model
        >>> inputs = processor(images=image, trimaps=trimap, return_tensors="pt")

        >>> with torch.no_grad():
        ...     alphas = model(**inputs).alphas
        >>> print(alphas.shape)
        torch.Size([1, 1, 640, 960])
        ```NzTraining is not yet supported)r—   r–   r{   r   )r   r   r   r   )r+   Úuse_return_dictr—   r–   ÚNotImplementedErrorr’   Úforward_with_filtered_kwargsÚfeature_mapsr“   r   r   r   )r/   r"   r–   r—   r˜   r™   r   Úoutputsrt   r   Úoutputs              r   rL   zVitMatteForImageMatting.forward  sÿ   € ðX &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐàˆØÐÜ%Ð&EÓFÐFà—-‘-×<Ñ<ØÐ/CÐWhð =ó 
ˆð ×'Ñ'¨Ñ+ˆØ—‘˜h¨Ó5ˆáØ�Y ¨¨ Ñ,ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä!ØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
r   )NNNNN)r   r   r   r@   r   ÚVITMATTE_INPUTS_DOCSTRINGÚformatr   r   Ú_CONFIG_FOR_DOCr   r   ÚTensorÚboolrL   rO   rP   s   @r   r�   r�   ý   s¦   ø„ ô
ñ +Ð+D×+KÑ+KÐLiÓ+jÓkÙÐ+=ÈOÔ\ð 04Ø,0Ø/3Ø)-Ø&*ñD
à˜uŸ|™|Ñ,ðD
ð $ D™>ðD
ð ' t™nð	D
ð
 ˜Ÿ™Ñ&ðD
ð ˜d‘^òD
ó ]ó lôD
r   r�   ) r   Údataclassesr   Útypingr   r   r   r   Úmodeling_utilsr   Úutilsr	   r
   r   r   Úutils.backbone_utilsr   Úconfiguration_vitmatter   r£   r   r!   ÚModuler7   rR   ri   ry   rƒ   ÚVITMATTE_START_DOCSTRINGr¡   r�   Ú__all__r   r   r   ú<module>r¯      só   ðñ å !ß "ã Ý å -÷ó õ 2Ý 2ð #€ð ô:˜ó :ó ð:ô6)˜oô )ô$˜2Ÿ9™9ô ô4 ˜Ÿ™ô  ôF˜"Ÿ)™)ô ô"�2—9‘9ô ô0& "§)¡)ô &ðRÐ ðÐ ñ  ØXØóôQ
Ð5ó Q
ó	ðQ
ðh %Ð&?Ð
@�r   