o  i{<@sVddlmZddlZddlmZmZmZmZddlm Z m Z ddl Z ddl m ZddlmZddlmZddlmZmZmZddlmZdd lmZmZmZmZmZd d lm Z ed ej!ed \Z"Z#ed ej!ed\Z$Z#e r}ddl%m&Z&m'Z'ddl(m)Z)n$ed ej!eddd\Z&Z#edej!eddd\Z)Z#ed ej!eddd\Z'Z#Gddde&Z*dS)) annotationsN)CallableIterableSequenceSized) TYPE_CHECKINGAny) DataLoader)DistributedSampler)IterationEventsdefault_metric_cmp_fndefault_prepare_batch) Decollated) IgniteInfo ensure_tuple is_scalar min_versionoptional_import)engine_apply_transformz ignite.engineStateEvents)Engine EventEnum)Metricr decorator)as_typezignite.metricsrrcseZdZdZddeddddedddddddfd>fd%d& Zd'd(Zd?d*d+Zd@dAd/d0Z dBd2d3Z dCfd4d5 Z dDd:d;Z dr=X/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/engines/workflow.pyset_sampler_epochsz,Workflow.__init__..set_sampler_epochr)rankseed iterationrBr r$outputbatchmetricsZmetric_details dataloaderrkey_metric_name best_metricbest_metric_epochz6`event_names` must be a list of strings or EventEnums.)r6r6r>rr;r<)(super__init__ _iteration isinstancer getattrr onrZ EPOCH_STARTEDrlen TypeErrorrdist is_availableis_initializedget_ranktorchrrAr"r&r(r0r3r9r:scalerr list ValueErrorstrrZregister_events issubclass_register_decollate_register_postprocessing_register_metrics_register_handlers)selfrr r"r$r&r(r)r+r-r/r0r1r3r4r6r8r9r:rFname __class__rCrErTgsr            zWorkflow.__init__cCs|tjddd}dS) zv Register the decollate operation for batch data, will execute after model forward and loss forward. r>rr;r<cSsXtddd}t|jjttfr||jj|j_t|jjttfr*||jj|j_dSdS)NT)keysdetach)rrVrArLradictrK)r> transformrDrDrE_decollate_datas z5Workflow._register_decollate.._decollate_dataNrRrXr MODEL_COMPLETED)rirqrDrDrEres zWorkflow._register_decollate posttranscs|tjdfdd }dS) zz Register the postprocessing logic to the engine, will execute them as a chain when iteration completed. r>rr;r<cst|jjtrt|jjts!t|jj|jjd\|j_|j_dStt|jj|jjD]\}\}}t||\|jj|<|jj|<q,dS)N)rLrKrp)rVrArLrarKr enumeratezip)r>ibortrDrE_run_postprocessings""z>Workflow._register_postprocessing.._run_postprocessingNrRrr)rirtr{rDrzrErfs z!Workflow._register_postprocessingk_metricro add_metricscst|tstdt|jdt|dj_t|}|dur=t |dkr=t|ts8tdt|jd| || D] \}}| |qA tjd fd d }dS) zi Register the key metric and additional metrics to the engine, supports ignite Metrics. z+`key_metric` must be None or a dict but is .rNz1Additional metrics must be None or a dict but is r>rr;r<cs|jj}|dur?|jj|}t|stddS|jjdks'||jjrAj d|d|||j_|jj |j_dSdSdS)NzKey metric is not a scalar value, skip the metric comparison with the current best metric.Please set other metrics as the key metric, or change the `reduction` mode to 'mean'.rGzGot new best metric of z: ) rArOrMrwarningswarnrQr0rPloggerinforB)r>rOZcurrent_val_metricrirDrE_compare_metricss   z4Workflow._register_metrics.._compare_metrics)r>rr;r<)rVrorZtype__name__rarmrArOrYupdateitemsattachrXrZEPOCH_COMPLETED)rir|r}rMrjmetricrrDrrErgs    zWorkflow._register_metricsrcCs t|}|D]}||qdS)zc Register the handlers to the engine, supports ignite Handlers with `attach` API. N)rr)rir1Z handlers_handlerrDrDrErhs zWorkflow._register_handlerscs4|jjdkr tddStj|j|jjddS)zT Execute training, validation or evaluation based on Ignite Engine. rz`dataloader` is empty or the specified `epoch_length` is 0, skip the `run`. If running distributed training, the program may hang in `all-gather`, `all-reduce`, etc. because not all the ranks run the same computation logic.N)datar )rAr$rrrSrunr"r rrkrDrErs z Workflow.runr>r batchdatadict[str, torch.Tensor]cCstd|jjd)a Abstract callback function for the processing logic of 1 iteration in Ignite Engine. Need subclass to implement different logics, like SupervisedTrainer/Evaluator, GANTrainer, etc. Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. batchdata: input data for this iteration, usually can be dictionary or tuple of Tensor data. Raises: NotImplementedError: When the subclass does not override this method. z Subclass z must implement this method.)NotImplementedErrorrlr)rir>rrDrDrErUs zWorkflow._iterationcsfdd|DS)a! Get the statistics information of the workflow process. Args: vars: variables name in the `self.state`, will use the variable name as the key and the state content as the value. if the variable doesn't exist, default value is `None`. csi|] }|tj|dqSr?)rWrA).0krrDrE 4sz&Workflow.get_stats..rD)rivarsrDrrE get_stats+s zWorkflow.get_stats)&rrr r!r"r#r$r%r&r'r(rr)r*r+r,r-r.r/r.r0rr1r2r3r'r4r5r6r7r8r'r9r7r:r7r;r<)rtrr;r<r?)r|ror}r7r;r<)r1rr;r<)r;r<)r>rrrr;ro)r __module__ __qualname____doc__r r rTrerfrgrhrrUr __classcell__rDrDrkrEr/s2<]   # r)+ __future__rrcollections.abcrrrrtypingrrr_torch.distributed distributedr[torch.utils.datar torch.utils.data.distributedr monai.engines.utilsr r r monai.transformsr monai.utilsrrrrrutilsrOPT_IMPORT_VERSIONr_r ignite.enginerrignite.metricsrrrDrDrDrEs6