
    YhA                     >   d dl Z d dlmZ d dlmZ d dlmZmZ d dlZd dl	m
Z
 d dlm
c mZ d dlmc 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e
j                   Z! G d de
j"                  Z# G d de
j$                  Z% G d de
j                   Z&de
j         dede'ddfdZ(de)de*e)e)e)e)f         de)dee         de'dede&fdZ+d ed!d"d#Z, G d$ d%e          Z- G d& d'e          Z. G d( d)e          Z/ G d* d+e          Z0 e             ed,e-j1        f-          dd.d/dee-         de'dede&fd0                        Z2 e             ed,e.j1        f-          dd.d/dee.         de'dede&fd1                        Z3 e             ed,e/j1        f-          dd.d/dee/         de'dede&fd2                        Z4 e             ed,e0j1        f-          dd.d/dee0         de'dede&fd3                        Z5dS )4    N)OrderedDict)partial)AnyOptional)Tensor   )ImageClassification)_log_api_usage_once   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface)	DenseNetDenseNet121_WeightsDenseNet161_WeightsDenseNet169_WeightsDenseNet201_Weightsdensenet121densenet161densenet169densenet201c                   8    e Zd Z	 ddedededededdf fd	Zd
ee         defdZ	dee         defdZ
ej        j        dee         defd            Zej        j        dee         defd            Zej        j        dedefd            ZdedefdZ xZS )_DenseLayerFnum_input_featuresgrowth_ratebn_size	drop_ratememory_efficientreturnNc                    t                                                       t          j        |          | _        t          j        d          | _        t          j        |||z  ddd          | _        t          j        ||z            | _	        t          j        d          | _
        t          j        ||z  |dddd          | _        t          |          | _        || _        d S )NTinplacer   Fkernel_sizestridebias   r'   r(   paddingr)   )super__init__nnBatchNorm2dnorm1ReLUrelu1Conv2dconv1norm2relu2conv2floatr    r!   )selfr   r   r   r    r!   	__class__s         m/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/models/densenet.pyr.   z_DenseLayer.__init__    s     	^$677
WT***
Y17[3HVW`ahmnnn
^Gk$9::
WT***
Yw4kqYZdelqrrr
y)) 0    inputsc                     t          j        |d          }|                     |                     |                     |                              }|S Nr   )torchcatr5   r3   r1   )r:   r>   concated_featuresbottleneck_outputs       r<   bn_functionz_DenseLayer.bn_function/   sE    !Ifa00 JJtzz$**=N2O2O'P'PQQ  r=   inputc                 $    |D ]}|j         r dS dS )NTF)requires_grad)r:   rF   tensors      r<   any_requires_gradz_DenseLayer.any_requires_grad5   s-     	 	F# ttur=   c                 8      fd}t          j        |g|R ddiS )Nc                  .                         |           S N)rE   )r>   r:   s    r<   closurez7_DenseLayer.call_checkpoint_bottleneck.<locals>.closure=   s    ##F+++r=   use_reentrantF)cp
checkpoint)r:   rF   rN   s   `  r<   call_checkpoint_bottleneckz&_DenseLayer.call_checkpoint_bottleneck;   s>    	, 	, 	, 	, 	, }WBuBBBEBBBr=   c                     d S rM    r:   rF   s     r<   forwardz_DenseLayer.forwardB       r=   c                     d S rM   rT   rU   s     r<   rV   z_DenseLayer.forwardF   rW   r=   c                    t          |t                    r|g}n|}| j        rX|                     |          rCt          j                                        rt          d          |                     |          }n| 	                    |          }| 
                    |                     |                     |                              }| j        dk    r!t          j        || j        | j                  }|S )Nz%Memory Efficient not supported in JITr   )ptraining)
isinstancer   r!   rJ   rA   jitis_scripting	ExceptionrR   rE   r8   r7   r6   r    Fdropoutr[   )r:   rF   prev_featuresrD   new_featuress        r<   rV   z_DenseLayer.forwardL   s    eV$$ 	""GMM!M  	@T%;%;M%J%J 	@y%%'' I GHHH $ ? ? N N $ 0 0 ? ?zz$**TZZ8I-J-J"K"KLL>A9\T^dm\\\Lr=   F)__name__
__module____qualname__intr9   boolr.   listr   rE   rJ   rA   r]   unusedrR   _overload_methodrV   __classcell__r;   s   @r<   r   r      s       rw1 1"%1471BE1RW1ko1	1 1 1 1 1 1!$v, !6 ! ! ! !tF|      YCV C C C C C YT&\ f      YV      
V         r=   r   c                   T     e Zd ZdZ	 ddedededededed	d
f fdZded	efdZ	 xZ
S )_DenseBlockr   F
num_layersr   r   r   r    r!   r"   Nc                     t                                                       t          |          D ]8}t          |||z  z   ||||          }|                     d|dz   z  |           9d S )N)r   r   r    r!   zdenselayer%dr   )r-   r.   ranger   
add_module)
r:   rq   r   r   r   r    r!   ilayerr;   s
            r<   r.   z_DenseBlock.__init__c   s     	z"" 	= 	=A"Q_4'#!1  E OONa!e4e<<<<	= 	=r=   init_featuresc                     |g}|                                  D ]%\  }} ||          }|                    |           &t          j        |d          S r@   )itemsappendrA   rB   )r:   rw   featuresnamerv   rc   s         r<   rV   z_DenseBlock.forwardw   sX    !?::<< 	* 	*KD% 5??LOOL))))y1%%%r=   rd   )re   rf   rg   _versionrh   r9   ri   r.   r   rV   rm   rn   s   @r<   rp   rp   `   s        H "'= ==  = 	=
 = = = 
= = = = = =(&V & & & & & & & & &r=   rp   c                   ,     e Zd Zdededdf fdZ xZS )_Transitionr   num_output_featuresr"   Nc                     t                                                       t          j        |          | _        t          j        d          | _        t          j        ||ddd          | _        t          j	        dd          | _
        d S )NTr$   r   Fr&   r   )r'   r(   )r-   r.   r/   r0   normr2   relur4   conv	AvgPool2dpool)r:   r   r   r;   s      r<   r.   z_Transition.__init__   sz    N#566	GD)))	I02EST]^ejkkk	LQq999			r=   )re   rf   rg   rh   r.   rm   rn   s   @r<   r   r      sR        :3 :S :T : : : : : : : : : :r=   r   c                   x     e Zd ZdZ	 	 	 	 	 	 	 dd	ed
eeeeef         dedededededdf fdZde	de	fdZ
 xZS )r   aK  Densenet-BC model class, based on
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_.

    Args:
        growth_rate (int) - how many filters to add each layer (`k` in paper)
        block_config (list of 4 ints) - how many layers in each pooling block
        num_init_features (int) - the number of filters to learn in the first convolution layer
        bn_size (int) - multiplicative factor for number of bottle neck layers
          (i.e. bn_size * k features in the bottleneck layer)
        drop_rate (float) - dropout rate after each dense layer
        num_classes (int) - number of classification classes
        memory_efficient (bool) - If True, uses checkpointing. Much more memory efficient,
          but slower. Default: *False*. See `"paper" <https://arxiv.org/pdf/1707.06990.pdf>`_.
                    @      r     Fr   block_confignum_init_featuresr   r    num_classesr!   r"   Nc                 :   t                                                       t          |            t          j        t          dt          j        d|dddd          fdt          j        |          fdt          j        d	
          fdt          j	        ddd          fg                    | _
        |}t          |          D ]\  }	}
t          |
|||||          }| j
                            d|	dz   z  |           ||
|z  z   }|	t          |          dz
  k    r:t          ||dz            }| j
                            d|	dz   z  |           |dz  }| j
                            dt          j        |                     t          j        ||          | _        |                                 D ]}t'          |t          j                  r%t          j                            |j                   At'          |t          j                  rKt          j                            |j        d           t          j                            |j        d           t'          |t          j                  r%t          j                            |j        d           d S )Nconv0r*      r   Fr+   norm0relu0Tr$   pool0r   )r'   r(   r,   )rq   r   r   r   r    r!   zdenseblock%d)r   r   ztransition%dnorm5r   )r-   r.   r
   r/   
Sequentialr   r4   r0   r2   	MaxPool2dr{   	enumeraterp   rt   lenr   Linear
classifiermodulesr\   initkaiming_normal_weight	constant_r)   )r:   r   r   r   r   r    r   r!   num_featuresru   rq   blocktransmr;   s                 r<   r.   zDenseNet.__init__   s    	D!!! bi+<!TU_`glmmmnbn->??@bgd3334blqANNNO	 	
 	
 )&|44 	1 	1MAz%#/'#!1  E M$$^q1u%=uEEE'*{*BBLC%%)))#|YeijYjkkk((1q5)A5III+q0 	  ".*F*FGGG )L+>>  	- 	-A!RY'' -''1111Ar~.. -!!!(A...!!!&!,,,,Ary)) -!!!&!,,,	- 	-r=   xc                     |                      |          }t          j        |d          }t          j        |d          }t	          j        |d          }|                     |          }|S )NTr$   )r   r   r   )r{   r`   r   adaptive_avg_pool2drA   flattenr   )r:   r   r{   outs       r<   rV   zDenseNet.forward   s`    ==##fXt,,,#C00mC##ooc""
r=   )r   r   r   r   r   r   F)re   rf   rg   __doc__rh   tupler9   ri   r.   r   rV   rm   rn   s   @r<   r   r      s         " 2A!#!&:- :-:- Cc3./:- 	:-
 :- :- :- :- 
:- :- :- :- :- :-x F        r=   r   modelweightsprogressr"   c                 n   t          j        d          }|                    |d          }t          |                                          D ]R}|                    |          }|r9|                    d          |                    d          z   }||         ||<   ||= S|                     |           d S )Nz]^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$T)r   
check_hashr   r   )recompileget_state_dictrj   keysmatchgroupload_state_dict)r   r   r   pattern
state_dictkeyresnew_keys           r<   _load_state_dictr      s    
 jh G ''d'KKJJOO%%&&    mmC   	 iillSYYq\\1G",S/Jw3	*%%%%%r=   r   r   r   kwargsc                     |)t          |dt          |j        d                              t          | ||fi |}|t	          |||           |S )Nr   
categories)r   r   r   )r   r   metar   r   )r   r   r   r   r   r   r   s          r<   	_densenetr      se     fmSl9S5T5TUUU[,0ALLVLLEugIIIILr=   )   r   z*https://github.com/pytorch/vision/pull/116z'These weights are ported from LuaTorch.)min_sizer   recipe_docsc            
       `    e Zd Z ed eed          i edddddid	d
d          ZeZdS )r   z<https://download.pytorch.org/models/densenet121-a639ec97.pth   	crop_sizeihy ImageNet-1KgƛR@g|?5V@zacc@1zacc@5gy&1@gQ>@
num_params_metrics_ops
_file_sizeurl
transformsr   N	re   rf   rg   r   r   r	   _COMMON_METAIMAGENET1K_V1DEFAULTrT   r=   r<   r   r     s        GJ7.#>>>

!##     
 
 
  M  GGGr=   r   c            
       `    e Zd Z ed eed          i edddddid	d
d          ZeZdS )r   z<https://download.pytorch.org/models/densenet161-8d451a50.pthr   r   i(r   gFHS@gp=
cW@r   gx@gV-[@r   r   Nr   rT   r=   r<   r   r     s        GJ7.#>>>

"##    !
 
 
  M  GGGr=   r   c            
       `    e Zd Z ed eed          i edddddid	d
d          ZeZdS )r   z<https://download.pytorch.org/models/densenet169-b2777c0a.pthr   r   ih r   gfffffR@g$3W@r   gzG
@gvZK@r   r   Nr   rT   r=   r<   r   r   3  s        GJ7.#>>>

"##     
 
 
  M  GGGr=   r   c            
       `    e Zd Z ed eed          i edddddid	d
d          ZeZdS )r   z<https://download.pytorch.org/models/densenet201-c1103571.pthr   r   ihc1r   gMbX9S@gHzWW@r   gDl)@gZd;WS@r   r   Nr   rT   r=   r<   r   r   G  s        GJ7.#>>>

"##     
 
 
  M  GGGr=   r   
pretrained)r   T)r   r   c                 X    t                               |           } t          ddd| |fi |S )a{  Densenet-121 model from
    `Densely Connected Convolutional Networks <https://arxiv.org/abs/1608.06993>`_.

    Args:
        weights (:class:`~torchvision.models.DenseNet121_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.DenseNet121_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.densenet.DenseNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/densenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.DenseNet121_Weights
        :members:
    r   r   r   )r   verifyr   r   r   r   s      r<   r   r   [  5    * "((11GR"gxJJ6JJJr=   c                 X    t                               |           } t          ddd| |fi |S )a{  Densenet-161 model from
    `Densely Connected Convolutional Networks <https://arxiv.org/abs/1608.06993>`_.

    Args:
        weights (:class:`~torchvision.models.DenseNet161_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.DenseNet161_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.densenet.DenseNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/densenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.DenseNet161_Weights
        :members:
    0   )r   r   $   r   `   )r   r   r   r   s      r<   r   r   u  r   r=   c                 X    t                               |           } t          ddd| |fi |S )a{  Densenet-169 model from
    `Densely Connected Convolutional Networks <https://arxiv.org/abs/1608.06993>`_.

    Args:
        weights (:class:`~torchvision.models.DenseNet169_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.DenseNet169_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.densenet.DenseNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/densenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.DenseNet169_Weights
        :members:
    r   )r   r   r   r   r   )r   r   r   r   s      r<   r   r     r   r=   c                 X    t                               |           } t          ddd| |fi |S )a{  Densenet-201 model from
    `Densely Connected Convolutional Networks <https://arxiv.org/abs/1608.06993>`_.

    Args:
        weights (:class:`~torchvision.models.DenseNet201_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.DenseNet201_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.densenet.DenseNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/densenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.DenseNet201_Weights
        :members:
    r   )r   r   r   r   r   )r   r   r   r   s      r<   r   r     r   r=   )6r   collectionsr   	functoolsr   typingr   r   rA   torch.nnr/   torch.nn.functional
functionalr`   torch.utils.checkpointutilsrQ   rP   r   transforms._presetsr	   r
   _apir   r   r   _metar   _utilsr   r   __all__Moduler   
ModuleDictrp   r   r   r   ri   r   rh   r   r   r   r   r   r   r   r   r   r   r   r   rT   r=   r<   <module>r      sv   				 # # # # # #                                       # # # # # # # # #       5 5 5 5 5 5 ' ' ' ' ' ' 6 6 6 6 6 6 6 6 6 6 ' ' ' ' ' ' B B B B B B B B
 
 
> > > > >") > > >B& & & & &"- & & &>: : : : :"- : : :R R R R Rry R R Rj&BI & &t &PT & & & &&S#s*+  k"	
      ( &::	     +   (    +   (    +   (    +   ( ,0C0Q!RSSS<@SW K K KH%89 KD Kcf Kks K K K TS K0 ,0C0Q!RSSS<@SW K K KH%89 KD Kcf Kks K K K TS K0 ,0C0Q!RSSS<@SW K K KH%89 KD Kcf Kks K K K TS K0 ,0C0Q!RSSS<@SW K K KH%89 KD Kcf Kks K K K TS K K Kr=   