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)

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.

classmethod acquire_lock_timeout() int

DeviceAllocatorContextManager uses a lockfile when coordinating device allocation.

This method can be used to specify a timeout. If the lock is not acquired within this time, the process fails.

Returns

The number of seconds to wait until timing out.

classmethod before_start()

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.

classmethod delay() float

DeviceAllocatorContextManager uses a lockfile when coordinating device allocation. If a file is locked, DeviceAllocatorContextManager tries periodically in the interval specified in this method to gain the lock.

Returns

The number of seconds to wait before a retry if the lock cannot be acquired.

abstract classmethod file_path() str

DeviceAllocatorContextManager uses a temporary file to corrdinate device allocation. This method should provide the path of that file.

The folder where the file should be placed must exist. The file itself should not exist (if it does, it will be deleted).

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

Returns

abstract classmethod gpu_memory_limit() 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.

abstract classmethod run_on_cpu() bool

Specify whether one process (in addition to the ones specified in ‘virtual_devices_per_gpu’ should run on the CPU.

Note that even for the processes running on the GPU, some part of the workload (e.g. preprocessing) is typically performed on the CPU and may already consume a lot of resources. Do not set this to true if you have a weak CPU - your PC may freeze.

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

Returns

A flag indicating if the CPU should also dispatch a job to the cpu.

abstract classmethod virtual_devices_per_gpu() Dict[int, int]

A dict mapping every GPU index to the number of processes which should be started concurrently on that GPU.

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

Returns

A mapping specifying how many processes of this ensemble should run concurrently per gpu.

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: uncertainty_wizard.models.ensemble_utils._save_config.SaveConfig) keras.engine.training.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: keras.engine.training.Model, save_config: uncertainty_wizard.models.ensemble_utils._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