
    9iQS                     B   S SK r S SKrS SKrS SKrS SKrS SKrSSKJrJr  SSK	J
r
  SSKJr  SSKJrJr  SSKJr  SS	KJr  SS
KJrJrJr  SS/rSS jr\r " S S\ R8                  S9r " S S\5      r " S S\ 5      r! " S S\!5      r" " S S\!5      r# " S S\!5      r$g)    N   )flagslogging)get_cache_dir)parse_device)UnsupportedAPIErrorUnsupportedParamError)CachedProperty   )Config)build_model_from_model_infobuild_runner_from_model_infoget_registered_model_infoPaddleModel	BaseModelc                    U c  Uc  [        S5      eU b  Ub  XR                  :w  a  [        S5      eOU c  Ub  UR                  n  [        U 5      n[        X!S9$ ! [         a  n[	        [        U 5       S35      UeSnAff = f)_create_modelNz;At least one of `model_name` and `config` must be not None.zgIf both `model_name` and `config` are not None, `model_name` should be the same as `config.model_name`.z  is not a registered model name.)
model_infoconfig)
ValueError
model_namer   KeyErrorr	   reprr   )r   r   r   es       \/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/paddlex/repo_apis/base/model.pyr   r   &   s    fnVWW		F$6***  +
 
	 2&&
.z:

 '*LL	  #J  @A
	s   A 
B$A<<Bc                     ^  \ rS rSrSrSrSrSU 4S jjr\R                             SS j5       r
\R                       SS j5       r\R                  SS j5       r\R                  S	 5       r\R                  SS
 j5       r\R                        SS j5       r\R"                  S 5       r\R"                  S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rS rSrU =r $ )r   >   z
Abstract base class of Model.

Model defines how Config and Runner interact with each other. In addition,
Model provides users with multiple APIs to perform model training,
prediction, etc.
)trainevaluatepredictexportinfercompressionzsupported_{api_name}_optsc                    > [         TU ]  5         Xl        [        U5      U l        [        U R                  5      U l        Uc!  [        R                  " S5        [        U5      nX l
        U R                  5         g)z
Initialize the instance.

Args:
    model_name (str): A registered model name.
    config (base.config.BaseConfig|None): Config. Default: None.
Na  We strongly discourage leaving `config` unset or setting it to None. Please note that when `config` is None, default settings will be used for every unspecified configuration item, which may lead to unexpected result. Please make sure that this is what you intend to do.)super__init__namer   r   r   runnerr   warningr   r   _patch_apis)selfr   r   	__class__s      r   r&   BaseModel.__init__J   sg     		3J? 34??C>OOl J'F    c                     [         e)a&  
Train a model.

Args:
    batch_size (int|None): Number of samples in each mini-batch. If
        multiple devices are used, this is the batch size on each device.
        If None, use a default setting. Default: None.
    learning_rate (float|None): Learning rate of model training. If
        None, use a default setting. Default: None.
    epochs_iters (int|None): Total epochs or iterations of model
        training. If None, use a default setting. Default: None.
    ips (str|None): If not None, enable multi-machine training mode.
        `ips` specifies Paddle cluster node ips, e.g.,
        '192.168.0.16,192.168.0.17'. Default: None.
    device (str): A string that describes the device(s) to use, e.g.,
        'cpu', 'gpu', 'gpu:1,2'. Default: 'gpu'.
    resume_path (str|None): If not None, resume training from the model
        snapshot corresponding to the weight file `resume_path`. If
        None, use a default setting. Default: None.
    dy2st (bool): Whether to enable dynamic-to-static training.
        Default: False.
    amp (str): Optimization level to use in AMP training. Choices are
        ['O1', 'O2', 'OFF']. Default: 'OFF'.
    num_workers (int|None): Number of subprocesses to use for data
        loading. If None, use a default setting. Default: None.
    use_vdl (bool): Whether to enable VisualDL during training.
        Default: True.
    save_dir (str|None): Directory to store model snapshots and logs. If
        None, use a default setting. Default: None.

Returns:
    paddlex.repo_apis.base.utils.subprocess.CompletedProcess
NotImplementedError)r+   
batch_sizelearning_rateepochs_itersipsdeviceresume_pathdy2stampnum_workersuse_vdlsave_dirkwargss                r   r   BaseModel.traine   s    b "!r.   c                     [         e)a  
Evaluate a model.

Args:
    weight_path (str): Path of the weights to initialize the model.
    batch_size (int|None): Number of samples in each mini-batch. If
        multiple devices are used, this is the batch size on each device.
        If None, use a default setting. Default: None.
    ips (str|None): If not None, enable multi-machine evaluation mode.
        `ips` specifies Paddle cluster node ips, e.g.,
        '192.168.0.16,192.168.0.17'. Default: None.
    device (str): A string that describes the device(s) to use, e.g.,
        'cpu', 'gpu', 'gpu:1,2'. Default: 'gpu'.
    amp (str): Optimization level to use in AMP training. Choices are
        ['O1', 'O2', 'OFF']. Default: 'OFF'.
    num_workers (int|None): Number of subprocesses to use for data
        loading. If None, use a default setting. Default: None.

Returns:
    paddlex.repo_apis.base.utils.subprocess.CompletedProcess
r0   )r+   weight_pathr2   r5   r6   r9   r:   r=   s           r   r   BaseModel.evaluate   s    @ "!r.   c                     [         e)a  
Make prediction with a pre-trained model.

Args:
    weight_path (str): Path of the weights to initialize the model.
    input_path (str): Path of the input file, e.g. an image.
    device (str): A string that describes the device to use, e.g.,
        'cpu', 'gpu'. Default: 'gpu'.
    save_dir (str|None): Directory to store prediction results. If None,
        use a default setting. Default: None.

Returns:
    paddlex.repo_apis.base.utils.subprocess.CompletedProcess
r0   )r+   r@   
input_pathr6   r<   r=   s         r   r    BaseModel.predict   
      "!r.   c                     [         e)z
Export a pre-trained model.

Args:
    weight_path (str): Path of the weights to initialize the model.
    save_dir (str): Directory to store the exported model.

Returns:
    paddlex.repo_apis.base.utils.subprocess.CompletedProcess
r0   )r+   r@   r<   r=   s       r   r!   BaseModel.export   s
     "!r.   c                     [         e)a  
Make inference with an exported inference model.

Args:
    model_dir (str): Path of the exported inference model.
    input_path (str): Path of the input file, e.g. an image.
    device (str): A string that describes the device(s) to use, e.g.,
        'cpu', 'gpu'. Default: 'gpu'.
    save_dir (str|None): Directory to store inference results. If None,
        use a default setting. Default: None.

Returns:
    paddlex.repo_apis.base.utils.subprocess.CompletedProcess
r0   )r+   	model_dirrC   r6   r<   r=   s         r   r"   BaseModel.infer   rE   r.   c                     [         e)a  
Perform quantization aware training (QAT) and export the quantized
model.

Args:
    weight_path (str): Path of the weights to initialize the model.
    batch_size (int|None): Number of samples in each mini-batch. If
        multiple devices are used, this is the batch size on each
        device. If None, use a default setting. Default: None.
    learning_rate (float|None): Learning rate of QAT. If None, use a
        default setting. Default: None.
    epochs_iters (int|None): Total epochs of iterations of model
        training. If None, use a default setting. Default: None.
    device (str): A string that describes the device(s) to use, e.g.,
        'cpu', 'gpu'. Default: 'gpu'.
    use_vdl (bool): Whether to enable VisualDL during training.
        Default: True.
    save_dir (str|None): Directory to store the results. If None, use a
        default setting. Default: None.

Returns:
    tuple[paddlex.repo_apis.base.utils.subprocess.CompletedProcess]
r0   )	r+   r@   r2   r3   r4   r6   r;   r<   r=   s	            r   r#   BaseModel.compression   s    F "!r.   c              #   p  #    U R                   nU R                  S   nSR                  UR                  R	                  5       U/5      nUS-   n[
        R                  (       d^  [        R                  " [        5       S9 n[        R                  R                  XT5      n[        USSS9    S S S 5        Uv   S S S 5        g [        R                  R                  [        5       U5      n[        USSS9    S S S 5        Uv   g ! , (       d  f       N]= f! , (       d  f       g = f! , (       d  f       N5= f7f)Nr   _z.ymldirwutf-8encodingr,   r   join__name__lowerr   DEBUGtempfileTemporaryDirectoryr   ospathopen)r+   clsr   tagyaml_file_nametdr]   s          r   _create_new_config_file!BaseModel._create_new_config_file  s     nn__\2
hh**,j9:v{{,,ARww||B7$g6 7
	 BA 77<<@DdC'2 3J 76 BA 32N   A=D6?+D*D,D8<D64D%6D6
D	D
D"D6%
D3/D6c              #   p  #    U R                   nU R                  S   nSR                  UR                  R	                  5       U/5      nUS-   n[
        R                  (       d^  [        R                  " [        5       S9 n[        R                  R                  XT5      n[        USSS9    S S S 5        Uv   S S S 5        g [        R                  R                  [        5       U5      n[        USSS9    S S S 5        Uv   g ! , (       d  f       N]= f! , (       d  f       g = f! , (       d  f       N5= f7f)Nr   rN   z
_test.jsonrO   rQ   rR   rS   rU   )r+   r_   r   r`   json_file_namerb   r]   s          r   _create_new_val_json_file#BaseModel._create_new_val_json_file#  s     nn__\2
hh**,j9:|+{{,,ARww||B7$g6 7
	 BA 77<<@DdC'2 3J 76 BA 32re   c                 :    U R                   R                  SS5      $ )zsupported apissupported_apisNr   getr+   s    r   rk   BaseModel.supported_apis5  s     ""#3T::r.   c                 h    U R                   R                  U R                  R                  SS9S5      $ )zsupported train optsr   api_nameNr   rm   _API_SUPPORTED_OPTS_KEY_PATTERNformatrn   s    r   supported_train_optsBaseModel.supported_train_opts:  4     ""00777I4
 	
r.   c                 h    U R                   R                  U R                  R                  SS9S5      $ )zsupported evaluate optsr   rq   Nrs   rn   s    r   supported_evaluate_opts!BaseModel.supported_evaluate_optsA  s4     ""00777Ld
 	
r.   c                 h    U R                   R                  U R                  R                  SS9S5      $ )zsupported predcit optsr    rq   Nrs   rn   s    r   supported_predict_opts BaseModel.supported_predict_optsH  s4     ""00777KT
 	
r.   c                 h    U R                   R                  U R                  R                  SS9S5      $ )zsupported infer optsr"   rq   Nrs   rn   s    r   supported_infer_optsBaseModel.supported_infer_optsO  rx   r.   c                 h    U R                   R                  U R                  R                  SS9S5      $ )zsupported copression optsr#   rq   Nrs   rn   s    r   supported_compression_opts$BaseModel.supported_compression_optsV  s5     ""00777OQU
 	
r.   c                 :    U R                   R                  SS5      $ )zsupported dataset typessupported_dataset_typesNrl   rn   s    r   r   !BaseModel.supported_dataset_types]  s     ""#<dCCr.   c                 X    [        U 5      S:  a  [        R                  " SU  S35        g g )Nr   z'Unconsumed keyword arguments detected: .)lenr   r)   )r=   s    r   _assert_empty_kwargsBaseModel._assert_empty_kwargsb  s(    v;?OOEfXQOP r.   c                 \  ^  U 4S jnU 4S jnT R                   nUb  [        T R                   5      nO[        T R                  5      nT R                   HU  n[        T U5      nXT;  a  U" U5      n[	        T XW5        M*  [
        R                  (       d  MA  U" U5      n[	        T XW5        MW     g )Nc                 J   >^  [         R                  " T 5      U U4S j5       nU$ )Nc                  V   > TR                   nTR                  n[        U SU S35      e)Nz does not support `z`.)r'   rW   r   )argsr=   r   rr   
bnd_methodr+   s       r   _unavailable_apiJBaseModel._patch_apis.<locals>._make_unavailable.<locals>._unavailable_apim  s5    !YY
%..)!l"5hZrB r.   )	functoolswraps)r   r   r+   s   ` r   _make_unavailable0BaseModel._patch_apis.<locals>._make_unavailablel  s&    __Z( ) $#r.   c                 t  >^ ^ [         R                  " T 5      U U4S j5       nT R                  n/ mUS:X  ay  TR                  nUbh  SU;   a  TR	                  [        US   SS95        SU;   a  TR	                  [        US   5      5        SU;   a  TR	                  [        US   5      5        U$ US:X  aV  TR                  nUbE  SU;   a  TR	                  [        US   SS95        SU;   a  TR	                  [        US   5      5        U$ US	:X  a3  TR                  nUb"  SU;   a  TR	                  [        US   S
S95        U$ US:X  a3  TR                  nUb"  SU;   a  TR	                  [        US   S
S95        U$ US:X  a3  TR                  nUb"  SU;   a  TR	                  [        US   SS95        U$ T $ )Nc                    > [         R                  R                  T5      nUR                  " U 0 UD6nUR                  nUR
                  R                  5        HH  nUR                  U;  d  M  UR                  UR                  Ld  M0  UR                  XER                  '   MJ     T H  nUR                  U5        M     T" U 0 UD6$ N)inspect	Signaturefrom_callablebind	arguments
parametersvaluesr'   defaultemptycheck)	r   r=   sigbnd_args	args_dictpr   r   checkss	          r   _api_with_prechecksJBaseModel._patch_apis.<locals>._add_prechecks.<locals>._api_with_prechecksx  s    ''55jA88T4V4$..	..0AvvY.199AGG3K,-II	&&) 1
 $EKK	* $ "42622r.   r   r6   Tcheck_mcr8   r9   r   r    Fr"   r#   )r   r   rW   rv   append_CheckDevice_CheckDy2St	_CheckAMPrz   r}   r   r   )r   r   rr   optsr   r+   s   `   @r   _add_prechecks-BaseModel._patch_apis.<locals>._add_prechecksw  s   __Z(3 )3  "**HF7"00#4'l4>D&QR$k$w-&@A}iU&<=4 '&3 Z'33#4'l4>D&QR}iU&<=& '&% Y&22#4'l4>E&RS '& W$00#4'l4>E&RS '& ]*66#4'l4>D&QR '& "!r.   )rk   set_API_FULL_LISTgetattrsetattrr   
CHECK_OPTS)r+   r   r   rk   avail_api_setrr   apidecorated_apis   `       r   r*   BaseModel._patch_apisk  s    		$7	'r ,,% 3 34M 3 34M++H$)C, !2# 6h6### %33$7MD(: ,r.   )r   r   r'   r(   r   )NNNNgpuNFOFFNTN)NNr   r   N)r   N)NNNr   TN)!rW   
__module____qualname____firstlineno____doc__r   rt   r&   abcabstractmethodr   r   r    r!   r"   r#   
contextlibcontextmanagerrc   rh   cached_propertyrk   rv   rz   r}   r   r   r   staticmethodr   r*   __static_attributes____classcell__r,   s   @r   r   r   >   s    XN&A#6 	 0" 0"d 	 " "B 	" "" 	" " 	" "" 	 "" ""H  "  " ; ; 
 
 
 
 
 
 
 
 
 
 D D Q QX; X;r.   )	metaclassc                   (    \ rS rSrS rSrS rS rSrg)_CheckFailedi  Tc                 (    Xl         X l        X0l        g r   arg_namearg_val
legal_vals)r+   r   r   r   s       r   r&   _CheckFailed.__init__  s     $r.   c                 T    SU R                    SU R                   SU R                   3$ )N`z*` is expected to be one of or conforms to z
, but got )r   r   r   rn   s    r   __str___CheckFailed.__str__  s.    4==/!KDOOK\\fgkgsgsftuur.   r   N)	rW   r   r   r   r   check_failed_errorr&   r   r    r.   r   r   r     s     %
vr.   r   c                   2   ^  \ rS rSrS rU 4S jrS rSrU =r$ )_APICallArgsCheckeri  c                 .   > [         TU ]  5         Xl        g r   )r%   r&   r   )r+   r   r,   s     r   r&   _APICallArgsChecker.__init__  s    $r.   c                     [         e)r   r0   )r+   r   s     r   r   _APICallArgsChecker.check  s    !!r.   )r   	rW   r   r   r   r   r&   r   r   r   r   s   @r   r   r     s    %" "r.   r   c                   6   ^  \ rS rSrS rSU 4S jjrS rSrU =r$ )r   i  c                 0   > [         TU ]  U5        X l        g r   )r%   r&   r   )r+   r   r   r,   s      r   r&   _CheckDevice.__init__  s    $ r.   c                    SU;   d   eUS   nUGb>  [        U5      u  p4U R                  (       d&  X0R                  ;  a  [        SX R                  5      egUS:w  a&  X0R                  ;  a  [        SX R                  5      egU S3nU S3nU S3n[	        U5      S::  aF  XPR                  ;  a6  X`R                  ;  a&  XpR                  ;  a  [        SX R                  5      egggSU;   d   eUS   b&  XpR                  ;  a  [        SX R                  5      egX`R                  ;  a&  XpR                  ;  a  [        SX R                  5      eggg)	r   r6   Nr   _n1c1_n1cx_nxcxr   r5   )r   r   r   r   r   )r+   r   r6   device_typedev_ids	n1c1_desc	n1cx_desc	nxcx_descs           r   r   _CheckDevice.check  s`   4h#/#7 K==oo5&xII 6 %'"//9*8V__MM : $/-u 5I#.-u 5I#.-u 5I7|q(%__< ) @ ) @".x"QQ !A !A =  %},};2(?&28V__&U U  @
 !* @$-__$D&28V__&U U %E !A r.   r   )Fr   r   s   @r   r   r     s    !* *r.   r   c                       \ rS rSrS rS rSrg)r   i  c                 .   SU;   d   eUS   n[        U R                  [        5      (       a4  [        U R                  5      S:X  d   e[	        U R                  S   5      nO[	        U R                  5      nUb  U(       a  U(       d  [        SX#/5      eggg)r   r8   r   r   N)
isinstancer   listr   boolr   )r+   r   r8   support_dy2sts       r   r   _CheckDy2St.check  s    $Wdoot,,t'1,,, !34M 1M]"7E?CC +u r.   r   NrW   r   r   r   r   r   r   r   r.   r   r   r     s
    r.   r   c                       \ rS rSrS rS rSrg)r   i(  c                 ~    SU;   d   eUS   nUb-  US:w  a&  X R                   ;  a  [        SX R                   5      eggg)r   r9   Nr   )r   r   )r+   r   r9   s      r   r   _CheckAMP.check+  sI    }}5k?e|?? :"5#?? !;| r.   r   Nr   r   r.   r   r   r   (  s
    r.   r   )NN)%r   r   r   r   r\   rZ   utilsr   r   utils.cacher   utils.devicer   utils.errorsr   r	   
utils.miscr
   r   r   r   registerr   r   r   __all__r   r   ABCMetar   	Exceptionr   objectr   r   r   r   r   r.   r   <module>r     s         	  # ( ( F ;   +
&M* E;#++ E;Pv9 v	"& 	"1& 1h% &# r.   