o
    1j                     @   sF   d dl Z ddlmZ ddlmZ G dd deZdedefd	d
ZdS )    N   )
BaseRunner)CompletedProcessc                   @   s   e Zd ZdZ	ddedededededefd	d
Zdededededef
ddZdedededefddZ		ddededededef
ddZ
dedededefddZdedededededefddZdS )	ClsRunnerz
Cls RunnerTconfig_pathcli_argsdeviceipssave_dirreturnc           
   
   C   s^   | j |||d\}}g |dd||}	|	dd| g | j|	|dddd| |dS )	a&  train model

        Args:
            config_path (str): the config file path used to train.
            cli_args (list): the additional parameters.
            device (str): the training device.
            ips (str): the ip addresses of nodes when using distribution.
            save_dir (str): the directory path to save training output.
            do_eval (bool, optional): whether or not to evaluate model during training. Defaults to True.

        Returns:
            CompletedProcess: the result of training subprocess execution.
        )Zlog_dirztools/train.py-c-ozGlobal.eval_during_train=TF)envswitch_wdirechosilentcapture_outputlog_path)distributedextendrun_cmdZ_get_train_log_path)
selfr   r   r   r	   r
   Zdo_evalargsr   cmd r   l/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/repo_apis/PaddleClas_api/cls/runner.pytrain   s   zClsRunner.trainc           
      C   s\   |  ||\}}g |dd||}| j||ddddd}|jdkr,t|j}	|	|_|S )a  run model evaluating

        Args:
            config_path (str): the config file path used to evaluate.
            cli_args (list): the additional parameters.
            device (str): the evaluating device.
            ips (str): the ip addresses of nodes when using distribution.

        Returns:
            CompletedProcess: the result of evaluating subprocess execution.
        ztools/eval.pyr   TF)r   r   r   r   r   r   )r   r   
returncode_extract_eval_metricsstdoutZmetrics)
r   r   r   r   r	   r   r   r   cpmetric_dictr   r   r   evaluate;   s   

zClsRunner.evaluatec                 C   s$   | j dd|g|}| j|ddddS )a7  run predicting using dynamic mode

        Args:
            config_path (str): the config file path used to predict.
            cli_args (list): the additional parameters.
            device (str): unused.

        Returns:
            CompletedProcess: the result of predicting subprocess execution.
        ztools/infer.pyr   TFr   r   r   pythonr   r   r   r   r   r   r   r   r   predictS   s   zClsRunner.predictNc                 C   s0   | j dd|g|dd}| j|dddd}|S )a  run exporting

        Args:
            config_path (str): the path of config file used to export.
            cli_args (list): the additional parameters.
            device (str): unused.
            save_dir (str, optional): the directory path to save exporting output. Defaults to None.

        Returns:
            CompletedProcess: the result of exporting subprocess execution.
        ztools/export_model.pyr   r   zGlobal.export_for_fd=TrueTFr#   r$   )r   r   r   r   r
   r   r    r   r   r   exportd   s   
zClsRunner.exportc                 C   s$   | j dd|g|}| j|ddddS )a<  run predicting using inference model

        Args:
            config_path (str): the path of config file used to predict.
            cli_args (list): the additional parameters.
            device (str): unused.

        Returns:
            CompletedProcess: the result of inferring subprocess execution.
        zpython/predict_cls.pyr   ZdeployTFr#   r$   r&   r   r   r   infer   s   zClsRunner.infertrain_cli_argsexport_cli_argstrain_save_dirc           	      C   sN   |  |||d|}tj|dd}g |dd| }| |||}||fS )a  run compression model

        Args:
            config_path (str): the path of config file used to predict.
            train_cli_args (list): the additional training parameters.
            export_cli_args (list): the additional exporting parameters.
            device (str): the running device.
            train_save_dir (str): the directory path to save output.

        Returns:
            CompletedProcess: the result of compression subprocess execution.
        NZ
best_modelmodelr   zGlobal.pretrained_model=)r   ospathjoinr(   )	r   r   r*   r+   r   r,   Zcp_trainZweight_pathZ	cp_exportr   r   r   compression   s   zClsRunner.compression)T)N)__name__
__module____qualname____doc__strlistr   r   r"   r'   r(   r)   r1   r   r   r   r   r      s|    	
#


r   r   r   c                 C   s   ddl }d}dd|dd|dd|dd|d	d|g}d
gd
dgg ddgdgg}t }t||D ]*\}}||}|  D ]}||}	|	r`t|tt|		 D ]\}
}|||
< qWqDq7|S )zextract evaluation metrics from training log

    Args:
        stdout (str): the training log

    Returns:
        dict: the training metric
    r   Nz([-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?z'\[Eval\]\[Epoch 0\]\[Avg\].*top1: (_dp)Z_dpz4\[Eval\]\[Epoch 0\]\[Avg\].*top1: (_dp), top5: (_dp)zF\[Eval\]\[Epoch 0\]\[Avg\].*recall1: (_dp), recall5: (_dp), mAP: (_dp)z<\[Eval\]\[Epoch 0\]\[Avg\].*MultiLabelMAP\(integral\): (_dp)z/\[Eval\]\[Epoch 0\]\[Avg\].*evalres:\ ma: (_dp)zval.top1zval.top5)Zrecall1Zrecall5ZmAPZMultiLabelMAPzevalres: ma)
rereplacedictzipcompile
splitlinessearchmapfloatgroups)r   r8   Z_DPpatternskeysr!   patternkeylinematchkvr   r   r   r      s:   	





r   )	r.   baser   Zbase.utils.subprocessr   r   r6   r:   r   r   r   r   r   <module>   s    