
    sgz              	          d Z ddlZddlmZmZmZmZ ddlZddlZddlm	Z	 ddl
mZ ddlmZmZ ddlmZ dd	lmZmZ dd
lmZmZmZmZmZ ddlmZ  ej8                  e      ZdZdZ g dZ!d;dejD                  de#de$dejD                  fdZ% G d de	jL                        Z' G d de	jL                        Z( G d de	jL                        Z) G d de	jL                        Z* G d de	jL                        Z+ G d d e	jL                        Z, G d! d"e	jL                        Z- G d# d$e	jL                        Z. G d% d&e	jL                        Z/ G d' d(e      Z0d)Z1d*Z2 ed+e1       G d, d-e0             Z3 G d. d/e	jL                        Z4 G d0 d1e	jL                        Z5 G d2 d3e	jL                        Z6 G d4 d5e	jL                        Z7 G d6 d7e	jL                        Z8 ed8e1       G d9 d:e0             Z9y)<zPyTorch GLPN model.    N)ListOptionalTupleUnion)nn   )ACT2FN)BaseModelOutputDepthEstimatorOutput)PreTrainedModel) find_pruneable_heads_and_indicesprune_linear_layer)add_code_sample_docstringsadd_start_docstrings%add_start_docstrings_to_model_forwardloggingreplace_return_docstrings   )
GLPNConfigr   zvinvino02/glpn-kitti)r   i         input	drop_probtrainingreturnc                    |dk(  s|s| S d|z
  }| j                   d   fd| j                  dz
  z  z   }|t        j                  || j                  | j
                        z   }|j                          | j                  |      |z  }|S )aF  
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).

    Comment by Ross Wightman: This is the same as the DropConnect impl I created for EfficientNet, etc networks,
    however, the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
    See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for changing the
    layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use 'survival rate' as the
    argument.
            r   r   )r   )dtypedevice)shapendimtorchrandr   r   floor_div)r   r   r   	keep_probr    random_tensoroutputs          Y/var/www/html/venv/lib/python3.12/site-packages/transformers/models/glpn/modeling_glpn.py	drop_pathr*   2   s     CxII[[^

Q 77E

5ELL YYMYYy!M1FM    c                   x     e Zd ZdZd	dee   ddf fdZdej                  dej                  fdZ	de
fdZ xZS )
GLPNDropPathzXDrop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).Nr   r   c                 0    t         |           || _        y N)super__init__r   )selfr   	__class__s     r)   r1   zGLPNDropPath.__init__J   s    "r+   hidden_statesc                 D    t        || j                  | j                        S r/   )r*   r   r   )r2   r4   s     r)   forwardzGLPNDropPath.forwardN   s    FFr+   c                 8    dj                  | j                        S )Nzp={})formatr   )r2   s    r)   
extra_reprzGLPNDropPath.extra_reprQ   s    }}T^^,,r+   r/   )__name__
__module____qualname____doc__r   floatr1   r"   Tensorr6   strr9   __classcell__r3   s   @r)   r-   r-   G   sG    b#(5/ #T #GU\\ Gell G-C -r+   r-   c                   (     e Zd ZdZ fdZd Z xZS )GLPNOverlapPatchEmbeddingsz+Construct the overlapping patch embeddings.c                     t         |           t        j                  |||||dz        | _        t        j
                  |      | _        y )N   kernel_sizestridepadding)r0   r1   r   Conv2dproj	LayerNorm
layer_norm)r2   
patch_sizerI   num_channelshidden_sizer3   s        r)   r1   z#GLPNOverlapPatchEmbeddings.__init__Y   sC    II"!O
	 ,,{3r+   c                     | j                  |      }|j                  \  }}}}|j                  d      j                  dd      }| j	                  |      }|||fS )NrF   r   )rL   r    flatten	transposerN   )r2   pixel_values
embeddings_heightwidths         r)   r6   z"GLPNOverlapPatchEmbeddings.forwarde   s^    YY|,
(..1fe  ''*44Q:
__Z0
65((r+   r:   r;   r<   r=   r1   r6   rA   rB   s   @r)   rD   rD   V   s    5
4)r+   rD   c                   2     e Zd ZdZ fdZd Z	 ddZ xZS )GLPNEfficientSelfAttentionzSegFormer's efficient self-attention mechanism. Employs the sequence reduction process introduced in the [PvT
    paper](https://arxiv.org/abs/2102.12122).c                 8   t         |           || _        || _        | j                  | j                  z  dk7  r&t	        d| j                   d| j                   d      t        | j                  | j                  z        | _        | j                  | j                  z  | _        t        j                  | j                  | j                        | _
        t        j                  | j                  | j                        | _        t        j                  | j                  | j                        | _        t        j                  |j                        | _        || _        |dkD  r9t        j"                  ||||      | _        t        j&                  |      | _        y y )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads ()r   )rH   rI   )r0   r1   rQ   num_attention_heads
ValueErrorintattention_head_sizeall_head_sizer   LinearquerykeyvalueDropoutattention_probs_dropout_probdropoutsr_ratiorK   srrM   rN   r2   configrQ   r_   sequence_reduction_ratior3   s        r)   r1   z#GLPNEfficientSelfAttention.__init__t   sT   &#6 d666!;#D$4$4#5 622316 
 $'t'7'7$:R:R'R#S !558P8PPYYt//1C1CD
99T--t/A/ABYYt//1C1CD
zz&"E"EF0#a'ii[6NWoDG !ll;7DO	 (r+   c                     |j                         d d | j                  | j                  fz   }|j                  |      }|j	                  dddd      S )Nr   rF   r   r   )sizer_   rb   viewpermute)r2   r4   	new_shapes      r)   transpose_for_scoresz/GLPNEfficientSelfAttention.transpose_for_scores   sT    !&&("-1I1I4KcKc0dd	%**95$$Q1a00r+   c                    | j                  | j                  |            }| j                  dkD  r{|j                  \  }}}|j	                  ddd      j                  ||||      }| j                  |      }|j                  ||d      j	                  ddd      }| j                  |      }| j                  | j                  |            }	| j                  | j                  |            }
t        j                  ||	j                  dd            }|t        j                  | j                        z  }t         j"                  j%                  |d      }| j'                  |      }t        j                  ||
      }|j	                  dddd      j)                         }|j+                         d d | j,                  fz   }|j/                  |      }|r||f}|S |f}|S )Nr   r   rF   rq   dimr   )rv   re   rk   r    rt   reshaperl   rN   rf   rg   r"   matmulrT   mathsqrtrb   r   
functionalsoftmaxrj   
contiguousrr   rc   rs   )r2   r4   rX   rY   output_attentionsquery_layer
batch_sizeseq_lenrP   	key_layervalue_layerattention_scoresattention_probscontext_layernew_context_layer_shapeoutputss                   r)   r6   z"GLPNEfficientSelfAttention.forward   s    //

=0IJ==10=0C0C-J)11!Q:BB:|]cejkM GGM2M)11*lBOWWXY[\^_`M OOM:M--dhh}.EF	//

=0IJ !<<Y5H5HR5PQ+dii8P8P.QQ --//0@b/I ,,7_kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**+BC6G=/2 O\M]r+   F)r:   r;   r<   r=   r1   rv   r6   rA   rB   s   @r)   r\   r\   p   s    1861  *r+   r\   c                   $     e Zd Z fdZd Z xZS )GLPNSelfOutputc                     t         |           t        j                  ||      | _        t        j
                  |j                        | _        y r/   )r0   r1   r   rd   denserh   hidden_dropout_probrj   )r2   rn   rQ   r3   s      r)   r1   zGLPNSelfOutput.__init__   s6    YY{K8
zz&"<"<=r+   c                 J    | j                  |      }| j                  |      }|S r/   )r   rj   )r2   r4   input_tensors      r)   r6   zGLPNSelfOutput.forward   s$    

=1]3r+   r:   r;   r<   r1   r6   rA   rB   s   @r)   r   r      s    >
r+   r   c                   ,     e Zd Z fdZd ZddZ xZS )GLPNAttentionc                     t         |           t        ||||      | _        t	        ||      | _        t               | _        y )N)rn   rQ   r_   ro   )rQ   )r0   r1   r\   r2   r   r(   setpruned_headsrm   s        r)   r1   zGLPNAttention.__init__   s@    .# 3%=	
	 %VEEr+   c                 >   t        |      dk(  ry t        || j                  j                  | j                  j                  | j
                        \  }}t        | j                  j                  |      | j                  _        t        | j                  j                  |      | j                  _        t        | j                  j                  |      | j                  _	        t        | j                  j                  |d      | j                  _        | j                  j                  t        |      z
  | j                  _        | j                  j                  | j                  j                  z  | j                  _        | j
                  j                  |      | _        y )Nr   r   ry   )lenr   r2   r_   rb   r   r   re   rf   rg   r(   r   rc   union)r2   headsindexs      r)   prune_headszGLPNAttention.prune_heads   s   u:?749900$))2O2OQUQbQb
u
 -TYY__eD		*499==%@		,TYY__eD		.t{{/@/@%QO )-		(E(EE
(R		%"&))"?"?$))B_B_"_		 --33E:r+   c                 j    | j                  ||||      }| j                  |d   |      }|f|dd  z   }|S )Nr   r   )r2   r(   )r2   r4   rX   rY   r   self_outputsattention_outputr   s           r)   r6   zGLPNAttention.forward   sE    yy?PQ;;|AF#%QR(88r+   r   )r:   r;   r<   r1   r   r6   rA   rB   s   @r)   r   r      s    	";$r+   r   c                   &     e Zd Zd fd	Zd Z xZS )
GLPNDWConvc           	      d    t         |           t        j                  ||dddd|      | _        y )Nr   r   T)biasgroups)r0   r1   r   rK   dwconv)r2   rz   r3   s     r)   r1   zGLPNDWConv.__init__   s*    iiS!QSIr+   c                     |j                   \  }}}|j                  dd      j                  ||||      }| j                  |      }|j	                  d      j                  dd      }|S )Nr   rF   )r    rT   rs   r   rS   )r2   r4   rX   rY   r   r   rP   s          r)   r6   zGLPNDWConv.forward   sj    ,9,?,?)
G\%//15:::|U[]bcM2%--a0::1a@r+   )i   r   rB   s   @r)   r   r      s    Jr+   r   c                   &     e Zd Zd fd	Zd Z xZS )
GLPNMixFFNc                    t         |           |xs |}t        j                  ||      | _        t        |      | _        t        |j                  t              rt        |j                     | _        n|j                  | _        t        j                  ||      | _        t        j                  |j                        | _        y r/   )r0   r1   r   rd   dense1r   r   
isinstance
hidden_actr@   r	   intermediate_act_fndense2rh   r   rj   )r2   rn   in_featureshidden_featuresout_featuresr3   s        r)   r1   zGLPNMixFFN.__init__  s    #2{ii_= 1f''-'-f.?.?'@D$'-'8'8D$ii>zz&"<"<=r+   c                     | j                  |      }| j                  |||      }| j                  |      }| j                  |      }| j	                  |      }| j                  |      }|S r/   )r   r   r   rj   r   )r2   r4   rX   rY   s       r)   r6   zGLPNMixFFN.forward  sb    M2M65A00?]3M2]3r+   )NNr   rB   s   @r)   r   r     s    
>r+   r   c                   *     e Zd ZdZ fdZddZ xZS )	GLPNLayerzCThis corresponds to the Block class in the original implementation.c                 F   t         |           t        j                  |      | _        t        ||||      | _        |dkD  rt        |      nt        j                         | _	        t        j                  |      | _
        t        ||z        }t        |||      | _        y )N)rQ   r_   ro   r   )r   r   )r0   r1   r   rM   layer_norm_1r   	attentionr-   Identityr*   layer_norm_2ra   r   mlp)	r2   rn   rQ   r_   r*   ro   	mlp_ratiomlp_hidden_sizer3   s	           r)   r1   zGLPNLayer.__init__   s    LL5&# 3%=	
 5>Oi0LL5kI56f+_r+   c                 
   | j                  | j                  |      |||      }|d   }|dd  }| j                  |      }||z   }| j                  | j	                  |      ||      }| j                  |      }||z   }	|	f|z   }|S )N)r   r   r   )r   r   r*   r   r   )
r2   r4   rX   rY   r   self_attention_outputsr   r   
mlp_outputlayer_outputs
             r)   r6   zGLPNLayer.forward.  s    !%m,/	 "0 "
 2!4(,  >>*:;(=8XXd//>N
 ^^J/
!M1/G+r+   r   rZ   rB   s   @r)   r   r     s    M`r+   r   c                   ,     e Zd Z fdZ	 	 	 ddZ xZS )GLPNEncoderc                    t         
|           || _        t        j                  d|j
                  t        |j                              D cg c]  }|j                          }}g }t        |j                        D ]h  }|j                  t        |j                  |   |j                  |   |dk(  r|j                  n|j                   |dz
     |j                   |                j t#        j$                  |      | _        g }d}t        |j                        D ]  }g }|dk7  r||j                  |dz
     z  }t        |j                  |         D ]\  }	|j                  t)        ||j                   |   |j*                  |   |||	z      |j,                  |   |j.                  |                ^ |j                  t#        j$                  |              t#        j$                  |      | _        t#        j$                  t        |j                        D cg c]$  }t#        j2                  |j                   |         & c}      | _        y c c}w c c}w )Nr   r   )rO   rI   rP   rQ   )rQ   r_   r*   ro   r   )r0   r1   rn   r"   linspacedrop_path_ratesumdepthsitemrangenum_encoder_blocksappendrD   patch_sizesstridesrP   hidden_sizesr   
ModuleListpatch_embeddingsr   r_   	sr_ratios
mlp_ratiosblockrM   rN   )r2   rn   xdprrV   iblockscurlayersjr3   s             r)   r1   zGLPNEncoder.__init__I  s    "'63H3H#fmmJ\!]^Aqvvx^^ 
v001 	A*%11!4!>>!,89Q!4!4FDWDWXY\]X]D^ & 3 3A 6		 !#j 9 v001 	1AFAvv}}QU++6==+, 
$*$7$7$:,2,F,Fq,I"%cAg,171A1A!1D"("3"3A"6	
 MM"--/0!	1$ ]]6*
 --;@AZAZ;[\aR\\&--a01\
O _P ]s   I)Ic                 
   |rdnd }|rdnd }|j                   d   }|}t        t        | j                  | j                  | j
                              D ]  \  }	}
|
\  }}} ||      \  }}}t        |      D ]!  \  }} |||||      }|d   }|s||d   fz   }#  ||      }|j                  |||d      j                  dddd      j                         }|s||fz   } |st        d |||fD              S t        |||      S )	N r   r   rq   r   rF   c              3   &   K   | ]	  }||  y wr/   r   ).0vs     r)   	<genexpr>z&GLPNEncoder.forward.<locals>.<genexpr>  s     mq_`_lms   last_hidden_stater4   
attentions)r    	enumeratezipr   r   rN   r{   rt   r   tupler
   )r2   rU   r   output_hidden_statesreturn_dictall_hidden_statesall_self_attentionsr   r4   idxr   embedding_layerblock_layer
norm_layerrX   rY   r   blklayer_outputss                      r)   r6   zGLPNEncoder.forwardy  sL    #7BD$5b4!''*
$D$9$94::t WX 	IFC784O[*+:=+I(M65#K0 T3 #M65BS T -a 0$*=qAQ@S*S'	T '}5M)11*feRPXXYZ\]_`bcdooqM#$58H$H!	I" m]4EGZ$[mmm++*
 	
r+   )FFTr   rB   s   @r)   r   r   H  s    .
f  "$
r+   r   c                   &    e Zd ZdZeZdZdZg Zd Z	y)GLPNPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    glpnrU   c                 "   t        |t        j                  t        j                  f      rm|j                  j
                  j                  d| j                  j                         |j                  %|j                  j
                  j                          yyt        |t        j                        rz|j                  j
                  j                  d| j                  j                         |j                  2|j                  j
                  |j                     j                          yyt        |t        j                        rJ|j                  j
                  j                          |j                  j
                  j                  d       yy)zInitialize the weightsr   )meanstdNg      ?)r   r   rd   rK   weightdatanormal_rn   initializer_ranger   zero_	Embeddingpadding_idxrM   fill_)r2   modules     r)   _init_weightsz!GLPNPreTrainedModel._init_weights  s   fryy"))45 MM&&CT[[5R5R&S{{&  &&( '-MM&&CT[[5R5R&S!!-""6#5#56<<> .-KK""$MM$$S) .r+   N)
r:   r;   r<   r=   r   config_classbase_model_prefixmain_input_name_no_split_modulesr  r   r+   r)   r   r     s$    
 L$O*r+   r   aG  
    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.

    Parameters:
        config ([`GLPNConfig`]): 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.
aG  

    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 [`GLPNImageProcessor.__call__`] for details.

        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. 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.
zfThe bare GLPN encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top.c                        e Zd Z fdZd Z eej                  d             ee	e
ede      	 	 	 ddej                  dee   dee   d	ee   d
eee
f   f
d              Z xZS )	GLPNModelc                 r    t         |   |       || _        t        |      | _        | j                          y r/   )r0   r1   rn   r   encoder	post_initr2   rn   r3   s     r)   r1   zGLPNModel.__init__  s1      #6* 	r+   c                     |j                         D ]7  \  }}| j                  j                  |   j                  j	                  |       9 y)z
        Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
        class PreTrainedModel
        N)itemsr  layerr   r   )r2   heads_to_pruner  r   s       r)   _prune_headszGLPNModel._prune_heads  sE    
 +002 	CLE5LLu%//;;EB	Cr+   z(batch_size, sequence_length)vision)
checkpointoutput_typer  modalityexpected_outputrU   r   r   r   r   c                 ,   ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }| j	                  ||||      }|d   }|s	|f|dd  z   S t        ||j                  |j                        S )Nr   r   r   r   r   r   )rn   r   r   use_return_dictr  r
   r4   r   )r2   rU   r   r   r   encoder_outputssequence_outputs          r)   r6   zGLPNModel.forward  s      2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B],,/!5#	 ' 
 *!,#%(;;;-)77&11
 	
r+   )NNN)r:   r;   r<   r1   r  r   GLPN_INPUTS_DOCSTRINGr8   r   _CHECKPOINT_FOR_DOCr
   _CONFIG_FOR_DOC_EXPECTED_OUTPUT_SHAPEr"   FloatTensorr   boolr   r   r6   rA   rB   s   @r)   r
  r
    s    C ++@+G+GHg+hi&#$. -1/3&*
''
 $D>
 'tn	

 d^
 
uo%	&
 j
r+   r
  c                   *     e Zd ZdZd fd	Zd Z xZS )GLPNSelectiveFeatureFusionz
    Selective Feature Fusion module, as explained in the [paper](https://arxiv.org/abs/2201.07436) (section 3.4). This
    module adaptively selects and integrates local and global features by attaining an attention map for each feature.
    c           	      V   t         |           t        j                  t        j                  t        |dz        |ddd      t        j                  |      t        j                               | _        t        j                  t        j                  |t        |dz        ddd      t        j                  t        |dz              t        j                               | _	        t        j                  t        |dz        dddd      | _
        t        j                         | _        y )NrF   r   r   )in_channelsout_channelsrH   rI   rJ   )r0   r1   r   
SequentialrK   ra   BatchNorm2dReLUconvolutional_layer1convolutional_layer2convolutional_layer3Sigmoidsigmoid)r2   
in_channelr3   s     r)   r1   z#GLPNSelectiveFeatureFusion.__init__   s    $&MMII#j1n"5J\]fgqrsNN:&GGI%
! %'MMII*3zA~;N\]fgqrsNN3zA~./GGI%
! %'IIJN+!ST^_%
! zz|r+   c                 F   t        j                  ||fd      }| j                  |      }| j                  |      }| j	                  |      }| j                  |      }||d d dd d d d f   j                  d      z  ||d d dd d d d f   j                  d      z  z   }|S )Nr   ry   r   )r"   catr,  r-  r.  r0  	unsqueeze)r2   local_featuresglobal_featuresfeaturesattnhybrid_featuress         r)   r6   z"GLPNSelectiveFeatureFusion.forward5  s    99no>AF,,X6,,X6,,X6||H%(41a
+;+E+Ea+HH?]aq!QJ^

)A,L  r+   )@   rZ   rB   s   @r)   r%  r%    s    
$*r+   r%  c                   &     e Zd Z fdZddZ xZS )GLPNDecoderStagec                     t         |           ||k(  }|st        j                  ||d      nt        j                         | _        t        |      | _        t        j                  ddd      | _	        y )Nr   )rH   rF   bilinearFscale_factormodealign_corners)
r0   r1   r   rK   r   convolutionr%  fusionUpsampleupsample)r2   r'  r(  should_skipr3   s       r)   r1   zGLPNDecoderStage.__init__G  s\    !\1Va299[,ANgigrgrgt0>SXYr+   c                 r    | j                  |      }|| j                  ||      }| j                  |      }|S r/   )rC  rD  rF  )r2   hidden_stateresiduals      r)   r6   zGLPNDecoderStage.forwardN  s<    ''5;;|X>L}}\2r+   r/   r   rB   s   @r)   r<  r<  F  s    Z	r+   r<  c                   b     e Zd Z fdZdeej                     deej                     fdZ xZS )GLPNDecoderc           	      .   t         |           |j                  d d d   }|j                  }t	        j
                  |D cg c]  }t        ||       c}      | _        d | j                  d   _        t	        j                  ddd      | _
        y c c}w )Nrq   r   rF   r>  Fr?  )r0   r1   r   decoder_hidden_sizer   r   r<  stagesrD  rE  final_upsample)r2   rn   reserved_hidden_sizesr(  rQ   r3   s        r)   r1   zGLPNDecoder.__init__[  s     & 3 3DbD 911mmLab[k<8b
 !%A kkqzY^_ cs   Br4   r   c                     g }d }t        |d d d   | j                        D ]  \  }} |||      }|j                  |       ! | j                  |      |d<   |S )Nrq   )r   rO  r   rP  )r2   r4   stage_hidden_statesstage_hidden_staterI  stages         r)   r6   zGLPNDecoder.forwardi  sr     !#&}TrT':DKK#H 	;L%!&|5G!H&&'9:	; #'"5"56H"IB""r+   	r:   r;   r<   r1   r   r"   r?   r6   rA   rB   s   @r)   rL  rL  Z  s.    `	#T%,,%7 	#D<N 	#r+   rL  c                   *     e Zd ZdZd fd	Zd Z xZS )	SiLogLossz
    Implements the Scale-invariant log scale loss [Eigen et al., 2014](https://arxiv.org/abs/1406.2283).

    $$L=\frac{1}{n} \sum_{i} d_{i}^{2}-\frac{1}{2 n^{2}}\left(\sum_{i} d_{i}^{2}\right)$$ where $d_{i}=\log y_{i}-\log
    y_{i}^{*}$.

    c                 0    t         |           || _        y r/   )r0   r1   lambd)r2   rZ  r3   s     r)   r1   zSiLogLoss.__init__~  s    
r+   c                 `   |dkD  j                         }t        j                  ||         t        j                  ||         z
  }t        j                  t        j                  |d      j                         | j                  t        j                  |j                         d      z  z
        }|S )Nr   rF   )detachr"   logr~   powr   rZ  )r2   predtarget
valid_maskdiff_loglosss         r)   r6   zSiLogLoss.forward  s    qj((*
99VJ/0599T*=M3NNzz%))Ha0557$**uyyQYQ^Q^Q`bcGd:dder+   )g      ?rZ   rB   s   @r)   rX  rX  u  s    r+   rX  c                   \     e Zd Z fdZdeej                     dej                  fdZ xZS )GLPNDepthEstimationHeadc                    t         |           || _        |j                  }t	        j
                  t	        j                  ||ddd      t	        j                  d      t	        j                  |dddd            | _        y )Nr   r   rG   F)inplace)	r0   r1   rn   rN  r   r)  rK   r+  head)r2   rn   channelsr3   s      r)   r1   z GLPNDepthEstimationHead.__init__  sd    --MMIIha1MGGE"IIhqAF
	r+   r4   r   c                     || j                   j                     }| j                  |      }t        j                  |      | j                   j
                  z  }|j                  d      }|S )Nr   ry   )rn   head_in_indexrh  r"   r0  	max_depthsqueeze)r2   r4   predicted_depths      r)   r6   zGLPNDepthEstimationHead.forward  sZ    %dkk&?&?@		-0--69N9NN)11a18r+   rV  rB   s   @r)   re  re    s(    

	T%,,%7 	ELL 	r+   re  z]GLPN Model transformer with a lightweight depth estimation head on top e.g. for KITTI, NYUv2.c                        e Zd Z fdZ eej                  d             eee	      	 	 	 	 dde
j                  dee
j                     dee   dee   dee   d	eee
j                      ef   fd
              Z xZS )GLPNForDepthEstimationc                     t         |   |       t        |      | _        t	        |      | _        t        |      | _        | j                          y r/   )	r0   r1   r
  r   rL  decoderre  rh  r  r  s     r)   r1   zGLPNForDepthEstimation.__init__  s@     f%	"6*+F3	 	r+   zbatch_size, sequence_length)r  r  rU   labelsr   r   r   r   c                    ||n| j                   j                  }||n| j                   j                  }| j                  ||d|      }|r|j                  n|d   }| j                  |      }| j                  |      }	d}
|t               } ||	|      }
|s|r
|	f|dd z   }n	|	f|dd z   }|
|
f|z   S |S t        |
|	|r|j                  nd|j                        S )a  
        labels (`torch.FloatTensor` of shape `(batch_size, height, width)`, *optional*):
            Ground truth depth estimation maps for computing the loss.

        Returns:

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, GLPNForDepthEstimation
        >>> import torch
        >>> import numpy as np
        >>> from PIL import Image
        >>> import requests

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> image_processor = AutoImageProcessor.from_pretrained("vinvino02/glpn-kitti")
        >>> model = GLPNForDepthEstimation.from_pretrained("vinvino02/glpn-kitti")

        >>> # prepare image for the model
        >>> inputs = image_processor(images=image, return_tensors="pt")

        >>> with torch.no_grad():
        ...     outputs = model(**inputs)

        >>> # interpolate to original size
        >>> post_processed_output = image_processor.post_process_depth_estimation(
        ...     outputs,
        ...     target_sizes=[(image.height, image.width)],
        ... )

        >>> # visualize the prediction
        >>> predicted_depth = post_processed_output[0]["predicted_depth"]
        >>> depth = predicted_depth * 255 / predicted_depth.max()
        >>> depth = depth.detach().cpu().numpy()
        >>> depth = Image.fromarray(depth.astype("uint8"))
        ```NTr  r   rF   )rc  rn  r4   r   )
rn   r  r   r   r4   rr  rh  rX  r   r   )r2   rU   rs  r   r   r   r   r4   outrn  rc  loss_fctr(   s                r)   r6   zGLPNForDepthEstimation.forward  s   b &1%<k$++B]B]$8$D $++JjJj 	 ))/!%#	  
 2=--'!*ll=)))C. {HOV4D#)+gabk9)+gabk9)-)9TGf$EvE#+3G'//T))	
 	
r+   )NNNN)r:   r;   r<   r1   r   r  r8   r   r   r   r"   r"  r   r#  r   r   r?   r6   rA   rB   s   @r)   rp  rp    s    
 ++@+G+GHe+fg+?o^ /3,0/3&*Q
''Q
 **+Q
 $D>	Q

 'tnQ
 d^Q
 
uU\\"$88	9Q
 _ hQ
r+   rp  )r   F):r=   r}   typingr   r   r   r   r"   torch.utils.checkpointr   activationsr	   modeling_outputsr
   r   modeling_utilsr   pytorch_utilsr   r   utilsr   r   r   r   r   configuration_glpnr   
get_loggerr:   loggerr   r  r!  r?   r>   r#  r*   Moduler-   rD   r\   r   r   r   r   r   r   r   GLPN_START_DOCSTRINGr  r
  r%  r<  rL  rX  re  rp  r   r+   r)   <module>r     s     / /    ! E - Q  + 
		H	%  - ) U\\ e T V[VbVb *-299 -) )4N Nd	RYY 	#BII #N  0(		 (VU
")) U
p*/ *:	  $ l9
# 9
	9
x) )Xryy (#")) #6		 *bii 2 g^
0 ^
	^
r+   