o
    *jQ                     @   s~   d dl Z d dlZd dlmZ d dl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 ejejdG dd deZdS )	    N)CallableDictOptional)Trainers)BaseTrainer)TRAINERS)module_namec                       sT   e Zd Z	ddedee f fddZdd Z	dded	eee	f fd
dZ
  ZS )FaceDetectionScrfdTrainerNcfg_filecfg_modify_fnc           (         s  ddl }ddlm}m} ddlm} ddlm}	m}
 ddl	m
} ddlm} ddlm} dd	lm} dd
lm} ddlm} ddlm} ddlm} ddlm} ddlm} ddlm} ddlm} t | | j }d|v ru|d |_!nt"#dt"$t"%|d |_!|&t"'|j! d|v r|d |_(d|_)d|v r|d |_*nt+d|_*|,dd}|,dd}d|v r|d | |j-j._/|d | |j-j._0d|v r|d | |j-j1_/|d | |j-j1_0d|v r|d |_2|dur||}d|v rd }||d fi |j3 | \}}t+||_*nd!}|4d"d!|_5t67d#t68 }t"#|j!| d$}|
||j9d%}t: } |	 }!d&#d'd( |!; D }"d)}#|<d*|# |" d& |#  |"| d+< |j=| d,< |<d-|  |<d.|j=  d/|v r|d/ |_>|4d0d!}$|<d1|d/  d2|$  ||d/ |$d3 nd|_>|j>| d/< t"%|| d4< ||j?}%|%@  ||j-j.g}&tA|jBd5krtCD|j-j1}'|j-j.jE|'_E|&F||' |jGdurt:|| dd6  |&d jHd7|jG_I|&d jH|%_H|| _ |&| _J|%| _?|| _K|| _L| | _I|| _MdS )8z High-level finetune api for SCRFD.

        Args:
            cfg_file: Path to configuration file.
            cfg_modify_fn: An input fn which is used to modify the cfg read out of the file.
        r   N)get_dist_info	init_dist)get_git_hash)collect_envget_root_logger)set_random_seed)build_detector)build_dataset)__version__)RetinaFaceDataset)DefaultFormatBundleV2)LoadAnnotationsV2)RotateV2)RandomSquareCrop)	ResNetV1e)	SCRFDHead)SCRFDwork_dirz./work_dirsresume_fromcudagpu_ids   labelfile_namezlabelv2.txtimgdir_namezimages/Z
train_rootZval_roottotal_epochslauncherTFno_validatez%Y%m%d_%H%M%Sz.log)log_file	log_level
c                 S   s   g | ]\}}| d | qS )z:  ).0kvr*   r*   t/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/trainers/cv/face_detection_scrfd_trainer.py
<listcomp>[   s    z6FaceDetectionScrfdTrainer.__init__.<locals>.<listcomp>z=------------------------------------------------------------
zEnvironment info:
env_infoconfigzDistributed training: zConfig:
seeddeterministiczSet random seed to z, deterministic: )r3   Zexp_name      )Zmmdet_versionCLASSES)NmmcvZmmcv.runnerr   r   Z
mmcv.utilsr   Zmmdet.utilsr   r   
mmdet.apisr   Zmmdet.modelsr   Zmmdet.datasetsr   Zmmdetr   Z>modelscope.models.cv.face_detection.scrfd.mmdet_patch.datasetsr   ZHmodelscope.models.cv.face_detection.scrfd.mmdet_patch.datasets.pipelinesr   r   r   r   ZFmodelscope.models.cv.face_detection.scrfd.mmdet_patch.models.backbonesr   ZHmodelscope.models.cv.face_detection.scrfd.mmdet_patch.models.dense_headsr   ZFmodelscope.models.cv.face_detection.scrfd.mmdet_patch.models.detectorsr   super__init__cfgr   ospjoinsplitextbasenameZmkdir_or_existabspathr   Zdevicer    rangepopdatatrainZann_fileZ
img_prefixvalr$   Zdist_paramsgetr&   timestrftime	localtimer(   dictitemsinfopretty_textr2   modelZinit_weightslenZworkflowcopydeepcopyZpipelineappendZcheckpoint_configr6   metadatasetsdistributed	timestamplogger)(selfr
   r   argskwargsr7   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   r"   r#   rU   _Z
world_sizerV   r'   rW   rS   Zenv_info_dictr0   Z	dash_lineZ_deterministicrN   rT   Zval_dataset	__class__r*   r.   r:      s   











z"FaceDetectionScrfdTrainer.__init__c              	   O   s8   ddl m} || j| j| j| j| jj | j| jd d S )Nr   )train_detector)rU   validaterV   rS   )	r8   r^   rN   rT   r;   rU   r&   rV   rS   )rX   rY   rZ   r^   r*   r*   r.   rD      s   
zFaceDetectionScrfdTrainer.traincheckpoint_pathreturnc                 O   s,   | j j}td|  td|  d S )Nz	eval cfg zcheckpoint_path )r;   Z
evaluationrW   rL   )rX   r`   rY   rZ   r;   r*   r*   r.   evaluate   s   z"FaceDetectionScrfdTrainer.evaluate)N)__name__
__module____qualname__strr   r   r:   rD   r   floatrb   __classcell__r*   r*   r\   r.   r	      s    y
r	   )rP   osZos.pathpathr<   rG   typingr   r   r   Zmodelscope.metainfor   Zmodelscope.trainers.baser   Zmodelscope.trainers.builderr   Zregister_moduleZface_detection_scrfdr	   r*   r*   r*   r.   <module>   s   