Engines¶
Multi-GPU data parallel¶
- monai.engines.multi_gpu_supervised_trainer.create_multigpu_supervised_evaluator(net, metrics=None, devices=None, non_blocking=False, prepare_batch=<function _prepare_batch>, output_transform=<function _default_eval_transform>, distributed=False)[source]¶
Derived from create_supervised_evaluator in Ignite.
Factory function for creating an evaluator for supervised models.
- Parameters
net (
Module
) – the model to train.metrics (
Optional
[Dict
[str
,Metric
]]) – a map of metric names to Metrics.devices (
Optional
[Sequence
[device
]]) – device(s) type specification (default: None). Applies to both model and batches. None is all devices used, empty list is CPU only.non_blocking (
bool
) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.prepare_batch (
Callable
) – function that receives batch, device, non_blocking and outputs tuple of tensors (batch_x, batch_y).output_transform (
Callable
) – function that receives ‘x’, ‘y’, ‘y_pred’ and returns value to be assigned to engine’s state.output after each iteration. Default is returning (y_pred, y,) which fits output expected by metrics. If you change it you should use output_transform in metrics.distributed (
bool
) – whether convert model to DistributedDataParallel, if True, devices must contain only 1 GPU or CPU for current distributed rank.
Note
engine.state.output for this engine is defined by output_transform parameter and is a tuple of (batch_pred, batch_y) by default.
- Returns
an evaluator engine with supervised inference function.
- Return type
Engine
- monai.engines.multi_gpu_supervised_trainer.create_multigpu_supervised_trainer(net, optimizer, loss_fn, devices=None, non_blocking=False, prepare_batch=<function _prepare_batch>, output_transform=<function _default_transform>, distributed=False)[source]¶
Derived from create_supervised_trainer in Ignite.
Factory function for creating a trainer for supervised models.
- Parameters
net (
Module
) – the network to train.optimizer (
Optimizer
) – the optimizer to use.loss_fn (
Callable
) – the loss function to use.devices (
Optional
[Sequence
[device
]]) – device(s) type specification (default: None). Applies to both model and batches. None is all devices used, empty list is CPU only.non_blocking (
bool
) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.prepare_batch (
Callable
) – function that receives batch, device, non_blocking and outputs tuple of tensors (batch_x, batch_y).output_transform (
Callable
) – function that receives ‘x’, ‘y’, ‘y_pred’, ‘loss’ and returns value to be assigned to engine’s state.output after each iteration. Default is returning loss.item().distributed (
bool
) – whether convert model to DistributedDataParallel, if True, devices must contain only 1 GPU or CPU for current distributed rank.
- Returns
a trainer engine with supervised update function.
- Return type
Engine
Note
engine.state.output for this engine is defined by output_transform parameter and is the loss of the processed batch by default.
Workflows¶
BaseWorkflow¶
Workflow¶
- class monai.engines.Workflow(device, max_epochs, data_loader, epoch_length=None, non_blocking=False, prepare_batch=<function default_prepare_batch>, iteration_update=None, postprocessing=None, key_metric=None, additional_metrics=None, metric_cmp_fn=<function default_metric_cmp_fn>, handlers=None, amp=False, event_names=None, event_to_attr=None, decollate=True)[source]¶
Workflow defines the core work process inheriting from Ignite engine. All trainer, validator and evaluator share this same workflow as base class, because they all can be treated as same Ignite engine loops. It initializes all the sharable data in Ignite engine.state. And attach additional processing logics to Ignite engine based on Event-Handler mechanism.
Users should consider inheriting from trainer or evaluator to develop more trainers or evaluators.
- Parameters
device (
device
) – an object representing the device on which to run.max_epochs (
int
) – the total epoch number for engine to run, validator and evaluator have only 1 epoch.data_loader (
Union
[Iterable
,DataLoader
]) – Ignite engine use data_loader to run, must be Iterable or torch.DataLoader.epoch_length (
Optional
[int
]) – number of iterations for one epoch, default to len(data_loader).non_blocking (
bool
) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.prepare_batch (
Callable
) – function to parse expected data (usually image, label and other network args) from engine.state.batch for every iteration, for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.create_supervised_trainer.html.iteration_update (
Optional
[Callable
[[Engine
,Any
],Any
]]) – the callable function for every iteration, expect to accept engine and engine.state.batch as inputs, return data will be stored in engine.state.output. if not provided, use self._iteration() instead. for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html.postprocessing (
Optional
[Callable
]) – execute additional transformation for the model output data. Typically, several Tensor based transforms composed by Compose.key_metric (
Optional
[Dict
[str
,Metric
]]) – compute metric when every iteration completed, and save average value to engine.state.metrics when epoch completed. key_metric is the main metric to compare and save the checkpoint into files.additional_metrics (
Optional
[Dict
[str
,Metric
]]) – more Ignite metrics that also attach to Ignite Engine.metric_cmp_fn (
Callable
) – function to compare current key metric with previous best key metric value, it must accept 2 args (current_metric, previous_best) and return a bool result: if True, will update best_metric and best_metric_epoch with current metric and epoch, default to greater than.handlers (
Optional
[Sequence
]) – every handler is a set of Ignite Event-Handlers, must have attach function, like: CheckpointHandler, StatsHandler, SegmentationSaver, etc.amp (
bool
) – whether to enable auto-mixed-precision training or inference, default is False.event_names (
Optional
[List
[Union
[str
,EventEnum
]]]) – additional custom ignite events that will register to the engine. new events can be a list of str or ignite.engine.events.EventEnum.event_to_attr (
Optional
[dict
]) – a dictionary to map an event to a state attribute, then add to engine.state. for more details, check: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html #ignite.engine.engine.Engine.register_events.decollate (
bool
) – whether to decollate the batch-first data to a list of data after model computation, recommend decollate=True when postprocessing uses components from monai.transforms. default to True.
- Raises
TypeError – When
device
is not atorch.Device
.TypeError – When
data_loader
is not atorch.utils.data.DataLoader
.TypeError – When
key_metric
is not aOptional[dict]
.TypeError – When
additional_metrics
is not aOptional[dict]
.
Trainer¶
- class monai.engines.Trainer(device, max_epochs, data_loader, epoch_length=None, non_blocking=False, prepare_batch=<function default_prepare_batch>, iteration_update=None, postprocessing=None, key_metric=None, additional_metrics=None, metric_cmp_fn=<function default_metric_cmp_fn>, handlers=None, amp=False, event_names=None, event_to_attr=None, decollate=True)[source]¶
Base class for all kinds of trainers, inherits from Workflow.
SupervisedTrainer¶
- class monai.engines.SupervisedTrainer(device, max_epochs, train_data_loader, network, optimizer, loss_function, epoch_length=None, non_blocking=False, prepare_batch=<function default_prepare_batch>, iteration_update=None, inferer=None, postprocessing=None, key_train_metric=None, additional_metrics=None, metric_cmp_fn=<function default_metric_cmp_fn>, train_handlers=None, amp=False, event_names=None, event_to_attr=None, decollate=True, optim_set_to_none=False)[source]¶
Standard supervised training method with image and label, inherits from
Trainer
andWorkflow
.- Parameters
device (
device
) – an object representing the device on which to run.max_epochs (
int
) – the total epoch number for trainer to run.train_data_loader (
Union
[Iterable
,DataLoader
]) – Ignite engine use data_loader to run, must be Iterable or torch.DataLoader.network (
Module
) – network to train in the trainer, should be regular PyTorch torch.nn.Module.optimizer (
Optimizer
) – the optimizer associated to the network, should be regular PyTorch optimizer from torch.optim or its subclass.loss_function (
Callable
) – the loss function associated to the optimizer, should be regular PyTorch loss, which inherit from torch.nn.modules.loss.epoch_length (
Optional
[int
]) – number of iterations for one epoch, default to len(train_data_loader).non_blocking (
bool
) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.prepare_batch (
Callable
) – function to parse expected data (usually image, label and other network args) from engine.state.batch for every iteration, for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.create_supervised_trainer.html.iteration_update (
Optional
[Callable
[[Engine
,Any
],Any
]]) – the callable function for every iteration, expect to accept engine and engine.state.batch as inputs, return data will be stored in engine.state.output. if not provided, use self._iteration() instead. for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html.inferer (
Optional
[Inferer
]) – inference method that execute model forward on input data, like: SlidingWindow, etc.postprocessing (
Optional
[Transform
]) – execute additional transformation for the model output data. Typically, several Tensor based transforms composed by Compose.key_train_metric (
Optional
[Dict
[str
,Metric
]]) – compute metric when every iteration completed, and save average value to engine.state.metrics when epoch completed. key_train_metric is the main metric to compare and save the checkpoint into files.additional_metrics (
Optional
[Dict
[str
,Metric
]]) – more Ignite metrics that also attach to Ignite Engine.metric_cmp_fn (
Callable
) – function to compare current key metric with previous best key metric value, it must accept 2 args (current_metric, previous_best) and return a bool result: if True, will update best_metric and best_metric_epoch with current metric and epoch, default to greater than.train_handlers (
Optional
[Sequence
]) – every handler is a set of Ignite Event-Handlers, must have attach function, like: CheckpointHandler, StatsHandler, SegmentationSaver, etc.amp (
bool
) – whether to enable auto-mixed-precision training, default is False.event_names (
Optional
[List
[Union
[str
,EventEnum
]]]) – additional custom ignite events that will register to the engine. new events can be a list of str or ignite.engine.events.EventEnum.event_to_attr (
Optional
[dict
]) – a dictionary to map an event to a state attribute, then add to engine.state. for more details, check: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html #ignite.engine.engine.Engine.register_events.decollate (
bool
) – whether to decollate the batch-first data to a list of data after model computation, recommend decollate=True when postprocessing uses components from monai.transforms. default to True.optim_set_to_none (
bool
) – when calling optimizer.zero_grad(), instead of setting to zero, set the grads to None. more details: https://pytorch.org/docs/stable/generated/torch.optim.Optimizer.zero_grad.html.
GanTrainer¶
- class monai.engines.GanTrainer(device, max_epochs, train_data_loader, g_network, g_optimizer, g_loss_function, d_network, d_optimizer, d_loss_function, epoch_length=None, g_inferer=None, d_inferer=None, d_train_steps=1, latent_shape=64, non_blocking=False, d_prepare_batch=<function default_prepare_batch>, g_prepare_batch=<function default_make_latent>, g_update_latents=True, iteration_update=None, postprocessing=None, key_train_metric=None, additional_metrics=None, metric_cmp_fn=<function default_metric_cmp_fn>, train_handlers=None, decollate=True, optim_set_to_none=False)[source]¶
Generative adversarial network training based on Goodfellow et al. 2014 https://arxiv.org/abs/1406.266, inherits from
Trainer
andWorkflow
.- Training Loop: for each batch of data size m
Generate m fakes from random latent codes.
Update discriminator with these fakes and current batch reals, repeated d_train_steps times.
If g_update_latents, generate m fakes from new random latent codes.
Update generator with these fakes using discriminator feedback.
- Parameters
device (
device
) – an object representing the device on which to run.max_epochs (
int
) – the total epoch number for engine to run.train_data_loader (
DataLoader
) – Core ignite engines uses DataLoader for training loop batchdata.g_network (
Module
) – generator (G) network architecture.g_optimizer (
Optimizer
) – G optimizer function.g_loss_function (
Callable
) – G loss function for optimizer.d_network (
Module
) – discriminator (D) network architecture.d_optimizer (
Optimizer
) – D optimizer function.d_loss_function (
Callable
) – D loss function for optimizer.epoch_length (
Optional
[int
]) – number of iterations for one epoch, default to len(train_data_loader).g_inferer (
Optional
[Inferer
]) – inference method to execute G model forward. Defaults toSimpleInferer()
.d_inferer (
Optional
[Inferer
]) – inference method to execute D model forward. Defaults toSimpleInferer()
.d_train_steps (
int
) – number of times to update D with real data minibatch. Defaults to1
.latent_shape (
int
) – size of G input latent code. Defaults to64
.non_blocking (
bool
) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.d_prepare_batch (
Callable
) – callback function to prepare batchdata for D inferer. Defaults to returnGanKeys.REALS
in batchdata dict. for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.create_supervised_trainer.html.g_prepare_batch (
Callable
) – callback function to create batch of latent input for G inferer. Defaults to return random latents. for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.create_supervised_trainer.html.g_update_latents (
bool
) – Calculate G loss with new latent codes. Defaults toTrue
.iteration_update (
Optional
[Callable
[[Engine
,Any
],Any
]]) – the callable function for every iteration, expect to accept engine and engine.state.batch as inputs, return data will be stored in engine.state.output. if not provided, use self._iteration() instead. for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html.postprocessing (
Optional
[Transform
]) – execute additional transformation for the model output data. Typically, several Tensor based transforms composed by Compose.key_train_metric (
Optional
[Dict
[str
,Metric
]]) – compute metric when every iteration completed, and save average value to engine.state.metrics when epoch completed. key_train_metric is the main metric to compare and save the checkpoint into files.additional_metrics (
Optional
[Dict
[str
,Metric
]]) – more Ignite metrics that also attach to Ignite Engine.metric_cmp_fn (
Callable
) – function to compare current key metric with previous best key metric value, it must accept 2 args (current_metric, previous_best) and return a bool result: if True, will update best_metric and best_metric_epoch with current metric and epoch, default to greater than.train_handlers (
Optional
[Sequence
]) – every handler is a set of Ignite Event-Handlers, must have attach function, like: CheckpointHandler, StatsHandler, SegmentationSaver, etc.decollate (
bool
) – whether to decollate the batch-first data to a list of data after model computation, recommend decollate=True when postprocessing uses components from monai.transforms. default to True.optim_set_to_none (
bool
) – when calling optimizer.zero_grad(), instead of setting to zero, set the grads to None. more details: https://pytorch.org/docs/stable/generated/torch.optim.Optimizer.zero_grad.html.
Evaluator¶
- class monai.engines.Evaluator(device, val_data_loader, epoch_length=None, non_blocking=False, prepare_batch=<function default_prepare_batch>, iteration_update=None, postprocessing=None, key_val_metric=None, additional_metrics=None, metric_cmp_fn=<function default_metric_cmp_fn>, val_handlers=None, amp=False, mode=ForwardMode.EVAL, event_names=None, event_to_attr=None, decollate=True)[source]¶
Base class for all kinds of evaluators, inherits from Workflow.
- Parameters
device (
device
) – an object representing the device on which to run.val_data_loader (
Union
[Iterable
,DataLoader
]) – Ignite engine use data_loader to run, must be Iterable or torch.DataLoader.epoch_length (
Optional
[int
]) – number of iterations for one epoch, default to len(val_data_loader).non_blocking (
bool
) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.prepare_batch (
Callable
) – function to parse expected data (usually image, label and other network args) from engine.state.batch for every iteration, for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.create_supervised_trainer.html.iteration_update (
Optional
[Callable
[[Engine
,Any
],Any
]]) – the callable function for every iteration, expect to accept engine and engine.state.batch as inputs, return data will be stored in engine.state.output. if not provided, use self._iteration() instead. for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html.postprocessing (
Optional
[Transform
]) – execute additional transformation for the model output data. Typically, several Tensor based transforms composed by Compose.key_val_metric (
Optional
[Dict
[str
,Metric
]]) – compute metric when every iteration completed, and save average value to engine.state.metrics when epoch completed. key_val_metric is the main metric to compare and save the checkpoint into files.additional_metrics (
Optional
[Dict
[str
,Metric
]]) – more Ignite metrics that also attach to Ignite Engine.metric_cmp_fn (
Callable
) – function to compare current key metric with previous best key metric value, it must accept 2 args (current_metric, previous_best) and return a bool result: if True, will update best_metric and best_metric_epoch with current metric and epoch, default to greater than.val_handlers (
Optional
[Sequence
]) – every handler is a set of Ignite Event-Handlers, must have attach function, like: CheckpointHandler, StatsHandler, SegmentationSaver, etc.amp (
bool
) – whether to enable auto-mixed-precision evaluation, default is False.mode (
Union
[ForwardMode
,str
]) – model forward mode during evaluation, should be ‘eval’ or ‘train’, which maps to model.eval() or model.train(), default to ‘eval’.event_names (
Optional
[List
[Union
[str
,EventEnum
]]]) – additional custom ignite events that will register to the engine. new events can be a list of str or ignite.engine.events.EventEnum.event_to_attr (
Optional
[dict
]) – a dictionary to map an event to a state attribute, then add to engine.state. for more details, check: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html #ignite.engine.engine.Engine.register_events.decollate (
bool
) – whether to decollate the batch-first data to a list of data after model computation, recommend decollate=True when postprocessing uses components from monai.transforms. default to True.
SupervisedEvaluator¶
- class monai.engines.SupervisedEvaluator(device, val_data_loader, network, epoch_length=None, non_blocking=False, prepare_batch=<function default_prepare_batch>, iteration_update=None, inferer=None, postprocessing=None, key_val_metric=None, additional_metrics=None, metric_cmp_fn=<function default_metric_cmp_fn>, val_handlers=None, amp=False, mode=ForwardMode.EVAL, event_names=None, event_to_attr=None, decollate=True)[source]¶
Standard supervised evaluation method with image and label(optional), inherits from evaluator and Workflow.
- Parameters
device (
device
) – an object representing the device on which to run.val_data_loader (
Union
[Iterable
,DataLoader
]) – Ignite engine use data_loader to run, must be Iterable, typically be torch.DataLoader.network (
Module
) – network to evaluate in the evaluator, should be regular PyTorch torch.nn.Module.epoch_length (
Optional
[int
]) – number of iterations for one epoch, default to len(val_data_loader).non_blocking (
bool
) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.prepare_batch (
Callable
) – function to parse expected data (usually image, label and other network args) from engine.state.batch for every iteration, for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.create_supervised_trainer.html.iteration_update (
Optional
[Callable
[[Engine
,Any
],Any
]]) – the callable function for every iteration, expect to accept engine and engine.state.batch as inputs, return data will be stored in engine.state.output. if not provided, use self._iteration() instead. for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html.inferer (
Optional
[Inferer
]) – inference method that execute model forward on input data, like: SlidingWindow, etc.postprocessing (
Optional
[Transform
]) – execute additional transformation for the model output data. Typically, several Tensor based transforms composed by Compose.key_val_metric (
Optional
[Dict
[str
,Metric
]]) – compute metric when every iteration completed, and save average value to engine.state.metrics when epoch completed. key_val_metric is the main metric to compare and save the checkpoint into files.additional_metrics (
Optional
[Dict
[str
,Metric
]]) – more Ignite metrics that also attach to Ignite Engine.metric_cmp_fn (
Callable
) – function to compare current key metric with previous best key metric value, it must accept 2 args (current_metric, previous_best) and return a bool result: if True, will update best_metric and best_metric_epoch with current metric and epoch, default to greater than.val_handlers (
Optional
[Sequence
]) – every handler is a set of Ignite Event-Handlers, must have attach function, like: CheckpointHandler, StatsHandler, SegmentationSaver, etc.amp (
bool
) – whether to enable auto-mixed-precision evaluation, default is False.mode (
Union
[ForwardMode
,str
]) – model forward mode during evaluation, should be ‘eval’ or ‘train’, which maps to model.eval() or model.train(), default to ‘eval’.event_names (
Optional
[List
[Union
[str
,EventEnum
]]]) – additional custom ignite events that will register to the engine. new events can be a list of str or ignite.engine.events.EventEnum.event_to_attr (
Optional
[dict
]) – a dictionary to map an event to a state attribute, then add to engine.state. for more details, check: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html #ignite.engine.engine.Engine.register_events.decollate (
bool
) – whether to decollate the batch-first data to a list of data after model computation, recommend decollate=True when postprocessing uses components from monai.transforms. default to True.
EnsembleEvaluator¶
- class monai.engines.EnsembleEvaluator(device, val_data_loader, networks, pred_keys, epoch_length=None, non_blocking=False, prepare_batch=<function default_prepare_batch>, iteration_update=None, inferer=None, postprocessing=None, key_val_metric=None, additional_metrics=None, metric_cmp_fn=<function default_metric_cmp_fn>, val_handlers=None, amp=False, mode=ForwardMode.EVAL, event_names=None, event_to_attr=None, decollate=True)[source]¶
Ensemble evaluation for multiple models, inherits from evaluator and Workflow. It accepts a list of models for inference and outputs a list of predictions for further operations.
- Parameters
device (
device
) – an object representing the device on which to run.val_data_loader (
Union
[Iterable
,DataLoader
]) – Ignite engine use data_loader to run, must be Iterable, typically be torch.DataLoader.epoch_length (
Optional
[int
]) – number of iterations for one epoch, default to len(val_data_loader).networks (
Sequence
[Module
]) – networks to evaluate in order in the evaluator, should be regular PyTorch torch.nn.Module.pred_keys (
Sequence
[str
]) – the keys to store every prediction data. the length must exactly match the number of networks.non_blocking (
bool
) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.prepare_batch (
Callable
) – function to parse expected data (usually image, label and other network args) from engine.state.batch for every iteration, for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.create_supervised_trainer.html.iteration_update (
Optional
[Callable
[[Engine
,Any
],Any
]]) – the callable function for every iteration, expect to accept engine and engine.state.batch as inputs, return data will be stored in engine.state.output. if not provided, use self._iteration() instead. for more details please refer to: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html.inferer (
Optional
[Inferer
]) – inference method that execute model forward on input data, like: SlidingWindow, etc.postprocessing (
Optional
[Transform
]) – execute additional transformation for the model output data. Typically, several Tensor based transforms composed by Compose.key_val_metric (
Optional
[Dict
[str
,Metric
]]) – compute metric when every iteration completed, and save average value to engine.state.metrics when epoch completed. key_val_metric is the main metric to compare and save the checkpoint into files.additional_metrics (
Optional
[Dict
[str
,Metric
]]) – more Ignite metrics that also attach to Ignite Engine.metric_cmp_fn (
Callable
) – function to compare current key metric with previous best key metric value, it must accept 2 args (current_metric, previous_best) and return a bool result: if True, will update best_metric and best_metric_epoch with current metric and epoch, default to greater than.val_handlers (
Optional
[Sequence
]) – every handler is a set of Ignite Event-Handlers, must have attach function, like: CheckpointHandler, StatsHandler, SegmentationSaver, etc.amp (
bool
) – whether to enable auto-mixed-precision evaluation, default is False.mode (
Union
[ForwardMode
,str
]) – model forward mode during evaluation, should be ‘eval’ or ‘train’, which maps to model.eval() or model.train(), default to ‘eval’.event_names (
Optional
[List
[Union
[str
,EventEnum
]]]) – additional custom ignite events that will register to the engine. new events can be a list of str or ignite.engine.events.EventEnum.event_to_attr (
Optional
[dict
]) – a dictionary to map an event to a state attribute, then add to engine.state. for more details, check: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html #ignite.engine.engine.Engine.register_events.decollate (
bool
) – whether to decollate the batch-first data to a list of data after model computation, recommend decollate=True when postprocessing uses components from monai.transforms. default to True.
Utilities¶
- class monai.engines.utils.GanKeys[source]¶
A set of common keys for generative adversarial networks.
- class monai.engines.utils.IterationEvents(value, event_filter=None, name=None)[source]¶
Additional Events engine can register and trigger in the iteration process. Refer to the example in ignite: https://pytorch.org/ignite/generated/ignite.engine.events.EventEnum.html. These Events can be triggered during training iteration: FORWARD_COMPLETED is the Event when network(image, label) completed. LOSS_COMPLETED is the Event when loss(pred, label) completed. BACKWARD_COMPLETED is the Event when loss.backward() completed. MODEL_COMPLETED is the Event when all the model related operations completed. INNER_ITERATION_STARTED is the Event when the iteration has an inner loop and the loop is started. INNER_ITERATION_COMPLETED is the Event when the iteration has an inner loop and the loop is completed.
- class monai.engines.utils.PrepareBatch[source]¶
Interface of customized prepare_batch in the trainer or evaluator workflows. It takes the data of current batch, target device and non_blocking flag as input.
- class monai.engines.utils.PrepareBatchDefault[source]¶
Default prepare batch method to return image and label only, it’s to be consistent with default_prepare_batch API.
- class monai.engines.utils.PrepareBatchExtraInput(extra_keys)[source]¶
Customized prepare_batch for trainer or evaluator that support extra input data for network. Extra items are specified by the extra_keys parameter.
- Parameters
extra_keys (
Union
[str
,Sequence
[str
],Dict
[str
,str
]]) – if a string or list provided, every item is the key of extra data in current batch, and will pass the extra data to the network(*args) in order. If a dictionary is provided, every {k, v} pair is the key of extra data in current batch, k is the param name in network, v is the key of extra data in current batch, and will pass the {k1: batch[v1], k2: batch[v2], …} as kwargs to the network.
- monai.engines.utils.default_metric_cmp_fn(current_metric, prev_best)[source]¶
The default function to compare metric values between current metric and previous best metric.
- Parameters
current_metric (
float
) – metric value of current round computation.prev_best (
float
) – the best metric value of previous rounds to compare with.
- Return type
bool
- monai.engines.utils.default_prepare_batch(batchdata, device=None, non_blocking=False)[source]¶
Default function to prepare the data for current iteration. Refer to ignite: https://pytorch.org/ignite/v0.4.5/generated/ignite.engine.create_supervised_trainer.html #ignite.engine.create_supervised_trainer.
- Return type
Union
[Tuple
[Tensor
,Optional
[Tensor
]],Tensor
]- Returns
image, label(optional).
- monai.engines.utils.engine_apply_transform(batch, output, transform)[source]¶
Apply transform on batch and output. If batch and output are dictionaries, temporarily combine them for the transform, otherwise, apply the transform for output data only.
- monai.engines.utils.get_devices_spec(devices=None)[source]¶
Get a valid specification for one or more devices. If devices is None get devices for all CUDA devices available. If devices is and zero-length structure a single CPU compute device is returned. In any other cases devices is returned unchanged.
- Parameters
devices (
Optional
[Sequence
[device
]]) – list of devices to request, None for all GPU devices, [] for CPU.- Raises
RuntimeError – When all GPUs are selected (
devices=None
) but no GPUs are available.- Returns
list of devices.
- Return type
list of torch.device