§
    “©ºjTR  ã                   ó   — d Z ddlZddlmZ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Zdd	lmZ dd
lmZ ddlmZ ddlmZ esdgZ ej        e ¦  «        Z!de"de#fd„Z$ G d„ de¦  «        Z% G d„ de%¦  «        Z&dS )z_
Finetuning Callback
^^^^^^^^^^^^^^^^^^^^

Freeze and unfreeze models for finetuning purposes.
é    N)Ú	GeneratorÚIterable)ÚAnyÚCallableÚOptionalÚUnion)ÚModuleÚ
ModuleDict)Ú
_BatchNorm)Ú	Optimizer)Úoverride)ÚCallback)ÚMisconfigurationException)Ú_TORCHVISION_AVAILABLE)Úrank_zero_warnÚBackboneFinetuningÚepochÚreturnc                 ó   — dS )Ng       @© )r   s    úv/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/pytorch_lightning/callbacks/finetuning.pyÚmultiplicativer   ,   s   € Øˆ3ó    c                   ó„  — e Zd ZdZd+d„Zedeeef         fd„¦   «         Z	edeeef         ddfd„¦   «         Z
ed,d„¦   «         Zedeeeeeef                  f         dee         fd„¦   «         Ze	 d-deeeeeef                  f         dededefd„¦   «         Zedeeeeeef                  f         ddfd„¦   «         Zededdfd„¦   «         Zed.deeeeeef                  f         deddfd„¦   «         Zedededefd„¦   «         Ze	 	 	 d/deeeeeef                  f         dedee         dededdfd„¦   «         Zedd	d
ddeddfd„¦   «         Zed eeeef                  d!edeeeef                  fd"„¦   «         Zd
dd#ed$ed%eeeef                  ddf
d&„Z ed,d'„¦   «         Z!d
dd(ededdfd)„Z"d0d*„Z#dS )1ÚBaseFinetuningaß  This class implements the base logic for writing your own Finetuning Callback.

    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.

    Override ``freeze_before_training`` and ``finetune_function`` methods with your own logic.

    ``freeze_before_training``: This method is called before ``configure_optimizers``
        and should be used to freeze any modules parameters.

    ``finetune_function``: This method is called on every train epoch start and should be used to
        ``unfreeze`` any parameters. Those parameters need to be added in a new ``param_group``
        within the optimizer.

    .. note:: Make sure to filter the parameters based on ``requires_grad``.

    Example::

        >>> from torch.optim import Adam
        >>> class MyModel(pl.LightningModule):
        ...     def configure_optimizer(self):
        ...         # Make sure to filter the parameters based on `requires_grad`
        ...         return Adam(filter(lambda p: p.requires_grad, self.parameters()))
        ...
        >>> class FeatureExtractorFreezeUnfreeze(BaseFinetuning):
        ...     def __init__(self, unfreeze_at_epoch=10):
        ...         super().__init__()
        ...         self._unfreeze_at_epoch = unfreeze_at_epoch
        ...
        ...     def freeze_before_training(self, pl_module):
        ...         # freeze any module you want
        ...         # Here, we are freezing `feature_extractor`
        ...         self.freeze(pl_module.feature_extractor)
        ...
        ...     def finetune_function(self, pl_module, current_epoch, optimizer):
        ...         # When `current_epoch` is 10, feature_extractor will start training.
        ...         if current_epoch == self._unfreeze_at_epoch:
        ...             self.unfreeze_and_add_param_group(
        ...                 modules=pl_module.feature_extractor,
        ...                 optimizer=optimizer,
        ...                 train_bn=True,
        ...             )

    r   Nc                 ó"   — i | _         d| _        d S ©NF)Ú_internal_optimizer_metadataÚ_restarting©Úselfs    r   Ú__init__zBaseFinetuning.__init__]   s   € ØMOˆÔ)Ø ˆÔÐÐr   c                 ó   — d| j         iS )NÚinternal_optimizer_metadata)r   r    s    r   Ú
state_dictzBaseFinetuning.state_dicta   s   € ð *¨4Ô+Lð
ð 	
r   r%   c                 óH   — d| _         d|v r|d         | _        d S || _        d S )NTr$   )r   r   )r!   r%   s     r   Úload_state_dictzBaseFinetuning.load_state_dictg   s9   € àˆÔØ(¨JÐ6Ð6Ø0:Ð;XÔ0YˆDÔ-Ð-Ð-ð 1;ˆDÔ-Ð-Ð-r   Útrainerú
pl.TrainerÚ	pl_moduleúpl.LightningModulec                 ó  — | j         r|| j        rlt          |                     ¦   «         ¦  «        }t	          |j        ¦  «        D ]6\  }}|| j        v r(|                      | j        |         |¦  «        }||_        Œ7d| _         d S d S r   )r   r   ÚdictÚnamed_parametersÚ	enumerateÚ
optimizersÚ_apply_mapping_to_param_groupsÚparam_groups)r!   r(   r*   r.   Úopt_idxÚ	optimizerr2   s          r   Úon_fit_startzBaseFinetuning.on_fit_startp   s§   € ð Ôð 		%ØÔ0ð >Ý#'¨	×(BÒ(BÑ(DÔ(DÑ#EÔ#EÐ Ý*3°GÔ4FÑ*GÔ*Gð >ð >Ñ&�G˜YØ $Ô"CÐCÐCØ'+×'JÒ'JØ Ô=¸gÔFÐHXñ(ô (˜ð 2>˜	Ô.øØ$ˆDÔÐÐð		%ð 		%r   Úmodulesc                 óF  — t          | t          ¦  «        r|                      ¦   «         } t          | t          ¦  «        rDg }| D ]/}|                     t
                               |¦  «        ¦  «         Œ0t          |¦  «        }n|                      ¦   «         }d„ |D ¦   «         S )aH  This function is used to flatten a module or an iterable of modules into a list of its leaf modules (modules
        with no children) and parent modules that have parameters directly themselves.

        Args:
            modules: A given module or an iterable of modules

        Returns:
            List of modules

        c                 ób   — g | ],}t          |                     ¦   «         ¦  «        r|j        ¯*|‘Œ-S r   )ÚlistÚchildrenÚ_parameters)Ú.0Úms     r   ú
<listcomp>z2BaseFinetuning.flatten_modules.<locals>.<listcomp>—   s4   € ÐOÐOÐO�a­4°·
²
±´Ñ+=Ô+=ÐOÀÄÐO�ÐOÐOÐOr   )	Ú
isinstancer
   Úvaluesr   Úextendr   Úflatten_modulesÚiterr6   )r6   Ú_flatten_modulesr=   Ú_moduless       r   rB   zBaseFinetuning.flatten_modules~   s©   € õ �g�zÑ*Ô*ð 	'Ø—n’nÑ&Ô&ˆGå�g�xÑ(Ô(ð 	)Ø!ÐØð Kð K�Ø ×'Ò'­×(FÒ(FÀqÑ(IÔ(IÑJÔJÐJÐJåÐ,Ñ-Ô-ˆHˆHà—’Ñ(Ô(ˆHð PÐO˜8ÐOÑOÔOÐOr   TÚtrain_bnÚrequires_gradc              #   óÈ   K  — t                                | ¦  «        } | D ]B}t          |t          ¦  «        r|sŒ|                     d¬¦  «        D ]}|j        |k    r|V — ŒŒCdS )am  Yields the `requires_grad` parameters of a given module or list of modules.

        Args:
            modules: A given module or an iterable of modules
            train_bn: Whether not to train the BatchNorm module
            requires_grad: Whether to create a generator for trainable or non-trainable parameters.
        Returns:
            Generator

        F©ÚrecurseN)r   rB   r?   r   Ú
parametersrG   )r6   rF   rG   ÚmodÚparams        r   Úfilter_paramszBaseFinetuning.filter_params™   sŠ   è è € õ !×0Ò0°Ñ9Ô9ˆØð 	 ð 	 ˆCÝ˜#�zÑ*Ô*ð °8ð ØàŸš°˜Ñ6Ô6ð  ð  �ØÔ&¨-Ò7Ð7Ø�K�K�Køð ð		 ð 	 r   c                 ó¼   — t                                | ¦  «        } | D ]>}t          |t          ¦  «        rd|_        |                     d¬¦  «        D ]	}d|_        Œ
Œ?dS )zˆUnfreezes the parameters of the provided modules.

        Args:
            modules: A given module or an iterable of modules

        TFrI   N)r   rB   r?   r   Útrack_running_statsrK   rG   )r6   ÚmodulerM   s      r   Úmake_trainablezBaseFinetuning.make_trainable°   sz   € õ !×0Ò0°Ñ9Ô9ˆØð 	+ð 	+ˆFÝ˜&¥*Ñ-Ô-ð 2Ø-1�Ô*à×*Ò*°5Ð*Ñ9Ô9ð +ð +�Ø&*�Ô#Ð#ð+ð		+ð 	+r   rQ   c                 ó~   — t          | t          ¦  «        rd| _        |                      d¬¦  «        D ]	}d|_        Œ
dS )zjFreezes the parameters of the provided module.

        Args:
            module: A given module

        FrI   N)r?   r   rP   rK   rG   )rQ   rM   s     r   Úfreeze_modulezBaseFinetuning.freeze_moduleÀ   sS   € õ �f�jÑ)Ô)ð 	/Ø).ˆFÔ&à×&Ò&¨uÐ&Ñ5Ô5ð 	(ð 	(ˆEØ"'ˆEÔÐð	(ð 	(r   c                 óÜ   — t                                | ¦  «        } | D ]N}t          |t          ¦  «        r|rt                                |¦  «         Œ4t                                |¦  «         ŒOdS )zôFreezes the parameters of the provided modules.

        Args:
            modules: A given module or an iterable of modules
            train_bn: If True, leave the BatchNorm layers in training mode

        Returns:
            None

        N)r   rB   r?   r   rR   rT   )r6   rF   rL   s      r   ÚfreezezBaseFinetuning.freezeÎ   sw   € õ !×0Ò0°Ñ9Ô9ˆØð 	2ð 	2ˆCÝ˜#�zÑ*Ô*ð 2¨xð 2Ý×-Ò-¨cÑ2Ô2Ð2Ð2å×,Ò,¨SÑ1Ô1Ð1Ð1ð		2ð 	2r   r4   Úparamsc                 óô   ‡— g }g }|D ]MŠt          ˆfd„| j        D ¦   «         ¦  «        s|                     ‰¦  «         Œ8|                     ‰¦  «         ŒN|r t          dt	          | ¦  «        › d�¦  «         |S )ac  This function is used to exclude any parameter which already exists in this optimizer.

        Args:
            optimizer: Optimizer used for parameter exclusion
            params: Iterable of parameters used to check against the provided optimizer

        Returns:
            List of parameters not contained in this optimizer param groups

        c              3   óX   •K  — | ]$}|d          D ]}t          j        |‰¦  «        V — ŒŒ%dS )rW   N)ÚtorchÚequal)r<   ÚgroupÚprM   s      €r   ú	<genexpr>z5BaseFinetuning.filter_on_optimizer.<locals>.<genexpr>ð   sD   øè è € ÐjÐj°ÐZ_Ð`hÔZiÐjÐjÐUV•u”{ 1 eÑ,Ô,ÐjÐjÐjÐjÐjÐjÐjr   z¾The provided params to be frozen already exist within another group of this optimizer. Those parameters will be skipped.
HINT: Did you init your optimizer in `configure_optimizer` as such:
 z<(filter(lambda p: p.requires_grad, self.parameters()), ...) )Úanyr2   Úappendr   Útype)r4   rW   Ú
out_paramsÚremoved_paramsrM   s       @r   Úfilter_on_optimizerz"BaseFinetuning.filter_on_optimizerá   s¹   ø€ ð ˆ
ØˆØð 	-ð 	-ˆEÝÐjÐjÐjÐj¸)Ô:PÐjÑjÔjÑjÔjð -Ø×!Ò! %Ñ(Ô(Ð(Ð(à×%Ò% eÑ,Ô,Ð,Ð,àð 	Ýðbõ ˜‘O”Oðbð bð bñô ð ð Ðr   ç      $@ÚlrÚinitial_denom_lrc                 ó<  — t                                | ¦  «         |€|j        d         d         nt          |¦  «        }|€|nd}t                                | |d¬¦  «        }t                                ||¦  «        }|r|                     |||z  dœ¦  «         dS dS )a…  Unfreezes a module and adds its parameters to an optimizer.

        Args:
            modules: A module or iterable of modules to unfreeze.
                Their parameters will be added to an optimizer as a new param group.
            optimizer: The provided optimizer will receive new parameters and will add them to
                `add_param_group`
            lr: Learning rate for the new param group.
            initial_denom_lr: If no lr is provided, the learning from the first param group will be used
                and divided by `initial_denom_lr`.
            train_bn: Whether to train the BatchNormalization layers.

        Nr   rf   g      ð?T)rF   rG   )rW   rf   )r   rR   r2   ÚfloatrN   rd   Úadd_param_group)r6   r4   rf   rg   rF   Ú	params_lrÚdenom_lrrW   s           r   Úunfreeze_and_add_param_groupz+BaseFinetuning.unfreeze_and_add_param_groupþ   s´   € õ* 	×%Ò% gÑ.Ô.Ð.Ø79°z�IÔ*¨1Ô-¨dÔ3Ð3ÅuÈRÁyÄyˆ	Ø') zÐ#Ð#°sˆÝ×-Ò-¨gÀÐX\Ð-Ñ]Ô]ˆÝ×3Ò3°I¸vÑFÔFˆØð 	VØ×%Ò%°¸yÈ8Ñ?SÐ&TÐ&TÑUÔUÐUÐUÐUð	Vð 	Vr   Ústagec                 ó„   — |                       |¦  «         ddlm} t          |j        |¦  «        rt          d¦  «        ‚d S )Nr   )ÚDeepSpeedStrategyz‚The Finetuning callback does not support running with the DeepSpeed strategy. Choose a different strategy or disable the callback.)Úfreeze_before_trainingÚpytorch_lightning.strategiesrp   r?   ÚstrategyÚNotImplementedError)r!   r(   r*   rn   rp   s        r   ÚsetupzBaseFinetuning.setup  s`   € à×#Ò# IÑ.Ô.Ð.àBÐBÐBÐBÐBÐBå�gÔ&Ð(9Ñ:Ô:ð 	Ý%ðHñô ð ð	ð 	r   r2   Úmappingc                 óª   ‡— g }| D ]L}d„ |                      ¦   «         D ¦   «         }ˆfd„|d         D ¦   «         |d<   |                     |¦  «         ŒM|S )Nc                 ó&   — i | ]\  }}|d k    ¯||“ŒS )rW   r   )r<   ÚkÚvs      r   ú
<dictcomp>zABaseFinetuning._apply_mapping_to_param_groups.<locals>.<dictcomp>,  s#   € ÐGÐGÐG¡D A q¸¸hº¸˜1˜a¸¸¸r   c                 ó    •— g | ]
}‰|         ‘ŒS r   r   )r<   r]   rv   s     €r   r>   zABaseFinetuning._apply_mapping_to_param_groups.<locals>.<listcomp>-  s   ø€ Ð$EÐ$EÐ$E°A W¨Q¤ZÐ$EÐ$EÐ$Er   rW   )Úitemsr`   )r2   rv   ÚoutputÚgÚgroup_states    `   r   r1   z-BaseFinetuning._apply_mapping_to_param_groups'  sn   ø€ àˆØð 	'ð 	'ˆAàGÐG¨A¯GªG©I¬IÐGÑGÔGˆKØ$EÐ$EÐ$EÐ$E¸¸8¼Ð$EÑ$EÔ$EˆK˜Ñ!Ø�MŠM˜+Ñ&Ô&Ð&Ð&Øˆr   r3   Únum_param_groupsÚcurrent_param_groupsc                 ó6  — d„ |                      ¦   «         D ¦   «         }|| j        vr |                      ||¦  «        | j        |<   d S |t          |¦  «        k    r>| j        |                              |                      ||d …         |¦  «        ¦  «         d S d S )Nc                 ó   — i | ]\  }}||“Œ	S r   r   )r<   Únr]   s      r   r{   z)BaseFinetuning._store.<locals>.<dictcomp>8  s   € ÐAÐAÐA™D˜A˜q�1�aÐAÐAÐAr   )r.   r   r1   ÚlenrA   )r!   r*   r3   r�   r‚   rv   s         r   Ú_storezBaseFinetuning._store1  sÃ   € ð BÐA I×$>Ò$>Ñ$@Ô$@ÐAÑAÔAˆØ˜$Ô;Ð;Ð;Ø9=×9\Ò9\Ø$ gñ:ô :ˆDÔ-¨gÑ6Ð6Ð6ð ¥Ð%9Ñ!:Ô!:Ò:Ð:àÔ-¨gÔ6×=Ò=Ø×3Ò3Ð4HÐIYÐIZÐIZÔ4[Ð]dÑeÔeñô ð ð ð ð ;Ð:r   c                 óØ   — t          |j        ¦  «        D ]T\  }}t          |j        ¦  «        }|                      ||j        |¦  «         |j        }|                      ||||¦  «         ŒUdS )úCalled when the epoch begins.N)r/   r0   r†   r2   Úfinetune_functionÚcurrent_epochr‡   )r!   r(   r*   r3   r4   r�   r‚   s          r   Úon_train_epoch_startz#BaseFinetuning.on_train_epoch_startC  sƒ   € õ #,¨GÔ,>Ñ"?Ô"?ð 	Tð 	TÑˆG�YÝ" 9Ô#9Ñ:Ô:ÐØ×"Ò" 9¨gÔ.CÀYÑOÔOÐOØ#,Ô#9Ð Ø�KŠK˜	 7Ð,<Ð>RÑSÔSÐSÐSð		Tð 	Tr   r   c                 ó   — t           ‚)z$Override to add your unfreeze logic.©rt   )r!   r*   r   r4   s       r   rŠ   z BaseFinetuning.finetune_functionL  ó   € å!Ð!r   c                 ó   — t           ‚)z"Override to add your freeze logic.rŽ   ©r!   r*   s     r   rq   z%BaseFinetuning.freeze_before_trainingP  r�   r   )r   N©r(   r)   r*   r+   r   N)TT)T)Nre   T©r*   r+   r   N)$Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   r   r-   Ústrr   r%   r'   r5   Ústaticmethodr   r	   r   r9   rB   Úboolr   rN   rR   rT   rV   r   rd   r   ri   rm   ru   r1   Úintr‡   rŒ   rŠ   rq   r   r   r   r   r   0   sN  € € € € € ð*ð *ðX!ð !ð !ð !ð ð
˜D  c œNð 
ð 
ð 
ñ „Xð
ð
 ð;¨$¨s°C¨x¬.ð ;¸Tð ;ð ;ð ;ñ „Xð;ð ð%ð %ð %ñ „Xð%ð ðP  v¨x¸¸fÀhÐ>NÔ8OÔ/PÐ'PÔ!Qð PÐVZÐ[aÔVbð Pð Pð Pñ „\ðPð4 àptð ð  Ø�v˜x¨¨f°hÐ.>Ô(?Ô@Ð@ÔAð ØMQð Øimð à	ð ð  ð  ñ „\ð ð, ð+  f¨h°u¸VÀXÐ=MÔ7NÔ.OÐ&OÔ Pð +ÐUYð +ð +ð +ñ „\ð+ð ð(˜fð (¨ð (ð (ð (ñ „\ð(ð ð2ð 2˜˜f h¨u°V¸XÐ5EÔ/FÔ&GÐGÔHð 2ÐTXð 2Ðdhð 2ð 2ð 2ñ „\ð2ð$ ð yð ¸(ð Àtð ð ð ñ „\ðð8 ð #Ø"&ØðVð VØ�v˜x¨¨f°hÐ.>Ô(?Ô@Ð@ÔAðVàðVð �UŒOðVð  ð	Vð
 ðVð 
ðVð Vð Vñ „\ðVð8 ð	˜\ð 	Ð6Jð 	ÐSVð 	Ð[_ð 	ð 	ð 	ñ „Xð	ð ð°T¸$¸sÀC¸x¼.Ô5Ið ÐTXð Ð]aÐbfÐgjÐloÐgoÔbpÔ]qð ð ð ñ „\ððà'ðð ðð ð	ð
 # 4¨¨S¨¤>Ô2ðð 
ðð ð ð ð$ ðTð Tð Tñ „XðTð"Ð+?ð "Èð "ÐXað "Ðfjð "ð "ð "ð "ð"ð "ð "ð "ð "ð "r   r   c                   ó(  ‡ — e Zd ZdZdedddddddf	d	ed
ededee         de	dede	de	deddfˆ fd„Z
edeeef         fd„¦   «         Zedeeef         ddfˆ fd„¦   «         Zed ˆ fd„¦   «         Zed!d„¦   «         Zedddededdfd„¦   «         Zˆ xZS )"r   a^  Finetune a backbone model based on a learning rate user-defined scheduling.

    When the backbone learning rate reaches the current model learning rate
    and ``should_align`` is set to True, it will align with it for the rest of the training.

    Args:
        unfreeze_backbone_at_epoch: Epoch at which the backbone will be unfreezed.
        lambda_func: Scheduling function for increasing backbone learning rate.
        backbone_initial_ratio_lr:
            Used to scale down the backbone learning rate compared to rest of model
        backbone_initial_lr: Optional, Initial learning rate for the backbone.
            By default, we will use ``current_learning /  backbone_initial_ratio_lr``
        should_align: Whether to align with current learning rate when backbone learning
            reaches it.
        initial_denom_lr: When unfreezing the backbone, the initial learning rate will
            ``current_learning_rate /  initial_denom_lr``.
        train_bn: Whether to make Batch Normalization trainable.
        verbose: Display current learning rate for model and backbone
        rounding: Precision for displaying learning rate

    Example::

        >>> import torch
        >>> import torch.nn as nn
        >>> from pytorch_lightning import LightningModule, Trainer
        >>> from pytorch_lightning.callbacks import BackboneFinetuning
        >>> import torchvision.models as models
        >>>
        >>> class TransferLearningModel(LightningModule):
        ...     def __init__(self, num_classes=10):
        ...         super().__init__()
        ...         # REQUIRED: Your model must have a 'backbone' attribute
        ...         self.backbone = models.resnet50(weights=None)
        ...         # Remove the final classification layer from backbone
        ...         self.backbone = nn.Sequential(*list(self.backbone.children())[:-1])
        ...
        ...         # Add your task-specific head
        ...         self.head = nn.Sequential(
        ...             nn.Flatten(),
        ...             nn.Linear(2048, 512),
        ...             nn.ReLU(),
        ...             nn.Linear(512, num_classes)
        ...         )
        ...
        ...     def forward(self, x):
        ...         # Extract features with backbone
        ...         features = self.backbone(x)
        ...         # Classify with head
        ...         return self.head(features)
        ...
        ...     def configure_optimizers(self):
        ...         # Initially only optimize the head - backbone will be added by callback
        ...         return torch.optim.Adam(self.head.parameters(), lr=1e-3)
        ...
        >>> # Setup the callback
        >>> multiplicative = lambda epoch: 1.5
        >>> backbone_finetuning = BackboneFinetuning(
        ...     unfreeze_backbone_at_epoch=10,  # Start unfreezing at epoch 10
        ...     lambda_func=multiplicative,     # Gradually increase backbone LR
        ...     backbone_initial_ratio_lr=0.1,  # Start backbone at 10% of head LR
        ... )
        >>> model = TransferLearningModel()
        >>> trainer = Trainer(callbacks=[backbone_finetuning])

    é
   gš™™™™™¹?NTre   Fé   Úunfreeze_backbone_at_epochÚlambda_funcÚbackbone_initial_ratio_lrÚbackbone_initial_lrÚshould_alignrg   rF   ÚverboseÚroundingr   c
                 óÔ   •— t          ¦   «                              ¦   «          || _        || _        || _        || _        || _        || _        || _        || _	        |	| _
        d | _        d S ©N)Úsuperr"   rŸ   r    r¡   r¢   r£   rg   rF   r¤   r¥   Úprevious_backbone_lr)r!   rŸ   r    r¡   r¢   r£   rg   rF   r¤   r¥   Ú	__class__s             €r   r"   zBackboneFinetuning.__init__˜  sn   ø€ õ 	‰Œ×ÒÑÔÐà/IˆÔ'Ø%0ˆÔØ0IˆÔ&Ø4GˆÔ Ø".ˆÔØ'7ˆÔØ&ˆŒØ$ˆŒØ%ˆŒØ59ˆÔ!Ð!Ð!r   c                 ó    — | j         | j        dœS )N)r$   r©   )r   r©   r    s    r   r%   zBackboneFinetuning.state_dict±  s   € ð ,0Ô+LØ$(Ô$=ð
ð 
ð 	
r   r%   c                 ód   •— |d         | _         t          ¦   «                              |¦  «         d S )Nr©   )r©   r¨   r'   )r!   r%   rª   s     €r   r'   z"BackboneFinetuning.load_state_dict¸  s/   ø€ à$.Ð/EÔ$FˆÔ!Ý‰Œ×Ò 
Ñ+Ô+Ð+Ð+Ð+r   r(   r)   r*   r+   c                 óº   •— t          |d¦  «        r<t          |j        t          ¦  «        r"t	          ¦   «                              ||¦  «        S t          d¦  «        ‚)zŠ
        Raises:
            MisconfigurationException:
                If LightningModule has no nn.Module `backbone` attribute.
        Úbackbonez@The LightningModule should have a nn.Module `backbone` attribute)Úhasattrr?   r®   r	   r¨   r5   r   )r!   r(   r*   rª   s      €r   r5   zBackboneFinetuning.on_fit_start½  sT   ø€ õ �9˜jÑ)Ô)ð 	<­j¸Ô9KÍVÑ.TÔ.Tð 	<Ý‘7”7×'Ò'¨°Ñ;Ô;Ð;Ý'Ð(jÑkÔkÐkr   c                 ó:   — |                       |j        ¦  «         d S r§   )rV   r®   r‘   s     r   rq   z)BackboneFinetuning.freeze_before_trainingÈ  s   € à�Š�IÔ&Ñ'Ô'Ð'Ð'Ð'r   r   r4   c           	      óâ  — || j         k    r¬|j        d         d         }| j        �| j        n	|| j        z  }|| _        |                      |j        ||| j        | j        ¬¦  «         | j	        rHt                               dt          || j        ¦  «        › dt          || j        ¦  «        › �¦  «         dS dS || j         k    rª|j        d         d         }|                      |dz   ¦  «        | j        z  }| j        r||k    r|n|}||j        d         d<   || _        | j	        rJt                               dt          || j        ¦  «        › dt          || j        ¦  «        › �¦  «         dS dS dS )	r‰   r   rf   N)rF   rg   zCurrent lr: z, Backbone lr: é   éÿÿÿÿ)rŸ   r2   r¢   r¡   r©   rm   r®   rF   rg   r¤   ÚlogÚinfoÚroundr¥   r    r£   )r!   r*   r   r4   Ú
current_lrÚinitial_backbone_lrÚnext_current_backbone_lrs          r   rŠ   z$BackboneFinetuning.finetune_functionÌ  sø  € ð �DÔ3Ò3Ð3Ø"Ô/°Ô2°4Ô8ˆJð Ô+Ð7ð Ô(Ð(à $Ô"@Ñ@ð  ð
 )<ˆDÔ%Ø×-Ò-ØÔ"ØØ#ØœØ!%Ô!6ð .ñ ô ð ð Œ|ð Ý—’ðP¥5¨°T´]Ñ#CÔ#Cð Pð PÝ$)Ð*=¸t¼}Ñ$MÔ$MðPð Pñô ð ð ð ðð ð �TÔ4Ò4Ð4Ø"Ô/°Ô2°4Ô8ˆJØ'+×'7Ò'7¸À¹	Ñ'BÔ'BÀTÔE^Ñ'^Ð$ð Ô%ð.Ø*BÀZÒ*OÐ*Oð �
à-ð %ð
 0HˆIÔ" 2Ô& tÑ,Ø(@ˆDÔ%ØŒ|ð Ý—’ðU¥5¨°T´]Ñ#CÔ#Cð Uð UÝ$)Ð*BÀDÄMÑ$RÔ$RðUð Uñô ð ð ð ð 5Ð4ðð r   r’   r“   )r”   r•   r–   r—   r   r›   r   ri   r   rš   r"   r   r-   r˜   r   r%   r'   r5   rq   r   rŠ   Ú__classcell__)rª   s   @r   r   r   U  sÐ  ø€ € € € € ð@ð @ðH +-Ø .Ø+0Ø/3Ø!Ø"&ØØØð:ð :à$'ð:ð ð:ð $)ð	:ð
 & eœ_ð:ð ð:ð  ð:ð ð:ð ð:ð ð:ð 
ð:ð :ð :ð :ð :ð :ð2 ð
˜D  c œNð 
ð 
ð 
ñ „Xð
ð ð,¨$¨s°C¨x¬.ð ,¸Tð ,ð ,ð ,ð ,ð ,ñ „Xð,ð ðlð lð lð lð lñ „Xðlð ð(ð (ð (ñ „Xð(ð ð%Ð+?ð %Èð %ÐXað %Ðfjð %ð %ð %ñ „Xð%ð %ð %ð %ð %r   )'r—   ÚloggingÚcollections.abcr   r   Útypingr   r   r   r   rZ   Útorch.nnr	   r
   Útorch.nn.modules.batchnormr   Útorch.optim.optimizerr   Útyping_extensionsr   Úpytorch_lightningÚplÚ$pytorch_lightning.callbacks.callbackr   Ú&pytorch_lightning.utilities.exceptionsr   Ú#pytorch_lightning.utilities.importsr   Ú%pytorch_lightning.utilities.rank_zeror   Ú__doctest_skip__Ú	getLoggerr”   r´   r›   ri   r   r   r   r   r   r   ú<module>rÊ      s¹  ððð ð €€€Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1à €€€Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø +Ð +Ð +Ð +Ð +Ð +Ø &Ð &Ð &Ð &Ð &Ð &à Ð Ð Ð Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø LÐ LÐ LÐ LÐ LÐ LØ FÐ FÐ FÐ FÐ FÐ FØ @Ð @Ð @Ð @Ð @Ð @àð .Ø,Ð-Ðð €gÔ˜Ñ!Ô!€ð˜#ð  %ð ð ð ð ðb"ð b"ð b"ð b"ð b"�Xñ b"ô b"ð b"ðJ	]ð ]ð ]ð ]ð ]˜ñ ]ô ]ð ]ð ]ð ]r   