
    Z jU                     d   S SK Jr  S SKrS SKJr  S SKJs  Jr  S SKJ	r	  SSK
Jr  SSKJrJrJrJr  SSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJrJr  SSKJr  SSKJ r J!r!  SSK"J#r#  SSK$J%r%  SSK&J'r'J(r(J)r)  SSK*J+r+J,r,  SSK-J.r.  SSK/J0r0  SSK1J2r2  SSK3J4r4  SSK5J6r6  \(" SS9\	 " S S\\5      5       5       r7\(" SS9\	 " S S\5      5       5       r8\(\." SS9 " S  S!\5      5       5       r9 " S" S#\45      r: " S$ S%\Rv                  5      r< " S& S'\Rv                  5      r= " S( S)\Rv                  5      r> " S* S+\5      r? " S, S-\Rv                  5      r@\( " S. S/\65      5       rA " S0 S1\A5      rB\(" S2S39 " S4 S5\\A5      5       rC " S6 S7\Rv                  5      rD\(" S8S39 " S9 S:\A5      5       rE/ S;QrFg)<    )SequenceN)strict   )ACT2FN)BackboneConfigMixinBackboneMixin%consolidate_backbone_kwargs_to_configfilter_output_hidden_states)PreTrainedConfig)BatchFeature)TorchvisionBackend)group_images_by_shapereorder_images)PILImageResamplingSizeDict)GradientCheckpointingLayer)BackboneOutputBaseModelOutputWithNoAttention)PreTrainedModel)Unpack)TransformersKwargsauto_docstringcan_return_tuple)
TensorTypemerge_with_config_defaults)requires)capture_outputs   )
AutoConfig)PPLCNetConvLayer)PPOCRV5ServerDetPreTrainedModelzPaddlePaddle/UVDoc_safetensors)
checkpointc                     ^  \ rS rSr% SrSrSr\\   S-  \	S'   Sr
\\   S-  \	S'   Sr\\\   \\S4   -     \	S	'   S
r\\\\\\\4   \\\-     -        \	S'   Sr\\\\S4   \\   -        \	S'   Sr\\	S'   U 4S jrSrU =r$ )UVDocBackboneConfig0   al  
resnet_head (`Sequence[list[int] | tuple[int, ...]]`, *optional*, defaults to `((3, 32), (32, 32))`):
    Configuration for the ResNet head layers in format [in_channels, out_channels].
resnet_configs (`Sequence[Sequence[tuple[int, int, int, bool] | list[int | bool]]]`, *optional*, defaults to `(((32, 32, 1, False),
    (32, 32, 3, False), (32, 32, 3, False)), ((32, 64, 1, True), (64, 64, 3, False), (64, 64, 3, False), (64, 64, 3, False)), ((64, 128, 1, True),
    (128, 128, 3, False), (128, 128, 3, False), (128, 128, 3, False), (128, 128, 3, False), (128, 128, 3, False)))`):
    Configuration for the ResNet stages in format [in_channels, out_channels, dilation_value, downsample].
stage_configs (Sequence[Sequence[tuple[int, ...] | list[int]]], *optional*, defaults to `(((128, 1),), ((128, 2),),
    ((128, 5),), ((128, 8),(128, 3),(128, 2),), ((128, 12), (128, 7), (128, 4),), ((128, 18), (128, 12), (128, 6),),)`):
    Configuration for the bridge module stages in format [in_channels, dilation_value].
    Each inner sequence corresponds to a single bridge block, and the outer sequence groups blocks by bridge stage.
uvdoc_backboneN_out_features_out_indices))r       )r)   r)   .resnet_head)))r)   r)      Fr)   r)   r   Fr,   ))r)   @   r+   Tr-   r-   r   Fr.   r.   ))r-      r+   Tr/   r/   r   Fr0   r0   r0   r0   resnet_configs)))r/   r+   )r/   r   ))r/      ))r/      )r/   r   r2   )r/      )r/      )r/      ))r/      r5   )r/      stage_configsr3   kernel_sizec                 n  > U R                    Vs/ s H  n[        U5      PM     snU l        S/[        S[        U R                   5      S-   5       Vs/ s H  nSU 3PM
     sn-   U l        U R                  UR                  SS 5      UR                  SS 5      S9  [        TU ]   " S0 UD6  g s  snf s  snf )Nstemr+   stageout_indicesout_features)r@   rA    )	r;   lendepthsrangestage_names"set_output_features_output_indicespopsuper__post_init__)selfkwargsstagesidx	__class__s       x/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/uvdoc/modular_uvdoc.pyrJ   !UVDocBackboneConfig.__post_init__w   s    151C1CD1Cvs6{1CD"8aTM_M_I`cdId@e&f@ese}@e&ff//

=$7fjjQ_aeFf 	0 	
 	'' E&fs   B-B2)rD   rF   )__name__
__module____qualname____firstlineno____doc__
model_typer'   liststr__annotations__r(   intr*   r   tupler1   boolr;   r<   rJ   __static_attributes____classcell__rO   s   @rP   r$   r$   0   s     "J&*M49t#*%)L$s)d"):K$s)eCHo56 
YNHXeCc4,?&@4d
CS&STU ,FM8HU38_tCy%@AB * K( (    r$   c                      ^  \ rS rSr% SrSrS\0rSr\	\
-  S-  \S'   Sr\\S'   Sr\\S	'   S
r\\S'   Sr\\   \\S4   -  \S'   Sr\\\   \\S4   -     \S'   U 4S jrSrU =r$ )UVDocConfigr/   a  
padding_mode (`str`, *optional*, defaults to `"reflect"`):
    Padding mode for convolutional layers. Supported modes are `"reflect"`, `"constant"`, and `"replicate"`.
kernel_size (`int`, *optional*, defaults to 5):
    Kernel size for convolutional layers in the backbone network.
bridge_connector (`list[int] | tuple[int, ...]`, *optional*, defaults to `(128, 128)`):
    Configuration for the bridge connector in format [in_channels, out_channels].
out_point_positions2D (`Sequence[list[int] | tuple[int, ...]]`, *optional*, defaults to `((128, 32), (32, 2))`):
    Configuration for the output point positions 2D layer in format [in_channels, out_channels].
uvdocbackbone_configNprelu
hidden_actreflectpadding_moder3   r<   )r/   r/   .bridge_connector))r/   r)   )r)   r   out_point_positions2Dc                 f   > [        SU R                  SS.UD6u  U l        n[        TU ]  " S0 UD6  g )Nr&   )re   default_config_typerB   )r	   re   rI   rJ   )rK   rL   rO   s     rP   rJ   UVDocConfig.__post_init__   sB    'L (
 00 0(
 (
$f
 	''ra   )re   )rR   rS   rT   rU   rV   rW   r   sub_configsre   dictr   rZ   rg   rY   ri   r<   r[   rj   rX   r\   rk   r   rJ   r^   r_   r`   s   @rP   rc   rc      s    	 J$j1K6:OT,,t3:J!L#!K4>d3i%S/1>CW8DIc3h$?@W( (ra   rc   )torch)backendsc                   "   \ rS rSrSrSrSSS.r\R                  r	S\
S   S\S	\S
\S\S\S\\
\   -  S-  S\\
\   -  S-  S\S-  S\\-  S-  S\4S jr SS\R&                  S\
\R&                     S\S\
\\\R&                  4      4S jjrSrg)UVDocImageProcessor   Ti    )heightwidthimagesztorch.Tensor	do_resizesize
do_rescalerescale_factordo_normalize
image_meanN	image_stddisable_groupingreturn_tensorsreturnc           	         [        XS9u  p0 nUR                  5        H/  u  nnU R                  UXEXgU5      nUS S 2/ SQS S 2S S 24   nUX'   M1     [        X5      nUR	                  5       n[        UU	S9u  p0 nUR                  5        H?  u  nnU(       a-  [
        R                  " UUR                  UR                  4SSS9nUUU'   MA     [        UU5      n[        UUS.U
S/S9$ )	N)r   )r   r+   r   bilinearTr{   modealign_corners)pixel_valuesoriginal_imagesr   )datatensor_typeskip_tensor_conversion)
r   itemsrescale_and_normalizer   copyFinterpolaterw   rx   r   )rK   ry   rz   r{   r|   r}   r~   r   r   r   r   rL   grouped_imagesgrouped_images_indexprocessed_images_groupedshapestacked_imagesrescale_and_normalize_imagesr   interpolated_images_groupedr   s                        rP   _preprocessUVDocImageProcessor._preprocess   s    0EV/o,#% %3%9%9%;!E>!77
LV_N ,Ay!Q,>?N.<$+ &< (66N'e$6;;=/D(;K0
, ')#%3%9%9%;!E>!""$++tzz)Bcg" 2@'. &< &&ACWX".?S&$5#6
 	
ra   
predictionr   scalec                    [        U5      n[        R                  " [        U5      UR                  S9n/ n[        U5       H  u  pgUR                  S:X  a  UR                  S5      nUR                  UR                  5      nUR                  SS u  p[        R                  " XUS-    X4SSS	9n
U
R                  SSSS5      n[        R                  " X{SS
9nUR                  S5      R                  SSS5      nX-  nUR                  S/S9R                  [        R                   SSS9nUR#                  SU05        GM     U$ )aN  
Post-process document rectification predictions to convert them into rectified images.

Args:
    prediction: Predicted 2D Bezier mesh coordinates, shape (B, 2, H, W)
    original_images: List of original input tensors, each of shape (C, H_i, W_i). Images may have different sizes.
    scale: Scaling factor for output images (default: 255.0)

Returns:
    List of dictionaries containing rectified images. Each dictionary has:
        - "images": Rectified image tensor of shape (H, W, 3) with dtype torch.uint8
                  and BGR channel order (suitable for OpenCV visualization)
)devicer   r   r   Nr+   r   Tr   )r   )dimsF)dtypenon_blockingr   ry   )rX   rq   tensorfloatr   	enumeratendim	unsqueezetor   r   r   permutegrid_samplesqueezeflipuint8append)rK   r   r   r   
image_listresultsioriginal_imageoriginal_heightoriginal_widthupsampled_meshrearranged_mesh	rectifiedimages                 rP   #post_process_document_rectification7UVDocImageProcessor.post_process_document_rectification   sF   & /*
U5\*2C2CD!*:!6A""a'!/!9!9!!<+..z/@/@AN.<.B.B12.F+O ]]q1u%%6"	N -44Q1a@O nUYZI %%a(00Aq9E MEJJRDJ),,5;;TX],^ENNHe,-9 "7< ra   rB   )g     o@)rR   rS   rT   rU   r|   rz   r{   r   BILINEARresamplerX   r]   r   r   rY   r   r   r   rq   Tensorrp   r   r^   rB   ra   rP   rt   rt      s    JIC(D!**H/
^$/
 /
 	/

 /
 /
 /
 DK'$./
 4;&-/
 +/
 j(4//
 
/
j 	5LL5 ell+5 	5
 
d3$%	&5 5ra   rt   c                   f   ^  \ rS rSrSr       SS\S\S\S\S\S\S	\S
\S\4U 4S jjjrSr	U =r
$ )UVDocConvLayeri  z<Convolutional layer with batch normalization and activation.in_channelsout_channelsr<   stridepaddingri   biasdilation
activationc
                 b   > [         T
U ]  5         [        R                  " UUUUUUUUS9U l        g )N)r   r<   r   r   ri   r   )rI   __init__nnConv2dconvolution)rK   r   r   r<   r   r   ri   r   r   r   rO   s             rP   r   UVDocConvLayer.__init__  s9     	99#%	
ra   )r   )r   r+   r   zerosFr+   relu)rR   rS   rT   rU   rV   r[   rY   r]   r   r^   r_   r`   s   @rP   r   r     s    F # 

 
 	

 
 
 
 
 
 
 
ra   r   c                      ^  \ rS rSrSr     SS\S\S\S\S\S\S	\S
\4U 4S jjjrS\	R                  S\	R                  4S jrSrU =r$ )UVDocResidualBlocki/  z*Base residual block with dilation support.r   r   r<   r   r   r   
downsampler   c	                 2  > [         T	U ]  5         U(       a  [        UUUUUS-  SS S9O[        R                  " 5       U l        [        UUUUUUSS9U l        [        UUUSUSUS S9U l        Ub  [        U   U l	        g [        R                  " 5       U l	        g )Nr   T)r   r   r<   r   r   r   r   )r   r   r<   r   r   r   r   r+   )r   r   r<   r   r   r   r   r   )
rI   r   r   r   Identity	conv_down
conv_start
conv_finalr   act_fn)
rK   r   r   r<   r   r   r   r   r   rO   s
            rP   r   UVDocResidualBlock.__init__2  s     	  ')'#q(  	 )#%#
 )$%#	
 -7,BfZ(ra   hidden_statesr   c                     U R                  U5      nU R                  U5      nU R                  U5      nX-   nU R                  U5      nU$ N)r   r   r   r   )rK   r   residuals      rP   forwardUVDocResidualBlock.forwardd  sH    >>-066%0M2ra   )r   r   r   r   )r+   r   r+   Fr   )rR   rS   rT   rU   rV   r[   r]   rY   r   rq   r   r   r^   r_   r`   s   @rP   r   r   /  s    4   0V0V 0V 	0V
 0V 0V 0V 0V 0V 0VdU\\ ell  ra   r   c                   f   ^  \ rS rSrSrU 4S jrS\R                  S\R                  4S jrSr	U =r
$ )UVDocResNetStageim  z3A ResNet stage containing multiple residual blocks.c                 
  > [         TU ]  5         UR                  U   n[        R                  " / 5      U l        U HD  u  pEpgU R
                  R                  [        UUU(       a  SOSUS-  UUUR                  S95        MF     g )Nr   r+   )r   r   r   r   r   r   r<   )	rI   r   r1   r   
ModuleListlayersr   r   r<   )	rK   configstage_indexrM   r   r   r   r   rO   s	           rP   r   UVDocResNetStage.__init__p  s|    &&{3mmB'?E;KxKK" +!- *1$qL%) & 2 2
 @Fra   r   r   c                 <    U R                    H  nU" U5      nM     U$ r   r   )rK   r   layers      rP   r   UVDocResNetStage.forward  s     [[E!-0M !ra   r   rR   rS   rT   rU   rV   r   rq   r   r   r^   r_   r`   s   @rP   r   r   m  s+    =$U\\ ell  ra   r   c                   f   ^  \ rS rSrSrU 4S jrS\R                  S\R                  4S jrSr	U =r
$ )UVDocResNeti  z$Initial resnet_head and resnet_down.c                 (  > [         TU ]  5         [        R                  " / 5      U l        [        [        UR                  5      5       H`  nU R                  R                  [        UR                  U   S   UR                  U   S   UR                  SUR                  S-  S95        Mb     [        R                  " / 5      U l
        [        [        UR                  5      5       H)  n[        X5      nU R                  R                  U5        M+     g )Nr   r+   r   )r   r   r<   r   r   )rI   r   r   r   r*   rE   rC   r   r   r<   resnet_downr1   r   )rK   r   r   r   r?   rO   s        rP   r   UVDocResNet.__init__  s    ==,s6--./A## & 2 21 5a 8!'!3!3A!6q!9 & 2 2"..!3 0 ==, V%:%:!;<K$V9E##E* =ra   r   r   c                 r    U R                    H  nU" U5      nM     U R                   H  nU" U5      nM     U$ r   )r*   r   )rK   r   headr?   s       rP   r   UVDocResNet.forward  s<    $$D /M %%%E!-0M &ra   )r   r*   r   r`   s   @rP   r   r     s+    .+&U\\ ell  ra   r   c                   p   ^  \ rS rSrSrU 4S jrS\R                  S\\	   S\R                  4S jr
SrU =r$ )	UVDocBridgeBlocki  zDBridge module with dilated convolutions for long-range dependencies.c           
         > [         TU ]  5         [        R                  " / 5      U l        UR
                  U   nU H(  u  pEU R                  R                  [        XDXUS95        M*     g )N)r   r   )rI   r   r   r   blocksr;   r   r   )rK   r   bridge_indexbridger   r   rO   s         rP   r   UVDocBridgeBlock.__init__  sU    mmB'%%l3%+!KKK~kPXlm &,ra   r   rL   r   c                 <    U R                    H  nU" U5      nM     U$ r   r   )rK   r   rL   blocks       rP   r   UVDocBridgeBlock.forward  s"    
 [[E!-0M !ra   r   )rR   rS   rT   rU   rV   r   rq   r   r   r   r   r^   r_   r`   s   @rP   r   r     s@    Nn|| +, 
	 ra   r   c                   f   ^  \ rS rSrSrU 4S jrS\R                  S\R                  4S jrSr	U =r
$ )UVDocPointPositions2Di  zDModule for predicting 2D point positions for document rectification.c           
        > [         TU ]  5         [        UR                  S   S   UR                  S   S   UR                  SUR                  S-  UR
                  UR                  S9U l        [        R                  " UR                  S   S   UR                  S   S   UR                  SUR                  S-  UR
                  S9U l
        g )Nr   r+   r   )r   r   r<   r   r   ri   r   )r   r   r<   r   r   ri   )rI   r   r   rk   r<   ri   rg   r   r   r   conv_uprK   r   rO   s     rP   r   UVDocPointPositions2D.__init__  s    '44Q7:55a8;**&&!+,,((
 yy44Q7:55a8;**&&!+,,
ra   r   r   c                 J    U R                  U5      nU R                  U5      nU$ r   r   r  )rK   r   s     rP   r   UVDocPointPositions2D.forward  s$    }5]3ra   r  r   r`   s   @rP   r  r    s+    N
,U\\ ell  ra   r  c                   N    \ rS rSrSrS\0r\R                  " 5       S 5       r	Sr
g)UVDocPreTrainedModeli  Tr   c                     [         R                  " U5         [        U[        R                  5      (       a  UR                  5         g g r   )r   _init_weights
isinstancer   PReLUreset_parameters)rK   modules     rP   r  "UVDocPreTrainedModel._init_weights  s5    %%f-%fbhh''##% (ra   rB   N)rR   rS   rT   rU   supports_gradient_checkpointingr   _can_record_outputsrq   no_gradr  r^   rB   ra   rP   r
  r
    s0    &*#) ]]_& &ra   r
  c                      ^  \ rS rSrU 4S jr\\S\R                  S\	\
   S\R                  4S j5       5       rSrU =r$ )UVDocBridgerv   c                   > [         TU ]  U5        [        R                  " / 5      U l        [        [        UR                  5      5       H'  nU R                  R                  [        X5      5        M)     U R                  5         g r   )rI   r   r   r   r   rE   rC   r;   r   r   	post_init)rK   r   r   rO   s      rP   r   UVDocBridge.__init__  s\     mmB'!#f&:&:";<LKK/EF =ra   r   rL   r   c                 J    U R                    H  nU" U5      nM     [        WS9$ )N)last_hidden_state)r   r   )rK   r   rL   r   features        rP   r   UVDocBridge.forward  s'     [[EM*G !-HHra   )r   )rR   rS   rT   rU   r   r   r   rq   r   r   r   r   r^   r_   r`   s   @rP   r  r    sS      I||I +,I 
	I   Ira   r  z6
    UVDoc backbone model for feature extraction.
    )custom_introc            	          ^  \ rS rSrSrSrS\4U 4S jjr\\	\
S\R                  S\\   S\4S	 j5       5       5       rS
rU =r$ )UVDocBackbonei  Fbackboner   c                 
  > [         TU ]  U5        UR                  S   S   /nUR                   H  nUR	                  US   S   5        M     X l        [        U5      U l        [        U5      U l	        U R                  5         g )Nr   r   r+   )rI   r   r*   r;   r   num_featuresr   resnetr  r   r  )rK   r   r#  r?   rO   s       rP   r   UVDocBackbone.__init__  sv     **2.r23))Ea, *(!&)!&)ra   r   rL   r   c                    SUS'   U R                  U5      nU R                  " U40 UD6nSn[        U R                  5       H(  u  pgXpR                  ;   d  M  XTR
                  U   4-  nM*     [        UUR
                  S9$ )NToutput_hidden_statesrB   )feature_mapsr   )r$  r   r   rF   rA   r   r   )rK   r   rL   r   outputsr(  rN   r?   s           rP   r   UVDocBackbone.forward  s     *.%&L1++m6v6#D$4$45JC)))!6!6s!; == 6 %!//
 	
ra   )r   r#  r$  )rR   rS   rT   rU   has_attentionsbase_model_prefixr$   r   r   r
   r   rq   FloatTensorr   r   r   r   r^   r_   r`   s   @rP   r   r     si     N"2   
''
 +,
 
	
  ! 
ra   r   c                      ^  \ rS rSrU 4S jrS\R                  S\\   S\R
                  R                  4S jr	Sr
U =r$ )	UVDocHeadi)  c           	         > [         TU ]  5         [        UR                  R                  5      U l        [        UR                  S   U R
                  -  UR                  S   SSSSS9U l        [        U5      U l	        g )Nr   r+   )r   r   r<   r   r   r   )
rI   r   rC   re   r;   num_bridge_layersr   rj   r  rk   r  s     rP   r   UVDocHead.__init__*  sv    !$V%;%;%I%I!J .//2T5K5KK003!
 &;6%B"ra   r   rL   r   c                 J    U R                  U5      nU R                  U5      nU$ r   )rj   rk   )rK   r   rL   s      rP   r   UVDocHead.forward9  s*    
 --m<22=Ara   )rj   r1  rk   )rR   rS   rT   rU   r   rq   r   r   r   r   r^   r_   r`   s   @rP   r/  r/  )  sC    C|| +, 
			 ra   r/  z
    The model takes raw document images (pixel values) as input, processes them through the UVDoc backbone to predict spatial transformation parameters,
    and outputs the rectified (corrected) document image tensor.
    c            	          ^  \ rS rSrS\4U 4S jjr\\S\R                  S\
\   S\\R                     \-  4S j5       5       rSrU =r$ )	
UVDocModeliC  r   c                    > [         TU ]  U5        [        UR                  5      U l        [        U5      U l        U R                  5         g r   )rI   r   r   re   r!  r/  r   r  r  s     rP   r   UVDocModel.__init__J  s8     %f&<&<=f%	ra   r   rL   r   c                     U R                   " U40 UD6n[        R                  " UR                  SS9nU R                  " U40 UD6n[        UUR                  S9$ )Nr+   )dim)r  r   )r!  rq   catr(  r   r   r   )rK   r   rL   backbone_outputsfused_outputsr  s         rP   r   UVDocModel.forwardQ  s\      ==@@		"2"?"?QG IIm>v>-/*88
 	
ra   )r!  r   )rR   rS   rT   rU   rc   r   r   r   rq   r-  r   r   r\   r   r   r^   r_   r`   s   @rP   r6  r6  C  sd    {  
''
 +,
 
u  	!$B	B	
  
ra   r6  )r  r   r$   rt   rc   r6  r
  )Gcollections.abcr   rq   torch.nnr   torch.nn.functional
functionalr   huggingface_hub.dataclassesr   activationsr   backbone_utilsr   r   r	   r
   configuration_utilsr   feature_extraction_utilsr   image_processing_backendsr   image_transformsr   r   image_utilsr   r   modeling_layersr   modeling_outputsr   r   modeling_utilsr   processing_utilsr   utilsr   r   r   utils.genericr   r   utils.import_utilsr   utils.output_capturingr   autor   pp_lcnet.modeling_pp_lcnetr    0pp_ocrv5_server_det.modeling_pp_ocrv5_server_detr!   r$   rc   rt   r   Moduler   r   r   r   r  r
  r  r   r/  r6  __all__rB   ra   rP   <module>rX     s    %     . !  4 4 ; E 7 9 N - & I I C * 5  9 ^ ;<K(-/? K(  =K(\ ;<(" (  =(> 	:l, l  l^
% 
:; ;|ryy 6")) <1 (BII > &: & &I& I( 
%
M#7 %

%
P		 4 
% 

2ra   