
    j(                        U 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Zeeeeef         f         ed<    G d dej                  Z	 ddededee         dz  dedef
dZd dededefdZd dededefdZd dededefdZdS )!    )deepcopy)Any)nn)VOCABS   FASTConvLayer)conv_sequence_ptload_pretrained_params)textnet_tinytextnet_smalltextnet_base)gh|?5?g=
ףp=?gV-?)gA`"?gl?g$C?r       r   frenchzOhttps://doctr-static.mindee.com/models?id=v0.8.1/textnet_tiny-27288d12.pt&src=0)meanstdinput_shapeclassesurlzPhttps://doctr-static.mindee.com/models?id=v0.8.1/textnet_small-43166ee6.pt&src=0zOhttps://doctr-static.mindee.com/models?id=v0.8.1/textnet_base-7f68d7e0.pt&src=0default_cfgsc                        e Zd ZdZ	 	 	 	 ddeeeee         f                  deeeef         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 )TextNeta  Implements TextNet architecture from `"FAST: Faster Arbitrarily-Shaped Text Detector with
    Minimalist Kernel Representation" <https://arxiv.org/abs/2111.02394>`_.
    Implementation based on the official Pytorch implementation: <https://github.com/czczup/FAST>`_.

    Args:
        stages (list[dict[str, list[int]]]): list of dictionaries containing the parameters of each stage.
        include_top (bool, optional): Whether to include the classifier head. Defaults to True.
        num_classes (int, optional): Number of output classes. Defaults to 1000.
        cfg (dict[str, Any], optional): Additional configuration. Defaults to None.
    r     TNstagesr   num_classesinclude_topcfgreturnc           
         g t          ddddddd          d |D             }|rr|                    t          j        t          j        d          t          j        d          t          j        |d	         d
         d	         |                                t                      j        |  || _	        | 
                                D ]}t          |t          j                  r(t          j                            |j        dd           Dt          |t          j                  rJt          j                            |j        d           t          j                            |j        d           d S )Nr   @   T   )   r#   )in_channelsout_channelsrelubnkernel_sizestridepaddingc                     g | ]Ft          j        d  fdt          t          d                             D             D              GS )c                 &    g | ]}t          d i |S ) r   ).0paramss     n/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/models/classification/textnet/pytorch.py
<listcomp>z/TextNet.__init__.<locals>.<listcomp>.<listcomp>E   s6           "++F++         c                 0    g | ]fd D             S )c                 .    i | ]}||                  S r-   r-   )r.   keyistages     r0   
<dictcomp>z:TextNet.__init__.<locals>.<listcomp>.<listcomp>.<dictcomp>G   s#    #H#H#H3CsA#H#H#Hr2   r-   )r.   r6   r7   s    @r0   r1   z/TextNet.__init__.<locals>.<listcomp>.<listcomp>G   s2    "s"s"sQ#H#H#H#H#H%#H#H#H"s"s"sr2   r$   )r   
Sequentialrangelen)r.   r7   s    @r0   r1   z$TextNet.__init__.<locals>.<listcomp>D   s{       
 	     "s"s"s"sRWX[\abo\pXqXqRrRr"s"s"s        r2   r#   r%   fan_outr&   )modenonlinearityr   )r
   appendr   r9   AdaptiveAvgPool2dFlattenLinearsuper__init__r   modules
isinstanceConv2dinitkaiming_normal_weightBatchNorm2d	constant_bias)	selfr   r   r   r   r   _layersm	__class__s	           r0   rE   zTextNet.__init__8   sx   $
BTdPQZ[ek  $
 
 $  	$
  	NN(++JqMMIfRj8<kJJ    	'"" 	- 	-A!RY'' -''yv'VVVVAr~.. -!!!(A...!!!&!,,,	- 	-r2   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   )rO   rS   rT   s      r0   from_pretrainedzTextNet.from_pretrained`   s"     	t[;;F;;;;;r2   )r   r   TN)__name__
__module____qualname____doc__listdictstrinttupleboolr   rE   rV   __classcell__)rR   s   @r0   r   r   ,   s        	 	 -8 %)&- &-T#tCy.)*&- 3S=)&- 	&-
 &- #s(^d"&- 
&- &- &- &- &- &-P<3 <# <$ < < < < < < < <r2   r   Narch
pretrainedignore_keysrT   r   c                 (   |                     dt          t          |          d                             |d<   |                     dt          |          d                   |d<   t          t          |                    }|d         |d<   |d         |d<   |                    d           t          di |}|rV|d         t          t          |          d                   k    r|nd }|                    t          |          d         |           ||_        |S )Nr   r   r   )rd   r-   )getr;   r   r   popr   rV   r   )rb   rc   rd   rT   _cfgmodel_ignore_keyss          r0   _textnetrk   j   s    #JJ}c,t:LY:W6X6XYYF=

9l4.@.KLLF9L&''D /DY'DO
JJy fE S '-]&;s<PTCUV_C`?a?a&a&a{{gkl407\RRREILr2   Fc                     t          d| fdgdz  dgdz  dgdz  g ddg ddgd	z  g d
g ddg ddgd	z  g dg ddg ddgd	z  g dg ddgddgd|S )a  Implements TextNet architecture from `"FAST: Faster Arbitrarily-Shaped Text Detector with
    Minimalist Kernel Representation" <https://arxiv.org/abs/2111.02394>`_.
    Implementation based on the official Pytorch implementation: <https://github.com/czczup/FAST>`_.

    >>> import torch
    >>> from doctr.models import textnet_tiny
    >>> model = textnet_tiny(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 TextNet architecture

    Returns:
        A textnet tiny model
    r   r!   r   r   r   )r#   r"   r#   r$   r%   r(   r)   )r!      ro   ro   ro      )rm   r#   r   rm   r   r#   )r"   r#   r#   r#   )ro      rs   rs   rs   )rm   rm   rr   rq   )rs      rt   rt   rt   )rm   rr   rq   rm   
7.2.weight7.2.biasr   rd   rk   rc   rT   s     r0   r   r      s    $   D1HrdQhPVxZ[|gpgpgpqq222!$	???&,,	   433!$	???&,,	   433!$	???&,,	 
* ":.1 2 3  r2   c                     t          d| fdgdz  dgdz  dgdz  ddgdg ddgd	z  g d
g ddg ddgd	z  g dg ddg ddgdz  g dg ddgddgd|S )a  Implements TextNet architecture from `"FAST: Faster Arbitrarily-Shaped Text Detector with
    Minimalist Kernel Representation" <https://arxiv.org/abs/2111.02394>`_.
    Implementation based on the official Pytorch implementation: <https://github.com/czczup/FAST>`_.

    >>> import torch
    >>> from doctr.models import textnet_small
    >>> model = textnet_small(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 TextNet architecture

    Returns:
        A TextNet small model
    r   r!   r"   rm   r#   rn   )r!   ro   ro   ro   ro   ro   ro   ro   ro      )rm   rq   rm   rr   rm   rr   rq   rm   r"   r#   r#   r#   r#   r#   r#   r#   ro   rs   rs   rs   rs   rs   rs   rs   rs   )rm   rm   rq   rr   rm   rq   rr   rm   rs   rt   rt   rt   rt   rt      )rm   rr   rq   rq   rr   r"   r#   r#   r#   r#   ru   rv   rw   rx   ry   s     r0   r   r      s    $   D1HrdQhPVxZ[|hiklgmnnFFF!$	___222	   HGG!$	___222	   988!$	GGG)//	 
* ":.1 2 3  r2   c                     t          d| fdgdz  dgdz  g dg ddg ddgdz  g d	g d
dg ddgdz  g dg ddg ddgdz  g dg ddgddgd|S )a  Implements TextNet architecture from `"FAST: Faster Arbitrarily-Shaped Text Detector with
    Minimalist Kernel Representation" <https://arxiv.org/abs/2111.02394>`_.
    Implementation based on the official Pytorch implementation: <https://github.com/czczup/FAST>`_.

    >>> import torch
    >>> from doctr.models import textnet_base
    >>> model = textnet_base(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 TextNet architecture

    Returns:
        A TextNet base model
    r   r!   
   )
rm   rm   rr   rm   rr   rm   rm   rq   rm   rm   )
r#   r"   r#   r#   r#   r#   r#   r#   r#   r#   rn   )
r!   ro   ro   ro   ro   ro   ro   ro   ro   ro   ro   )
rm   rq   rm   rr   rm   rm   rr   rr   rm   rm   )
r"   r#   r#   r#   r#   r#   r#   r#   r#   r#   r}   rs   r{   )rm   rm   rm   rq   rm   rr   rm   rr   r|   r~   rt   r   )rm   rq   rr   rr   rq   r   ru   rv   rw   rx   ry   s     r0   r   r      s    $ 
 !#tby!#r	ooo888	   QPP!$
ooo888	   HGG!$	___222	   988!$	GGG)//	 '
4 ":.; < =  r2   )N)F)copyr   typingr   torchr   doctr.datasetsr   modules.layersr	   utilsr
   r   __all__r[   r   r\   r]   __annotations__r9   r   r`   rk   r   r   r   r-   r2   r0   <module>r      sL                      ! ! ! ! ! ! + + + + + + = = = = = = = =
;
;
; &$"4x())`  &$"4x())a  &$"4x())` + +d3S#X&'   2;< ;< ;< ;< ;<bm ;< ;< ;<B %) 
 cT! 	
    8, ,T ,S ,W , , , ,^, ,d ,c ,g , , , ,^1 1T 1S 1W 1 1 1 1 1 1r2   