
    j7                        U d dl mZ d dl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 d dlmZ d dlmZmZmZ d	d
lmZmZ ddlmZmZ g dZddddddddddddddddZeeeeef         f         ed<    G d dej                  Z G d dej        e          Z 	 	 d)dede!dee!gej        f         de"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 )+    )Callable)AnyN)nn)
functional)IntermediateLayerGetter)
CLASS_NAME)resnet18resnet34resnet50   )_bf16_to_float32load_pretrained_params   )LinkNetPostProcessor_LinkNet)LinkNetlinknet_resnet18linknet_resnet34linknet_resnet50)r      r   )gK7?gQ?gNbX9?)gL7A`?gj+?g|?5^?zShttps://doctr-static.mindee.com/models?id=v0.7.0/linknet_resnet18-e47a14dc.pt&src=0)input_shapemeanstdurlzShttps://doctr-static.mindee.com/models?id=v0.7.0/linknet_resnet34-9ca2df3e.pt&src=0zShttps://doctr-static.mindee.com/models?id=v0.7.0/linknet_resnet50-6cf565c1.pt&src=0)r   r   r   default_cfgsc            	            e Zd Zdeeeeef                  ddf fdZededededej	        fd            Z
d	eej                 dej        fd
Z xZS )
LinkNetFPNlayer_shapesreturnNc                 B    t                                                       d t          |d d         |dd                    D             }d |D             } fdt          |d d         |dd          |          D             }t          j        |           _        d S )Nc                 @    g | ]\  }}|d          |d          k    rdndS )r       ).0in_shape	out_shapes      i/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/models/detection/linknet/pytorch.py
<listcomp>z'LinkNetFPN.__init__.<locals>.<listcomp>1   sB     
 
 
#) 2,)B-//AAa
 
 
    r"   r   c                     g | ]
}|d          S )r   r$   )r%   shapes     r(   r)   z'LinkNetFPN.__init__.<locals>.<listcomp>6   s    444eq444r*   c                 F    g | ]\  }}}                     |||          S r$   )decoder_block)r%   ichanochanstrideselfs       r(   r)   z'LinkNetFPN.__init__.<locals>.<listcomp>8   s>     
 
 
9MvDueV44
 
 
r*   )super__init__zipr   
ModuleListdecoders)r2   r   strideschans_decoder_layers	__class__s   `    r(   r4   zLinkNetFPN.__init__/   s    
 
'*<+<l122>N'O'O
 
 

 54|444
 
 
 
QTUZ[^\^[^U_afghgigiajlsQtQt
 
 
 o66r*   in_chanout_chanr1   c                    | dz  }t          j        t          j        | |dd          t          j        |          t          j        d          t          j        ||dd|dz
  |d          t          j        |          t          j        d          t          j        ||dd          t          j        |          t          j        d          	  	        S )	zCreates a LinkNet decoder block   r   F)kernel_sizebiasTinplacer   )paddingoutput_paddingr1   rA   )r   
SequentialConv2dBatchNorm2dReLUConvTranspose2d)r<   r=   r1   mid_chans       r(   r.   zLinkNetFPN.decoder_block>   s     a<}IgxQUCCCN8$$GD!!!x1aPVYZPZcipuvvvN8$$GD!!!IhaeDDDN8$$GD!!!

 

 
	
r*   featsc                     |d         }t          | j        d d d         |d d         d d d                   D ]\  }} ||          |z   } | j        d         |          }|S )Nr"   r   )r5   r7   )r2   rL   outdecoderfmaps        r(   forwardzLinkNetFPN.forwardN   sw    Bi ttt!4eCRCj26FGG 	& 	&MGT'#,,%CCdmAs##
r*   )__name__
__module____qualname__listtupleintr4   staticmethodr   rF   r.   torchTensorrQ   __classcell__r;   s   @r(   r   r   .   s        7T%S#*>%? 7D 7 7 7 7 7 7 
s 
c 
3 
2= 
 
 
 \
T%,/ EL        r*   r   c                   @    e Zd ZdZddddddegfdeded	ed
ededede	e
ef         dz  dee
         ddf fdZde
deddfdZ	 	 	 d!dej        deej                 dz  dededede	e
ef         fdZ	 	 	 d"dej        deej                 dedededej        fd Z xZS )#r   a  LinkNet as described in `"LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation"
    <https://arxiv.org/pdf/1707.03718.pdf>`_.

    Args:
        feature extractor: the backbone serving as feature extractor
        bin_thresh: threshold for binarization of the output feature map
        box_thresh: minimal objectness score to consider a box
        head_chans: number of channels in the head layers
        assume_straight_pages: if True, fit straight bounding boxes only
        exportable: onnx exportable returns only logits
        cfg: the configuration dict of the model
        class_names: list of class names
    g?    TFNfeat_extractor
bin_thresh
box_thresh
head_chansassume_straight_pages
exportablecfgclass_namesr   c	                 (   t                                                       || _        t          | j                  }	|| _        || _        || _        || _        | j                                         t          j
                    5  d}
|                     t          j        dg|
R                     }d |                                D             }|d         d         |
d         dz  |
d         dz  fg|z   }d d d            n# 1 swxY w Y   | j                                         t          |          | _        t!          j        t!          j        |d         d         |ddddd	          t!          j        |          t!          j        d
          t!          j        ||ddd          t!          j        |          t!          j        d
          t!          j        ||	dd                    | _        t/          | j        ||          | _        |                                 D ]\  }}|                    d          rt7          |t           j        t           j        f          rRt           j                            |j        j        dd           |j         |j         j        !                                 t7          |t           j                  r=|j        j        "                    d           |j         j        !                                 d S )N)r      rh   r   c                 2    g | ]\  }}|j         d d         S )r   N)r,   )r%   _vs      r(   r)   z$LinkNet.__init__.<locals>.<listcomp>   s&    ;;;tq!qwqrr{;;;r*   r   r?   r#   r   F)r@   rD   rE   r1   rA   TrB   )r@   rD   rA   )r@   r1   )rc   r`   ra   zfeat_extractor.fan_outrelu)modenonlinearityg      ?)#r3   r4   rf   lenre   rd   rc   r_   evalrY   no_gradzerositemstrainr   fpnr   rF   rJ   rH   rI   rG   
classifierr   postprocessornamed_modules
startswith
isinstanceinitkaiming_normal_weightdatarA   zero_fill_)r2   r_   r`   ra   rb   rc   rd   re   rf   num_classesr&   rN   _shapesnmr;   s                  r(   r4   zLinkNet.__init__g   s%    	&t/00$%:",  """]__ 	V 	V$H%%ek1.x..&A&ABBC;;syy{{;;;G
1x{a'7!9IJKgUG	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	!!###g&&-
1zq!TU^_fk   N:&&GD!!!Ij*!QUSSSN:&&GD!!!z;AaPPP

 

 2"&"<`j
 
 
 &&(( 
	$ 
	$DAq||-.. !bi);<== $''ITZ'[[[6%FK%%'''Ar~.. $##C(((!!###
	$ 
	$s   A3DDD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   )r2   r   r   s      r(   from_pretrainedzLinkNet.from_pretrained   s"     	t[;;F;;;;;r*   xtargetreturn_model_outputreturn_predsc                 H                          |                               fdt          t                              D                       }                     |          }i } j        r||d<   |S |s||r!t          t          j        |                    }|r||d<   ||r[t          j	        j
        dt          j        dt          t          t          t          f                  f fd            }	 |	|          |d<   |                     ||          }
|
|d<   |S )	Nc                 :    g | ]}t          |                   S r$   str)r%   idxrL   s     r(   r)   z#LinkNet.forward.<locals>.<listcomp>   s#    HHHs5S?HHHr*   logitsout_mapprob_mapr   c                     fd                     |                                                                                     d                                                    D             S )Nc                 T    g | ]$}t          t          j        |                    %S r$   )dictr5   rf   )r%   predsr2   s     r(   r)   z9LinkNet.forward.<locals>._postprocess.<locals>.<listcomp>   s>        T-u5566  r*   )r   r#   r   r   )rx   detachcpupermutenumpy)r   r2   s    r(   _postprocessz%LinkNet.forward.<locals>._postprocess   sm       !%!3!3HOO4E4E4I4I4K4K4S4ST`4a4a4g4g4i4i!j!j   r*   r   loss)r_   rv   rangerp   rw   rd   r   rY   sigmoidcompilerdisablerZ   rU   r   r   r   compute_loss)r2   r   r   r   r   r   r   rN   r   r   r   rL   s   `          @r(   rQ   zLinkNet.forward   sE    ##A&&HHHHeCJJ6G6GHHHII(( ? 	"CMJ 	?&.L.'f(=(=>>H 	&%C	N>\>^#u| T#s(^8L      $# (<11CL$$VV44DCK
r*          @      ?:0yE>r   gammaalphaepsc                    |                      ||j        dd                   \  }}t          j        |                              |j                  t          j        |          }	}|                    |j                  |	                    |j                  }	}|	                    t          j                  }	t          j	        ||d          }
t          j
        |          }|dk     rt          d          ||z  d|z
  d|z
  z  z   }||z  d|z
  d|z
  z  z   }|d|z
  |z  z  |
z  }|	|z                      d          |	                    d          z  }t          | j                  dk    rt          j        |d	          n|}|	|z  |z                      d
          }|	||z   z                      d
          }dd|z  ||z   z  z
                                  }||z   S )a1  Compute linknet loss, BCE with boosted box edges or focal loss. Focal loss implementation based on
        <https://github.com/tensorflow/addons/>`_.

        Args:
            out_map: output feature map of the model of shape (N, num_classes, H, W)
            target: list of dictionary where each dict has a `boxes` and a `flags` entry
            gamma: modulating factor in the focal loss formula
            alpha: balancing factor in the focal loss formula
            eps: epsilon factor in dice loss

        Returns:
            A loss tensor
        r   N)dtypenone)	reductionr   z7Value of gamma should be greater than or equal to zero.)r   r   r#   r   )dim)r   r#   r   r#   )build_targetr,   rY   
from_numpytor   devicefloat32F binary_cross_entropy_with_logitsr   
ValueErrorsumrp   rf   softmaxr   )r2   r   r   r   r   r   _target_mask
seg_targetseg_maskbce_loss	proba_mapp_talpha_t
focal_lossdice_mapintercardinality	dice_losss                      r(   r   zLinkNet.compute_loss   s   * **67=3DEE$/88;;'-;PPRWRbchRiRiH
)}}W^<<hkk'.>Y>YH
;;U];335gzU[\\\M'**	 199VWWW*$I!j.'II*$E	a*n'EECE11H<
+00>>lA[A[[
 588H4I4IA4M4M5=a0000S\H$z166yAA8j#89>>yIIUkC&788>>@@	 I%%r*   )NFF)r   r   r   )rR   rS   rT   __doc__r   r   floatrW   boolr   r   r   rU   r4   r   rY   rZ   npndarrayrQ   r   r[   r\   s   @r(   r   r   X   s        "  &* %)",:$ :$/:$ :$ 	:$
 :$  $:$ :$ #s(^d":$ #Y:$ 
:$ :$ :$ :$ :$ :$x<3 <# <$ < < < < +/$)"& &<& RZ 4'& "	&
 & & 
c3h& & & &X 0& 0&0& RZ 0& 	0&
 0& 0& 
0& 0& 0& 0& 0& 0& 0& 0&r*   r   Tarch
pretrainedbackbone_fn
fpn_layerspretrained_backboneignore_keysr   r   c                    |o| } ||          }t          |d t          |          D                       }|                    dd           s+t          |                              dt          g          |d<   nt          |d                   |d<   t          |fdt          |          i|}	|r]|d         t          |                              dt          g          k    r|nd }
|	                    t          |          d         |
           |	S )Nc                 4    i | ]\  }}|t          |          S r$   r   )r%   r   
layer_names      r(   
<dictcomp>z_linknet.<locals>.<dictcomp>  s$    KKK/#zSXXKKKr*   rf   re   r   )r   )r   	enumerategetr   r   sortedr   r   )r   r   r   r   r   r   r   backboner_   model_ignore_keyss              r(   _linknetr     s,    .@j. {.//H,KKYz5J5JKKK N ::mT** > ,T 2 6 6}zl S S} &vm'< = =} NEET(:EfEEE S "-0L4F4J4J=[eZf4g4gggKKmq 	 	l407\RRRLr*   Fc                 8    t          d| t          g dfdddgi|S )af  LinkNet as described in `"LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation"
    <https://arxiv.org/pdf/1707.03718.pdf>`_.

    >>> import torch
    >>> from doctr.models import linknet_resnet18
    >>> model = linknet_resnet18(pretrained=True).eval()
    >>> input_tensor = torch.rand((1, 3, 1024, 1024), dtype=torch.float32)
    >>> out = model(input_tensor)

    Args:
        pretrained (bool): If True, returns a model pre-trained on our text detection dataset
        **kwargs: keyword arguments of the LinkNet architecture

    Returns:
        text detection architecture
    r   layer1layer2layer3layer4r   classifier.6.weightclassifier.6.bias)r   r	   r   r   s     r(   r   r   +  I    " 000	
 
 "

 
 
 
r*   c                 8    t          d| t          g dfdddgi|S )af  LinkNet as described in `"LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation"
    <https://arxiv.org/pdf/1707.03718.pdf>`_.

    >>> import torch
    >>> from doctr.models import linknet_resnet34
    >>> model = linknet_resnet34(pretrained=True).eval()
    >>> input_tensor = torch.rand((1, 3, 1024, 1024), dtype=torch.float32)
    >>> out = model(input_tensor)

    Args:
        pretrained (bool): If True, returns a model pre-trained on our text detection dataset
        **kwargs: keyword arguments of the LinkNet architecture

    Returns:
        text detection architecture
    r   r   r   r   r   )r   r
   r   s     r(   r   r   I  r   r*   c                 8    t          d| t          g dfdddgi|S )af  LinkNet as described in `"LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation"
    <https://arxiv.org/pdf/1707.03718.pdf>`_.

    >>> import torch
    >>> from doctr.models import linknet_resnet50
    >>> model = linknet_resnet50(pretrained=True).eval()
    >>> input_tensor = torch.rand((1, 3, 1024, 1024), dtype=torch.float32)
    >>> out = model(input_tensor)

    Args:
        pretrained (bool): If True, returns a model pre-trained on our text detection dataset
        **kwargs: keyword arguments of the LinkNet architecture

    Returns:
        text detection architecture
    r   r   r   r   r   )r   r   r   s     r(   r   r   g  r   r*   )TN)F)'collections.abcr   typingr   r   r   rY   r   torch.nnr   r   torchvision.models._utilsr   doctr.file_utilsr   doctr.models.classificationr	   r
   r   utilsr   r   baser   r   __all__r   r   r   __annotations__Moduler   r   r   rU   r   r   r   r   r$   r*   r(   <module>r      s   % $ $ $ $ $ $                  $ $ $ $ $ $ = = = = = = ' ' ' ' ' ' D D D D D D D D D D = = = = = = = = 0 0 0 0 0 0 0 0
Q
Q
Q
 '%%d	  '%%d	  '%%d	 + +d3S#X&'   ,' ' ' ' ' ' ' 'Tl& l& l& l& l&bi l& l& l&h !%$(! !
!! 4&")+,! S		!
 ! cT!! ! ! ! ! !H       <       <         r*   