o
    1ÎjJK  ã                   @   sp   d dl Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lmZ dd
lmZ G dd„ de
ƒZdS )é    Né   )ÚDEFAULT_CACHE_DIR)Úparse_device)Údownload)Úabspathé   )Ú	BaseModel)ÚCLIArgument)ÚCompletedProcessc                   @   s4  e Zd ZdZ											d$deded	ed
ededededededededefdd„Z						d%deded
e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'dd„Z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(dededed	ededededefd"d#„ZdS ))ÚSegModelzSemantic Segmentation ModelNÚgpuFÚOFFTÚ
batch_sizeÚlearning_rateÚepochs_itersÚipsÚdeviceÚresume_pathÚdy2stÚampÚnum_workersÚuse_vdlÚsave_dirÚreturnc           !   	   K   sÐ  | j  ¡ }g }|dur| td|ƒ¡ |dur| td|ƒ¡ |dur+| td|ƒ¡ |dur=t|ƒ\}}| td|ƒ¡ | dd¡}|du rR|durRtj |d¡}|dur˜t	|ƒ}tj 
|¡dkrit|› dƒ‚tj |¡svt|› d	ƒ‚tj |¡}tj |d
¡}tj |¡st|› dƒ‚| td|ƒ¡ |rŸ| |¡ |
r¨| tdƒ¡ |dur±t	|ƒ}n	t	tj dd¡ƒ}| td|ƒ¡ | dd¡}|durÔ| td|ƒ¡ | dd¡}| dd¡}| dd¡}| dd¡}|durø| td|ƒ¡ | dd¡}|dur| td|ƒ¡ | dd¡}|dur!| tdg|¢R Ž ¡ | dd¡}|durØ| dd¡}| dd¡}| dd¡}| dd¡}| d d¡}	| |¡ | d!d¡}| | d"d¡¡ | | d#d¡¡ |dur~t|tƒsvJ d$ƒ‚| td%|ƒ¡ |	dur•t|	tƒsJ d&ƒ‚| td'|	ƒ¡ |dur¬t|tƒs¤J d(ƒ‚| td)|ƒ¡ |d*v rÁ| td+d,ƒ¡ | td-|ƒ¡ |dur×| ¡ D ]\}}t|ƒtj|< qÊnA|durò|d.krò| td+d,ƒ¡ | td-|ƒ¡ |	durÿ| td'|	ƒ¡ |dur| td%|ƒ¡ |dur| td)|ƒ¡ | d/d¡}| d0d¡}| d/|¡ | d1| j¡ |r;| d0|¡ |  |¡ |  ¡ } | | ¡ | jj| |||||d2W  d  ƒ S 1 saw   Y  dS )3ah  train self

        Args:
            batch_size (int, optional): the train batch size value. Defaults to None.
            learning_rate (float, optional): the train learning rate value. Defaults to None.
            epochs_iters (int, optional): the train epochs value. Defaults to None.
            ips (str, optional): the ip addresses of nodes when using distribution. Defaults to None.
            device (str, optional): the running device. Defaults to 'gpu'.
            resume_path (str, optional): the checkpoint file path to resume training. Train from scratch if it is set
                to None. Defaults to None.
            dy2st (bool, optional): Enable dynamic to static. Defaults to False.
            amp (str, optional): the amp settings. Defaults to 'OFF'.
            num_workers (int, optional): the workers number. Defaults to None.
            use_vdl (bool, optional): enable VisualDL. Defaults to True.
            save_dir (str, optional): the directory path to save train output. Defaults to None.

        Returns:
           CompletedProcess: the result of training subprocess execution.
        Nú--batch_sizeú--learning_rateú--itersú--deviceÚ
resume_dirzmodel.pdparamsz has an incorrect file name.z does not exist.zmodel.pdoptz must exist.z--resume_modelú	--use_vdlÚoutputÚtrainú
--save_dirÚsave_intervalz--save_intervalÚdo_evalTÚrepeatsÚseedÚprofilez--profiler_optionsÚ	log_itersz--log_itersÚinput_shapeú--input_shapeÚ	benchmarkÚenvFr   r   Zprint_mem_infoÚshufflezrepeats must be an integer.z	--repeatsznum_workers must be an integer.ú--num_workerszseed must be an integer.z--seed)ZO1ZO2ú--precisionÚfp16ú--amp_levelr   Úuniform_output_enabledÚexport_with_pirÚpdx_model_name)r$   ) ÚconfigÚcopyÚappendr	   r   ÚpopÚosÚpathÚjoinr   ÚbasenameÚ
ValueErrorÚexistsÚFileNotFoundErrorÚdirnameZupdate_dy2stÚgetZupdate_log_ranksZupdate_print_mem_infoZupdate_shuffleÚ
isinstanceÚintÚitemsÚstrÚenvironÚset_valÚnameÚ_assert_empty_kwargsÚ_create_new_config_fileÚdumpÚrunnerr!   )!Úselfr   r   r   r   r   r   r   r   r   r   r   Úkwargsr5   Úcli_argsÚdevice_typeÚ_r   Z	opts_pathr#   r$   r%   r&   r'   r(   r)   r+   ZenvsÚenv_nameZ	env_valuer2   r3   Úconfig_path© rT   új/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/repo_apis/PaddleSeg_api/seg/model.pyr!      sÄ   
"










€







ÿ&ýzSegModel.trainÚweight_pathc                 K   s   | j  ¡ }g }	t|ƒ}|	 td|ƒ¡ |dur|dkrtdƒ‚|dur1t|ƒ\}
}|	 td|
ƒ¡ |durI|dkrI|	 tddƒ¡ |	 td	|ƒ¡ |durU|	 td
|ƒ¡ |  |¡ |  ¡ }| 	|¡ | j
 ||	||¡}|W  d  ƒ S 1 syw   Y  dS )aš  evaluate self using specified weight

        Args:
            weight_path (str): the path of model weight file to be evaluated.
            batch_size (int, optional): the batch size value in evaluating. Defaults to None.
            ips (str, optional): the ip addresses of nodes when using distribution. Defaults to None.
            device (str, optional): the running device. Defaults to 'gpu'.
            amp (str, optional): the AMP setting. Defaults to 'OFF'.
            num_workers (int, optional): the workers number in evaluating. Defaults to None.

        Returns:
            CompletedProcess: the result of evaluating subprocess execution.
        ú--model_pathNé   z)Batch size other than 1 is not supported.r   r   r/   r0   r1   r.   )r5   r6   r   r7   r	   r=   r   rI   rJ   rK   rL   Úevaluate)rM   rV   r   r   r   r   r   rN   r5   rO   rP   rQ   rS   ÚcprT   rT   rU   rY   ½   s,   



$ýzSegModel.evaluateÚ
input_pathc                 K   sæ   | j  ¡ }g }t|ƒ}| td|ƒ¡ t|ƒ}| td|ƒ¡ |dur1t|ƒ\}}	| td|ƒ¡ |dur:t|ƒ}n	ttj dd¡ƒ}| td|ƒ¡ |  	|¡ |  
¡ }
| |
¡ | j |
||¡W  d  ƒ S 1 slw   Y  dS )a×  predict using specified weight

        Args:
            weight_path (str): the path of model weight file used to predict.
            input_path (str): the path of image file to be predicted.
            device (str, optional): the running device. Defaults to 'gpu'.
            save_dir (str, optional): the directory path to save predict output. Defaults to None.

        Returns:
            CompletedProcess: the result of predicting subprocess execution.
        rW   ú--image_pathNr   r    Úpredictr"   )r5   r6   r   r7   r	   r   r9   r:   r;   rI   rJ   rK   rL   r]   )rM   rV   r[   r   r   rN   r5   rO   rP   rQ   rS   rT   rT   rU   r]   ó   s$   




$þzSegModel.predictc                 K   sÔ   | j  ¡ }g }t|ƒ}| td|ƒ¡ |dur%t|ƒ\}}	| td|ƒ¡ |dur.t|ƒ}n	ttj dd¡ƒ}| td|ƒ¡ |  	|¡ |  
¡ }
| |
¡ | j |
|||¡}|W  d  ƒ S 1 scw   Y  dS )ÚanalyserW   Nr   r    Zanalysisr"   )r5   r6   r   r7   r	   r   r9   r:   r;   rI   rJ   rK   rL   r^   )rM   rV   r   r   r   rN   r5   rO   rP   rQ   rS   rZ   rT   rT   rU   r^      s"   




$ýzSegModel.analysec                 K   sš  | j  ¡ }g }| d¡st|ƒ}ntj |¡}tj t|¡}t	||ddd |}| 
td|ƒ¡ |dur9t|ƒ}n	ttj dd¡ƒ}| 
td|ƒ¡ | d	d¡}|dur_| 
td
g|¢R Ž ¡ z|d }	W n
   | dd¡}	Y |	dur„|	dv s|J dƒ‚| 
td|	ƒ¡ | dd¡}
| dd¡}| d|
¡ | d| j¡ |r¥| d|¡ |  |¡ |  ¡ }| |¡ | j ||d¡W  d  ƒ S 1 sÆw   Y  dS )a3  export the dynamic model to static model

        Args:
            weight_path (str): the model weight file path that used to export.
            save_dir (str): the directory path to save export output.

        Returns:
            CompletedProcess: the result of exporting subprocess execution.
        ÚhttpT)Zprint_progressÚ	overwriterW   Nr    Úexportr"   r)   r*   Ú	output_op)ZsoftmaxZargmaxÚnonez2`output_op` must be 'none', 'softmax' or 'argmax'.z--output_opr2   r3   Fr4   )r5   r6   Ú
startswithr   r9   r:   r<   r;   r   r   r7   r	   r8   rG   rH   rI   rJ   rK   rL   ra   )rM   rV   r   rN   r5   rO   ÚfilenameZ	save_pathr)   rb   r2   r3   rS   rT   rT   rU   ra   :  sH   





ü


$þzSegModel.exportÚ	model_dirc                 K   sä   | j  ¡ }g }t|ƒ}t|ƒ}| td|ƒ¡ |dur)t|ƒ\}}	| td|ƒ¡ |dur2t|ƒ}n	ttj dd¡ƒ}| td|ƒ¡ |  	|¡ |  
¡ }
| |
¡ tj |d¡}| j |||¡W  d  ƒ S 1 skw   Y  dS )aê  predict image using infernece model

        Args:
            model_dir (str): the directory path of inference model files that would use to predict.
            input_path (str): the path of image that would be predict.
            device (str, optional): the running device. Defaults to 'gpu'.
            save_dir (str, optional): the directory path to save output. Defaults to None.

        Returns:
            CompletedProcess: the result of inferring subprocess execution.
        r\   Nr   r    Úinferr"   zinference.yml)r5   r6   r   r7   r	   r   r9   r:   r;   rI   rJ   rK   rL   rg   )rM   rf   r[   r   r   rN   r5   rO   rP   rQ   rS   Zdeploy_config_pathrT   rT   rU   rg   v  s$   




$ýzSegModel.inferc                 K   st  | j  ¡ }	g }
g }t|ƒ}|
 td|ƒ¡ |dur!|
 td|ƒ¡ |dur-|
 td|ƒ¡ |dur9|
 td|ƒ¡ |durKt|ƒ\}}|
 td|ƒ¡ |rT|
 tdƒ¡ |dur]t|ƒ}n	ttj dd	¡ƒ}|
 td
|ƒ¡ | td
tj |d¡ƒ¡ | 	dd¡}|dur| tdg|¢R Ž ¡ |  
|¡ |  ¡ }|	 |¡ | j ||
|||¡W  d  ƒ S 1 s³w   Y  dS )a  compression model

        Args:
            weight_path (str): the path to weight file of model.
            batch_size (int, optional): the batch size value of compression training. Defaults to None.
            learning_rate (float, optional): the learning rate value of compression training. Defaults to None.
            epochs_iters (int, optional): the epochs or iters of compression training. Defaults to None.
            device (str, optional): the device to run compression training. Defaults to 'gpu'.
            use_vdl (bool, optional): whether or not to use VisualDL. Defaults to True.
            save_dir (str, optional): the directory to save output. Defaults to None.

        Returns:
            CompletedProcess: the result of compression subprocess execution.
        rW   Nr   r   r   r   r   r    Úcompressr"   ra   r)   r*   )r5   r6   r   r7   r	   r   r9   r:   r;   r8   rI   rJ   rK   rL   Úcompression)rM   rV   r   r   r   r   r   r   rN   r5   Ztrain_cli_argsZexport_cli_argsrP   rQ   r)   rS   rT   rT   rU   ri   ¢  sB   

ÿ



ÿ$þzSegModel.compression)NNNNr   NFr   NTN)NNr   r   N)r   N)Nr   N)NNNr   TN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rC   ÚfloatrE   Úboolr
   r!   rY   r]   r^   ra   rg   ri   rT   rT   rT   rU   r      sÜ    ôþýüûúùø	÷
öõô
ò $ùþýüûúù	
÷:ûþýüû
ù
-@ûþýüû
ù/øþýüûúùø
ör   )r9   Zutils.cacher   Zutils.devicer   Zutils.downloadr   Z
utils.miscr   Úbaser   Zbase.utils.argr	   Zbase.utils.subprocessr
   r   rT   rT   rT   rU   Ú<module>   s   