o
    *j5                     @   s   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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                       sL   e Zd Z fddZ fddZ fddZdd Zd	d
 Zdd Z  Z	S )ImageInpaintingTrainerc                       t  j|i | d S N)super__init__selfargskwargs	__class__ p/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/trainers/cv/image_inpainting_trainer.pyr         zImageInpaintingTrainer.__init__c                    r   r   )r   trainr   r   r   r   r       r   zImageInpaintingTrainer.trainc                    s   t  j|i |}|S r   )r   evaluate)r   r   r   Zmetric_valuesr   r   r   r!      s   zImageInpaintingTrainer.evaluatec                 C   s   d S r   r   )r   modelinputsr   r   r   prediction_step   s   z&ImageInpaintingTrainer.prediction_stepc                 C   s   |  tj d| _| j  t| j| jD ][}|  tj t	|D ]A\}}t
|| j}|| _|| _tdD ]}|  tj | | j|| |  tj q5| `|  jd7  _tj| _|d | jkrb nq!|  tj |  jd7  _q|  tj dS )z; Training loop used by `EpochBasedTrainer.train()`
        r         N)Zinvoke_hookr   Z
before_run_epochr"   r    rangeZ_max_epochsZbefore_train_epoch	enumerater   Zdevice
data_batchZ_inner_iterZbefore_train_iter
train_stepZafter_train_iterZ_iterr   TRAIN_modeZiters_per_epochZafter_train_epochZ	after_run)r   Zdata_loader_ir*   idxr   r   r   
train_loop!   s,   
z!ImageInpaintingTrainer.train_loopc           
         s(  |   tj| _t|tr t|js |jj	d	i |d|i}n|jj	||d}t|t
s1tdd|vrdg}tg }|D ] | fdd| D  q>i }|D ],}||d}	|	durt rvt rv|	j }	t|	t  |||	 i qS| 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.
        optimizer_idx)r2   z$"model.forward()" must return a dictlog_varsZlossc                    s   g | ]} |v r|qS r   r   ).0keyZkey_pr   r   
<listcomp>a   s    z5ImageInpaintingTrainer.train_step.<locals>.<listcomp>Nr   )r    r   r,   r-   
isinstancer   r   forwardr"   Z_do_stepdict	TypeErrorsetupdatekeysgetdistZis_availableZis_initializeddatacloneZ
all_reduceZdiv_Zget_world_sizeitemZ
log_buffertrain_outputs)
r   r"   r#   r0   rD   Zdefault_keys_patternZ
match_keysr3   r5   valuer   r6   r   r+   =   s<   


z!ImageInpaintingTrainer.train_step)
__name__
__module____qualname__r   r    r!   r$   r1   r+   __classcell__r   r   r   r   r      s    r   )timecollections.abcr   Ztorchr   r@   Zmodelscope.metainfor   Zmodelscope.trainers.builderr   Zmodelscope.trainers.trainerr   Zmodelscope.utils.constantr   r   r	   r
   r   r   r   r   Zmodelscope.utils.data_utilsr   Zmodelscope.utils.file_utilsr   Zregister_moduleZimage_inpaintingr   r   r   r   r   <module>   s   (