o
    h3                     @   sX  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)de*de*de*dedej+f dej,f
ddZ-de*de*dedej+f dej,fdd Z.d!ej+f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+Z3d,e	ej4ej5f d-e*dedej+f fd.d/Z6G d0d deZ7e ed1e7j8fd2e j9fd3dd4de j9ddd5d6ee7 d7e:d8ee* d9ee  d:ee* deedej+f  d;ede'fd<dZ;dS )=    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*   j/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torchvision/models/detection/ssdlite.py_prediction_block   s   	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 ]!}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_weightZbiasZ	constant_)r.   layerr*   r*   r+   _normal_initI   s   
r7   c                
       s^   e Zd Zdee dee dededejf f fddZdee	 d	e
ee	f fd
dZ  ZS )SSDLiteHeadr    num_anchorsnum_classesr#   .c                    s,   t    t||||| _t|||| _d S N)super__init__SSDLiteClassificationHeadclassification_headSSDLiteRegressionHeadregression_head)selfr    r9   r:   r#   	__class__r*   r+   r=   R   s   
zSSDLiteHead.__init__xr$   c                 C   s   |  || |dS )N)Zbbox_regression
cls_logits)rA   r?   )rB   rE   r*   r*   r+   forwardY   s   zSSDLiteHead.forward)__name__
__module____qualname__listintr   r   Moduler=   r	   dictstrrG   __classcell__r*   r*   rC   r+   r8   Q   s    &r8   c                
       s@   e Zd Zdee dee dededejf f fddZ  Z	S )r>   r    r9   r:   r#   .c                    sN   t  }t||D ]\}}|t||| d| q	t| t || d S )Nr
   r   
ModuleListzipappendr,   r7   r<   r=   )rB   r    r9   r:   r#   rF   channelsanchorsrC   r*   r+   r=   a   s
   z"SSDLiteClassificationHead.__init__
rH   rI   rJ   rK   rL   r   r   rM   r=   rP   r*   r*   rC   r+   r>   `   s    r>   c                       s<   e Zd Zdee dee dedejf f fddZ  Z	S )r@   r    r9   r#   .c                    sN   t  }t||D ]\}}|t|d| d| q	t| t |d d S )N   r
   rQ   )rB   r    r9   r#   Zbbox_regrU   rV   rC   r*   r+   r=   l   s
   zSSDLiteRegressionHead.__init__rW   r*   r*   rC   r+   r@   k   s    4r@   c                       s^   e Zd Z		ddejdededejf dedef
 fd	d
Zde	de
ee	f f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>   s    z;SSDLiteFeatureExtractorMobileNet.__init__.<locals>.<lambda>i         )r<   r=   r   Zuse_res_connect
ValueErrorr   r'   blockfeaturesrR   r-   r!   r7   extra)rB   r\   r]   r#   r^   r_   Z	get_depthrj   rC   rb   r+   r=   u   s$   

$*
z)SSDLiteFeatureExtractorMobileNet.__init__rE   r$   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>   s    z<SSDLiteFeatureExtractorMobileNet.forward.<locals>.<listcomp>)ri   rT   rj   r   	enumerate)rB   rE   outputrh   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*   rC   r+   rY   t   s    "!rY   r\   trainable_layersc                 C   s   | j } dgdd t| D  t| d g }t|}d|  kr'|ks,td td|dkr4t| n|||  }| d | D ]}| D ]}|d qFq@t| |d |S )Nr   c                 S   s    g | ]\}}t |d dr|qS )Z_is_cnF)getattr)rk   rl   br*   r*   r+   rn      s     z(_mobilenet_extractor.<locals>.<listcomp>r   zYtrainable_layers should be in the range [0, {num_stages}], instead got {trainable_layers}F)ri   ro   lenrg   
parametersZrequires_grad_rY   )r\   rr   r#   Zstage_indicesZ
num_stagesZfreeze_beforert   Z	parameterr*   r*   r+   _mobilenet_extractor   s   &rx   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   progressr:   r   trainable_backbone_layersr#   r   r   r:   r   r   kwargsc                 K   s  t | } t|}d|v rtd | dur%d}td|t| jd }n|du r+d}t| dup3|du|dd}|du }|du rHt	t
jdd	d
}td||||d|}|du r\t| t|||}d}	tdd tdD ddd}
t||	}|
 }t|t|
jkrt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.Nr:   ry   [      gMbP?r/   )epsZmomentum)r   r   r#   Zreduced_tail)@  r   c                 S   s   g | ]}d dgqS )r   r
   r*   )rk   _r*   r*   r+   rn   ,  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,  )      ?r   r   )Zscore_threshZ
nms_threshZdetections_per_imgZtopk_candidatesZ
image_meanZ	image_stdheadT)r   Z
check_hashr*   )r   verifyr   warningswarnr   rv   r|   r   r   r   ZBatchNorm2dr   r7   rx   r   range	det_utilsZretrieve_out_channelsZnum_anchors_per_locationZaspect_ratiosrg   r   r8   Zload_state_dictZget_state_dict)r   r   r:   r   r   r#   r   Zreduce_tailr\   r   Zanchor_generatorr!   r9   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   _metar   r   r   r   Zmobilenetv3r   r   r   Zanchor_utilsr   Zbackbone_utilsr   Zssdr   r   __all__rL   rM   r'   r,   r-   r7   r8   r>   r@   rY   ZMobileNetV2ZMobileNetV3rx   r   r}   ZIMAGENET1K_V1boolr   r*   r*   r*   r+   <module>   s    
&	0
	