o
    *ÎjNB  ã                   @   s<  d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZ d dl	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 d dlmZ d dlmZmZ d dlmZmZ d dlm Z m!Z!m"Z" d d	l#m$Z$ d d
l%m&Z& d dl'm(Z(m)Z) d dl*m+Z+ d dl,m-Z-m.Z. e&j/ej0dG dd„ de$ƒƒZ1G dd„ dƒZ2dS )é    N)ÚCallableÚDictÚOptional)ÚEasyDict)Útqdm)ÚTrainers)ÚDBModelÚ
DBModel_v2)Úboxes_from_bitmapÚpolygons_from_bitmap)Ú
DataLoaderÚImageDatasetÚQuadMeasurer)ÚBaseTrainer)ÚTRAINERS)ÚDEFAULT_MODEL_REVISIONÚ	ModelFile)Ú
get_logger)Úget_rankÚsynchronize)Úmodule_namec                       sl   e Zd Zddddefdededededef
‡ fdd	„Zd
d„ Z	ddedeee	f fdd„Z
dd„ Z‡  ZS )ÚOCRDetectionDBTrainerNTÚmodelÚcfg_fileÚload_pretrainÚ
cache_pathÚmodel_revisionc           	         s„  |dur|   ||¡| _|du rtj | jtj¡| _n|dur"|dus&J dƒ‚|dur4|| _|dur4|| _tƒ  	| j¡ | j
}|rXd|v rK|d |j_ntj | j| j
jj¡|j_d| j
v rb|  |¡}d|v rl|d |j_d|v rv|d |j_d|v r€|d |j_d|v rŠ|d |j_d	|v r”|d	 |j_d
|v rž|d
 |j_d|v r¨|d |j_d|v r²|d |j_|jj| _t| jƒ| _|| _
dS )aN   High-level finetune api for dbnet.

        Args:
            model: Model id of modelscope models.
            cfg_file: Path to configuration file.
            load_pretrain: Whether load pretrain model for finetune.
                if False, means training from scratch.
            cache_path: cache path of model files.
        Nz;cfg_file and cache_path is needed, if model is not providedZpretrain_modelZ	frameworkÚgpu_idsÚ
batch_sizeÚ
max_epochsÚbase_lrÚtrain_data_dirÚval_data_dirÚtrain_data_listÚval_data_list)Zget_or_download_model_dirr   ÚosÚpathÚjoinr   ZCONFIGURATIONr   ÚsuperÚ__init__ÚcfgÚtrainÚfinetune_pathr   ÚweightsÚ_config_transformr   r   Útotal_epochsr    Údatasetr!   r"   r#   r$   ÚlenZ
world_size)	Úselfr   r   r   r   r   ÚargsÚkwargsr*   ©Ú	__class__© úp/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/trainers/cv/ocr_detection_db_trainer.pyr)   "   sZ   ÿ
ÿ€ÿ
ÿ



zOCRDetectionDBTrainer.__init__c                 C   s   t | jƒ}|jdd d S ©Nr   )Ú
local_rank)Ú	DBTrainerr*   r+   )r2   Ztrainerr7   r7   r8   r+   c   s   
zOCRDetectionDBTrainer.trainÚcheckpoint_pathÚreturnc                 O   s,   |d ur	|| j j_t| j ƒ}|jdd d S r9   )r*   Útestr<   r;   Úevaluate)r2   r<   r3   r4   Z	evaluaterr7   r7   r8   r?   g   s   

zOCRDetectionDBTrainer.evaluatec                 C   s¼   t i ƒ}|jj|_|jj|j_|j|_|j|_|j|_|j|_t	|jj
ƒ|jj_t	|jj
ƒ|jjj |jj_t	|jj
ƒ|jjj |jj_|jj|j_d|jj_d|jj_|jjj|jj_|S )Né   é   )Úeasydictr+   ÚmiscsZwork_dirÚ
output_dirr   r0   Z
evaluationr>   r1   r   Ú
dataloaderZnum_gpusZbatch_size_per_gpur   Zworkers_per_gpuZnum_workersr   r/   Ú	transformZ
collect_fn)r2   ÚconfigZ
new_configr7   r7   r8   r.   p   s.   
ÿ
ÿÿ
ÿ

z'OCRDetectionDBTrainer._config_transform©N)Ú__name__Ú
__module__Ú__qualname__r   ÚstrÚboolr)   r+   r   Úfloatr?   r.   Ú__classcell__r7   r7   r5   r8   r      s2    ûÿþýüûAÿÿ

ý	r   c                   @   s¨   e Zd Zdd„ Zdd„ Zdd„ Zd*dd	„Zd
d„ Zdd„ Zd+dd„Z	dd„ Z
d,dd„Zdd„ Zdd„ Zd,dd„Zd-dd„Zd d!„ Zd"d#„ Zd$d%„ Zd-d&d'„Zd(d)„ ZdS ).r;   c                 C   s¬   |   ¡  || _|jj| _|jj| _d| _d| _	t
|jjƒdkr#d| _nd| _tj |jj|jj¡| _tƒ dkr?tj| jdd ttj | jd¡ƒ| _| j d | j¡¡ d S )Nr   r@   TF©Úexist_okztrain_log.txtzcfg value:
{})Úinit_devicer*   rC   rD   Údir_pathr+   r    ÚlrÚ
current_lrÚtotalr1   r   Údistributedr%   r&   r'   Zexp_nameÚ	file_namer   Úmakedirsr   ÚloggerÚinfoÚformat)r2   r*   r7   r7   r8   r)   ‰   s   


zDBTrainer.__init__c                 C   s*   t j ¡ rt  d¡| _d S t  d¡| _d S )NÚcudaÚcpu)Útorchr]   Úis_availableÚdevice©r2   r7   r7   r8   rR   £   s   
zDBTrainer.init_devicec                 C   s   t | j| j|ƒ}|S rH   )r	   ra   rW   )r2   r:   r   r7   r7   r8   Ú
init_model©   s   zDBTrainer.init_modelNc                 C   s0   d}t  d|t| jjjd ƒ  |¡}|| j S )NçÍÌÌÌÌÌì?g      ð?r@   )ÚnpÚpowerrN   r*   r+   r/   rT   )r2   ÚepochÚstepÚfactorZrater7   r7   r8   Úget_learning_rate­   s
   ÿ
zDBTrainer.get_learning_ratec                 C   s*   |   ||¡}|jD ]}||d< q	|| _d S )NrT   )rj   Zparam_groupsrU   )r2   Ú	optimizerrg   rh   rT   Úgroupr7   r7   r8   Úupdate_learning_rate´   s   


zDBTrainer.update_learning_ratec                 C   s"   t j||dd}|j|dd d S )NT©Zmap_locationZweights_onlyF)Ústrict)r_   ÚloadÚload_state_dict)r2   r   Z
model_pathra   Ú
state_dictr7   r7   r8   Úrestore_model»   s   ÿzDBTrainer.restore_modelçyé&1¬|?rd   ç-Cëâ6?c           
      C   sÄ   g g g }}}| j  ¡ D ]9\}}t|dƒr#t|jtjƒr#| |j¡ t|tjƒs-d|v r4| |j	¡ qt|dƒrFt|j	tjƒrF| |j	¡ qt
jj|||dd}	|	 ||dœ¡ |	 d|i¡ |	S )NÚbiasZbnÚweightT)rT   ÚmomentumZnesterov)ÚparamsÚweight_decayry   )r   Znamed_modulesÚhasattrÚ
isinstancerv   ÚnnÚ	ParameterÚappendZBatchNorm2drw   r_   ZoptimZSGDZadd_param_group)
r2   rT   rx   rz   Zbn_groupZweight_groupZ
bias_groupÚkÚvrk   r7   r7   r8   Úcreate_optimizerÀ   s$   €ÿþzDBTrainer.create_optimizerc                 C   s<   || j jj dkr| j dt|ƒ ¡ |  |||¡ d S d S )Nr   zsave interval model for step )r*   rC   Zsave_intervalrZ   r[   rL   Ú
save_model)r2   r   rg   rh   r7   r7   r8   Úmaybe_save_modelÔ   s   þzDBTrainer.maybe_save_modelc                 C   sX   t |tƒr| ¡ D ]\}}|  |||¡}|  ||¡ q	d S |  d||¡}|  ||¡ d S )Nr   )r|   ÚdictÚitemsÚmake_checkpoint_nameÚsave_checkpoint)r2   r   rg   rh   ÚnameÚnetZcheckpoint_namer7   r7   r8   rƒ   Ù   s   
þzDBTrainer.save_modelc                 C   sL   t j| jdd t | ¡ t j | j|¡¡ | j 	dt j | j|¡ ¡ d S )NTrP   zsave_checkpoint to: )
r%   rY   rS   r_   Úsaverr   r&   r'   rZ   r[   )r2   rŠ   r‰   r7   r7   r8   rˆ   â   s
   
ÿzDBTrainer.save_checkpointc           
      C   s¼   t ƒ  | j¡}| ¡ }t| ¡ ƒ}tjtj	 
| j|¡| jdd}t| ¡ ƒ}d}tt|ƒƒD ]}	|||	  |v rH||||	    ¡  ¡ |||	 < q.| |¡ t | ¡ tj	 
| j|¡¡ d S )NTrn   zmodel.module.)r   Útora   rr   ÚlistÚkeysr_   rp   r%   r&   r'   rS   ÚsetÚranger1   r^   rN   rq   r‹   )
r2   Zfinetune_model_nameZinfer_model_nameZinfer_modelZmodel_state_dictZ
model_keysZ
saved_dictZ
saved_keysÚprefixÚir7   r7   r8   Úconvert_model_for_inferenceè   s&   ý
ÿ€

ÿz%DBTrainer.convert_model_for_inferencec                 C   s.   |d u s|d u r|d }|S d  |||¡}|S )Nz
_latest.ptz{}_epoch_{}_minibatch_{}.pt)r\   )r2   r‰   rg   rh   Zc_namer7   r7   r8   r‡   ý   s
   ÿzDBTrainer.make_checkpoint_nameFc                 C   sX   t ||jj|jjƒ}t||jjd|d}t ||jj|jjƒ}t||j	jd|d}||fS )NT)Zis_trainrW   F)
r   r0   r!   r#   r   r+   rE   r"   r$   r>   )r2   r*   rW   Ztrain_datasetZtrain_dataloaderZtest_datasetZtest_dataloaderr7   r7   r8   Úget_data_loader  s&   
ÿü
ÿüzDBTrainer.get_data_loaderc                 C   sF  |   |¡| _|  | j| j¡\| _| _d| _| jjj	d ur4| j
 d| jjj	› ¡ |  | j| jjj	| j¡ d}|  | j¡}| j
 d¡ | j ¡  	 | j
 dt|ƒ ¡ t| jƒ| _| jD ]&}|  ||| j¡ | j| j|||| jd |  | j|| j¡ |  jd7  _q[|d7 }|| jjjkr¢|  | jd¡ |  dd	¡ | j
 d
¡ d S qH)Nr   zfinetune from zStart Training...TzTraining epoch )rg   rh   r@   zfinal.ptzpytorch_model.ptzTraining done)rc   r   r”   r*   rW   Útrain_data_loaderÚvalidation_loadersZstepsr+   r,   rZ   r[   rs   ra   r‚   rT   rL   r1   rV   rm   Ú
train_stepr„   r/   rˆ   r“   )r2   r:   rg   rk   Úbatchr7   r7   r8   r+     sB   ÿÿ

ÿÿìzDBTrainer.trainc                 C   sR  |  ¡  |j|dd}t|ƒdkr|\}}i }	nt|ƒdkr#|\}}}	t|tƒrLg }
t d¡ ¡ }| ¡ D ]\}}|| 	¡ 7 }|
 
d || 	¡ ¡¡ q5n| 	¡ }| ¡  | ¡  || jjjj dkr¥t|tƒrd |
¡}
d g d	¢¡ |||
| j¡}| j |¡ n| j d
||| ¡ | jf ¡ |	 ¡ D ]\}}| j d|| 	¡ f ¡ q”d S d S )NT©Ztrainingé   é   g        zloss_{0}:{1:.4f}r   ú	)z
step:{:6d}zepoch:{:3d}z{}z	lr:{:.4f}z)step: %6d, epoch: %3d, loss: %.6f, lr: %fz%s: %6f)Z	zero_gradÚforwardr1   r|   r…   r_   Ztensorr]   r†   Úmeanr   r\   Zbackwardrh   r*   r+   rC   Zprint_interval_itersr'   rU   rZ   r[   Úitem)r2   r   rk   r˜   rg   rh   ÚresultsÚlÚpredÚmetricsÚlineZlossÚkeyZl_valZlog_infor‰   Úmetricr7   r7   r8   r—   B  sB   

þ

ÿþÿõ
zDBTrainer.train_stepc                 C   s>   t  d¡ t j ¡ rt  d¡| _t  d¡ d S t  d¡| _d S )Nztorch.FloatTensorr]   ztorch.cuda.FloatTensorr^   )r_   Zset_default_tensor_typer]   r`   ra   rb   r7   r7   r8   Úinit_torch_tensord  s
   

zDBTrainer.init_torch_tensorc                 C   s°   |d }t |tƒr|d }n|}|| jjjk}g }g }t| d¡ƒD ]1}	|d |	 \}
}|r<t||	 ||	 ||
ƒ\}}nt||	 ||	 ||
ƒ\}}| 	|¡ | 	|¡ q"||fS )a­  
        batch: (image, polygons, ignore_tags
        batch: a dict produced by dataloaders.
            image: tensor of shape (N, C, H, W).
            polygons: tensor of shape (N, K, 4, 2), the polygons of objective regions.
            ignore_tags: tensor of shape (N, K), indicates whether a region is ignorable or not.
            shape: the original shape of images.
            filename: the original filenames of images.
        pred:
            binary: text region segmentation map, with shape (N, 1, H, W)
            thresh: [if exists] thresh hold prediction with shape (N, 1, H, W)
            thresh_binary: [if exists] binarized with threshhold, (N, 1, H, W)
        ÚimageÚbinaryr   Úshape)
r|   r…   r*   r>   Zthreshr   Úsizer   r
   r   )r2   r˜   Z_predÚis_output_polygonZimagesr¢   ZsegmentationZboxes_batchZscores_batchZbatch_indexÚheightÚwidthZboxesZscoresr7   r7   r8   Ú	representm  s*   


þþ
zDBTrainer.representc                 C   s&  |   ¡  |  |¡}|  || jjj| j¡ |  | j| j¡\| _	| _
tƒ }| ¡  t ¡ X g }tt| j
ƒt| j
ƒdD ]&\}}|j|dd}|  ||| jjj¡}|j||| jjjdd}	| |	¡ q:| |¡}
|
 ¡ D ]\}}| j d||j|jf ¡ qjW d   ƒ n1 s†w   Y  | j d¡ d S )N)rV   Fr™   g333333Ó?)r¬   Z
box_threshz%s : %f (%d)zEvaluation done)r§   rc   rs   r*   r>   r<   ra   r”   rW   r•   r–   r   Úevalr_   Zno_gradr   Ú	enumerater1   r   r¯   Zreturn_polygonZvalidate_measurer   Zgather_measurer†   rZ   r[   ZavgÚcount)r2   r:   r   Zquad_measurerZraw_metricsr’   r˜   r¢   ÚoutputZ
raw_metricr£   r¥   r¦   r7   r7   r8   r?   ‘  sB   
ÿ
þÿü
ÿÿñzDBTrainer.evaluaterH   )rt   rd   ru   )NN)F)rI   rJ   rK   r)   rR   rc   rj   rm   rs   r‚   r„   rƒ   rˆ   r“   r‡   r”   r+   r—   r§   r¯   r?   r7   r7   r7   r8   r;   ‡   s&    


	

-"
	$r;   )3ÚcopyÚdatetimeÚmathr%   ÚtimeÚtypingr   r   r   Únumpyre   r_   Ztorch.distributedrW   ÚdistZtorch.multiprocessingÚmultiprocessingÚmpZtorch.nnr}   rB   r   r   Zmodelscope.metainfor   Z0modelscope.models.cv.ocr_detection.modules.dbnetr   r	   Z(modelscope.models.cv.ocr_detection.utilsr
   r   Z?modelscope.msdatasets.dataset_cls.custom_datasets.ocr_detectionr   r   r   Zmodelscope.trainers.baser   Zmodelscope.trainers.builderr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   Zmodelscope.utils.torch_utilsr   r   Zregister_moduleZocr_detection_dbr   r;   r7   r7   r7   r8   Ú<module>   s2   g