U
    (dg                  	   @   s^  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 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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	d lm*Z* e*d!e%j&j+iZ,dS )"    )OrderedDict)partial)AnyDictOptional)nnTensor)
functional   )SemanticSegmentation)_log_api_usage_once   )WeightsEnumWeights)_VOC_CATEGORIES)IntermediateLayerGetterhandle_legacy_interface_ovewrite_value_param)MobileNetV3MobileNet_V3_Large_Weightsmobilenet_v3_large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__ J/tmp/pip-unpacked-wheel-vx7f76es/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+   featuresr1   resultr(   r(   r)   forward+   s    

zLRASPP.forward)r   )__name__
__module____qualname____doc__r   Moduleintr"   r   r   strr7   __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.   )rA   rB   r2   r3   r4   rC   rD   )r%   r+   rE   rF   xsr(   r(   r)   r7   F   s    

zLRASPPHead.forward)
r8   r9   r:   r=   r"   r   r>   r   r7   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>r@   rE   rF   )Zreturn_layers)r5   	enumeratelenZout_channelsr   r>   r   )r   r   Zstage_indicesZlow_posZhigh_posr   r   r(   r(   r)   _lraspp_mobilenetv3R   s    &

rR   c                   @   s>   e Zd Zedeedddeddddd	d
idddZeZdS )r   zJhttps://download.pytorch.org/models/lraspp_mobilenet_v3_large-d234d4ea.pthi  )Zresize_sizei"(1 )r@   r@   z]https://github.com/pytorch/vision/tree/main/references/segmentation#lraspp_mobilenet_v3_largezCOCO-val2017-VOC-labelsg33333L@gV@)ZmiouZ	pixel_accz
                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_docs)urlZ
transformsmetaN)	r8   r9   r:   r   r   r   r   COCO_WITH_VOC_LABELS_V1DEFAULTr(   r(   r(   r)   r   `   s    
Z
pretrainedZpretrained_backbone)weightsweights_backboneNT)rX   progressr   rY   )rX   rZ   r   rY   kwargsr    c                 K   s   | ddrtdt| } t|}| dk	rJd}t|t| jd }n|dkrVd}t|dd}t	||}| dk	r|
| j|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 lossNrS      T)rX   Zdilated)rZ   )popNotImplementedErrorr   verifyr   r   rQ   rU   r   rR   Zload_state_dictZget_state_dict)rX   rZ   r   rY   r[   r   modelr(   r(   r)   r   x   s    %


)
_ModelURLsZlraspp_mobilenet_v3_large_coco)-collectionsr   	functoolsr   typingr   r   r   Ztorchr   r   Ztorch.nnr	   r2   Ztransforms._presetsr   utilsr   Z_apir   r   Z_metar   _utilsr   r   r   Zmobilenetv3r   r   r   __all__r<   r   r#   r=   rR   r   rV   ZIMAGENET1K_V1boolr   ra   rT   Z
model_urlsr(   r(   r(   r)   <module>   sJ   
#7 