o
    1jH"                     @   sR   d dl Z ddlm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gather_opts_args)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 Z
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d ZdS )	SegRunnerzSemantic Segmentation RunnerTconfig_pathcli_argsdeviceipssave_dirreturnc           
   
   C   sh   | j |||d\}}| |}g |d}	|r|	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	--do_eval--configTFenvswitch_wdirechosilentcapture_outputlog_path)distributed_gather_opts_argsappendextendrun_cmd_get_train_log_path)
selfr   r	   r
   r   r   Zdo_evalargsr   cmd r!   k/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/repo_apis/PaddleSeg_api/seg/runner.pytrain   s   

zSegRunner.trainc           
      C   sf   |  ||\}}| |}g |dd||}| j||ddddd}|jdkr1t|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/val.pyr   TFr   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SegRunner.evaluatec                 C   .   |  |}| 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/predict.pyr   TFr   r   r   r   pythonr   r   r   r	   r
   r    r!   r!   r"   predictZ   s   
zSegRunner.predictc           	      C   sL   |  ||\}}| |}g |dd||}| j||ddddd}|S )analyseztools/analyse.pyr   TFr$   )r   r   r   )	r   r   r	   r
   r   r   r   r    r(   r!   r!   r"   r1   l   s   
zSegRunner.analysec                 C   s4   |  |}| jddd|g|}| 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.

        Returns:
            CompletedProcess: the result of exporting subprocess execution.
        ztools/export.py--for_fdr   TFr,   r-   )r   r   r	   r
   r    r(   r!   r!   r"   exportw   s   
	zSegRunner.exportc                 C   r+   )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deploy/python/infer.pyr   TFr,   r-   r/   r!   r!   r"   infer   s   
zSegRunner.infertrain_cli_argsexport_cli_argstrain_save_dirc              
   C   s   | j ||d\}}| |}g |ddd||}| j||dddd| |d}	| |}tj|dd	}
| jd
dd|d|
g|}| j|dd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.
        r   zdeploy/slim/quant/qat_train.pyr   r   TFr   Z
best_modelzmodel.pdparamszdeploy/slim/quant/qat_export.pyr2   z--model_pathr,   )r   r   r   r   ospathjoinr.   )r   r   r5   r6   r
   r7   r   r   r    Zcp_trainZweight_pathZ	cp_exportr!   r!   r"   compression   sL   


zSegRunner.compressionc                 C   s
   t |dS )Nz--optsr   )r   r   r!   r!   r"   r      s   
zSegRunner._gather_opts_argsN)T)__name__
__module____qualname____doc__strlistr   r#   r*   r0   r1   r3   r4   r;   r   r!   r!   r!   r"   r      sj    	
&


<r   r'   r   c                 C   sz   ddl }d}dd|}g d}t }||}|  }|D ]}||}|r:t|tt|	 D ]\}	}
|
||	< q1q|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;Images: \d+ mIoU: (_dp) Acc: (_dp) Kappa: (_dp) Dice: (_dp)Z_dp)ZmIoUZAccKappaZDice)
rereplacedictcompile
splitlinessearchzipmapfloatgroups)r'   rC   Z_DPpatternkeysr)   lineslinematchkvr!   r!   r"   r&      s    	


r&   )r8   baser   Zbase.utils.argr   Zbase.utils.subprocessr   r   r@   rE   r&   r!   r!   r!   r"   <module>   s    U