§
    ·¯¸jQ)  ã                   ó  — d Z ddlZddlmZ ddlZddlmZ ddlmZm	Z	m
Z
mZmZ ddlmZ d„ Zefd	„Zd
„ Zd„ Zd„ Z G d„ d¦  «        Zg d¢fd„Zg ddfd„Zg dfd„Zg dfd„Zddej        j        dej        j        fd„ZdS )zM Model / state_dict utils

Hacked together by / Copyright 2020 Ross Wightman
é    N©Údeepcopy)ÚFrozenBatchNorm2d)ÚBatchNormAct2dÚSyncBatchNormActÚFrozenBatchNormAct2dÚfreeze_batch_norm_2dÚunfreeze_batch_norm_2dé   )ÚModelEmac                 óè   — t          | t          ¦  «        rt          | j        ¦  «        S t	          | d¦  «        rt          | j        ¦  «        S t	          | d¦  «        rt          | j        ¦  «        S | S )NÚmoduleÚ	_orig_mod)Ú
isinstancer   Úunwrap_modelÚemaÚhasattrr   r   )Úmodels    ú`/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/timm/utils/model.pyr   r      sm   € Ý�%�Ñ"Ô"ð Ý˜EœIÑ&Ô&Ð&å�5˜(Ñ#Ô#ð 	Ý ¤Ñ-Ô-Ð-Ý�U˜KÑ(Ô(ð 	Ý ¤Ñ0Ô0Ð0àˆLó    c                 ó<   —  || ¦  «                              ¦   «         S ©N)Ú
state_dict)r   Ú	unwrap_fns     r   Úget_state_dictr      s   € Øˆ9�UÑÔ×&Ò&Ñ(Ô(Ð(r   c                 ó€   — t          j        |                     g d¢¬¦  «        dz  ¦  «                             ¦   «         S )zA calculate average channel square mean of output activations
    ©r   é   é   ©Úaxisr   )ÚtorchÚmeanÚitem©r   ÚinputÚoutputs      r   Úavg_sq_ch_meanr(       s7   € õ Œ:�f—k’k y y y�kÑ1Ô1°QÑ6Ñ7Ô7×<Ò<Ñ>Ô>Ð>r   c                 óz   — t          j        |                     g d¢¬¦  «        ¦  «                             ¦   «         S ©z> calculate average channel variance of output activations
    r   r    ©r"   r#   Úvarr$   r%   s      r   Ú
avg_ch_varr-   &   ó2   € õ Œ:�f—j’j i i i�jÑ0Ô0Ñ1Ô1×6Ò6Ñ8Ô8Ð8r   c                 óz   — t          j        |                     g d¢¬¦  «        ¦  «                             ¦   «         S r*   r+   r%   s      r   Úavg_ch_var_residualr0   ,   r.   r   c                   ó$   — e Zd ZdZd„ Zd„ Zd„ ZdS )ÚActivationStatsHooka  Iterates through each of `model`'s modules and matches modules using unix pattern 
    matching based on `hook_fn_locs` and registers `hook_fn` to the module if there is 
    a match. 

    Arguments:
        model (nn.Module): model from which we will extract the activation stats
        hook_fn_locs (List[str]): List of `hook_fn` locations based on Unix type string 
            matching with the name of model's modules. 
        hook_fns (List[Callable]): List of hook functions to be registered at every
            module in `layer_names`.
    
    Inspiration from https://docs.fast.ai/callback.hook.html.

    Refer to https://gist.github.com/amaarora/6e56942fcb46e67ba203f3009b30d950 for an example 
    on how to plot Signal Propagation Plots using `ActivationStatsHook`.
    c                 ó"  — || _         || _        || _        t          |¦  «        t          |¦  «        k    rt	          d¦  «        ‚t          d„ |D ¦   «         ¦  «        | _        t          ||¦  «        D ]\  }}|                      ||¦  «         Œd S )Nz_Please provide `hook_fns` for each `hook_fn_locs`,                 their lengths are different.c              3   ó(   K  — | ]}|j         g fV — Œd S r   )Ú__name__)Ú.0Úhook_fns     r   ú	<genexpr>z/ActivationStatsHook.__init__.<locals>.<genexpr>K   s*   è è € ÐIÐI°W˜7Ô+¨RÐ0ÐIÐIÐIÐIÐIÐIr   )	r   Úhook_fn_locsÚhook_fnsÚlenÚ
ValueErrorÚdictÚstatsÚzipÚregister_hook)Úselfr   r9   r:   Úhook_fn_locr7   s         r   Ú__init__zActivationStatsHook.__init__D   s§   € ØˆŒ
Ø(ˆÔØ ˆŒÝˆ|ÑÔ¥ H¡¤Ò-Ð-Ýð .ñ /ô /ð /åÐIÐIÀÐIÑIÔIÑIÔIˆŒ
Ý$'¨°hÑ$?Ô$?ð 	5ð 	5Ñ ˆK˜Ø×Ò˜{¨GÑ4Ô4Ð4Ð4ð	5ð 	5r   c                 ó   ‡ ‡— ˆˆ fd„}|S )Nc                 ól   •—  ‰| ||¦  «        }‰j         ‰j                                      |¦  «         d S r   )r>   r5   Úappend)r   r&   r'   Úoutr7   rA   s       €€r   Úappend_activation_statszAActivationStatsHook._create_hook.<locals>.append_activation_statsP   s:   ø€ Ø�'˜& %¨Ñ0Ô0ˆCØŒJ�wÔ'Ô(×/Ò/°Ñ4Ô4Ð4Ð4Ð4r   © )rA   r7   rH   s   `` r   Ú_create_hookz ActivationStatsHook._create_hookO   s*   øø€ ð	5ð 	5ð 	5ð 	5ð 	5ð 	5ð 'Ð&r   c                 óÀ   — | j                              ¦   «         D ]C\  }}t          j        ||¦  «        sŒ|                     |                      |¦  «        ¦  «         ŒDd S r   )r   Únamed_modulesÚfnmatchÚregister_forward_hookrJ   )rA   rB   r7   Únamer   s        r   r@   z!ActivationStatsHook.register_hookV   sm   € Ø œJ×4Ò4Ñ6Ô6ð 	Eð 	E‰LˆD�&Ý”? 4¨Ñ5Ô5ð ØØ×(Ò(¨×):Ò):¸7Ñ)CÔ)CÑDÔDÐDÐDð	Eð 	Er   N)r5   Ú
__module__Ú__qualname__Ú__doc__rC   rJ   r@   rI   r   r   r2   r2   2   sP   € € € € € ðð ð"	5ð 	5ð 	5ð'ð 'ð 'ðEð Eð Eð Eð Er   r2   )é   r   éà   rT   c                 óv   — t          j        dd|¦  «        }t          | ||¬¦  «        } | |¦  «        }|j        S )a  Extract average square channel mean and variance of activations during 
        forward pass to plot Signal Propagation Plots (SPP).
    
    Paper: https://arxiv.org/abs/2101.08692

    Example Usage: https://gist.github.com/amaarora/6e56942fcb46e67ba203f3009b30d950
    g        g      ð?)r9   r:   )r"   Únormalr2   r>   )r   r9   r:   Úinput_shapeÚxÚhookÚ_s          r   Úextract_spp_statsr[   ]   s@   € õ 	Œ�R˜˜[Ñ)Ô)€AÝ˜u°<È(ÐSÑSÔS€DØˆˆa‰Œ€AØŒ:Ðr   TÚfreezec                 ó~  ‡ — |dv s
J d¦   «         ‚t          ‰ t          j        j        j        j        t          j        j        j        j        t          t          f¦  «        rt          d¦  «        ‚t          |t          ¦  «        r|g}|}ˆ fd„|D ¦   «         }t          |¦  «        s+t          t          ‰                      ¦   «         Ž ¦  «        \  }}t          ||¦  «        D ]Û\  }}|                     ¦   «         D ]}|dk    rdnd|_        Œ|r®d„ }|dk    rmt#          |¦  «        }	t          |t          j        j        j        j        t          j        j        j        j        t          t          f¦  «        r |‰ ||	¦  «         Œ£t%          |¦  «        }	t          |t&          t(          f¦  «        r |‰ ||	¦  «         ŒÜd	S )
a9  
    Freeze or unfreeze parameters of the specified modules and those of all their hierarchical descendants. This is
        done in place.

    Args:
        root_module (nn.Module, optional): Root module relative to which the `submodules` are referenced.
        submodules (list[str]): List of modules for which the parameters will be (un)frozen. They are to be provided as
            named modules relative to the root module (accessible via `root_module.named_modules()`). An empty list
            means that the whole root module will be (un)frozen. Defaults to []
        include_bn_running_stats (bool): Whether to also (un)freeze the running statistics of batch norm 2d layers.
            Defaults to `True`.
        mode (bool): Whether to freeze ("freeze") or unfreeze ("unfreeze"). Defaults to `"freeze"`.
    )r\   Úunfreezez,`mode` must be one of "freeze" or "unfreeze"z£You have provided a batch norm layer as the `root module`. Please use `timm.utils.model.freeze_batch_norm_2d` or `timm.utils.model.unfreeze_batch_norm_2d` instead.c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS rI   )Úget_submodule)r6   ÚmÚroot_modules     €r   ú
<listcomp>z$_freeze_unfreeze.<locals>.<listcomp>Ž   s'   ø€ ÐCÐCÐC°1�+×+Ò+¨AÑ.Ô.ÐCÐCÐCr   r\   FTc                 óò   — |                      dd¦  «        }t          |¦  «        dk    r7|                      |d         ¦  «                             |d         |¦  «         d S |                      ||¦  «         d S )Nú.r   r   )Úrsplitr;   r`   Ú
add_module)r   rO   Ú	submoduleÚsplits       r   Ú_add_submodulez(_freeze_unfreeze.<locals>._add_submodule™   sr   € ØŸš C¨Ñ+Ô+�Ý�u‘:”: ’>�>Ø×(Ò(¨¨q¬Ñ2Ô2×=Ò=¸eÀA¼hÈ	ÑRÔRÐRÐRÐRà×%Ò% d¨IÑ6Ô6Ð6Ð6Ð6r   N)r   r"   ÚnnÚmodulesÚ	batchnormÚBatchNorm2dÚSyncBatchNormr   r   ÚAssertionErrorÚstrr;   Úlistr?   Únamed_childrenÚ
parametersÚrequires_gradr	   r
   r   r   )
rb   Ú
submodulesÚinclude_bn_running_statsÚmoderL   Únra   Úprj   Úress
   `         r   Ú_freeze_unfreezer|   o   sý  ø€ ð Ð)Ð)Ð)Ð)Ð+YÑ)Ô)Ð)å�+ÝŒHÔÔ&Ô2ÝŒHÔÔ&Ô4ÝÝð	 ñ ô ð 	mõ ðlñmô mð 	mõ �*�cÑ"Ô"ð "Ø �\ˆ
à€MØCÐCÐCÐC¸
ÐCÑCÔC€Jåˆz‰?Œ?ð MÝ$(­¨k×.HÒ.HÑ.JÔ.JÐ)KÑ$LÔ$LÑ!ˆ�zå�M :Ñ.Ô.ð 8ð 8‰ˆˆ1à—’‘”ð 	Bð 	BˆAØ'+¨xÒ'7Ð'7˜e˜e¸TˆAŒOˆOØ#ð 	8ð7ð 7ð 7ð �xÒÐÝ*¨1Ñ-Ô-�õ ˜aÝœÔ(Ô2Ô>ÝœÔ(Ô2Ô@Ý&Ý(ð	"ñ ô ð 8ð #�N ;°°3Ñ7Ô7Ð7øõ -¨QÑ/Ô/�å˜aÕ"3Õ5IÐ!JÑKÔKð 8Ø"�N ;°°3Ñ7Ô7Ð7øð?8ð 8r   c                 ó,   — t          | ||d¬¦  «         dS )a¢  
    Freeze parameters of the specified modules and those of all their hierarchical descendants. This is done in place.

    Args:
        root_module (nn.Module): Root module relative to which `submodules` are referenced.
        submodules (list[str]): List of modules for which the parameters will be frozen. They are to be provided as
            named modules relative to the root module (accessible via `root_module.named_modules()`). An empty list
            means that the whole root module will be frozen. Defaults to `[]`.
        include_bn_running_stats (bool): Whether to also freeze the running statistics of `BatchNorm2d` and
            `SyncBatchNorm` layers. These will be converted to `FrozenBatchNorm2d` in place. Hint: During fine tuning,
            it's good practice to freeze batch norm stats. And note that these are different to the affine parameters
            which are just normal PyTorch parameters. Defaults to `True`.

    Hint: If you want to freeze batch norm ONLY, use `timm.utils.model.freeze_batch_norm_2d`.

    Examples::

        >>> model = timm.create_model('resnet18')
        >>> # Freeze up to and including layer2
        >>> submodules = [n for n, _ in model.named_children()]
        >>> print(submodules)
        ['conv1', 'bn1', 'act1', 'maxpool', 'layer1', 'layer2', 'layer3', 'layer4', 'global_pool', 'fc']
        >>> freeze(model, submodules[:submodules.index('layer2') + 1])
        >>> # Check for yourself that it works as expected
        >>> print(model.layer2[0].conv1.weight.requires_grad)
        False
        >>> print(model.layer3[0].conv1.weight.requires_grad)
        True
        >>> # Unfreeze
        >>> unfreeze(model)
    r\   ©rw   rx   N©r|   ©rb   rv   rw   s      r   r\   r\   µ   s$   € õ@ �[ *ÐG_ÐfnÐoÑoÔoÐoÐoÐor   c                 ó,   — t          | ||d¬¦  «         dS )a  
    Unfreeze parameters of the specified modules and those of all their hierarchical descendants. This is done in place.

    Args:
        root_module (nn.Module): Root module relative to which `submodules` are referenced.
        submodules (list[str]): List of submodules for which the parameters will be (un)frozen. They are to be provided
            as named modules relative to the root module (accessible via `root_module.named_modules()`). An empty
            list means that the whole root module will be unfrozen. Defaults to `[]`.
        include_bn_running_stats (bool): Whether to also unfreeze the running statistics of `FrozenBatchNorm2d` layers.
            These will be converted to `BatchNorm2d` in place. Defaults to `True`.

    See example in docstring for `freeze`.
    r^   r~   Nr   r€   s      r   r^   r^   Ø   s#   € õ �[ *ÐG_ÐfpÐqÑqÔqÐqÐqÐqr   Fr   Úreturnc                 óJ   ‡— |st          | ¦  «        } ˆfd„Š ‰| ¦  «         | S )Nc                 óL  •— |                       ¦   «         D ]�\  }}t          |d¦  «        r$t          | ||                     ¦   «         ¦  «         nIt          |d¦  «        r|                     ¦   «          n$t          |d¦  «        r|                     ¦   «           ‰|¦  «         ŒŽd S )NÚfuseÚreparameterizeÚswitch_to_deploy)rs   r   Úsetattrr…   r†   r‡   )ra   Ú
child_nameÚchildÚ_fuses      €r   r‹   z#reparameterize_model.<locals>._fuseí   s¸   ø€ Ø!"×!1Ò!1Ñ!3Ô!3ð 	ð 	ÑˆJ˜Ý�u˜fÑ%Ô%ð )Ý˜˜: u§z¢z¡|¤|Ñ4Ô4Ð4Ð4Ý˜Ð 0Ñ1Ô1ð )Ø×$Ò$Ñ&Ô&Ð&Ð&Ý˜Ð 2Ñ3Ô3ð )Ø×&Ò&Ñ(Ô(Ð(ØˆE�%‰LŒLˆLˆLð	ð 	r   r   )r   Úinplacer‹   s     @r   Úreparameterize_modelr�   é   sC   ø€ Øð  Ý˜‘”ˆðð ð ð ð ð 
€Eˆ%�L„L€LØ€Lr   )F)rR   rM   Úcopyr   r"   Útorchvision.ops.miscr   Útimm.layersr   r   r   r	   r
   Ú	model_emar   r   r   r(   r-   r0   r2   r[   r|   r\   r^   rk   ÚModuler�   rI   r   r   ú<module>r“      sß  ððð ð €€€Ø Ð Ð Ð Ð Ð à €€€Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2ð1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1à Ð Ð Ð Ð Ð ð	ð 	ð 	ð %1ð )ð )ð )ð )ð?ð ?ð ?ð9ð 9ð 9ð9ð 9ð 9ð(Eð (Eð (Eð (Eð (Eñ (Eô (Eð (Eð^ %Ð$Ð$ð	ð ð ð ð$ .0È$ÐU]ð C8ð C8ð C8ð C8ðL $&Àð  pð  pð  pð  pðF &(À$ð rð rð rð rð"ð  ¤¤ð À5Ä8Ä?ð ð ð ð ð ð r   