Shortcuts

ignite.utils#

Module with helper methods

ignite.utils.apply_to_tensor(input_, func)[source]#

Apply a function on a tensor or mapping, or sequence of tensors.

Parameters
Return type

Union[Tensor, Sequence, Mapping, str, bytes]

ignite.utils.apply_to_type(input_, input_type, func)[source]#

Apply a function on a object of input_type or mapping, or sequence of objects of input_type.

Parameters
Return type

Union[Any, Sequence, Mapping, str, bytes]

ignite.utils.convert_tensor(input_, device=None, non_blocking=False)[source]#

Move tensors to relevant device.

Parameters
Return type

Union[Tensor, Sequence, Mapping, str, bytes]

ignite.utils.setup_logger(name=None, level=20, format='%(asctime)s %(name)s %(levelname)s: %(message)s', filepath=None, distributed_rank=None)[source]#

Setups logger: name, level, format etc.

Parameters
  • name (str, optional) – new name for the logger. If None, the standard logger is used.

  • level (int) – logging level, e.g. CRITICAL, ERROR, WARNING, INFO, DEBUG

  • format (str) – logging format. By default, %(asctime)s %(name)s %(levelname)s: %(message)s

  • filepath (str, optional) – Optional logging file path. If not None, logs are written to the file.

  • distributed_rank (int, optional) – Optional, rank in distributed configuration to avoid logger setup for workers.

  • None (If) –

  • process. (distributed_rank is initialized to the rank of) –

Returns

logging.Logger

Return type

Logger

For example, to improve logs readability when training with a trainer and evaluator:

from ignite.utils import setup_logger

trainer = ...
evaluator = ...

trainer.logger = setup_logger("trainer")
evaluator.logger = setup_logger("evaluator")

trainer.run(data, max_epochs=10)

# Logs will look like
# 2020-01-21 12:46:07,356 trainer INFO: Engine run starting with max_epochs=5.
# 2020-01-21 12:46:07,358 trainer INFO: Epoch[1] Complete. Time taken: 00:5:23
# 2020-01-21 12:46:07,358 evaluator INFO: Engine run starting with max_epochs=1.
# 2020-01-21 12:46:07,358 evaluator INFO: Epoch[1] Complete. Time taken: 00:01:02
# ...
ignite.utils.to_onehot(indices, num_classes)[source]#

Convert a tensor of indices of any shape (N, …) to a tensor of one-hot indicators of shape (N, num_classes, …) and of type uint8. Output’s device is equal to the input’s device.

Parameters
Return type

Tensor