a
    d3                     @   s@  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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Z0ej-ddd Z1G d!d" d"ej-Z2G d#d$ d$e*Z3G d%d& d&e*Z4G d'd( d(ej-Z5eej6ej7f e,edej-f d)d*d+Z8G d,d deZ9e ed-e9j:fd.e"j;fd/dd0de"j;ddd1e
e9 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DictList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,   m/var/www/html/stable-diffusion-webui/venv/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_)r0   Zlayerr,   r,   r-   _normal_initI   s
    
r7   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"   r:   r;   r%   	__class__r,   r-   r>   R   s    
zSSDLiteHead.__init__xr&   c                 C   s   |  || |dS )N)Zbbox_regression
cls_logits)rB   r@   )rC   rG   r,   r,   r-   forwardY   s    zSSDLiteHead.forward)__name__
__module____qualname__r   intr   r
   Moduler>   r   r   strrI   __classcell__r,   r,   rD   r-   r8   Q   s   r8   c                       s:   e Zd Zee ee eedejf d fddZ  Z	S )r?   .r9   c                    sN   t  }t||D ] \}}|t||| d| qt| t || d S )Nr   r
   
ModuleListzipappendr.   r7   r=   r>   )rC   r"   r:   r;   r%   rH   channelsanchorsrD   r,   r-   r>   a   s
    z"SSDLiteClassificationHead.__init__
rJ   rK   rL   r   rM   r   r
   rN   r>   rP   r,   r,   rD   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 )rA   .)r"   r:   r%   c                    sN   t  }t||D ] \}}|t|d| d| qt| t |d d S )N   r   rQ   )rC   r"   r:   r%   Zbbox_regrU   rV   rD   r,   r-   r>   l   s
    zSSDLiteRegressionHead.__init__rW   r,   r,   rD   r-   rA   k   s   rA   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<   )maxrM   )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#   r7   extra)rC   r\   r]   r%   r^   r_   Z	get_depthrk   rD   rb   r-   r>   u   s$    

$*z)SSDLiteFeatureExtractorMobileNet.__init__rF   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)rC   rG   outputri   r,   r,   r-   rI      s    

z(SSDLiteFeatureExtractorMobileNet.forward)rZ   r[   )rJ   rK   rL   r
   rN   rM   r   floatr>   r   r   rO   rI   rP   r,   r,   rD   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)rJ   rK   rL   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   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 |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.Nr;   rz   [      gMbP?r1   )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head)r   )r    verifyr   warningswarnr   rw   r}   r   r   r
   ZBatchNorm2dr   r7   ry   r   range	det_utilsZretrieve_out_channelsZnum_anchors_per_locationZaspect_ratiosrh   r   r8   Zload_state_dictZget_state_dict)r   r   r;   r   r   r%   r   Zreduce_tailr\   r   Zanchor_generatorr#   r:   defaultsmodelr,   r,   r-   r!      sp    8



	
)>r   collectionsr   	functoolsr   typingr   r   r   r   r   r	   r5   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__rM   rN   r)   r.   r/   r7   r8   r?   rA   rY   ZMobileNetV2ZMobileNetV3ry   r    r~   ZIMAGENET1K_V1boolr!   r,   r,   r,   r-   <module>   sn     	1