
    ^j                    b    d Z ddlmZ ddlZddlmZ ddlZ ej                  e      Z	dgZ
ddZy)a  Checkpoint conversion utilities for the PTL training stack.

Provides :func:`convert_legacy_checkpoint` to convert RF-DETR ``*.pth`` checkpoints (produced by the pre-PTL
``engine.py`` training loop) into the ``*.ckpt`` format expected by ``pytorch_lightning.Trainer``.

Auto-detection of legacy format at load time is handled by
:meth:`rfdetr.training.module_model.RFDETRModelModule.on_load_checkpoint`.
    )annotationsN)Anyconvert_legacy_checkpointc                   t        j                  | dd      }d|vrt        d| d      |j                  d      }t	        |t
              r|}n|i }n	 t        |      }|d   j                         D ci c]  \  }}d
|z   | c}}|j                  dd      d|dd}d|v r|d   |d<   t        j                  ||       y# t        $ r. t        j                  d	t        |      j                         i }Y w xY wc c}}w )a  Convert a legacy RF-DETR ``.pth`` checkpoint to PTL ``.ckpt`` format.

    Loads a checkpoint saved by the pre-PTL ``engine.py`` training loop and rewrites it in the structure expected by
    ``pytorch_lightning.Trainer``:

    * ``state_dict`` keys are prefixed with ``"model."`` to match the
      attribute path inside :class:`~rfdetr.training.module_model.RFDETRModelModule`.
    * ``args`` (``argparse.Namespace`` or ``dict``) is normalised to a plain
      ``dict`` and stored as ``hyper_parameters``.
    * ``legacy_checkpoint_format: True`` is written so
      :meth:`~rfdetr.training.module_model.RFDETRModelModule.on_load_checkpoint` can distinguish converted files from
      native PTL checkpoints.
    * If an ``ema_model`` key is present it is preserved verbatim under
      ``legacy_ema_state_dict`` for optional EMA weight restoration.

    Args:
        old_path: Path to the source legacy ``.pth`` checkpoint.
        new_path: Destination path for the converted ``.ckpt`` file.
    cpuF)map_locationweights_onlymodelzThe checkpoint at zd does not contain a 'model' key. Only RF-DETR legacy .pth files produced by engine.py are supported.argsNzICannot extract hyper_parameters from args of type %s; storing empty dict.zmodel.epochr   T)
state_dictr   global_stephyper_parameterslegacy_checkpoint_format	ema_modellegacy_ema_state_dict)torchload
ValueErrorget
isinstancedictvars	TypeErrorloggerwarningtype__name__itemssave)old_pathnew_patholdargs_objr   kvnews           e/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/rfdetr/training/checkpoint.pyr   r      s$   (  **XEPUVCc  -S S
 	

 wwvH(D!+3			"#H~ 47w<3E3E3GH41ax!|QH!$,$(C c (+;'7#$	JJsH)  	"NN[X''  "	" Is   C 7C>4C;:C;)r!   strr"   r)   returnNone)__doc__
__future__r   loggingtypingr   r   	getLoggerr   r   __all__r        r(   <module>r4      s7    #   			8	$&
'8r3   