U
    (d!4                     @   sR  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZ d dl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 d
dlmZmZ d
dlmZ d
dl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gZ*e+e+e+edej,f ej-dddZ.e+e+edej,f ej-dddZ/ej,ddd Z0G d!d" d"ej,Z1G d#d$ d$e)Z2G d%d& d&e)Z3G d'd( d(ej,Z4eej5ej6f e+edej,f d)d*d+Z7G d,d deZ8ed-e8j9fd.e j:fd/dd0de j:ddd1e
e8 e;e
e+ e
e  e
e+ e
edej,f  ee(d2d3dZ<d
d4lm=Z= e=d5e8j9j>iZ?dS )6    N)OrderedDict)partial)AnyCallableDictListOptionalUnion)nnTensor   )Conv2dNormActivation)ObjectDetection)_log_api_usage_once   )	mobilenet)WeightsEnumWeights)_COCO_CATEGORIES)handle_legacy_interface_ovewrite_value_param)MobileNet_V3_Large_Weightsmobilenet_v3_large   )_utils)DefaultBoxGenerator)_validate_trainable_layers)SSDSSDScoringHead%SSDLite320_MobileNet_V3_Large_Weightsssdlite320_mobilenet_v3_large.)in_channelsout_channelskernel_size
norm_layerreturnc              
   C   s(   t t| | || |t jdt | |dS )N)r#   groupsr$   activation_layerr   )r
   
Sequentialr   ReLU6Conv2d)r!   r"   r#   r$    r+   H/tmp/pip-unpacked-wheel-vx7f76es/torchvision/models/detection/ssdlite.py_prediction_block   s    	r-   )r!   r"   r$   r%   c                 C   sJ   t j}|d }t t| |d||dt||dd|||dt||d||dS )Nr   r   )r#   r$   r'   r   )r#   Zstrider&   r$   r'   )r
   r)   r(   r   )r!   r"   r$   Z
activationZintermediate_channelsr+   r+   r,   _extra_block0   s6        
    r.   )convc                 C   sP   |   D ]B}t|tjrtjjj|jddd |jd k	rtjj	|jd qd S )Ng        Q?)ZmeanZstd)
modules
isinstancer
   r*   torchinitZnormal_ZweightZbiasZ	constant_)r/   Zlayerr+   r+   r,   _normal_initI   s
    
r5   c                       sV   e Zd Zee ee eedejf d fddZee	 e
ee	f dddZ  ZS )SSDLiteHead.r!   num_anchorsnum_classesr$   c                    s,   t    t||||| _t|||| _d S N)super__init__SSDLiteClassificationHeadclassification_headSSDLiteRegressionHeadregression_head)selfr!   r8   r9   r$   	__class__r+   r,   r<   R   s    
zSSDLiteHead.__init__xr%   c                 C   s   |  || |dS )N)Zbbox_regression
cls_logits)r@   r>   )rA   rE   r+   r+   r,   forwardY   s    zSSDLiteHead.forward)__name__
__module____qualname__r   intr   r
   Moduler<   r   r   strrG   __classcell__r+   r+   rB   r,   r6   Q   s      r6   c                       s:   e Zd Zee ee eedejf d fddZ  Z	S )r=   .r7   c                    sN   t  }t||D ] \}}|t||| d| qt| t || d S )Nr   r
   
ModuleListzipappendr-   r5   r;   r<   )rA   r!   r8   r9   r$   rF   channelsanchorsrB   r+   r,   r<   a   s
    z"SSDLiteClassificationHead.__init__
rH   rI   rJ   r   rK   r   r
   rL   r<   rN   r+   r+   rB   r,   r=   `   s
      r=   c                       s8   e Zd Zee ee edejf d fddZ  Z	S )r?   .)r!   r8   r$   c                    sN   t  }t||D ] \}}|t|d| d| qt| t |d d S )N   r   rO   )rA   r!   r8   r$   Zbbox_regrS   rT   rB   r+   r,   r<   l   s
    zSSDLiteRegressionHead.__init__rU   r+   r+   rB   r,   r?   k   s   r?   c                       sP   e Zd Zd
ejeedejf eed fddZe	e
ee	f ddd	Z  ZS ) SSDLiteFeatureExtractorMobileNet      ?   .)backbonec4_posr$   
width_mult	min_depthc              	      s   t    t|  || jr$tdttj|d | || jd f tj|| jdd  f||d d   | _ fdd}t	t
|d j|d|t
|d|d|t
|d|d|t
|d|d	|g}t| || _d S )
Nz0backbone[c4_pos].use_res_connect should be Falser   r   c                    s   t  t|  S r:   )maxrK   )dr]   r\   r+   r,   <lambda>       z;SSDLiteFeatureExtractorMobileNet.__init__.<locals>.<lambda>i         )r;   r<   r   Zuse_res_connect
ValueErrorr
   r(   blockfeaturesrP   r.   r"   r5   extra)rA   rZ   r[   r$   r\   r]   Z	get_depthri   rB   r`   r,   r<   u   s$    

 (z)SSDLiteFeatureExtractorMobileNet.__init__rD   c                 C   sV   g }| j D ]}||}|| q
| jD ]}||}|| q(tdd t|D S )Nc                 S   s   g | ]\}}t ||fqS r+   )rM   ).0ivr+   r+   r,   
<listcomp>   s     z<SSDLiteFeatureExtractorMobileNet.forward.<locals>.<listcomp>)rh   rR   ri   r   	enumerate)rA   rE   outputrg   r+   r+   r,   rG      s    

z(SSDLiteFeatureExtractorMobileNet.forward)rX   rY   )rH   rI   rJ   r
   rL   rK   r   floatr<   r   r   rM   rG   rN   r+   r+   rB   r,   rW   t   s     !rW   )rZ   trainable_layersr$   c                 C   s   | j } dgdd t| D  t| d g }t|}d|  krH|ksRn td|dkrbt| n
|||  }| d | D ]}| D ]}|d qqzt| |d |S )Nr   c                 S   s    g | ]\}}t |d dr|qS )Z_is_cnF)getattr)rj   rk   br+   r+   r,   rm      s      z(_mobilenet_extractor.<locals>.<listcomp>r   zYtrainable_layers should be in the range [0, {num_stages}], instead got {trainable_layers}F)rh   rn   lenrf   
parametersZrequires_grad_rW   )rZ   rq   r$   Zstage_indicesZ
num_stagesZfreeze_beforers   Z	parameterr+   r+   r,   _mobilenet_extractor   s    &rw   c                
   @   s4   e Zd Zedededddddiidd	d
ZeZdS )r   zShttps://download.pytorch.org/models/ssdlite320_mobilenet_v3_large_coco-a79551df.pthi}4 )r   r   z]https://github.com/pytorch/vision/tree/main/references/detection#ssdlite320-mobilenetv3-largezCOCO-val2017Zbox_mapgL5@zSThese weights were produced by following a similar training recipe as on the paper.)Z
num_params
categoriesZmin_sizeZrecipeZ_metricsZ_docs)urlZ
transformsmetaN)rH   rI   rJ   r   r   r   COCO_V1DEFAULTr+   r+   r+   r,   r      s     Z
pretrainedZpretrained_backbone)weightsweights_backboneT)r}   progressr9   r~   trainable_backbone_layersr$   )r}   r   r9   r~   r   r$   kwargsr%   c                 K   s  t | } t|}d|kr&td | dk	rHd}t|t| jd }n|dkrTd}t| dk	pd|dk	|dd}|dk}|dkrt	t
jddd	}tf ||||d
|}|dkrt| t|||}d}	tdd tdD ddd}
t||	}|
 }t|t|
jkr(tdt| dt|
j dddddddgdddgd}||}t||
|	|fdt||||i|}| dk	r|| j|d |S )a  SSDlite model architecture with input size 320x320 and a MobileNetV3 Large backbone, as
    described at `Searching for MobileNetV3 <https://arxiv.org/abs/1905.02244>`__ and
    `MobileNetV2: Inverted Residuals and Linear Bottlenecks <https://arxiv.org/abs/1801.04381>`__.

    .. betastatus:: detection module

    See :func:`~torchvision.models.detection.ssd300_vgg16` for more details.

    Example:

        >>> model = torchvision.models.detection.ssdlite320_mobilenet_v3_large(weights=SSDLite320_MobileNet_V3_Large_Weights.DEFAULT)
        >>> model.eval()
        >>> x = [torch.rand(3, 320, 320), torch.rand(3, 500, 400)]
        >>> predictions = model(x)

    Args:
        weights (:class:`~torchvision.models.detection.SSDLite320_MobileNet_V3_Large_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.detection.SSDLite320_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).
        weights_backbone (:class:`~torchvision.models.MobileNet_V3_Large_Weights`, optional): The pretrained
            weights for the backbone.
        trainable_backbone_layers (int, optional): number of trainable (not frozen) layers
            starting from final block. Valid values are between 0 and 6, with 6 meaning all
            backbone layers are trainable. If ``None`` is passed (the default) this value is
            set to 6.
        norm_layer (callable, optional): Module specifying the normalization layer to use.
        **kwargs: parameters passed to the ``torchvision.models.detection.ssd.SSD``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/detection/ssd.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.detection.SSDLite320_MobileNet_V3_Large_Weights
        :members:
    sizez?The size of the model is already fixed; ignoring the parameter.Nrx   [      gMbP?r0   )ZepsZmomentum)r}   r   r$   Zreduced_tail)@  r   c                 S   s   g | ]}d dgqS )r   r   r+   )rj   _r+   r+   r,   rm   )  s     z1ssdlite320_mobilenet_v3_large.<locals>.<listcomp>g?gffffff?)Z	min_ratioZ	max_ratioz4The length of the output channels from the backbone z? do not match the length of the anchor generator aspect ratios g?i,  g      ?)Zscore_threshZ
nms_threshZdetections_per_imgZtopk_candidatesZ
image_meanZ	image_stdhead)r   )r   verifyr   warningswarnr   ru   rz   r   r   r
   ZBatchNorm2dr   r5   rw   r   range	det_utilsZretrieve_out_channelsZnum_anchors_per_locationZaspect_ratiosrf   r   r6   Zload_state_dictZget_state_dict)r}   r   r9   r~   r   r$   r   Zreduce_tailrZ   r   Zanchor_generatorr"   r8   defaultsmodelr+   r+   r,   r       s|    7


      
	
)
_ModelURLsZ"ssdlite320_mobilenet_v3_large_coco)@r   collectionsr   	functoolsr   typingr   r   r   r   r   r	   r3   r
   r   Zops.miscr   Ztransforms._presetsr   utilsr    r   Z_apir   r   Z_metar   r   r   r   Zmobilenetv3r   r   r   Zanchor_utilsr   Zbackbone_utilsr   Zssdr   r   __all__rK   rL   r(   r-   r.   r5   r6   r=   r?   rW   ZMobileNetV2ZMobileNetV3rw   r   r{   ZIMAGENET1K_V1boolr    r   ry   Z
model_urlsr+   r+   r+   r,   <module>   s~        	1y 