a
    PSic:                  
   @   s  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 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 de Z$G dd dej%Z&G dd dej%Z'G dd dej%Z(G dd dej)Z*ee+ee, e$dddZ-edd d!Z.G d"d# d#eZ/G d$d% d%eZ0G d&d' d'eZ1ee+ee, e$dd(d)Z2ed*e/j3fd+ej4fd,dd-ddej4d.ee/ e,ee+ ee, ee ee$d/d0d1Z5ed*e0j3fd+ej4fd,dd-ddej4d.ee0 e,ee+ ee, ee ee$d/d2d3Z6ed*e1j3fd+ej4fd,dd-ddej4d.ee1 e,ee+ ee, ee ee$d/d4d5Z7dd6lm8Z8 e8e/j3j9e0j3j9e1j3j9d7Z:dS )8    )partial)AnyListOptionalN)nn)
functional   )SemanticSegmentation   )WeightsEnumWeights)_VOC_CATEGORIES)IntermediateLayerGetterhandle_legacy_interface_ovewrite_value_param)MobileNetV3MobileNet_V3_Large_Weightsmobilenet_v3_large)ResNetresnet50	resnet101ResNet50_WeightsResNet101_Weights   )_SimpleSegmentationModel)FCNHead)	DeepLabV3DeepLabV3_ResNet50_WeightsDeepLabV3_ResNet101_Weights$DeepLabV3_MobileNet_V3_Large_Weightsdeeplabv3_mobilenet_v3_largedeeplabv3_resnet50deeplabv3_resnet101c                   @   s   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__ r'   r'   e/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/models/segmentation/deeplabv3.pyr      s   r   c                       s&   e Zd Zeedd fddZ  ZS )DeepLabHeadN)in_channelsnum_classesreturnc                    sF   t  t|g dtjddddddtdt td|d d S )N)      $      r   r   F)paddingbias)super__init__ASPPr   Conv2dBatchNorm2dReLU)selfr*   r+   	__class__r'   r(   r4   1   s    zDeepLabHead.__init__r#   r$   r%   intr4   __classcell__r'   r'   r:   r(   r)   0   s   r)   c                       s(   e Zd Zeeedd fddZ  ZS )ASPPConvN)r*   out_channelsdilationr,   c                    s6   t j||d||ddt |t  g}t j|  d S )Nr   F)r1   rA   r2   )r   r6   r7   r8   r3   r4   )r9   r*   r@   rA   modulesr:   r'   r(   r4   <   s
    zASPPConv.__init__r<   r'   r'   r:   r(   r?   ;   s   r?   c                       s:   e Zd Zeedd fddZejejdddZ  ZS )ASPPPoolingN)r*   r@   r,   c              	      s4   t  tdtj||dddt|t  d S )Nr   Fr2   )r3   r4   r   AdaptiveAvgPool2dr6   r7   r8   )r9   r*   r@   r:   r'   r(   r4   F   s    zASPPPooling.__init__xr,   c                 C   s2   |j dd  }| D ]}||}qtj||dddS )NbilinearF)sizemodealign_corners)shapeFinterpolate)r9   rG   rJ   modr'   r'   r(   forwardN   s    
zASPPPooling.forward)	r#   r$   r%   r=   r4   torchTensorrQ   r>   r'   r'   r:   r(   rC   E   s   rC   c                       sB   e Zd Zd	eee edd fddZejejdddZ  Z	S )
r5   r0   N)r*   atrous_ratesr@   r,   c              
      s   t    g }|ttj||dddt|t  t|}|D ]}|t	||| qF|t
|| t|| _ttjt| j| |dddt|t td| _d S )Nr   FrD   g      ?)r3   r4   appendr   
Sequentialr6   r7   r8   tupler?   rC   
ModuleListconvslenDropoutproject)r9   r*   rT   r@   rB   ratesrater:   r'   r(   r4   V   s     
$zASPP.__init__rF   c                 C   s6   g }| j D ]}||| q
tj|dd}| |S )Nr   )dim)rY   rU   rR   catr\   )r9   rG   Z_resconvresr'   r'   r(   rQ   l   s
    
zASPP.forward)r0   )
r#   r$   r%   r=   r   r4   rR   rS   rQ   r>   r'   r'   r:   r(   r5   U   s   r5   )backboner+   auxr,   c                 C   sH   ddi}|rd|d< t | |d} |r.td|nd }td|}t| ||S )Nlayer4outrd   layer3return_layersi   i   )r   r   r)   r   )rc   r+   rd   ri   aux_classifier
classifierr'   r'   r(   _deeplabv3_resnett   s    
rl   )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                
   @   s@   e Zd Zedeeddi edddddd	i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@ZmiouZ	pixel_acc
num_paramsrecipe_metricsurl
transformsmetaN	r#   r$   r%   r   r   r	   _COMMON_METACOCO_WITH_VOC_LABELS_V1DEFAULTr'   r'   r'   r(   r      s    
r   c                
   @   s@   e Zd Zedeeddi edddddd	id
dZeZdS )r   zIhttps://download.pytorch.org/models/deeplabv3_resnet101_coco-586e9e4e.pthrp   rq   ijzQhttps://github.com/pytorch/vision/tree/main/references/segmentation#fcn_resnet101rs   gP@rt   ru   rv   rz   Nr~   r'   r'   r'   r(   r      s    
r   c                
   @   s@   e Zd Zedeeddi edddddd	id
dZeZdS )r   zMhttps://download.pytorch.org/models/deeplabv3_mobilenet_v3_large-fc3c493d.pthrp   rq   iPK z`https://github.com/pytorch/vision/tree/main/references/segmentation#deeplabv3_mobilenet_v3_largers   gfffff&N@gV@ru   rv   rz   Nr~   r'   r'   r'   r(   r      s    
r   c                 C   s   | j } dgdd t| D  t| d g }|d }| | j}|d }| | j}t|di}|rld|t|< t| |d	} |rt||nd }	t||}
t| |
|	S )
Nr   c                 S   s    g | ]\}}t |d dr|qS )_is_cnF)getattr).0ibr'   r'   r(   
<listcomp>       z*_deeplabv3_mobilenetv3.<locals>.<listcomp>r   rf   rd   rh   )	features	enumeraterZ   r@   strr   r   r)   r   )rc   r+   rd   stage_indicesZout_posZout_inplanesZaux_posZaux_inplanesri   rj   rk   r'   r'   r(   _deeplabv3_mobilenetv3   s    &


r   
pretrainedpretrained_backbone)weightsweights_backboneT)r   progressr+   aux_lossr   )r   r   r+   r   r   kwargsr,   c                 K   s   t | } t|}| dur@d}t|t| jd }t|d}n|du rLd}t|g dd}t|||}| dur|| j	|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:
    Nrm   T   FTTr   replace_stride_with_dilationr   )
r   verifyr   r   rZ   r}   r   rl   load_state_dictget_state_dictr   r   r+   r   r   r   rc   modelr'   r'   r(   r!      s    $

r!   c                 K   s   t | } t|}| dur@d}t|t| jd }t|d}n|du rLd}t|g dd}t|||}| dur|| j	|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:
    Nrm   Tr   r   r   r   )
r   r   r   r   rZ   r}   r   rl   r   r   r   r'   r'   r(   r"     s    $

r"   c                 K   s   t | } t|}| dur@d}t|t| jd }t|d}n|du rLd}t|dd}t|||}| dur~|| j	|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:
    Nrm   Tr   )r   dilatedr   )
r   r   r   r   rZ   r}   r   r   r   r   r   r'   r'   r(   r    K  s    "

r    )
_ModelURLs)Zdeeplabv3_resnet50_cocoZdeeplabv3_resnet101_cocoZ!deeplabv3_mobilenet_v3_large_coco);	functoolsr   typingr   r   r   rR   r   torch.nnr   rN   Ztransforms._presetsr	   _apir   r   _metar   _utilsr   r   r   mobilenetv3r   r   r   resnetr   r   r   r   r   r   fcnr   __all__r   rV   r)   r?   rC   Moduler5   r=   boolrl   r   r   r   r   r   r   IMAGENET1K_V1r!   r"   r    r   r{   
model_urlsr'   r'   r'   r(   <module>   s   
 
332