o
    ॵiQ                     @   s   d dl Z d dlmZ 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mZmZmZmZmZmZmZ d dlmZ d d	lmZ e	jejd
G dd deZdS )    N)Mapping)distributed)Trainers)TRAINERS)EpochBasedTrainer)DEFAULT_MODEL_REVISIONConfigFields
ConfigKeysHubsModeKeys	ModelFileTasksTrainerStages)	to_device)func_receive_dict_inputs)module_namec                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	OCRRecognitionTrainerc                    s   t  j|i |}|S N)superevaluate)selfargskwargsmetric_values	__class__ b/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/trainers/cv/ocr_recognition_trainer.pyr      s   zOCRRecognitionTrainer.evaluatec                 C   s   d S r   r   )r   modelinputsr   r   r   prediction_step   s   z%OCRRecognitionTrainer.prediction_stepc           	         s   |   tj| _||}t|tstdd|vrldg}tg }|D ] |	 fdd|
 D  q#i }|D ],}||d}|durdt r[t r[|j }t|t  |	|| i q8| j	| n| j	|d  || _dS )aZ   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lossc                    s   g | ]} |v r|qS r   r   ).0keykey_pr   r   
<listcomp>;   s    z4OCRRecognitionTrainer.train_step.<locals>.<listcomp>N)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_keysr!   r$   valuer   r%   r   
train_step   s0   



z OCRRecognitionTrainer.train_stepc                 C   s*   | j r| jjn| j}|  ||}|S )zsPerform a evaluation step on a batch of inputs.

        Subclass and override to inject custom behavior.

        )_distr   moduleevalr+   )r   r6   r   resultr   r   r   evaluation_stepK   s   
z%OCRRecognitionTrainer.evaluation_step)__name__
__module____qualname__r   r    rA   rF   __classcell__r   r   r   r   r      s
    /r   )timecollections.abcr   torchr   r3   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   r   <module>   s   (