
    ڧg:                        d dl mZ d dlmZmZm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mZmZ dd
lmZ ddlmZmZmZ ddlmZmZmZ ddlmZmZmZmZm Z  ddlm!Z! ddl"m#Z# g dZ$ G d de!          Z% G d dej&                  Z' G d dej&                  Z( G d dej&                  Z) G d dej*                  Z+dede,dee-         de%fd Z.ed!d"d#Z/ G d$ d%e          Z0 G d& d'e          Z1 G d( d)e          Z2dede,dee-         de%fd*Z3 e             ed+e0j4        fd,e j5        f-          dd.dde j5        d/d0ee0         d1e-dee,         d2ee-         d3ee          d4ede%fd5                        Z6 e             ed+e1j4        fd,ej5        f-          dd.ddej5        d/d0ee1         d1e-dee,         d2ee-         d3ee         d4ede%fd6                        Z7 e             ed+e2j4        fd,ej5        f-          dd.ddej5        d/d0ee2         d1e-dee,         d2ee-         d3ee         d4ede%fd7                        Z8dS )8    )partial)AnyOptionalSequenceN)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                       e Zd ZdZdS )r   a  
    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____doc__     e/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/models/segmentation/deeplabv3.pyr   r      s          	Dr+   r   c            	       >     e Zd Zddededee         ddf fdZ xZS )	DeepLabHead      $   in_channelsnum_classesatrous_ratesreturnNc                 
   t                                          t          ||          t          j        ddddd          t          j        d          t          j                    t          j        d|d                     d S )N   r	   r   F)paddingbias)super__init__ASPPr   Conv2dBatchNorm2dReLU)selfr3   r4   r5   	__class__s       r,   r<   zDeepLabHead.__init__1   su    l++Ic315999N3GIIIc;**	
 	
 	
 	
 	
r+   )r/   )r&   r'   r(   intr   r<   __classcell__rB   s   @r,   r.   r.   0   sd        
 
C 
c 
RU 
jn 
 
 
 
 
 
 
 
 
 
r+   r.   c                   0     e Zd Zdedededdf fdZ xZS )ASPPConvr3   out_channelsdilationr6   Nc                     t          j        ||d||d          t          j        |          t          j                    g} t	                      j        |  d S )Nr	   F)r9   rI   r:   )r   r>   r?   r@   r;   r<   )rA   r3   rH   rI   modulesrB   s        r,   r<   zASPPConv.__init__<   sW    Ik<Hx^cdddN<((GII

 	'""""r+   )r&   r'   r(   rC   r<   rD   rE   s   @r,   rG   rG   ;   sY        #C #s #c #d # # # # # # # # # #r+   rG   c                   P     e Zd Zdededdf fdZdej        dej        fdZ xZS )ASPPPoolingr3   rH   r6   Nc           	          t                                          t          j        d          t          j        ||dd          t          j        |          t          j                               d S )Nr   Fr:   )r;   r<   r   AdaptiveAvgPool2dr>   r?   r@   )rA   r3   rH   rB   s      r,   r<   zASPPPooling.__init__F   sc     ##Ik<???N<((GII		
 	
 	
 	
 	
r+   xc                 p    |j         dd          }| D ]} ||          }t          j        ||dd          S )NbilinearF)sizemodealign_corners)shapeFinterpolate)rA   rQ   rU   mods       r,   forwardzASPPPooling.forwardN   sH    wrss| 	 	CAAA}QT
%PPPPr+   )	r&   r'   r(   rC   r<   torchTensorr\   rD   rE   s   @r,   rM   rM   E   s        
C 
s 
t 
 
 
 
 
 
Q Q%, Q Q Q Q Q Q Q Qr+   rM   c            	       b     e Zd Zd
dedee         deddf fdZdej        dej        fd	Z xZ	S )r=   r8   r3   r5   rH   r6   Nc           
         t                                                       g }|                    t          j        t          j        ||dd          t          j        |          t          j                                         t          |          }|D ]&}|                    t          |||                     '|                    t          ||                     t          j        |          | _        t          j        t          j        t          | j                  |z  |dd          t          j        |          t          j                    t          j        d                    | _        d S )Nr   FrO   g      ?)r;   r<   appendr   
Sequentialr>   r?   r@   tuplerG   rM   
ModuleListconvslenDropoutproject)rA   r3   r5   rH   rK   ratesraterB   s          r,   r<   zASPP.__init__V   s6   M")KquMMMr~^jOkOkmomtmvmvww	
 	
 	
 l## 	F 	FDNN8KtDDEEEE{;==>>>]7++
}Ic$*oo4lAERRRN<((GIIJsOO	
 
r+   rQ   c                     g }| j         D ] }|                     ||                     !t          j        |d          }|                     |          S )Nr   )dim)re   ra   r]   catrh   )rA   rQ   _resconvress        r,   r\   zASPP.forwardl   s[    J 	! 	!DKKQ    i!$$$||C   r+   )r8   )
r&   r'   r(   rC   r   r<   r]   r^   r\   rD   rE   s   @r,   r=   r=   U   s        
 
C 
x} 
TW 
bf 
 
 
 
 
 
,! !%, ! ! ! ! ! ! ! !r+   r=   backboner4   auxr6   c                     ddi}|rd|d<   t          | |          } |rt          d|          nd }t          d|          }t          | ||          S )Nlayer4outrr   layer3return_layersi   i   )r   r   r.   r   )rq   r4   rr   rx   aux_classifier
classifiers         r,   _deeplabv3_resnetr{   t   sl    
 u%M
 ("'h&x}MMMH36@WT;///DNT;//JXz>:::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                   b    e Zd Z ed eed          i edddddd	id
dd          ZeZdS )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metaN	r&   r'   r(   r   r   r
   _COMMON_METACOCO_WITH_VOC_LABELS_V1DEFAULTr*   r+   r,   r   r      s        %gV7/SAAA

"n) !%, , !
 
 
  " &GGGr+   r   c                   b    e Zd Z ed eed          i edddddd	id
dd          ZeZdS )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   Nr   r*   r+   r,   r    r       s        %gW7/SAAA

"i) !%, , !
 
 
  " &GGGr+   r    c                   b    e Zd Z ed eed          i edddddd	id
dd          ZeZdS )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   Nr   r*   r+   r,   r!   r!      s        %g[7/SAAA

"x) !%, ,  
 
 
  " &GGGr+   r!   c                    | j         } dgd t          |           D             z   t          |           dz
  gz   }|d         }| |         j        }|d         }| |         j        }t	          |          di}|rd|t	          |          <   t          | |          } |rt          ||          nd }	t          ||          }
t          | |
|	          S )	Nr   c                 :    g | ]\  }}t          |d d          |S )_is_cnF)getattr).0ibs      r,   
<listcomp>z*_deeplabv3_mobilenetv3.<locals>.<listcomp>   s.    \\\A8UZ@[@[\1\\\r+   r   ru   rr   rw   )	features	enumeraterf   rH   strr   r   r.   r   )rq   r4   rr   stage_indicesout_posout_inplanesaux_posaux_inplanesrx   ry   rz   s              r,   _deeplabv3_mobilenetv3r      s    
  H C\\8)<)<\\\\`cdl`m`mpq`q_rrMBGG$1LBGG$1L\\5)M
 ,&+c'll#&x}MMMH;>HW\;777DN\;77JXz>:::r+   
pretrainedpretrained_backbone)weightsweights_backboneT)r   progressr4   aux_lossr   r   r   r   r   kwargsc                    t                               |           } t          j        |          }| =d}t          d|t	          | j        d                             }t          d|d          }n|d}t          |g d          }t          |||          }| *|                    | 	                    |d	                     |S )
ad  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   rf   r   r   r{   load_state_dictget_state_dictr   r   r4   r   r   r   rq   models           r,   r#   r#      s    J )//88G'./?@@+M;GLYeLfHgHghh(XtDD		 0ObObObcccHhX>>Eg44hSW4XXYYYLr+   c                    t                               |           } t          j        |          }| =d}t          d|t	          | j        d                             }t          d|d          }n|d}t          |g d          }t          |||          }| *|                    | 	                    |d	                     |S )
ai  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   rf   r   r   r{   r   r   r   s           r,   r$   r$     s    J *0099G(/0@AA+M;GLYeLfHgHghh(XtDD		!1PcPcPcdddHhX>>Eg44hSW4XXYYYLr+   c                    t                               |           } t          j        |          }| =d}t          d|t	          | j        d                             }t          d|d          }n|d}t          |d          }t          |||          }| *|                    | 	                    |d                     |S )	ak  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   rf   r   r   r   r   r   r   s           r,   r"   r"   S  s    F 399'BBG189IJJ+M;GLYeLfHgHghh(XtDD		!*:DIIIH"8[(CCEg44hSW4XXYYYLr+   )9	functoolsr   typingr   r   r   r]   r   torch.nnr   rY   transforms._presetsr
   _apir   r   r   _metar   _utilsr   r   r   mobilenetv3r   r   r   resnetr   r   r   r   r   r   fcnr   __all__r   rb   r.   rG   rM   Moduler=   rC   boolr{   r   r   r    r!   r   r   IMAGENET1K_V1r#   r$   r"   r*   r+   r,   <module>r      s         * * * * * * * * * *        $ $ $ $ $ $ 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 # # # # # # \ \ \ \ \ \ \ \ \ \ U U U U U U U U U U U U U U U U U U U U U U U U , , , , , ,        	 	 	 	 	( 	 	 	&
 
 
 
 
"- 
 
 
# # # # #r} # # #Q Q Q Q Q"- Q Q Q ! ! ! ! !29 ! ! !>;;; 
$; 	; ; ; ;  " & & & & & & & &*& & & & &+ & & &*& & & & &; & & &*;;; 
$; 	; ; ; ;. 5MN+-=-KL   59!%#3C3Q0 0 0010 0 #	0
 tn0 /00 0 0 0 0	  
0f 6NO+->-LM   6:!%#4E4S0 0 0120 0 #	0
 tn0 010 0 0 0 0	  
0f ?WX+-G-UV   ?C!%#=W=e. . .:;. . #	.
 tn. 9:. . . . .	  
. . .r+   