o
    ,h;                     @   sl  d dl mZ d dlmZ d dl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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 g dZ G dd de	j!Z"G dd de	j#Z$G dd dZ%G dd de	j#Z&de'e% de(dee de)dede&fdd Z*d!ed"d#d$Z+G d%d& d&eZ,G d'd( d(eZ-G d)d* d*eZ.G d+d, d,eZ/e ed-e,j0fd.dd/d0dee, de)dede&fd1d2Z1e ed-e-j0fd.dd/d0dee- de)dede&fd3d4Z2e ed-e.j0fd.dd/d0dee. de)dede&fd5d6Z3e ed-e/j0fd.dd/d0dee/ de)dede&fd7d8Z4dS )9    )Sequence)partial)AnyCallableOptionalN)nnTensor)
functional   )Conv2dNormActivationPermute)StochasticDepth)ImageClassification)_log_api_usage_once   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface)	ConvNeXtConvNeXt_Tiny_WeightsConvNeXt_Small_WeightsConvNeXt_Base_WeightsConvNeXt_Large_Weightsconvnext_tinyconvnext_smallconvnext_baseconvnext_largec                   @   s   e Zd ZdedefddZdS )LayerNorm2dxreturnc                 C   s>   | dddd}t|| j| j| j| j}| dddd}|S )Nr   r
      r   )permuteF
layer_normnormalized_shapeweightbiasepsselfr!    r-   V/var/www/html/scripts/venv/lib/python3.10/site-packages/torchvision/models/convnext.pyforward    s   zLayerNorm2d.forwardN)__name__
__module____qualname__r   r/   r-   r-   r-   r.   r       s    r    c                
       sR   e Zd Z	ddededeedejf  ddf fddZd	e	de	fd
dZ
  ZS )CNBlockNlayer_scalestochastic_depth_prob
norm_layer.r"   c                    s   t    |d u rttjdd}ttj||dd|ddtg d||tj|d| dd	t	 tjd| |dd	tg d
| _
tt|dd| | _t|d| _d S )Nư>r*      r#   T)kernel_sizepaddinggroupsr)   )r   r
   r#   r      )in_featuresout_featuresr)   )r   r#   r   r
   r   row)super__init__r   r   	LayerNorm
SequentialConv2dr   LinearGELUblock	Parametertorchonesr4   r   stochastic_depth)r,   dimr4   r5   r6   	__class__r-   r.   rB   (   s   


	zCNBlock.__init__inputc                 C   s&   | j | | }| |}||7 }|S N)r4   rH   rL   )r,   rP   resultr-   r-   r.   r/   ?   s   
zCNBlock.forwardrQ   )r0   r1   r2   floatr   r   r   ModulerB   r   r/   __classcell__r-   r-   rN   r.   r3   '   s    r3   c                   @   s8   e Zd Zdedee deddfddZdefdd	ZdS )
CNBlockConfiginput_channelsout_channels
num_layersr"   Nc                 C   s   || _ || _|| _d S rQ   )rW   rX   rY   )r,   rW   rX   rY   r-   r-   r.   rB   H   s   
zCNBlockConfig.__init__c                 C   s>   | j jd }|d7 }|d7 }|d7 }|d7 }|jdi | jS )N(zinput_channels={input_channels}z, out_channels={out_channels}z, num_layers={num_layers})r-   )rO   r0   format__dict__)r,   sr-   r-   r.   __repr__R   s   zCNBlockConfig.__repr__)r0   r1   r2   intr   rB   strr_   r-   r-   r-   r.   rV   F   s    

rV   c                       s   e Zd Z					ddee dededed	eed
e	j
f  deed
e	j
f  deddf fddZdedefddZdedefddZ  ZS )r           r7     Nblock_settingr5   r4   num_classesrH   .r6   kwargsr"   c                    s  t    t|  |stdt|trtdd |D s!td|d u r't}|d u r1t	t
dd}g }|d j}	|td|	d	d	d|d d
d tdd |D }
d}|D ]D}g }t|jD ]}|| |
d  }|||j|| |d7 }q]|tj|  |jd ur|t||jtj|j|jddd qTtj| | _td| _|d }|jd ur|jn|j}t||tdt||| _|  D ] }t|tjtjfrtjj|jdd |jd urtj |j qd S )Nz%The block_setting should not be emptyc                 S   s   g | ]}t |tqS r-   )
isinstancerV   ).0r^   r-   r-   r.   
<listcomp>k   s    z%ConvNeXt.__init__.<locals>.<listcomp>z/The block_setting should be List[CNBlockConfig]r7   r8   r   r#   r=   T)r:   strider;   r6   activation_layerr)   c                 s   s    | ]}|j V  qd S rQ   )rY   )rh   cnfr-   r-   r.   	<genexpr>   s    z$ConvNeXt.__init__.<locals>.<genexpr>g      ?r   r
   )r:   rj   g{Gz?)std)!rA   rB   r   
ValueErrorrg   r   all	TypeErrorr3   r   r    rW   appendr   sumrangerY   r   rD   rX   rE   featuresAdaptiveAvgPool2davgpoolFlattenrF   
classifiermodulesinittrunc_normal_r(   r)   zeros_)r,   rd   r5   r4   re   rH   r6   rf   layersfirstconv_output_channelstotal_stage_blocksstage_block_idrl   stage_sd_prob	lastblocklastconv_output_channelsmrN   r-   r.   rB   \   sp   





zConvNeXt.__init__r!   c                 C   s"   |  |}| |}| |}|S rQ   )rv   rx   rz   r+   r-   r-   r.   _forward_impl   s   


zConvNeXt._forward_implc                 C   s
   |  |S rQ   )r   r+   r-   r-   r.   r/      s   
zConvNeXt.forward)rb   r7   rc   NN)r0   r1   r2   listrV   rS   r`   r   r   r   rT   r   rB   r   r   r/   rU   r-   r-   rN   r.   r   [   s2    	Nr   rd   r5   weightsprogressrf   r"   c                 K   sR   |d urt |dt|jd  t| fd|i|}|d ur'||j|dd |S )Nre   
categoriesr5   T)r   
check_hash)r   lenmetar   load_state_dictget_state_dict)rd   r5   r   r   rf   modelr-   r-   r.   	_convnext   s   r   )    r   zNhttps://github.com/pytorch/vision/tree/main/references/classification#convnexta  
        These weights improve upon the results of the original paper by using a modified version of TorchVision's
        `new training recipe
        <https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/>`_.
    )min_sizer   recipe_docsc                	   @   D   e Zd Zedeedddi eddddd	id
dddZeZdS )r   z>https://download.pytorch.org/models/convnext_tiny-983f1562.pth      	crop_sizeresize_sizeiH<ImageNet-1KgzGT@gMbX	X@zacc@1zacc@5gm@gV-G[@
num_params_metrics_ops
_file_sizeurl
transformsr   N	r0   r1   r2   r   r   r   _COMMON_METAIMAGENET1K_V1DEFAULTr-   r-   r-   r.   r      $    r   c                	   @   r   )r   z?https://download.pytorch.org/models/convnext_small-0c510722.pthr      r   iHZr   gClT@g)X@r   g|?5^!@g"~g@r   r   Nr   r-   r-   r-   r.   r      r   r   c                	   @   r   )r   z>https://download.pytorch.org/models/convnext_base-6075fbad.pthr      r   ihGr   gU@gHz7X@r   g(\µ.@g/$!u@r   r   Nr   r-   r-   r-   r.   r      r   r   c                	   @   r   )r   z?https://download.pytorch.org/models/convnext_large-ea097f82.pthr   r   r   ir   g"~U@gX9v>X@r   g|?5.A@gK@r   r   Nr   r-   r-   r-   r.   r     r   r   
pretrained)r   T)r   r   c                 K   X   t | } tdddtdddtdddtdddg}|dd	}t||| |fi |S )
a  ConvNeXt Tiny model architecture from the
    `A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_ paper.

    Args:
        weights (:class:`~torchvision.models.convnext.ConvNeXt_Tiny_Weights`, optional): The pretrained
            weights to use. See :class:`~torchvision.models.convnext.ConvNeXt_Tiny_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.
        **kwargs: parameters passed to the ``torchvision.models.convnext.ConvNext``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/convnext.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ConvNeXt_Tiny_Weights
        :members:
    `      r#        	   Nr5   g?)r   verifyrV   popr   r   r   rf   rd   r5   r-   r-   r.   r   "     




r   c                 K   r   )
a  ConvNeXt Small model architecture from the
    `A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_ paper.

    Args:
        weights (:class:`~torchvision.models.convnext.ConvNeXt_Small_Weights`, optional): The pretrained
            weights to use. See :class:`~torchvision.models.convnext.ConvNeXt_Small_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.
        **kwargs: parameters passed to the ``torchvision.models.convnext.ConvNext``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/convnext.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ConvNeXt_Small_Weights
        :members:
    r   r   r#   r   r      Nr5   g?)r   r   rV   r   r   r   r-   r-   r.   r   A     




r   c                 K   r   )
a  ConvNeXt Base model architecture from the
    `A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_ paper.

    Args:
        weights (:class:`~torchvision.models.convnext.ConvNeXt_Base_Weights`, optional): The pretrained
            weights to use. See :class:`~torchvision.models.convnext.ConvNeXt_Base_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.
        **kwargs: parameters passed to the ``torchvision.models.convnext.ConvNext``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/convnext.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ConvNeXt_Base_Weights
        :members:
          r#   i   i   r   Nr5         ?)r   r   rV   r   r   r   r-   r-   r.   r   b  r   r   c                 K   r   )
a  ConvNeXt Large model architecture from the
    `A ConvNet for the 2020s <https://arxiv.org/abs/2201.03545>`_ paper.

    Args:
        weights (:class:`~torchvision.models.convnext.ConvNeXt_Large_Weights`, optional): The pretrained
            weights to use. See :class:`~torchvision.models.convnext.ConvNeXt_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.
        **kwargs: parameters passed to the ``torchvision.models.convnext.ConvNext``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/convnext.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ConvNeXt_Large_Weights
        :members:
    r   r   r#   r   i   r   Nr5   r   )r   r   rV   r   r   r   r-   r-   r.   r     r   r   )5collections.abcr   	functoolsr   typingr   r   r   rJ   r   r   torch.nnr	   r%   ops.miscr   r   ops.stochastic_depthr   transforms._presetsr   utilsr   _apir   r   r   _metar   _utilsr   r   __all__rC   r    rT   r3   rV   r   r   rS   boolr   r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   <module>   s    Y
**