o
    1Îjr8  ã                   @   sX   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
 G d	d
„ d
eƒZdS )é    Né   )Úlogging)Úabspathé   )Ú	BaseModel)ÚCLIArgument)ÚCompletedProcessc                   @   s6  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defdd„Zdededefdd„Z			d'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defd"d#„ZdS ))ÚClsModelzImage Classification ModelNÚgpuFÚOFFTÚ
batch_sizeÚlearning_rateÚepochs_itersÚipsÚdeviceÚresume_pathÚdy2stÚampÚnum_workersÚuse_vdlÚsave_dirÚreturnc              	   K   s¦  |durt |ƒ}|  ¡ ;}| j ¡ }| |¡ | |¡ | |
¡ |dur+| |¡ |dur4| |¡ |dur=| 	|¡ | 
|¡ |durKt |ƒ}nt | ¡ ƒ}| |¡ |	dur_| |	¡ g }| dd¡}| dd¡}|dury| td|ƒ¡ | dd¡}|durô| dd¡}| dd¡}| dd	¡}| d
d¡}	| |¡ | | dd¡¡ | | dd	¡¡ | | dd	¡¡ | | dd¡¡ | | dd¡¡ |	durÖ| |	¡ |durß| |¡ |duró| ¡ D ]\}}t|ƒtj|< qçn| |¡ | d¡d }| dd¡}| dd	¡}| d|› g¡ | d| j› g¡ |r+| d|› g¡ |  |¡ |  !|¡ | j"j#||||||dW  d  ƒ S 1 sLw   Y  dS )ah  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Údo_evalTÚprofilez--profiler_optionsÚ	benchmarkÚenvÚseedFr   r   ZdaliÚshuffleZshared_memoryZprint_mem_infoú:r   Úuniform_output_enabledÚexport_with_pirúGlobal.uniform_output_enabled=úGlobal.pdx_model_name=úGlobal.export_with_pir=)r   )$r   Ú_create_new_config_fileÚconfigÚcopyÚupdate_deviceZ_update_to_staticÚ_update_use_vdlÚupdate_batch_sizeÚupdate_learning_rateÚ_update_epochsZ_update_checkpointsÚget_train_save_dirÚ_update_output_dirÚupdate_num_workersÚpopÚappendr   ÚgetZupdate_log_ranksÚ_update_ampZupdate_daliZupdate_shuffleZupdate_shared_memoryZupdate_print_mem_infoZupdate_seedÚitemsÚstrÚosÚenvironÚsplitÚupdateÚnameÚdumpÚ_assert_empty_kwargsÚrunnerÚtrain)Úselfr   r   r   r   r   r   r   r   r   r   r   ÚkwargsÚconfig_pathr%   Zcli_argsr   r   r   Zenvsr   Úenv_nameZ	env_valueZdevice_typer   r    © rB   úk/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/repo_apis/PaddleClas_api/cls/model.pyr=      sv   "













€


ÿ&ÂzClsModel.trainÚweight_pathc                 K   sª   t |ƒ}|  ¡ C}| j ¡ }	|	 |¡ |	 |¡ |	 |¡ |dur&|	 |¡ |dur/|	 |¡ |	 	|¡ |  
|¡ | j |g ||¡}
|
W  d  ƒ S 1 sNw   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.
        N)r   r$   r%   r&   r2   r'   Úupdate_pretrained_weightsr)   r.   r:   r;   r<   Úevaluate)r>   rD   r   r   r   r   r   r?   r@   r%   ÚcprB   rB   rC   rF   ‚   s   








$ðzClsModel.evaluateÚ
input_pathÚinput_list_pathc           	      K   s    t |ƒ}t |ƒ}|rt |ƒ}|  ¡ 4}| j ¡ }| |¡ | ||¡ | |¡ | |¡ | |¡ |  	|¡ | j
 |g |¡W  d  ƒ S 1 sIw   Y  dS )aJ  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.
            input_list_path (str, optional): the paths of images to be predicted if is not None. Defaults to None.
            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.
        N)r   r$   r%   r&   rE   Z_update_predict_imgr'   Z_update_save_predict_resultr:   r;   r<   Úpredict)	r>   rD   rH   rI   r   r   r?   r@   r%   rB   rB   rC   rJ   ­   s   






$ôzClsModel.predictc           	      K   sø   |  d¡s	t|ƒ}t|ƒ}|  ¡ a}| j ¡ }| |¡ | |¡ | dd¡}|r.| |¡ | dd¡}| dd¡}| 	d|› g¡ | 	d	| j
› g¡ |rX| 	d
|› g¡ | |¡ |  |¡ | j |g d|¡W  d  ƒ S 1 suw   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.
        Úhttpr   Nr   Tr    Fr!   r"   r#   )Ú
startswithr   r$   r%   r&   rE   Z_update_save_inference_dirr/   r'   r8   r9   r:   r;   r<   Úexport)	r>   rD   r   r?   r@   r%   r   r   r    rB   rB   rC   rM   Õ   s(   








$ìzClsModel.exportÚ	model_dirÚ	dict_pathc           	      K   sä   t |ƒ}t |ƒ}|durt d¡ tj |d¡}| j ¡ }| |¡ | 	|¡ | 
|¡ | |¡ |dur>t |ƒ}| |¡ d|v rJ| | d¡¡ |  ¡ }| |¡ |  |¡ | j |g |¡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.
            dict_path (str, optional): the label dict file path. Defaults to None.

        Returns:
            CompletedProcess: the result of inferring subprocess execution.
        Nz`save_dir` will not be used.zinference.ymlZenable_mkldnn)r   r   Úwarningr5   ÚpathÚjoinr%   r&   ÚloadZ_update_inference_model_dirZ_update_infer_imgZ_update_infer_deviceZupdate_label_dict_pathZ_update_enable_mkldnnr/   r$   r:   r;   r<   Úinfer)	r>   rN   rH   r   r   rO   r?   r@   r%   rB   rB   rC   rT   ù   s(   









$ûzClsModel.inferc              
   K   s   t |ƒ}|  ¡ ~}	| j ¡ }
|
 d¡ |
 |¡ |
 |¡ |
 | jd ¡ |
 	|¡ |dur3|
 
|¡ |dur<|
 |¡ |durE|
 |¡ |durNt |ƒ}nt |
 ¡ ƒ}|
 |¡ |
 |	¡ g }| tddtj |d¡› ƒ¡ |  |¡ | j |	g |||¡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.
        NZauto_compression_config_pathz-ozGlobal.save_inference_dir=rM   )r   r$   r%   r&   r2   r'   r(   Z_update_slim_configZ
model_inforE   r)   r*   r+   r,   r-   r:   r0   r   r5   rQ   rR   r;   r<   Úcompression)r>   rD   r   r   r   r   r   r   r?   r@   r%   Zexport_cli_argsrB   rB   rC   rU   %  s>   











þÿ

ÿ$ßzClsModel.compression)NNNNr
   NFr   NTN)NNr
   r   N)Nr
   N)r
   NN)NNNr
   TN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚintÚfloatr4   Úboolr   r=   rF   rJ   rM   rT   rU   rB   rB   rB   rC   r	      sä    ôþýüûúùø	÷
öõô
òjùþýüûúù	
÷/úþýüûú
ø((úþýüûú
ø/øþýüûúùø
ör	   )r5   Úutilsr   Z
utils.miscr   Úbaser   Zbase.utils.argr   Zbase.utils.subprocessr   r	   rB   rB   rB   rC   Ú<module>   s   