
    9iQ                         S SK r S SKJr  S SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJrJrJrJrJrJrJrJr  S SKJr  S S	KJr  \	R2                  " \R4                  S
9 " S S\5      5       rg)    N)Mapping)distributed)Trainers)TRAINERS)EpochBasedTrainer)DEFAULT_MODEL_REVISIONConfigFields
ConfigKeysHubsModeKeys	ModelFileTasksTrainerStages)	to_device)func_receive_dict_inputs)module_namec                   :   ^  \ rS rSrU 4S jrS rS rS rSrU =r	$ )OCRRecognitionTrainer   c                 (   > [         TU ]  " U0 UD6nU$ N)superevaluate)selfargskwargsmetric_values	__class__s       n/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/trainers/cv/ocr_recognition_trainer.pyr   OCRRecognitionTrainer.evaluate   s    ($9&9    c                     g r    )r   modelinputss      r   prediction_step%OCRRecognitionTrainer.prediction_step   s    r!   c                 t   UR                  5         [        R                  U l        UR	                  U5      n[        U[        5      (       d  [        S5      eSU;  Ga2  S/n[        / 5      nU H:  nUR                  UR                  5        Vs/ s H  ovU;   d  M
  UPM     sn5        M<     0 nU H  nUR                  US5      n	U	c  M  [        R                  " 5       (       al  [        R                  " 5       (       aR  U	R                  R!                  5       n	[        R"                  " U	R%                  [        R&                  " 5       5      5        UR                  XyR)                  5       05        M     U R*                  R                  U5        OU R*                  R                  US   5        X0l        gs  snf )a	  Perform a training step on a batch of inputs.

Subclass and override to inject custom behavior.

Args:
    model (`TorchModel`): The model to train.
    inputs (`Dict[str, Union[torch.Tensor, Any]]`):
        The inputs and targets of the model.

        The dictionary will be unpacked before being fed to the model. Most models expect the targets under the
        argument `labels`. Check your model's documentation for all accepted arguments.

Return:
    `torch.Tensor`: The tensor with training loss on this batch.
z$"model.forward()" must return a dictlog_varslossN)trainr   TRAIN_modedo_step
isinstancedict	TypeErrorsetupdatekeysgetdistis_availableis_initializeddataclone
all_reducediv_get_world_sizeitem
log_buffertrain_outputs)
r   r$   r%   r@   default_keys_pattern
match_keyskey_pkeyr)   values
             r   
train_step OCRRecognitionTrainer.train_step   sV   $ 	^^
f--..BCC ]*$*8 RJ-!!$1$6$6$8I$8SSLS$8IK . H!%))#t4$((**t/B/B/D/D %

 0 0 2

43F3F3H(IJOOS**,$78 " OO""8,OO""=#<=* Js   	F5
F5
c                     U R                   (       a  U R                  R                  OU R                  nUR                  5         UR	                  U5      nU$ )zcPerform a evaluation step on a batch of inputs.

Subclass and override to inject custom behavior.

)_distr$   moduleevalr.   )r   r9   r$   results       r   evaluation_step%OCRRecognitionTrainer.evaluation_stepK   s;     &*ZZ

!!TZZ

t$r!   )r-   r@   )
__name__
__module____qualname____firstlineno__r   r&   rF   rM   __static_attributes____classcell__)r   s   @r   r   r      s    -+^	 	r!   r   )timecollections.abcr   torchr   r6   modelscope.metainfor   modelscope.trainers.builderr   modelscope.trainers.trainerr   modelscope.utils.constantr   r	   r
   r   r   r   r   r   modelscope.utils.data_utilsr   modelscope.utils.file_utilsr   register_moduleocr_recognitionr   r#   r!   r   <module>r`      sb     #  % ( 0 9= = = 2 @ 
h&>&>?A- A @Ar!   