uncertainty_wizard.models.ensemble_utils package

Classes

class uncertainty_wizard.models.ensemble_utils.CpuOnlyContextManager(model_id: int, varargs: Optional[dict] = None)

Disables all GPU use, and runs all processes on the CPU

Note: Tensorflow will still see that cuda is installed, but will not find any GPU devices and thus print a warning accordingly.

static disable_all_gpus()

Makes sure no GPUs are visible

class uncertainty_wizard.models.ensemble_utils.DeviceAllocatorContextManager(model_id: int, varargs: Optional[dict] = None)

DEPRECATED. Please use DeviceAllocatorContextManagerV2 instead.

This context manager configures tensorflow such a user-defined amount of processes for every available gpu are started. In addition, running a process on the CPU can be enabled.

This is an abstract context manager. To use it, one has to subclass it and override (at least) the abstract methods.

abstract classmethod gpu_memory_limit() Optional[int]

Override this method to specify the amount of MB which should be used when creating the virtual device on the GPU. Ignored for CPUs.

Attention: This function must be pure: Repeated calls should always return the same value.

Returns:

The amount of MB which will be reserved on the selected gpu in the created context.

class uncertainty_wizard.models.ensemble_utils.DeviceAllocatorContextManagerV2(model_id: int, varargs: Optional[dict] = None)

Distributes processes over multiple GPUs.

You can specify how many processes should be started on each GPU. To use this context manager, you have to subclass it and override the abstract methods.

classmethod gpu_memory_limit() Optional[int]

Not needed in DeviceAllocatorContextManagerV2 anymore. Ignored.

class uncertainty_wizard.models.ensemble_utils.DynamicGpuGrowthContextManager(model_id: int, varargs: Optional[dict] = None)

This context manager configures tensorflow such that multiple processes can use the main GPU at the same time. It is the default in a lazy ensemble multiprocessing environment

classmethod enable_dynamic_gpu_growth()

Configures tensorflow to set memory growth to ture on all GPUs :return: None

class uncertainty_wizard.models.ensemble_utils.EnsembleContextManager(model_id: int, varargs: Optional[dict] = None)

An abstract superclass of context managers which can be used to instantiate a context on a newly created process.

Note that subclasses may override the constuctor, but they must not add or remove any arguments from it.

classmethod after_end() None

This method is called at the end of every context-aware call (e.g., create, modify, predict_quantified, …). Hence, it is not called for every atomic model individually.

Typically, this method would be used to setup requirements that the later created EnsembleContextManager instances are relying on.

Default behavior if method not overridden: Nothing is done.

classmethod before_start() None

This method is called once whenever a context-aware call is made on an ensemble (e.g., create, modify, predict_quantified, …). Hence, it is not called for every atomic model individually.

Typically, this method would be used to setup requirements that the later created EnsembleContextManager instances are relying on.

Default behavior if method not overridden: Nothing is done.

load_single_model(model_id: int, save_config: SaveConfig) Model

This method will be called to load a single atomic model in the ensemble. :param model_id: The id of the atomic model. Is between 0 and the number of atomic model in the ensemble. :param save_config: A save_config instance, providing information about the base path of the ensemble :return The loaded keras model.

classmethod max_sequential_tasks_per_process() int

This method is used to specify how often a process (i.e., for how many atomic models) every single process can be used before being replaced with a new spawn process. Extend this method and set the value to something low to prevent process pollution (e.g. if there’s a memory leak in your process), but beware that process creation (which implies tf initialization) is a costly task.

The method must be pure; every time it is called it should return the same value.

The method is ignored if using num_processes = 0, i.e., when executing in the main process.

Default If not overridden, this method returns 1000 which means that processes is infinitely reused in any reasonable ensemble.

Returns:

A positive integer, specifying how often a process can be used before being replaced.

save_single_model(model_id: int, model: Model, save_config: SaveConfig) None

This method will be called to store a single atomic model in the ensemble. :param model_id: The id of the atomic model. Is between 0 and the number of atomic model in the ensemble. :param model: The keras model to be saved. :param save_config: A save_config instance, providing information about the base path of the ensemble

class uncertainty_wizard.models.ensemble_utils.NoneContextManager(model_id: int, varargs: Optional[dict] = None)

This context manager makes nothing at all, i.e., the model will be executed in exactly the state the process was created.

This for example implies that the tensorflow default GPU configuration will be used.

It is save to use this ContextManager on an existing processes where there is already a tf.session available.

class uncertainty_wizard.models.ensemble_utils.SaveConfig(ensemble_save_base_path: str, delete_existing: bool, expect_model: bool)

This is a class containing information and utility methods about the saving of the ensemble. Currently, it only contains a few static fields. This may change in the future.

Instances of the SaveConfig can be used in the save_single_model and load_single_model

methods of EnsembleContextManagers. However, consider SaveConfigs as read-only classes. In addition, instances should only be created internally by uncertainty wizard and not in custom user code.

property ensemble_save_base_path: str

Returns the path (as string) where this ensemble is stored. This path is always a folder and after successful creation of the ensemble, it will contain the ensemble config file and a subfolder for every atomic model. :return: The save path of this ensemble as string

filepath(model_id: int)

This methods builds the path on which a particular atomic model should be saved / found :param model_id: the id of the atomic model :return: A path, where a folder named after the model id is appended to self.ensemble_save_base_path