
    -iK7                        d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lmZ d
dlm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e G d de
             Z ed       G d de             ZddgZy)zrPyTorch UperNet model. Based on OpenMMLab's implementation, found in https://github.com/open-mmlab/mmsegmentation.    N)nn)CrossEntropyLoss   )load_backbone)SemanticSegmenterOutput)PreTrainedModel)auto_docstring   )UperNetConfigc                        e Zd ZdZ	 	 	 ddededeeeef   z  deeeef   z  ez  dedeeeef   z  dd	f fd
Zde	j                  de	j                  fdZ xZS )UperNetConvModulez
    A convolutional block that bundles conv/norm/activation layers. This block simplifies the usage of convolution
    layers, which are commonly used with a norm layer (e.g., BatchNorm) and activation layer (e.g., ReLU).
    in_channelsout_channelskernel_sizepaddingbiasdilationreturnNc                     t         |           t        j                  ||||||      | _        t        j
                  |      | _        t        j                         | _        y )N)r   r   r   r   r   r   )	super__init__r   Conv2dconvBatchNorm2d
batch_normReLU
activation)selfr   r   r   r   r   r   	__class__s          h/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/models/upernet/modeling_upernet.pyr   zUperNetConvModule.__init__!   sQ     	II#%#
	 ..6'')    inputc                 l    | j                  |      }| j                  |      }| j                  |      }|S N)r   r   r   )r   r"   outputs      r    forwardzUperNetConvModule.forward6   s1    5!((r!   )r   Fr
   )__name__
__module____qualname____doc__inttuplestrboolr   torchTensorr&   __classcell__r   s   @r    r   r      s     01*+$$ $ 5c?*	$
 uS#X&,$ $ c3h'$ 
$*U\\ ell r!   r   c                   h     e Zd Zdedededdf fdZdej                  dej                  fdZ xZS )	UperNetPyramidPoolingBlock
pool_scaler   channelsr   Nc                     t         |           t        j                  |      t	        ||d      g| _        t        | j
                        D ]   \  }}| j                  t        |      |       " y )Nr
   r   )	r   r   r   AdaptiveAvgPool2dr   layers	enumerate
add_moduler-   )r   r5   r   r6   ilayerr   s         r    r   z#UperNetPyramidPoolingBlock.__init__?   sa      ,k8C
 "$++. 	+HAuOOCFE*	+r!   r"   c                 <    |}| j                   D ]
  } ||      } |S r$   )r:   )r   r"   hidden_stater>   s       r    r&   z"UperNetPyramidPoolingBlock.forwardH   s*    [[ 	/E .L	/r!   )	r'   r(   r)   r+   r   r/   r0   r&   r1   r2   s   @r    r4   r4   >   s?    +3 +S +C +D +U\\ ell r!   r4   c            
            e Zd ZdZdeedf   dedededdf
 fd	Zd
ej                  de
ej                     fdZ xZS )UperNetPyramidPoolingModulea}  
    Pyramid Pooling Module (PPM) used in PSPNet.

    Args:
        pool_scales (`tuple[int]`):
            Pooling scales used in Pooling Pyramid Module.
        in_channels (`int`):
            Input channels.
        channels (`int`):
            Channels after modules, before conv_seg.
        align_corners (`bool`):
            align_corners argument of F.interpolate.
    pool_scales.r   r6   align_cornersr   Nc                    t         |           || _        || _        || _        || _        g | _        t        |      D ]I  \  }}t        |||      }| j                  j                  |       | j                  t        |      |       K y )N)r5   r   r6   )r   r   rC   rD   r   r6   blocksr;   r4   appendr<   r-   )	r   rC   r   r6   rD   r=   r5   blockr   s	           r    r   z$UperNetPyramidPoolingModule.__init__^   s    &*& &{3 	+MAz.*R]hpqEKKu%OOCFE*	+r!   xc                     g }| j                   D ]Y  } ||      }t        j                  j                  ||j	                         dd  d| j
                        }|j                  |       [ |S )N   bilinearsizemoderD   )rF   r   
functionalinterpolaterN   rD   rG   )r   rI   ppm_outsppmppm_outupsampled_ppm_outs         r    r&   z#UperNetPyramidPoolingModule.forwardj   sn    ;; 	/C!fG " 9 9affhqrl4K]K] !: ! OO-.	/ r!   )r'   r(   r)   r*   r,   r+   r.   r   r/   r0   listr&   r1   r2   s   @r    rB   rB   O   s[    
+E#s(O 
+# 
+QT 
+ei 
+nr 
+ $u||*< r!   rB   c                   `     e Zd ZdZ fdZd Zdej                  dej                  fdZ xZ	S )UperNetHeadz
    Unified Perceptual Parsing for Scene Understanding. This head is the implementation of
    [UPerNet](https://huggingface.co/papers/1807.10221).
    c                    t         |           || _        |j                  | _        || _        |j
                  | _        d| _        t        j                  | j                  |j                  d      | _        t        | j                  | j                  d   | j                  | j                        | _        t        | j                  d   t        | j                        | j                  z  z   | j                  dd      | _        t        j"                         | _        t        j"                         | _        | j                  d d D ]s  }t        || j                  d      }t        | j                  | j                  dd      }| j$                  j)                  |       | j&                  j)                  |       u t        t        | j                        | j                  z  | j                  dd      | _        y )NFr
   r8   )rD   r   r   r   )r   r   configrC   r   hidden_sizer6   rD   r   r   
num_labels
classifierrB   psp_modulesr   len
bottleneck
ModuleListlateral_convs	fpn_convsrG   fpn_bottleneck)r   r\   r   l_convfpn_convr   s        r    r   zUperNetHead.__init__{   s   !--&**"))DMM63D3DRST 7R MM,,	
 ,R 3t'7'7#84==#HHMM	
  ]]_++CR0 	,K&{DMMqQF(ST^_`H%%f-NN!!(+		, 0  !DMM1MM	
r!   c                     |d   }|g}|j                  | j                  |             t        j                  |d      }| j	                  |      }|S )NrZ   r
   dim)extendr`   r/   catrb   )r   inputsrI   psp_outsr%   s        r    psp_forwardzUperNetHead.psp_forward   sL    2J3((+,99X1-*r!   encoder_hidden_statesr   c                 P   t        | j                        D cg c]  \  }} |||          }}}|j                  | j                  |             t	        |      }t        |dz
  dd      D ]V  }||dz
     j                  dd  }||dz
     t        j                  j                  ||   |d| j                        z   ||dz
  <   X t        |dz
        D cg c]  } | j                  |   ||          }}|j                  |d          t        |dz
  dd      D ]E  }t        j                  j                  ||   |d   j                  dd  d| j                        ||<   G t        j                  |d      }| j                  |      }| j                  |      }|S c c}}w c c}w )Nr
   r   rZ   rK   rL   rM   rj   )r;   rd   rG   rp   ra   rangeshaper   rP   rQ   rD   re   r/   rm   rf   r_   )	r   rq   r=   lateral_convlateralsused_backbone_levels
prev_shapefpn_outsr%   s	            r    r&   zUperNetHead.forward   s   R[\`\n\nRopq,L!6q!9:pp(()>?@  #8}+a/B7 	A!!a%..qr2J&q1uo0I0I*:TM_M_ 1J 1 HQUO	 =BBVYZBZ<[\q%DNN1%hqk2\\%+a/B7 	A--33(1+"3"3AB"7jX\XjXj 4 HQK	 99X1-$$X.(3 q ]s   FF#)
r'   r(   r)   r*   r   rp   r/   r0   r&   r1   r2   s   @r    rX   rX   u   s.    
%
NU\\ ell r!   rX   c            
            e Zd ZdZ	 d
dededeeeef   z  ddf fdZdej                  dej                  fd	Z	 xZ
S )UperNetFCNHeada  
    Fully Convolution Networks for Semantic Segmentation. This head is the implementation of
    [FCNNet](https://huggingface.co/papers/1411.4038>).

    Args:
        config:
            Configuration.
        in_channels (int):
            Number of input channels.
        kernel_size (int):
            The kernel size for convs in the head. Default: 3.
        dilation (int):
            The dilation rate for convs in the head. Default: 1.
    in_indexr   r   r   Nc           
      l   t         	|           || _        |j                  ||   n|j                  | _        |j
                  | _        |j                  | _        |j                  | _
        || _        |dz  |z  }g }|j                  t        | j                  | j                  |||             t        | j                  dz
        D ]5  }|j                  t        | j                  | j                  |||             7 | j                  dk(  rt        j                          | _        nt        j$                  | | _        | j                  r8t        | j                  | j                  z   | j                  ||dz        | _        t        j(                  | j                  |j*                  d      | _        y )NrK   )r   r   r   r
   r   r[   r8   )r   r   r\   auxiliary_in_channelsr   auxiliary_channelsr6   auxiliary_num_convs	num_convsauxiliary_concat_inputconcat_inputr|   rG   r   rs   r   Identityconvs
Sequentialconv_catr   r^   r_   )
r   r\   r   r|   r   r   conv_paddingr   r=   r   s
            r    r   zUperNetFCNHead.__init__   sv    	%+%A%A%IK!vOkOk 	 1133"99 #q(H4  $--[R^iq	

 t~~)* 	ALL!MM4==kS_jr	 >>QDJ.DJ-  4==0$--[bmqrbrDM ))DMM63D3DRSTr!   rq   c                     || j                      }| j                  |      }| j                  r(| j                  t	        j
                  ||gd            }| j                  |      }|S )Nr
   rj   )r|   r   r   r   r/   rm   r_   )r   rq   hidden_statesr%   s       r    r&   zUperNetFCNHead.forward   sX    -dmm<M*]]599mV-D!#LMF(r!   )rK   r   r
   )r'   r(   r)   r*   r+   r,   r   r/   r0   r&   r1   r2   s   @r    r{   r{      sg      op$U-0$UCF$UVY\abegjbj\kVk$U	$ULU\\ ell r!   r{   c                   $    e Zd ZU eed<   dZdZg Zy)UperNetPreTrainedModelr\   pixel_values)imageN)r'   r(   r)   r   __annotations__main_input_nameinput_modalities_no_split_modules r!   r    r   r   	  s    $O!r!   r   zW
    UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.
    )custom_introc                        e Zd Z fdZe	 	 	 	 	 d
dej                  dz  dedz  dedz  dej                  dz  dedz  dee	z  fd	       Z
 xZS )UperNetForSemanticSegmentationc                    t         |   |       t        |      | _        t	        || j                  j
                        | _        |j                  r!t        || j                  j
                        nd | _	        | j                          y )N)r   )r   r   r   backbonerX   r6   decode_headuse_auxiliary_headr{   auxiliary_head	post_init)r   r\   r   s     r    r   z'UperNetForSemanticSegmentation.__init__  sj     %f- 'v4==;Q;QRJPJcJcN6t}}/E/EFim 	
 	r!   Nr   output_attentionsoutput_hidden_stateslabelsreturn_dictr   c                    |$| j                   j                  dk(  rt        d      ||n| j                   j                  }||n| j                   j                  }||n| j                   j
                  }| j                  j                  |||      }|j                  }| j                  |      }	t        j                  j                  |	|j                  dd dd      }	d}
| j                  A| j                  |      }
t        j                  j                  |
|j                  dd dd      }
d}|Pt        | j                   j                   	      } ||	|      }|
% ||
|      }|| j                   j"                  |z  z  }|s|r
|	f|dd z   }n	|	f|dd z   }||f|z   S |S t%        ||	|j&                  |j(                  
      S )a  
        labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*):
            Ground truth semantic segmentation maps for computing the loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels > 1`, a classification loss is computed (Cross-Entropy).

        Examples:
        ```python
        >>> from transformers import AutoImageProcessor, UperNetForSemanticSegmentation
        >>> from PIL import Image
        >>> from huggingface_hub import hf_hub_download

        >>> image_processor = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny")
        >>> model = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny")

        >>> filepath = hf_hub_download(
        ...     repo_id="hf-internal-testing/fixtures_ade20k", filename="ADE_val_00000001.jpg", repo_type="dataset"
        ... )
        >>> image = Image.open(filepath).convert("RGB")

        >>> inputs = image_processor(images=image, return_tensors="pt")

        >>> outputs = model(**inputs)

        >>> logits = outputs.logits  # shape (batch_size, num_labels, height, width)
        >>> list(logits.shape)
        [1, 150, 512, 512]
        ```Nr
   z/The number of labels should be greater than one)r   r   rK   rL   FrM   )ignore_index)losslogitsr   
attentions)r\   r^   
ValueErroruse_return_dictr   r   r   forward_with_filtered_kwargsfeature_mapsr   r   rP   rQ   rt   r   r   loss_ignore_indexauxiliary_loss_weightr   r   r   )r   r   r   r   r   r   kwargsoutputsfeaturesr   auxiliary_logitsr   loss_fctauxiliary_lossr%   s                  r    r&   z&UperNetForSemanticSegmentation.forward%  s   J $++"8"8A"=NOO%0%<k$++B]B]$8$D $++JjJj 	 2C1N-TXT_T_TqTq--<</CWh = 
 ''!!(+**68J8J128NU_ot*u*#228<!}}88 |'9'9!"'=J^c  9   'T[[5R5RSHFF+D+!)*:F!C99NJJ# WQR[0 WQR[0)-)9TGf$EvE&!//))	
 	
r!   )NNNNN)r'   r(   r)   r   r	   r/   r0   r.   r,   r   r&   r1   r2   s   @r    r   r     s      -1)-,0&*#'Q
llT)Q
  $;Q
 #Tk	Q

 t#Q
 D[Q
 
(	(Q
 Q
r!   r   )r*   r/   r   torch.nnr   backbone_utilsr   modeling_outputsr   modeling_utilsr   utilsr	   configuration_upernetr   Moduler   r4   rB   rX   r{   r   r   __all__r   r!   r    <module>r      s    y   % + 7 - # 0 		  F "#")) #LQ")) Qh=RYY =@ _   
a
%; a

a
H ,-E
Fr!   