
    j3                     <   U 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 d d	l	mZ d d
l	mZ d dlmZ ddlmZmZ g dZddd eed                   ddddd eed                   ddddd eed                   ddddd eed                   ddddd eed                   dddZeeeeef         f         ed<   dededededeej                 f
d Z  G d! d"ej!                  Z	 d3d#ed$e"dee         d%ee         d&ee         d'ee"         d(ee#eef         dz           d)ee         dz  d*edefd+Z$	 d3d#ed$e"d)ee         dz  d*edef
d,Z%d4d$e"d*edefd.Zd4d$e"d*edefd/Z&d4d$e"d*edefd0Zd4d$e"d*edefd1Z'd4d$e"d*edefd2ZdS )5    N)Callable)deepcopy)Any)nn)
BasicBlock)ResNet)resnet18)resnet34)resnet50)VOCABS   )conv_sequence_ptload_pretrained_params)r   r	   resnet31r
   r   resnet34_wideresnet_stage)gh|?5?g=
ףp=?gV-?)gA`"?gl?g$C?)r       r   frenchzKhttps://doctr-static.mindee.com/models?id=v0.4.1/resnet18-244bf390.pt&src=0)meanstdinput_shapeclassesurlzKhttps://doctr-static.mindee.com/models?id=v0.4.1/resnet31-1056cc5c.pt&src=0zKhttps://doctr-static.mindee.com/models?id=v0.5.0/resnet34-bd8725db.pt&src=0zKhttps://doctr-static.mindee.com/models?id=v0.5.0/resnet50-1a6c155e.pt&src=0zPhttps://doctr-static.mindee.com/models?id=v0.6.0/resnet34_wide-b4b3e39e.pt&src=0)r	   r   r
   r   r   default_cfgsin_channelsout_channels
num_blocksstridereturnc                     g }| }|}t          |          D ]U}d}||k    r!t          j        t          ||ddd|           }|                    t          ||||                     |}d}V|S )zBuild a ResNet stageNFT   )kernel_sizer   )r   
downsample)ranger   
Sequentialr   appendr   )	r   r   r   r   _layersin_chans_r#   s	            m/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/models/classification/resnet/pytorch.pyr   r   A   s    !GGA:  
l""(8,PUW[ijst(u(u(uvJz'<jYYYZZZN    c                        e Zd ZdZ	 	 	 	 	 	 ddee         dee         dee         d	ee         d
eeeef         dz           dededeege	j
        f         dz  dededeeef         dz  ddf fdZdededdfdZ xZS )r   a  Implements a ResNet-31 architecture from `"Show, Attend and Read:A Simple and Strong Baseline for Irregular
    Text Recognition" <https://arxiv.org/pdf/1811.00751.pdf>`_.

    Args:
        num_blocks: number of resnet block in each stage
        output_channels: number of channels in each stage
        stage_conv: whether to add a conv_sequence after each stage
        stage_pooling: pooling to add after each stage (if None, no pooling)
        origin_stem: whether to use the orginal ResNet stem or ResNet-31's
        stem_channels: number of output channels of the stem convolutions
        attn_module: attention module to use in each stage
        include_top: whether the classifier head should be instantiated
        num_classes: number of output classes
    T@   N  r   output_channelsstage_stride
stage_convstage_poolingorigin_stemstem_channelsattn_moduleinclude_topnum_classescfgr   c                    |r0g t          d|ddddd          t          j        ddd          }nFg t          d|dz  dddd          t          |dz  |dddd          t          j        d          }|g|d d	         z   }t          ||||||          D ]\  }}}}}}t	          ||||          }||                     ||                     |r(|                    t          ||dddd                     |'|                    t          j        |                     |                    t          j        |            |	rW|                    t          j        d          t          j	        d          t          j
        |d	         |
d
          g            t                      j        |  || _        |                                 D ]}t          |t          j                  r(t          j                            |j        dd           Dt          |t          j        t          j        f          rJt          j                            |j        d           t          j                            |j        d           d S )Nr   T      )r"   paddingr   r!   )r"   r   r=   )r"   r=   )biasfan_outrelu)modenonlinearityr   )r   r   	MaxPool2dzipr   r&   extendr%   AdaptiveAvgPool2dFlattenLinearsuper__init__r9   modules
isinstanceConv2dinitkaiming_normal_weightBatchNorm2d	GroupNorm	constant_r?   )selfr   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r'   in_chansn_blocksr(   out_chanr   convpool_stagem	__class__s                         r+   rK   zResNet.__init__d   s     
	!!]D$AWXabccc1a@@@GG
!!]a%7tQR\]^^^!-1"4mT4]^hijjj QG
 "?_SbS%99?B/<]@
 @
 
	3 
	3;Hgxt "'8XvFFF&kk(33444 j.x4[\fghhhiiibl400111NN2=&12222 	NN$Q''
1	/"-{FFF    	'"" 	- 	-A!RY'' -''yv'VVVVA=>> -!!!(A...!!!&!,,,	- 	-r,   path_or_urlkwargsc                 "    t          | |fi | dS zLoad pretrained parameters onto the model

        Args:
            path_or_url: the path or URL to the model parameters (checkpoint)
            **kwargs: additional arguments to be passed to `doctr.models.utils.load_pretrained_params`
        Nr   rU   r^   r_   s      r+   from_pretrainedzResNet.from_pretrained   "     	t[;;F;;;;;r,   )Tr.   NTr/   N)__name__
__module____qualname____doc__listintbooltupler   r   Moduledictstrr   rK   rd   __classcell__)r]   s   @r+   r   r   T   sB        , !9= %)6- 6-I6- c6- 3i	6-
 J6- E#s(Od236- 6- 6- seRY./$66- 6- 6- #s(^d"6- 
6- 6- 6- 6- 6- 6-p<3 <# <$ < < < < < < < <r,   r   arch
pretrainedr0   r1   r2   r3   ignore_keysr_   c                 (   |                     dt          t          |          d                             |d<   |                     dt          |          d                   |d<   t          t          |                    }	|d         |	d<   |d         |	d<   |                    d           t          |||||fd|	i|}
|rV|d         t          t          |          d                   k    r|nd }|
                    t          |          d         |           |
S )Nr8   r   r9   r   rt   )getlenr   r   popr   rd   )rr   rs   r   r0   r1   r2   r3   rt   r_   _cfgmodel_ignore_keyss               r+   _resnetr}      s    #JJ}c,t:LY:W6X6XYYF=

9l4.@.KLLF9L&''D /DY'DO
JJy :j-ll]alekllE S '-]&;s<PTCUV_C`?a?a&a&a{{gkl407\RRRLr,   c                    |                     dt          t          |          d                             |d<   |                     dt          |          d                   |d<   t          t          |                    }|d         |d<   |d         |d<   |                    d            |d
i |dd i}dt
          dt          dd fd}t          j        ||          |_	        |rV|d         t          t          |          d                   k    r|nd }|	                    t          |          d         |	           ||_
        |S )Nr8   r   weightsr^   r_   r   c                 "    t          | |fi | dS ra   rb   rc   s      r+   rd   z#_tv_resnet.<locals>.from_pretrained   re   r,   r   rv    )rw   rx   r   r   ry   rp   r   types
MethodTyperd   r9   )	rr   rs   arch_fnrt   r_   rz   r{   rd   r|   s	            r+   
_tv_resnetr      sZ    #JJ}c,t:LY:W6X6XYYF=

9l4.@.KLLF9L&''D /DY'DO
JJy G++f++d+++E<3 <# <$ < < < < ",_eDDE S '-]&;s<PTCUV_C`?a?a&a&a{{gkl407\RRREILr,   Fc                 2    t          d| t          fdddgi|S )a
  ResNet-18 architecture as described in `"Deep Residual Learning for Image Recognition",
    <https://arxiv.org/pdf/1512.03385.pdf>`_.

    >>> import torch
    >>> from doctr.models import resnet18
    >>> model = resnet18(pretrained=False)
    >>> input_tensor = torch.rand((1, 3, 512, 512), dtype=torch.float32)
    >>> out = model(input_tensor)

    Args:
        pretrained: boolean, True if model is pretrained
        **kwargs: keyword arguments of the ResNet architecture

    Returns:
        A resnet18 model
    r	   rt   	fc.weightfc.bias)r   tv_resnet18rs   r_   s     r+   r	   r	      =    "   !),	
   r,   c           
      L    t          d| g dg dg ddgdz  g dfdd	d
dgd|S )au  Resnet31 architecture with rectangular pooling windows as described in
    `"Show, Attend and Read:A Simple and Strong Baseline for Irregular Text Recognition",
    <https://arxiv.org/pdf/1811.00751.pdf>`_. Downsizing: (H, W) --> (H/8, W/4)

    >>> import torch
    >>> from doctr.models import resnet31
    >>> model = resnet31(pretrained=False)
    >>> input_tensor = torch.rand((1, 3, 512, 512), dtype=torch.float32)
    >>> out = model(input_tensor)

    Args:
        pretrained: boolean, True if model is pretrained
        **kwargs: keyword arguments of the ResNet architecture

    Returns:
        A resnet31 model
    r   )r!   r<      r   )   r      r   )r!   r!   r!   r!   T   ))r<   r<   )r<   r!   NNF   z	13.weightz13.biasr4   r5   rt   r}   r   s     r+   r   r   	  si    $ 	
$$$  ),    r,   c                 2    t          d| t          fdddgi|S )a
  ResNet-34 architecture as described in `"Deep Residual Learning for Image Recognition",
    <https://arxiv.org/pdf/1512.03385.pdf>`_.

    >>> import torch
    >>> from doctr.models import resnet34
    >>> model = resnet34(pretrained=False)
    >>> input_tensor = torch.rand((1, 3, 512, 512), dtype=torch.float32)
    >>> out = model(input_tensor)

    Args:
        pretrained: boolean, True if model is pretrained
        **kwargs: keyword arguments of the ResNet architecture

    Returns:
        A resnet34 model
    r
   rt   r   r   )r   tv_resnet34r   s     r+   r
   r
   *  r   r,   c           
      P    t          d| g dg dg ddgdz  dgdz  fdd	d
dgd|S )a<  ResNet-34 architecture as described in `"Deep Residual Learning for Image Recognition",
    <https://arxiv.org/pdf/1512.03385.pdf>`_ with twice as many output channels.

    >>> import torch
    >>> from doctr.models import resnet34_wide
    >>> model = resnet34_wide(pretrained=False)
    >>> input_tensor = torch.rand((1, 3, 512, 512), dtype=torch.float32)
    >>> out = model(input_tensor)

    Args:
        pretrained: boolean, True if model is pretrained
        **kwargs: keyword arguments of the ResNet architecture

    Returns:
        A resnet34_wide model
    r   )r   r      r   )r   r   r   i   )r!   r<   r<   r<   Fr   NTr   z	10.weightz10.biasr   r   r   s     r+   r   r   D  si    " 	!	
  ),    r,   c                 2    t          d| t          fdddgi|S )a
  ResNet-50 architecture as described in `"Deep Residual Learning for Image Recognition",
    <https://arxiv.org/pdf/1512.03385.pdf>`_.

    >>> import torch
    >>> from doctr.models import resnet50
    >>> model = resnet50(pretrained=False)
    >>> input_tensor = torch.rand((1, 3, 512, 512), dtype=torch.float32)
    >>> out = model(input_tensor)

    Args:
        pretrained: boolean, True if model is pretrained
        **kwargs: keyword arguments of the ResNet architecture

    Returns:
        A resnet50 model
    r   rt   r   r   )r   tv_resnet50r   s     r+   r   r   d  r   r,   )N)F)(r   collections.abcr   copyr   typingr   torchr   torchvision.models.resnetr   r   TVResNetr	   r   r
   r   r   r   doctr.datasetsr   utilsr   r   __all__rj   r   ro   rp   __annotations__rk   rn   r   r%   rl   rm   r}   r   r   r   r   r,   r+   <module>r      sS    $ $ $ $ $ $                   0 0 0 0 0 0 8 8 8 8 8 8 = = = = = = = = = = = = = = = = = = ! ! ! ! ! ! = = = = = = = =
e
e
e
 &$"4x())\  &$"4x())\  &$"4x())\  &$"4x())\  &$"4x())a ;$+ $+d3S#X&' $ $ $Nc  # s W[\^\eWf    &O< O< O< O< O<R] O< O< O<t %) 
 S	 #Y	
 s) T
 c3h$./ cT!     F %)	' '
'' cT!	'
 ' ' ' ' 'T       4       B       4 d c f    @         r,   