
    h:                     V   S SK Jr  S SKJr  S SKJrJr  S SKrS SK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  S	SKJrJrJr  S	SKJrJrJr  S	SKJrJrJrJ r J!r!  SSKJ"r"  SSK#J$r$  / SQr% " S S\"5      r& " S S\RN                  5      r( " S S\RN                  5      r) " S S\RN                  5      r* " S S\RV                  5      r,S\S\-S\\.   S \&4S! jr/\S"S#S$.r0 " S% S&\5      r1 " S' S(\5      r2 " S) S*\5      r3S\S\-S\\.   S \&4S+ jr4\" 5       \" S,\1Rj                  4S-\!Rl                  4S.9SS/SS\!Rl                  S0.S1\\1   S2\.S\\-   S3\\.   S4\\!   S5\S \&4S6 jj5       5       r7\" 5       \" S,\2Rj                  4S-\Rl                  4S.9SS/SS\Rl                  S0.S1\\2   S2\.S\\-   S3\\.   S4\\   S5\S \&4S7 jj5       5       r8\" 5       \" S,\3Rj                  4S-\Rl                  4S.9SS/SS\Rl                  S0.S1\\3   S2\.S\\-   S3\\.   S4\\   S5\S \&4S8 jj5       5       r9g)9    )Sequence)partial)AnyOptionalN)nn)
functional   )SemanticSegmentation   )register_modelWeightsWeightsEnum)_VOC_CATEGORIES)_ovewrite_value_paramhandle_legacy_interfaceIntermediateLayerGetter)mobilenet_v3_largeMobileNet_V3_Large_WeightsMobileNetV3)ResNet	resnet101ResNet101_Weightsresnet50ResNet50_Weights   )_SimpleSegmentationModel)FCNHead)	DeepLabV3DeepLabV3_ResNet50_WeightsDeepLabV3_ResNet101_Weights$DeepLabV3_MobileNet_V3_Large_Weightsdeeplabv3_mobilenet_v3_largedeeplabv3_resnet50deeplabv3_resnet101c                       \ rS rSrSrSrg)r      ai  
Implements DeepLabV3 model from
`"Rethinking Atrous Convolution for Semantic Image Segmentation"
<https://arxiv.org/abs/1706.05587>`_.

Args:
    backbone (nn.Module): the network used to compute the features for the model.
        The backbone should return an OrderedDict[Tensor], with the key being
        "out" for the last feature map used, and "aux" if an auxiliary classifier
        is used.
    classifier (nn.Module): module that takes the "out" element returned from
        the backbone and returns a dense prediction.
    aux_classifier (nn.Module, optional): auxiliary classifier used during training
 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r'       a/var/www/auris/envauris/lib/python3.13/site-packages/torchvision/models/segmentation/deeplabv3.pyr   r      s     	r.   r   c            	       F   ^  \ rS rSrS	S\S\S\\   SS4U 4S jjjrSrU =r$ )
DeepLabHead1   in_channelsnum_classesatrous_ratesreturnNc                    > [         TU ]  [        X5      [        R                  " SSSSSS9[        R
                  " S5      [        R                  " 5       [        R                  " SUS5      5        g )N   r	   r   F)paddingbias)super__init__ASPPr   Conv2dBatchNorm2dReLU)selfr3   r4   r5   	__class__s       r/   r<   DeepLabHead.__init__2   sQ    +IIc3159NN3GGIIIc;*	
r.   r'   ))      $   )	r(   r)   r*   r+   intr   r<   r-   __classcell__rB   s   @r/   r1   r1   1   s/    
C 
c 
RU 
jn 
 
r.   r1   c                   <   ^  \ rS rSrS\S\S\SS4U 4S jjrSrU =r$ )	ASPPConv<   r3   out_channelsdilationr6   Nc           	         > [         R                  " XSX3SS9[         R                  " U5      [         R                  " 5       /n[        TU ]  " U6   g )Nr	   F)r9   rN   r:   )r   r>   r?   r@   r;   r<   )rA   r3   rM   rN   modulesrB   s        r/   r<   ASPPConv.__init__=   sA    IIkH^cdNN<(GGI

 	'"r.   r'   )r(   r)   r*   r+   rG   r<   r-   rH   rI   s   @r/   rK   rK   <   s)    #C #s #c #d # #r.   rK   c                   r   ^  \ rS rSrS\S\SS4U 4S jjrS\R                  S\R                  4S jrS	r	U =r
$ )
ASPPPoolingF   r3   rM   r6   Nc           
         > [         TU ]  [        R                  " S5      [        R                  " XSSS9[        R
                  " U5      [        R                  " 5       5        g )Nr   Fr:   )r;   r<   r   AdaptiveAvgPool2dr>   r?   r@   )rA   r3   rM   rB   s      r/   r<   ASPPPooling.__init__G   sC      #IIk?NN<(GGI		
r.   xc                 n    UR                   SS  nU  H  nU" U5      nM     [        R                  " XSSS9$ )NbilinearF)sizemodealign_corners)shapeFinterpolate)rA   rY   r]   mods       r/   forwardASPPPooling.forwardO   s7    wwrs|CAA }}Q
%PPr.   r'   )r(   r)   r*   r+   rG   r<   torchTensorrd   r-   rH   rI   s   @r/   rS   rS   F   sA    
C 
s 
t 
Q Q%,, Q Qr.   rS   c            	          ^  \ rS rSr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$ )r=   V   r3   r5   rM   r6   Nc                   > [         TU ]  5         / nUR                  [        R                  " [        R
                  " XSSS9[        R                  " U5      [        R                  " 5       5      5        [        U5      nU H  nUR                  [        XU5      5        M      UR                  [        X5      5        [        R                  " U5      U l        [        R                  " [        R
                  " [        U R                  5      U-  USSS9[        R                  " U5      [        R                  " 5       [        R                  " S5      5      U l        g )Nr   FrV   g      ?)r;   r<   appendr   
Sequentialr>   r?   r@   tuplerK   rS   
ModuleListconvslenDropoutproject)rA   r3   r5   rM   rP   ratesraterB   s          r/   r<   ASPP.__init__W   s    MM"))KquMr~~^jOkmomtmtmvw	
 l#DNN8KtDE  	{;=>]]7+
}}IIc$**o4lAERNN<(GGIJJsO	
r.   rY   c                     / nU R                    H  nUR                  U" U5      5        M     [        R                  " USS9nU R	                  U5      $ )Nr   )dim)ro   rk   rf   catrr   )rA   rY   _resconvress        r/   rd   ASPP.forwardm   sD    JJDKKQ  ii!$||C  r.   )ro   rr   )r8   )r(   r)   r*   r+   rG   r   r<   rf   rg   rd   r-   rH   rI   s   @r/   r=   r=   V   sO    
C 
x} 
TW 
bf 
 
,! !%,, ! !r.   r=   backboner4   auxr6   c                     SS0nU(       a  SUS'   [        XS9n U(       a  [        SU5      OS n[        SU5      n[        XU5      $ )Nlayer4outr~   layer3return_layersi   i   )r   r   r1   r   )r}   r4   r~   r   aux_classifier
classifiers         r/   _deeplabv3_resnetr   u   sN    
 u%M
"'h&xMH36WT;/DNT;/JX>::r.   )r   r   z
        These weights were trained on a subset of COCO, using only the 20 categories that are present in the Pascal VOC
        dataset.
    )
categoriesmin_size_docsc                   P    \ rS rSr\" S\" \SS90 \ESSSSS	S
.0SSS.ES9r\r	Sr
g)r      zHhttps://download.pytorch.org/models/deeplabv3_resnet50_coco-cd0a2569.pth  resize_sizeijzVhttps://github.com/pytorch/vision/tree/main/references/segmentation#deeplabv3_resnet50COCO-val2017-VOC-labelsgP@皙W@miou	pixel_accgvWf@gGzd@
num_paramsrecipe_metrics_ops
_file_sizeurl
transformsmetar'   Nr(   r)   r*   r+   r   r   r
   _COMMON_METACOCO_WITH_VOC_LABELS_V1DEFAULTr-   r'   r.   r/   r   r      sU    %V/SA

"n) !%, !
" &Gr.   r   c                   P    \ rS rSr\" S\" \SS90 \ESSSSS	S
.0SSS.ES9r\r	Sr
g)r       zIhttps://download.pytorch.org/models/deeplabv3_resnet101_coco-586e9e4e.pthr   r   ijzQhttps://github.com/pytorch/vision/tree/main/references/segmentation#fcn_resnet101r   gP@r   r   gS+p@gm&m@r   r   r'   Nr   r'   r.   r/   r    r       sU    %W/SA

"i) !%, !
" &Gr.   r    c                   P    \ rS rSr\" S\" \SS90 \ESSSSS	S
.0SSS.ES9r\r	Sr
g)r!      zMhttps://download.pytorch.org/models/deeplabv3_mobilenet_v3_large-fc3c493d.pthr   r   iPK z`https://github.com/pytorch/vision/tree/main/references/segmentation#deeplabv3_mobilenet_v3_larger   gfffff&N@gV@r   gCl$@gJ+&E@r   r   r'   Nr   r'   r.   r/   r!   r!      sU    %[/SA

"x) !%,  
" &Gr.   r!   c           
         U R                   n S/[        U 5       VVs/ s H  u  p4[        USS5      (       d  M  UPM     snn-   [        U 5      S-
  /-   nUS   nX   R                  nUS   nX   R                  n	[        U5      S0n
U(       a  SU
[        U5      '   [        X
S	9n U(       a  [        X5      OS n[        Xq5      n[        XU5      $ s  snnf )
Nr   _is_cnFr   r   r~   r   )
features	enumerategetattrrp   rM   strr   r   r1   r   )r}   r4   r~   ibstage_indicesout_posout_inplanesaux_posaux_inplanesr   r   r   s                r/   _deeplabv3_mobilenetv3r      s    
   H C8)<\)<8UZ@[1)<\\`cdl`mpq`q_rrMBG$11LBG$11L\5)M
&+c'l#&xMH;>W\7DN\7JX>:: ]s
   CC
pretrainedpretrained_backbone)weightsweights_backboneT)r   progressr4   aux_lossr   r   r   r   r   kwargsc                 F   [         R                  U 5      n [        R                  " U5      nU b3  Sn[        SU[	        U R
                  S   5      5      n[        SUS5      nOUc  Sn[        U/ SQS9n[        XbU5      nU b  UR                  U R                  USS	95        U$ )
a  Constructs a DeepLabV3 model with a ResNet-50 backbone.

.. betastatus:: segmentation module

Reference: `Rethinking Atrous Convolution for Semantic Image Segmentation <https://arxiv.org/abs/1706.05587>`__.

Args:
    weights (:class:`~torchvision.models.segmentation.DeepLabV3_ResNet50_Weights`, optional): The
        pretrained weights to use. See
        :class:`~torchvision.models.segmentation.DeepLabV3_ResNet50_Weights` below for
        more details, and possible values. By default, no pre-trained
        weights are used.
    progress (bool, optional): If True, displays a progress bar of the
        download to stderr. Default is True.
    num_classes (int, optional): number of output classes of the model (including the background)
    aux_loss (bool, optional): If True, it uses an auxiliary loss
    weights_backbone (:class:`~torchvision.models.ResNet50_Weights`, optional): The pretrained weights for the
        backbone
    **kwargs: unused

.. autoclass:: torchvision.models.segmentation.DeepLabV3_ResNet50_Weights
    :members:
Nr4   r   r   T   FTTr   replace_stride_with_dilationr   
check_hash)
r   verifyr   r   rp   r   r   r   load_state_dictget_state_dictr   r   r4   r   r   r   r}   models           r/   r#   r#      s    J )//8G'../?@+M;GLLYeLfHgh(XtD		 0ObcHhX>Eg44hSW4XYLr.   c                 F   [         R                  U 5      n [        R                  " U5      nU b3  Sn[        SU[	        U R
                  S   5      5      n[        SUS5      nOUc  Sn[        U/ SQS9n[        XbU5      nU b  UR                  U R                  USS	95        U$ )
a!  Constructs a DeepLabV3 model with a ResNet-101 backbone.

.. betastatus:: segmentation module

Reference: `Rethinking Atrous Convolution for Semantic Image Segmentation <https://arxiv.org/abs/1706.05587>`__.

Args:
    weights (:class:`~torchvision.models.segmentation.DeepLabV3_ResNet101_Weights`, optional): The
        pretrained weights to use. See
        :class:`~torchvision.models.segmentation.DeepLabV3_ResNet101_Weights` below for
        more details, and possible values. By default, no pre-trained
        weights are used.
    progress (bool, optional): If True, displays a progress bar of the
        download to stderr. Default is True.
    num_classes (int, optional): number of output classes of the model (including the background)
    aux_loss (bool, optional): If True, it uses an auxiliary loss
    weights_backbone (:class:`~torchvision.models.ResNet101_Weights`, optional): The pretrained weights for the
        backbone
    **kwargs: unused

.. autoclass:: torchvision.models.segmentation.DeepLabV3_ResNet101_Weights
    :members:
Nr4   r   r   Tr   r   r   r   )
r    r   r   r   rp   r   r   r   r   r   r   s           r/   r$   r$     s    J *009G(//0@A+M;GLLYeLfHgh(XtD		!1PcdHhX>Eg44hSW4XYLr.   c                 B   [         R                  U 5      n [        R                  " U5      nU b3  Sn[        SU[	        U R
                  S   5      5      n[        SUS5      nOUc  Sn[        USS9n[        XbU5      nU b  UR                  U R                  USS95        U$ )	a'  Constructs a DeepLabV3 model with a MobileNetV3-Large backbone.

Reference: `Rethinking Atrous Convolution for Semantic Image Segmentation <https://arxiv.org/abs/1706.05587>`__.

Args:
    weights (:class:`~torchvision.models.segmentation.DeepLabV3_MobileNet_V3_Large_Weights`, optional): The
        pretrained weights to use. See
        :class:`~torchvision.models.segmentation.DeepLabV3_MobileNet_V3_Large_Weights` below for
        more details, and possible values. By default, no pre-trained
        weights are used.
    progress (bool, optional): If True, displays a progress bar of the
        download to stderr. Default is True.
    num_classes (int, optional): number of output classes of the model (including the background)
    aux_loss (bool, optional): If True, it uses an auxiliary loss
    weights_backbone (:class:`~torchvision.models.MobileNet_V3_Large_Weights`, optional): The pretrained weights
        for the backbone
    **kwargs: unused

.. autoclass:: torchvision.models.segmentation.DeepLabV3_MobileNet_V3_Large_Weights
    :members:
Nr4   r   r   Tr   )r   dilatedr   )
r!   r   r   r   rp   r   r   r   r   r   r   s           r/   r"   r"   T  s    F 399'BG1889IJ+M;GLLYeLfHgh(XtD		!*:DIH"8(CEg44hSW4XYLr.   ):collections.abcr   	functoolsr   typingr   r   rf   r   torch.nnr   ra   transforms._presetsr
   _apir   r   r   _metar   _utilsr   r   r   mobilenetv3r   r   r   resnetr   r   r   r   r   r   fcnr   __all__r   rl   r1   rK   rS   Moduler=   rG   boolr   r   r   r    r!   r   r   IMAGENET1K_V1r#   r$   r"   r'   r.   r/   <module>r      s9   $      $ 7 7 7 # \ \ U U U U , 	( 	&
"-- 
#r}} #Q"-- Q !299 !>;;; 
$; 	;  "& &*&+ &*&; &*;;; 
$; 	;. 5MMN+-=-K-KL 59!%#3C3Q3Q0010 0 #	0
 tn0 /00 0 0	 
0f 6NNO+->-L-LM 6:!%#4E4S4S0120 0 #	0
 tn0 010 0 0	 
0f ?WWX+-G-U-UV ?C!%#=W=e=e.:;. . #	.
 tn. 9:. . .	 
.r.   