inferno.utils package

Submodules

inferno.utils.exceptions module

Exceptions and Error Handling

exception inferno.utils.exceptions.ClassNotFoundError[source]

Bases: LookupError

exception inferno.utils.exceptions.DTypeError[source]

Bases: TypeError

exception inferno.utils.exceptions.DeviceError[source]

Bases: ValueError

exception inferno.utils.exceptions.FrequencyTypeError[source]

Bases: TypeError

exception inferno.utils.exceptions.FrequencyValueError[source]

Bases: ValueError

exception inferno.utils.exceptions.NotSetError[source]

Bases: ValueError

exception inferno.utils.exceptions.NotTorchModuleError[source]

Bases: TypeError

exception inferno.utils.exceptions.NotUnwrappableError[source]

Bases: NotImplementedError

exception inferno.utils.exceptions.ShapeError[source]

Bases: ValueError

inferno.utils.exceptions.assert_(condition, message='', exception_type=<class 'AssertionError'>)[source]

Like assert, but with arbitrary exception types.

inferno.utils.io_utils module

inferno.utils.io_utils.fromh5(path, datapath=None, dataslice=None, asnumpy=True, preptrain=None)[source]

Opens a hdf5 file at path, loads in the dataset at datapath, and returns dataset as a numpy array.

inferno.utils.io_utils.print_tensor(tensor, prefix, directory)[source]

Prints a image or volume tensor to file as images.

inferno.utils.io_utils.toh5(data, path, datapath='data', compression=None, chunks=None)[source]

Write data to a HDF5 volume.

inferno.utils.io_utils.yaml2dict(path)[source]

inferno.utils.math_utils module

inferno.utils.math_utils.max_allowed_ds_steps(shape, factor)[source]
How often can a shape be down-sampled by a given factor
such that non of the divisions will give non-integers.
Parameters:
  • shape (listlike) – tensor shape
  • factor (integer) – downsample factor
Returns:

maximum allowed downsample operations

Return type:

int

inferno.utils.model_utils module

class inferno.utils.model_utils.ModelTester(input_shape, expected_output_shape)[source]

Bases: object

cuda()[source]
get_input()[source]
class inferno.utils.model_utils.MultiscaleModelTester(input_shape, expected_output_shape)[source]

Bases: inferno.utils.model_utils.ModelTester

inferno.utils.model_utils.is_model_cuda(model)[source]

inferno.utils.python_utils module

Utility functions with no external dependencies.

inferno.utils.python_utils.as_tuple_of_len(x, len_)[source]
class inferno.utils.python_utils.delayed_keyboard_interrupt[source]

Bases: object

Delays SIGINT over critical code. Borrowed from: https://stackoverflow.com/questions/842557/ how-to-prevent-a-block-of-code-from-being-interrupted-by-keyboardinterrupt-in-py

handler(sig, frame)[source]
inferno.utils.python_utils.deprecated(reason)[source]

This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.

Borrowed from https://stackoverflow.com/questions/2536307/ decorators-in-the-python-standard-lib-deprecated-specifically by Laurent LAPORTE https://stackoverflow.com/users/1513933/laurent-laporte

inferno.utils.python_utils.ensure_dir(directory)[source]

ensure the existence of e directory at a given path

If the directory does not exist it is created
Parameters:directory (str) – path of the directory
Returns:path of the directory
Return type:str
inferno.utils.python_utils.from_iterable(x)[source]
inferno.utils.python_utils.get_config_for_name(config, name)[source]
inferno.utils.python_utils.has_callable_attr(object_, name)[source]
inferno.utils.python_utils.is_listlike(x)[source]
inferno.utils.python_utils.is_maybe_list_of(check_function)[source]
inferno.utils.python_utils.require_dict_kwagrs(kwargs, msg=None)[source]
Ensure arguments passed kwargs are either None or a dict.
If arguments are neither a dict nor None a RuntimeError is thrown
Parameters:
  • kwargs (object) – possible dict or None
  • msg (None, optional) – Error msg
Returns:

kwargs dict

Return type:

dict

Raises:

RuntimeError – if the passed value is neither a dict nor None this error is raised

inferno.utils.python_utils.robust_len(x)[source]
inferno.utils.python_utils.to_iterable(x)[source]

inferno.utils.test_utils module

inferno.utils.test_utils.generate_random_data(num_samples, shape, num_classes, hardness=0.3, dtype=None)[source]

Generate a random dataset with a given hardness and number of classes.

inferno.utils.test_utils.generate_random_dataloader(num_samples, shape, num_classes, hardness=0.3, dtype=None, batch_size=1, shuffle=False, num_workers=0, pin_memory=False)[source]

Generate a loader with a random dataset of given hardness and number of classes.

inferno.utils.test_utils.generate_random_dataset(num_samples, shape, num_classes, hardness=0.3, dtype=None)[source]

Generate a random dataset with a given hardness and number of classes.

inferno.utils.torch_utils module

inferno.utils.torch_utils.assert_same_size(tensor_1, tensor_2)[source]
inferno.utils.torch_utils.flatten_samples(tensor_or_variable)[source]

Flattens a tensor or a variable such that the channel axis is first and the sample axis is second. The shapes are transformed as follows:

(N, C, H, W) –> (C, N * H * W) (N, C, D, H, W) –> (C, N * D * H * W) (N, C) –> (C, N)

The input must be atleast 2d.

inferno.utils.torch_utils.is_image_or_volume_tensor(object_)[source]
inferno.utils.torch_utils.is_image_tensor(object_)[source]
inferno.utils.torch_utils.is_label_image_or_volume_tensor(object_)[source]
inferno.utils.torch_utils.is_label_image_tensor(object_)[source]
inferno.utils.torch_utils.is_label_tensor(object_)[source]
inferno.utils.torch_utils.is_label_volume_tensor(object_)[source]
inferno.utils.torch_utils.is_matrix_tensor(object_)[source]
inferno.utils.torch_utils.is_scalar_tensor(object_)[source]
inferno.utils.torch_utils.is_tensor(object_)[source]
inferno.utils.torch_utils.is_volume_tensor(object_)[source]
inferno.utils.torch_utils.unwrap(tensor_or_variable, to_cpu=True, as_numpy=False, extract_item=False)[source]
inferno.utils.torch_utils.where(condition, if_true, if_false)[source]

Torch equivalent of numpy.where.

Parameters:
  • condition (torch.ByteTensor or torch.cuda.ByteTensor or torch.autograd.Variable) – Condition to check.
  • if_true (torch.Tensor or torch.cuda.Tensor or torch.autograd.Variable) – Output value if condition is true.
  • if_false (torch.Tensor or torch.cuda.Tensor or torch.autograd.Variable) – Output value if condition is false
Returns:

Return type:

torch.Tensor

Raises:
  • AssertionError – if if_true and if_false are not both variables or both tensors.
  • AssertionError – if if_true and if_false don’t have the same datatype.

inferno.utils.train_utils module

Utilities for training.

class inferno.utils.train_utils.AverageMeter[source]

Bases: object

Computes and stores the average and current value. Taken from https://github.com/pytorch/examples/blob/master/imagenet/main.py

reset()[source]
update(val, n=1)[source]
class inferno.utils.train_utils.CLUI[source]

Bases: object

Command Line User Interface

class inferno.utils.train_utils.Duration(value=None, units=None)[source]

Bases: inferno.utils.train_utils.Frequency

Like frequency, but measures a duration.

compare(iteration_count=None, epoch_count=None)[source]
match(iteration_count=None, epoch_count=None, when_equal_return=False, **_)[source]
class inferno.utils.train_utils.Frequency(value=None, units=None)[source]

Bases: object

UNIT_PRIORITY = 'iterations'
VALID_UNIT_NAME_MAPPING = {'epoch': 'epochs', 'epochs': 'epochs', 'iteration': 'iterations', 'iterations': 'iterations'}
assert_units_consistent(units=None)[source]
assert_value_consistent(value=None)[source]
classmethod build_from(args, priority='iterations')[source]
by_epoch
by_iteration
epoch()[source]
every(value)[source]
classmethod from_string(string)[source]
is_consistent
iteration()[source]
match(iteration_count=None, epoch_count=None, persistent=False, match_zero=True)[source]
units
value
class inferno.utils.train_utils.MovingAverage(momentum=0)[source]

Bases: object

Computes the moving average of a given float.

relative_change
reset()[source]
update(val)[source]
class inferno.utils.train_utils.NoLogger(logdir=None)[source]

Bases: object

log_value(*kwargs)[source]
inferno.utils.train_utils.get_state(module, key, default=None)[source]

Gets key from module’s state hooks.

inferno.utils.train_utils.set_state(module, key, value)[source]

Writes key-value pair to module’s state hook.

Module contents