
    g2                        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S ) 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j        ed<   dZ
eeej                          ed<   dZeeej                          ed<   dS )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        j/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/vitmatte/modeling_vitmatte.pyr   r   &   s          ( )-D(5$
%,,, $FE$$$8<M8E%"345<<<59Ju01299999r   r   c                   (    e Zd ZdZeZdZdZg Zd Z	dS )VitMattePreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    pixel_valuesTc                     t          |t          j                  rR|j        j                            d| j        j                   |j        "|j        j        	                                 d S d S d S )Ng        )meanstd)

isinstancer   Conv2dweightdatanormal_configinitializer_rangebiaszero_)selfmodules     r   _init_weightsz%VitMattePreTrainedModel._init_weightsM   sn    fbi(( 	)M&&CT[5R&SS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   sE         
 "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.
       r   c                     t                                                       t          j        ||d||d          | _        t          j        ||j                  | _        t          j                    | _	        d S )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+   r:   r;   r=   r>   	__class__s         r   rA   zVitMatteBasicConv3x3.__init__Y   so    I#%
 
 
	 .6;PQQQGII			r   c                     |                      |          }|                     |          }|                     |          }|S N)rB   rE   rG   r/   hidden_states     r   forwardzVitMatteBasicConv3x3.forwardf   s;    yy..|44yy..r   )r8   r   r   r   r   r   rA   rM   __classcell__rH   s   @r   r7   r7   T   sV                    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 ]H}| j        |         }| j        |dz            }| j                            t          |||                     Id S )N   r   )r@   rA   backbone_confignum_channelsconvstream_hidden_sizesr   
ModuleListconvs
conv_chansrangelenappendr7   )r/   r+   r:   r;   iin_chan_	out_chan_rH   s          r   rA   zVitMatteConvStream.__init__s   s     !- 0=K5]__
&-,6s4?++a/00 	Q 	QAq)HA.IJ268YOOPPPP	Q 	Qr   c                     d|i}|}t          t          | j                            D ]2} | j        |         |          }dt          |dz             z   }|||<   3|S )Ndetailed_feature_map_0detailed_feature_map_r   )r[   r\   rY   str)r/   r"   out_dict
embeddingsr^   name_s         r   rM   zVitMatteConvStream.forward   si    ,l;!
s4:'' 	) 	)A&Az22J+c!a%jj8E(HUOOr   rN   rP   s   @r   rR   rR   n   sV         Q Q Q Q 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                 z    t                                                       t          |||dd          | _        d S )Nr   )r=   r>   )r@   rA   r7   rB   )r/   r+   r:   r;   rH   s       r   rA   zVitMatteFusionBlock.__init__   s9    (lST^_```			r   c                     t           j                            |ddd          }t          j        ||gd          }|                     |          }|S )Nr8   bilinearF)scale_factormodealign_cornersr   )dim)r   
functionalinterpolater   catrB   )r/   featuresdetailed_feature_mapupscaled_featuresouts        r   rM   zVitMatteFusionBlock.forward   sR    M55hQU_ot5uui-/@AqIIIiinn
r   rN   rP   s   @r   ri   ri      sV         a a a a 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                 B   t                                                       |j        d         }d}t          j        t          j        ||ddd          t          j        |          t          j        d          t          j        |dddd                    | _        d S )N   r   r   )r<   r=   r>   Tr   )	r@   rA   fusion_hidden_sizesr   
Sequentialr'   rC   rF   matting_convs)r/   r+   r:   mid_channelsrH   s       r   rA   zVitMatteHead.__init__   s    04]Ik<QqRSTTTN<((GDMMIlA1QJJJ	
 
r   c                 0    |                      |          }|S rJ   )r   rK   s     r   rM   zVitMatteHead.forward   s    )),77r   rN   rP   s   @r   ry   ry      sQ         
 
 
 
 
      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   k    rt          d          || _        t          |          | _        | j        j	        | _	        t          j                    | _        |j        g|j        z   | _        t          t          | j                  dz
            D ]W}| j                            t#          || j        |         | j	        |dz             z   | j        |dz                                 Xt%          |          | _        d S )Nr   z_The length of fusion_hidden_sizes should be equal to the length of convstream_hidden_sizes + 1.)r+   r:   r;   )r@   rA   r\   r}   rW   
ValueErrorr+   rR   
convstreamrZ   r   rX   fusion_blockshidden_sizefusion_channelsr[   r]   ri   ry   matting_head)r/   r+   r^   rH   s      r   rA   z$VitMatteDetailCaptureModule.__init__   s>   v)**c&2P.Q.QTU.UUUq   ,V44/4]__ & 23f6PPs4/001455 	 	A%%#! $ 4Q 7$/APQE(:S S!%!5a!e!<      )00r   c                 T   |                      |          }t          t          | j                            D ]I}dt	          t          | j                  |z
  dz
            z   } | j        |         |||                   }Jt          j        |                     |                    }|S )Nrc   r   )r   r[   r\   r   rd   r   sigmoidr   )r/   rt   r"   detail_featuresr^   detailed_feature_map_namer   s          r   rM   z#VitMatteDetailCaptureModule.forward   s    //,77s4-..// 	c 	cA(?#c$J\F]F]`aFadeFeBfBf(f%,t)!,XG`7abbHHt00::;;r   rN   rP   s   @r   r   r      sQ         1 1 1 1 12      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                    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          |          | _        |                                  d S rJ   )r@   rA   r+   r   backboner   decoder	post_init)r/   r+   rH   s     r   rA   z VitMatteForImageMatting.__init__  sX       %f--26:: 	r   zbatch_size, sequence_length)output_typer2   Nr"   output_attentionsoutput_hidden_stateslabelsreturn_dictc                 v   ||n| j         j        }||n| j         j        }||n| j         j        }d}|t	          d          | j                            |||          }|j        d         }|                     ||          }	|s|	f|dd         z   }
||f|
z   n|
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   rM   zVitMatteForImageMatting.forward  s	   X &1%<kk$+B]$8$D  $+Jj 	 2C1N--TXT_Tq%&EFFF-<</CWh = 
 
 '+h55 	FY,F)-)9TGf$$vE!!/)	
 
 
 	
r   )NNNNN)r   r   r   rA   r   VITMATTE_INPUTS_DOCSTRINGformatr   r   _CONFIG_FOR_DOCr   r   TensorboolrM   rO   rP   s   @r   r   r      s        
     +*+D+K+KLi+j+jkk+=O\\\ 04,0/3)-&*D
 D
u|,D
 $D>D
 'tn	D

 &D
 d^D
 D
 D
 ]\ lkD
 D
 D
 D
 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   r   r   r   <module>r      s|     ! ! ! ! ! ! " " " " " " " "        - - - - - -            2 1 1 1 1 1 2 2 2 2 2 2 # : : : : : : : :6) ) ) ) )o ) ) )$    29   4               F    ")   "    29   0& & & & &") & & &R    X Q
 Q
 Q
 Q
 Q
5 Q
 Q
	 Q
 Q
 Q
r   