U
    ­Ê¹jÛ  ã                
   @   sL  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 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dgZ G dd„ de	j!ƒZ"G dd„ de	j!ƒZ#ee$e"dœdd„Z%G dd„ deƒZ&eƒ ede&j'fdej(fd�dddej(dœee& e)ee$ ee ee"dœdd„ƒƒZ*dS ) é    )ÚOrderedDict)Úpartial)ÚAnyÚDictÚOptional)ÚnnÚTensor)Ú
functionalé   )ÚSemanticSegmentation)Ú_log_api_usage_onceé   )Úregister_modelÚWeightsÚWeightsEnum)Ú_VOC_CATEGORIES)Ú_ovewrite_value_paramÚhandle_legacy_interfaceÚIntermediateLayerGetter)Úmobilenet_v3_largeÚMobileNet_V3_Large_WeightsÚMobileNetV3ÚLRASPPÚ!LRASPP_MobileNet_V3_Large_WeightsÚlraspp_mobilenet_v3_largec                       sL   e Zd ZdZd
ejeeeeddœ‡ fdd„Zee	e
ef dœdd	„Z‡  ZS )r   a  
    Implements a Lite R-ASPP Network for semantic segmentation from
    `"Searching for MobileNetV3"
    <https://arxiv.org/abs/1905.02244>`_.

    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
            "high" for the high level feature map and "low" for the low level feature map.
        low_channels (int): the number of channels of the low level features.
        high_channels (int): the number of channels of the high level features.
        num_classes (int, optional): number of output classes of the model (including the background).
        inter_channels (int, optional): the number of channels for intermediate computations.
    é€   N)ÚbackboneÚlow_channelsÚhigh_channelsÚnum_classesÚinter_channelsÚreturnc                    s,   t ƒ  ¡  t| ƒ || _t||||ƒ| _d S )N)ÚsuperÚ__init__r   r   Ú
LRASPPHeadÚ
classifier)Úselfr   r   r   r   r    ©Ú	__class__© úe/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/segmentation/lraspp.pyr#   #   s    
zLRASPP.__init__©Úinputr!   c                 C   sB   |   |¡}|  |¡}tj||jdd … ddd�}tƒ }||d< |S )NéþÿÿÿÚbilinearF©ÚsizeÚmodeZalign_cornersÚout)r   r%   ÚFÚinterpolateÚshaper   )r&   r,   Úfeaturesr2   Úresultr)   r)   r*   Úforward+   s    

zLRASPP.forward)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚModuleÚintr#   r   r   Ústrr8   Ú__classcell__r)   r)   r'   r*   r      s    ÿ    þc                       sB   e Zd Zeeeeddœ‡ fdd„Zeeef edœdd„Z‡  Z	S )r$   N)r   r   r   r    r!   c              	      s‚   t ƒ  ¡  t tj||ddd�t |¡tjdd�¡| _t t d¡tj||ddd�t 	¡ ¡| _
t ||d¡| _t ||d¡| _d S )Né   F)ZbiasT)Zinplace)r"   r#   r   Z
SequentialZConv2dZBatchNorm2dZReLUÚcbrZAdaptiveAvgPool2dZSigmoidÚscaleÚlow_classifierÚhigh_classifier)r&   r   r   r   r    r'   r)   r*   r#   7   s    

ýýzLRASPPHead.__init__r+   c                 C   s\   |d }|d }|   |¡}|  |¡}|| }tj||jdd … ddd�}|  |¡|  |¡ S )NÚlowÚhighr-   r.   Fr/   )rB   rC   r3   r4   r5   rD   rE   )r&   r,   rF   rG   ÚxÚsr)   r)   r*   r8   F   s    

zLRASPPHead.forward)
r9   r:   r;   r>   r#   r   r?   r   r8   r@   r)   r)   r'   r*   r$   6   s   r$   )r   r   r!   c                 C   sz   | j } dgdd„ t| ƒD ƒ t| ƒd g }|d }|d }| | j}| | j}t| t|ƒdt|ƒdid	�} t| |||ƒS )
Nr   c                 S   s    g | ]\}}t |d dƒr|‘qS )Z_is_cnF)Úgetattr)Ú.0ÚiÚbr)   r)   r*   Ú
<listcomp>V   s      z'_lraspp_mobilenetv3.<locals>.<listcomp>rA   éüÿÿÿéÿÿÿÿrF   rG   )Zreturn_layers)r6   Ú	enumerateÚlenZout_channelsr   r?   r   )r   r   Zstage_indicesZlow_posZhigh_posr   r   r)   r)   r*   Ú_lraspp_mobilenetv3R   s    &

rS   c                   @   sB   e Zd Zedeedd�deddddd	d
œiddddœd�ZeZdS )r   zJhttps://download.pytorch.org/models/lraspp_mobilenet_v3_large-d234d4ea.pthi  )Zresize_sizei"(1 )rA   rA   z]https://github.com/pytorch/vision/tree/main/references/segmentation#lraspp_mobilenet_v3_largezCOCO-val2017-VOC-labelsg33333óL@gÍÌÌÌÌÌV@)ZmiouZ	pixel_accgã¥›Ä ° @g{®Gáú(@z¦
                These weights were trained on a subset of COCO, using only the 20 categories that are present in the
                Pascal VOC dataset.
            )Z
num_paramsÚ
categoriesZmin_sizeZrecipeZ_metricsZ_opsÚ
_file_sizeZ_docs)ÚurlZ
transformsÚmetaN)	r9   r:   r;   r   r   r   r   ÚCOCO_WITH_VOC_LABELS_V1ÚDEFAULTr)   r)   r)   r*   r   `   s$   
þÿóýZ
pretrainedZpretrained_backbone)ÚweightsÚweights_backboneNT)rZ   Úprogressr   r[   )rZ   r\   r   r[   Úkwargsr!   c                 K   sŽ   |  dd¡rtdƒ‚t | ¡} t |¡}| dk	rLd}td|t| jd ƒƒ}n|dkrXd}t|dd	�}t	||ƒ}| dk	rŠ| 
| j|dd
�¡ |S )a|  Constructs a Lite R-ASPP Network model with a MobileNetV3-Large backbone from
    `Searching for MobileNetV3 <https://arxiv.org/abs/1905.02244>`_ paper.

    .. betastatus:: segmentation module

    Args:
        weights (:class:`~torchvision.models.segmentation.LRASPP_MobileNet_V3_Large_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.segmentation.LRASPP_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: parameters passed to the ``torchvision.models.segmentation.LRASPP``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/segmentation/lraspp.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.segmentation.LRASPP_MobileNet_V3_Large_Weights
        :members:
    Zaux_lossFz&This model does not use auxiliary lossNr   rT   é   T)rZ   Zdilated)r\   Z
check_hash)ÚpopÚNotImplementedErrorr   Úverifyr   r   rR   rW   r   rS   Zload_state_dictZget_state_dict)rZ   r\   r   r[   r]   r   Úmodelr)   r)   r*   r   z   s    &


)+Úcollectionsr   Ú	functoolsr   Útypingr   r   r   Ztorchr   r   Ztorch.nnr	   r3   Ztransforms._presetsr   Úutilsr   Z_apir   r   r   Ú_metar   Z_utilsr   r   r   Zmobilenetv3r   r   r   Ú__all__r=   r   r$   r>   rS   r   rX   ZIMAGENET1K_V1Úboolr   r)   r)   r)   r*   Ú<module>   s@   
#þûù