a
    h3                     @   s8  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	 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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 Z/G d!d" d"ej+Z0G d#d$ d$e(Z1G d%d& d&e(Z2G d'd( d(ej+Z3e	ej4ej5f e*edej+f d)d*d+Z6G d,d deZ7e ed-e7j8fd.e j9fd/dd0de j9ddd1ee7 e:ee* ee  ee* eedej+f  ee'd2d3dZ;dS )4    N)OrderedDict)partial)AnyCallableOptionalUnion)nnTensor   )Conv2dNormActivation)ObjectDetection)_log_api_usage_once   )	mobilenet)register_modelWeightsWeightsEnum)_COCO_CATEGORIES)_ovewrite_value_paramhandle_legacy_interface)mobilenet_v3_largeMobileNet_V3_Large_Weights   )_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*   R/var/www/auris/lib/python3.9/site-packages/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   s&    


r-   )convc                 C   sP   |   D ]B}t|tjrtjjj|jddd |jd urtjj	|jd qd S )Ng        Q?)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__listintr   r   Moduler<   r	   dict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   rK   rL   r   r   rM   r<   rP   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
   rQ   )rA   r    r8   r#   Zbbox_regrU   rV   rB   r*   r+   r<   l   s
    zSSDLiteRegressionHead.__init__rW   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g |d | || jd R  tj|| jdd  g||d d  R  | _ 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:   )maxrL   )dr_   r^   r*   r+   <lambda>       z;SSDLiteFeatureExtractorMobileNet.__init__.<locals>.<lambda>i         )r;   r<   r   Zuse_res_connect
ValueErrorr   r'   blockfeaturesrR   r-   r!   r5   extra)rA   r\   r]   r#   r^   r_   Z	get_depthrk   rB   rb   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*   )rO   ).0ivr*   r*   r+   
<listcomp>   rd   z<SSDLiteFeatureExtractorMobileNet.forward.<locals>.<listcomp>)rj   rT   rk   r   	enumerate)rA   rE   outputri   r*   r*   r+   rG      s    

z(SSDLiteFeatureExtractorMobileNet.forward)rZ   r[   )rH   rI   rJ   r   rM   rL   r   floatr<   r	   rN   rO   rG   rP   r*   r*   rB   r+   rY   t   s     !rY   )r\   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)rl   rm   br*   r*   r+   ro      rd   z(_mobilenet_extractor.<locals>.<listcomp>r   zYtrainable_layers should be in the range [0, {num_stages}], instead got {trainable_layers}F)rj   rp   lenrh   
parametersZrequires_grad_rY   )r\   rs   r#   Zstage_indicesZ
num_stagesZfreeze_beforeru   Z	parameterr*   r*   r+   _mobilenet_extractor   s    &ry   c                   @   s8   e Zd Zedededddddiidd	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@g-?gt*@zSThese weights were produced by following a similar training recipe as on the paper.)Z
num_params
categoriesZmin_sizeZrecipeZ_metricsZ_ops
_file_sizeZ_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|v r&td | durJd}td|t| jd }n|du rVd}t| dupf|du|dd}|du }|du rt	t
jdd	d
}tf ||||d|}|du 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g dg dd}i ||}t||
|	|fdt||||i|}| dur|| j|d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/ssdlite.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.Nr9   rz   [      gMbP?r/   )ZepsZmomentum)r   r   r#   Zreduced_tail)@  r   c                 S   s   g | ]}d dgqS )r   r
   r*   )rl   _r*   r*   r+   ro   ,  rd   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,  )      ?r   r   )Zscore_threshZ
nms_threshZdetections_per_imgZtopk_candidatesZ
image_meanZ	image_stdheadT)r   Z
check_hash)r   verifyr   warningswarnr   rw   r}   r   r   r   ZBatchNorm2dr   r5   ry   r   range	det_utilsZretrieve_out_channelsZnum_anchors_per_locationZaspect_ratiosrh   r   r6   Zload_state_dictZget_state_dict)r   r   r9   r   r   r#   r   Zreduce_tailr\   r   Zanchor_generatorr!   r8   defaultsmodelr*   r*   r+   r      sp    8



	
)<r   collectionsr   	functoolsr   typingr   r   r   r   r3   r   r	   Zops.miscr   Ztransforms._presetsr   utilsr    r   Z_apir   r   r   Z_metar   r   r   r   Zmobilenetv3r   r   r   Zanchor_utilsr   Zbackbone_utilsr   Zssdr   r   __all__rL   rM   r'   r,   r-   r5   r6   r=   r?   rY   ZMobileNetV2ZMobileNetV3ry   r   r~   ZIMAGENET1K_V1boolr   r*   r*   r*   r+   <module>   sn    	1